[DEPRECATED] ST_Anything - Arduino/ESP8266/ESP32

I will explore the OTA some more.

I guess I just figured if too many people had your password or you got a new router it would be nice to change but we haven’t changed ours in a few years. If you got a new router and set the name and SSID as well as the IP address all the same I suppose they all would still work.

Thanks

1 Like

Hi Dan’
You helped me greatly in post 1152 to get my nodemcu working with 4 temps sensors and 4 contacts. It works great andI I was that impressed i have bought another! Do you happen to have the equivalent software to make it control 4 relays (with timers if possible) and as many contacts as possible. Also on very rare occasions the original Nodemcu requires me to reset with the reset button. The node is positioned out of the way somewhat. Is there anyway to externally reset it, other than soldering wires to either side of the reset switch. (Any pin to gnd combination?) many thanks for you help.

Ray Bradshaw

Dan,

Just seems to be with the contact sensor’s state in the ST application. I am going to try another contact sensor set up with a different type of switch to rule out any defect in the micro switch or circuit. Anyway here is the code.

#include <SmartThingsESP8266WiFi.h>

// ST_Anything Library 

#include <Constants.h>       
#include <Device.h>          
#include <Sensor.h>          
#include <Executor.h>       
#include <InterruptSensor.h> 
#include <PollingSensor.h>  
#include <Everything.h>      


#include <IS_Contact.h>      
#include <S_TimedRelay.h>   


#define D0 16 
#define D2  4

#define PIN_CONTACT_1             D2  
#define PIN_TIMEDRELAY_1          D0  


String str_ssid     = "**********";                          
String str_password = "************";                  
IPAddress ip(192, 168, 1, 249);       
IPAddress gateway(192, 168, 1, 1);   
IPAddress subnet(255, 255, 255, 0);  
IPAddress dnsserver(167, 203, 13, 180);  
const unsigned int serverPort = 8090; 

IPAddress hubIp(192, 168, 1, 17);    
const unsigned int hubPort = 39500;   


//st::Everything::callOnMsgSend() optional callback routine.  This is a sniffer to monitor 
//    data being sent to ST.  This allows a user to act on data changes locally within the 
//    Arduino sktech.

void callback(const String &msg)
{
//  Serial.print(F("ST_Anything Callback: Sniffed data = "));
//  Serial.println(msg);
  
  //TODO:  Add local logic here to take action when a device's value/state is changed
  
  //Masquerade as the ThingShield to send data to the Arduino, as if from the ST Cloud (uncomment and edit following line)
  //st::receiveSmartString("Put your command here!");  //use same strings that the Device Handler would send
}

//******************************************************************************************
//Arduino Setup() routine
//******************************************************************************************
void setup()
{
 static st::IS_Contact             sensor3(F("contact1"), PIN_CONTACT_1, LOW, true);
 static st::S_TimedRelay           sensor8(F("relaySwitch1"), PIN_TIMEDRELAY_1, LOW, true, 750, 0, 1);
  
 
  st::Everything::debug=true;
  st::Executor::debug=true;
  st::Device::debug=true;
  st::PollingSensor::debug=true;
  st::InterruptSensor::debug=true;

  //*****************************************************************************
  //Initialize the "Everything" Class
  //*****************************************************************************

  //Initialize the optional local callback routine (safe to comment out if not desired)
  st::Everything::callOnMsgSend = callback;
  
  //Create the SmartThings ESP8266WiFi Communications Object
    //STATIC IP Assignment - Recommended
    st::Everything::SmartThing = new st::SmartThingsESP8266WiFi(str_ssid, str_password, ip, gateway, subnet, dnsserver, serverPort, hubIp, hubPort, st::receiveSmartString);
 
   //Run the Everything class' init() routine which establishes WiFi communications with SmartThings Hub
  st::Everything::init();
  
  //*****************************************************************************
  //Add each sensor to the "Everything" Class
  //*****************************************************************************

  st::Everything::addSensor(&sensor3);
  st::Everything::addSensor(&sensor8);  
      
  //*****************************************************************************
  //Add each executor to the "Everything" Class
  //*****************************************************************************
 // st::Everything::addExecutor(&executor1);
//  st::Everything::addExecutor(&executor2);
    
  //*****************************************************************************
  //Initialize each of the devices which were added to the Everything Class
  //*****************************************************************************
  st::Everything::initDevices();
  
}

//******************************************************************************************
//Arduino Loop() routine
//******************************************************************************************
void loop()
{
  //*****************************************************************************
  //Execute the Everything run method which takes care of "Everything"
  //*****************************************************************************
  st::Everything::run();
}

Ray,

I have attached a sketch below that implements 4 Timed Relay devices and 2 contact sensors. I used ‘safe pins’ on the ESP8266 that will allow you to safely reboot and flash the NodeMCU ESP8266 board without any concern of the external devices causing boot/flash issues (see my ReadMe for GPIO pin restrictions for the ESP8266.) Depending on the relays you choose, it is possible to utilize more of the GPIO pins. But for now, this should be a good start. You could also look into using an ESP32 board instead which supports significantly more I/O pins. (Note: I have not tested this sketch at all.)

As for the reboot question? The best advice I can give is make sure you’re using a good quality power supply (i.e. > 1 amp) as the ESP8266 uses quite a bit of power when transmitting via WiFi. If you want an easier way to reset it, just remove power from the board for a few seconds (i.e. unplug it).

//******************************************************************************************
//  File: ST_Anything_Multiples_ESP8266WiFi.ino
//  Authors: Dan G Ogorchock & Daniel J Ogorchock (Father and Son)
//
//  Summary:  This Arduino Sketch, along with the ST_Anything library and the revised SmartThings 
//            library, demonstrates the ability of one NodeMCU ESP8266 to 
//            implement a multi input/output custom device for integration into SmartThings.
//            The ST_Anything library takes care of all of the work to schedule device updates
//            as well as all communications with the NodeMCU ESP8266's WiFi.
//
//            ST_Anything_Multiples implements the following ST Capabilities as a demo of what is possible with a single NodeMCU ESP8266
//              - 1 x Alarm device (using a simple digital output)
//              - 1 x Contact Sensor devices (used to monitor magnetic door sensors)
//              - 1 x Switch devices (used to turn on a digital output (e.g. LED, relay, etc...)
//              - 1 x Motion devices (used to detect motion)
//              - 1 x Smoke Detector devices (using simple digital input)
//              - 1 x Temperature Measurement devices (Temperature from Dallas Semi 1-Wire DS18B20 device)
//              - 1 x Relay Switch devices (used to turn on a digital output for a set number of cycles And On/Off times (e.g.relay, etc...))
//              - 2 x Button devices (sends "pushed" if held for less than 1 second, else sends "held"
//              - 1 x Water Sensor devices (using the 1 analog input pin to measure voltage from a water detector board)
//    
//  Change History:
//
//    Date        Who            What
//    ----        ---            ----
//    2015-01-03  Dan & Daniel   Original Creation
//    2017-02-12  Dan Ogorchock  Revised to use the new SMartThings v2.0 library
//    2017-04-17  Dan Ogorchock  New example showing use of Multiple device of same ST Capability
//                               used with new Parent/Child Device Handlers (i.e. Composite DH)
//    2017-05-25  Dan Ogorchock  Revised example sketch, taking into account limitations of NodeMCU GPIO pins
//    2018-02-09  Dan Ogorchock  Added support for Hubitat Elevation Hub
//
//******************************************************************************************
//******************************************************************************************
// SmartThings Library for ESP8266WiFi
//******************************************************************************************
#include <SmartThingsESP8266WiFi.h>

//******************************************************************************************
// ST_Anything Library 
//******************************************************************************************
#include <Constants.h>       //Constants.h is designed to be modified by the end user to adjust behavior of the ST_Anything library
#include <Device.h>          //Generic Device Class, inherited by Sensor and Executor classes
#include <Sensor.h>          //Generic Sensor Class, typically provides data to ST Cloud (e.g. Temperature, Motion, etc...)
#include <Executor.h>        //Generic Executor Class, typically receives data from ST Cloud (e.g. Switch)
#include <InterruptSensor.h> //Generic Interrupt "Sensor" Class, waits for change of state on digital input 
#include <PollingSensor.h>   //Generic Polling "Sensor" Class, polls Arduino pins periodically
#include <Everything.h>      //Master Brain of ST_Anything library that ties everything together and performs ST Shield communications

#include <PS_Illuminance.h>  //Implements a Polling Sensor (PS) to measure light levels via a photo resistor

#include <PS_TemperatureHumidity.h>  //Implements a Polling Sensor (PS) to measure Temperature and Humidity via DHT library
#include <PS_DS18B20_Temperature.h>  //Implements a Polling Sensor (PS) to measure Temperature via DS18B20 libraries 
#include <PS_Water.h>        //Implements a Polling Sensor (PS) to measure presence of water (i.e. leak detector)
#include <IS_Motion.h>       //Implements an Interrupt Sensor (IS) to detect motion via a PIR sensor
#include <IS_Contact.h>      //Implements an Interrupt Sensor (IS) to monitor the status of a digital input pin
#include <IS_Smoke.h>        //Implements an Interrupt Sensor (IS) to monitor the status of a digital input pin
#include <IS_DoorControl.h>  //Implements an Interrupt Sensor (IS) and Executor to monitor the status of a digital input pin and control a digital output pin
#include <IS_Button.h>       //Implements an Interrupt Sensor (IS) to monitor the status of a digital input pin for button presses
#include <EX_Switch.h>       //Implements an Executor (EX) via a digital output to a relay
#include <EX_Alarm.h>        //Implements Executor (EX)as an Alarm Siren capability via a digital output to a relay
#include <S_TimedRelay.h>    //Implements a Sensor to control a digital output pin with timing capabilities

//*************************************************************************************************
//NodeMCU v1.0 ESP8266-12e Pin Definitions (makes it much easier as these match the board markings)
//       Note: Do not uncomment these lines as they are for your information only.  The Arduino IDE
//       takes care of defining these pin definitions based on the board you choose. 
//*************************************************************************************************
//#define LED_BUILTIN 16
//#define BUILTIN_LED 16
//
//#define D0 16  //no internal pull-up resistor
//#define D1  5
//#define D2  4
//#define D3  0  //must not be pulled low during power on/reset, toggles value during boot
//#define D4  2  //must not be pulled low during power on/reset, toggles value during boot
//#define D5 14
//#define D6 12
//#define D7 13
//#define D8 15  //must not be pulled high during power on/reset

//******************************************************************************************
//Define which Arduino Pins will be used for each device
//******************************************************************************************

#define PIN_TIMEDRELAY_1          D0  //SmartThings Capability "Relay Switch"
#define PIN_TIMEDRELAY_2          D1  //SmartThings Capability "Relay Switch"
#define PIN_TIMEDRELAY_3          D2  //SmartThings Capability "Relay Switch"
#define PIN_TIMEDRELAY_4          D5  //SmartThings Capability "Relay Switch"

#define PIN_CONTACT_1             D6  //SmartThings Capability "Contact Sensor"
#define PIN_CONTACT_2             D7  //SmartThings Capability "Contact Sensor"

//******************************************************************************************
//ESP8266 WiFi Information
//******************************************************************************************
String str_ssid     = "yourSSIDhere";                           //  <---You must edit this line!
String str_password = "yourWiFiPasswordhere";                   //  <---You must edit this line!
IPAddress ip(192, 168, 1, 227);       //Device IP Address       //  <---You must edit this line!
IPAddress gateway(192, 168, 1, 1);    //Router gateway          //  <---You must edit this line!
IPAddress subnet(255, 255, 255, 0);   //LAN subnet mask         //  <---You must edit this line!
IPAddress dnsserver(192, 168, 1, 1);  //DNS server              //  <---You must edit this line!
const unsigned int serverPort = 8090; // port to run the http server on

// SmartThings / Hubitat Hub TCP/IP Address
IPAddress hubIp(192, 168, 1, 149);    // smartthings/hubitat hub ip //  <---You must edit this line!

// SmartThings / Hubitat Hub TCP/IP Address: UNCOMMENT line that corresponds to your hub, COMMENT the other
const unsigned int hubPort = 39500;   // smartthings hub port
//const unsigned int hubPort = 39501;   // hubitat hub port

//******************************************************************************************
//st::Everything::callOnMsgSend() optional callback routine.  This is a sniffer to monitor 
//    data being sent to ST.  This allows a user to act on data changes locally within the 
//    Arduino sketch.
//******************************************************************************************
void callback(const String &msg)
{
//  Serial.print(F("ST_Anything Callback: Sniffed data = "));
//  Serial.println(msg);
  
  //TODO:  Add local logic here to take action when a device's value/state is changed
  
  //Masquerade as the ThingShield to send data to the Arduino, as if from the ST Cloud (uncomment and edit following line)
  //st::receiveSmartString("Put your command here!");  //use same strings that the Device Handler would send
}

//******************************************************************************************
//Arduino Setup() routine
//******************************************************************************************
void setup()
{
  //******************************************************************************************
  //Declare each Device that is attached to the Arduino
  //  Notes: - For each device, there is typically a corresponding "tile" defined in your 
  //           SmartThings Device Handler Groovy code, except when using new COMPOSITE Device Handler
  //         - For details on each device's constructor arguments below, please refer to the 
  //           corresponding header (.h) and program (.cpp) files.
  //         - The name assigned to each device (1st argument below) must match the Groovy
  //           Device Handler names.  (Note: "temphumid" below is the exception to this rule
  //           as the DHT sensors produce both "temperature" and "humidity".  Data from that
  //           particular sensor is sent to the ST Hub in two separate updates, one for 
  //           "temperature" and one for "humidity")
  //         - The new Composite Device Handler is comprised of a Parent DH and various Child
  //           DH's.  The names used below MUST not be changed for the Automatic Creation of
  //           child devices to work properly.  Simply increment the number by +1 for each duplicate
  //           device (e.g. contact1, contact2, contact3, etc...)  You can rename the Child Devices
  //           to match your specific use case in the ST Phone Application.
  //******************************************************************************************
  //Polling Sensors
  
  //Special sensors/executors (uses portions of both polling and executor classes)
  static st::S_TimedRelay           sensor1(F("relaySwitch1"), PIN_TIMEDRELAY_1, LOW, false, 1000, 0, 1);
  static st::S_TimedRelay           sensor2(F("relaySwitch2"), PIN_TIMEDRELAY_2, LOW, false, 1000, 0, 1);
  static st::S_TimedRelay           sensor3(F("relaySwitch3"), PIN_TIMEDRELAY_3, LOW, false, 1000, 0, 1);
  static st::S_TimedRelay           sensor4(F("relaySwitch4"), PIN_TIMEDRELAY_4, LOW, false, 1000, 0, 1);

  //Interrupt Sensors 
  static st::IS_Contact             sensor5(F("contact1"), PIN_CONTACT_1, LOW, true);
  static st::IS_Contact             sensor6(F("contact2"), PIN_CONTACT_2, LOW, true);
  
  //Executors

  //*****************************************************************************
  //  Configure debug print output from each main class 
  //  -Note: Set these to "false" if using Hardware Serial on pins 0 & 1
  //         to prevent communication conflicts with the ST Shield communications
  //*****************************************************************************
  st::Everything::debug=true;
  st::Executor::debug=true;
  st::Device::debug=true;
  st::PollingSensor::debug=true;
  st::InterruptSensor::debug=true;

  //*****************************************************************************
  //Initialize the "Everything" Class
  //*****************************************************************************

  //Initialize the optional local callback routine (safe to comment out if not desired)
  st::Everything::callOnMsgSend = callback;
  
  //Create the SmartThings ESP8266WiFi Communications Object
    //STATIC IP Assignment - Recommended
    st::Everything::SmartThing = new st::SmartThingsESP8266WiFi(str_ssid, str_password, ip, gateway, subnet, dnsserver, serverPort, hubIp, hubPort, st::receiveSmartString);
 
    //DHCP IP Assignment - Must set your router's DHCP server to provide a static IP address for this device's MAC address
    //st::Everything::SmartThing = new st::SmartThingsESP8266WiFi(str_ssid, str_password, serverPort, hubIp, hubPort, st::receiveSmartString);

  //Run the Everything class' init() routine which establishes WiFi communications with SmartThings Hub
  st::Everything::init();
  
  //*****************************************************************************
  //Add each sensor to the "Everything" Class
  //*****************************************************************************
  st::Everything::addSensor(&sensor1);
  st::Everything::addSensor(&sensor2);
  st::Everything::addSensor(&sensor3);
  st::Everything::addSensor(&sensor4); 
  st::Everything::addSensor(&sensor5); 
  st::Everything::addSensor(&sensor6); 
      
  //*****************************************************************************
  //Add each executor to the "Everything" Class
  //*****************************************************************************

    
  //*****************************************************************************
  //Initialize each of the devices which were added to the Everything Class
  //*****************************************************************************
  st::Everything::initDevices();
  
}

//******************************************************************************************
//Arduino Loop() routine
//******************************************************************************************
void loop()
{
  //*****************************************************************************
  //Execute the Everything run method which takes care of "Everything"
  //*****************************************************************************
  st::Everything::run();
}

Thanks so much Dan, you’re and absolute ace.
Best Regards Ray

1 Like

Your code looks fine to me. Keep us updated on your progress.

Dan,
My ST_Anything reports for about 7hrs and then stops forever. I have to reset it to get it to resume reporting. I see this with the temperature…

thanks,

Chin

Will this work for ST_Anything?

or this:

If they include some GPIO headers too will be awesome.

Sounds like the recent memory leak introduced in the Newer Arduino ESP8266 board package for the Arduino IDE. Revert back to v2.3 instead of v2.4.1 as this has solved the problem for all other ESP8266 users. This is not an issue with ST_Anything. If you review the last month or two of posts in this thread, you’ll see other relevant posts.

As for the boards you posted from Amazon…they may work. I haven’t tried them. Some of those boards are problematic as they implement a serial communications protocol between the ESP8266 chip and another which controls the relays. As long as the relays show up as individually addressable ESP8266 GPIO pins, it should work.

Dan,
Thanks, I’ll try reverting the IDE.

As for the boards, yes the relays are on GPIOs. In these case what is the suitable board to use in the IDE.

thanks,

Chin

Dan,
This maybe a side topic. I’m retrofitting “alexa” into my intercom systems stations using raspberry pi zero Ws as the original has died. I’m thinking if ST_Anything can be ported to rapsberry pi too. That way I can added sensors, controls and integrate it to ST without another device needed. Raspberry pi has audio capabilities, hence nodemcu will not work.

thanks,

Chin

You’ll need to select one of the ESP32 variants for the first one and an ESP8266 variant for the second one. You’ll just need some trial and error to figure it out. Use a very basic sketch, like BLINK, to figure out how the GPIO pins map to the various relays.

I have no immediate plans to add support for the Raspberry Pi platform. It is technically possible, but so much of ST_Anything is tightly wound around the Arduino IDE platform, IDE, and Libraries…

I want to try to see if I can send a http POST to ST as a test. I think if I create a ST_Anything device in the ST and then program the MAC address and IP address. I can see if that works. Can you give me a test “message” to send to it from python in raspberry pi?

Just did my whole Bathroom with this - 3 sets of lights, exhaust fan, temperature, humidity, and motion sensors. Big thumbs up and thank you. I wanted the light switches to work like toggles, so a minor change in the momentary example did it. I think you should update the sample and put a conditional on top so it can work with both momentary as well as for toggling. I used the existing switches but instead of 110Volts they are all on low voltage and every thing is controlled by relays.

Thanks again for all the good work - this really rocks…:+1::+1::+1:

1 Like

Inside the SmartThingsESP8266WiFi.cpp file you can see exactly how data is sent to the ST Hub. Here is the section of code you’re looking for. The ‘message’ that is passed in a simply a name/value pair, space delimited, and case sensitive - for example “contact1 open” or “temperature1 76.34”

		if (st_client.connect(st_hubIP, st_hubPort))
		{
			st_client.println(F("POST / HTTP/1.1"));
			st_client.print(F("HOST: "));
			st_client.print(st_hubIP);
			st_client.print(F(":"));
			st_client.println(st_hubPort);
			st_client.println(F("CONTENT-TYPE: text"));
			st_client.print(F("CONTENT-LENGTH: "));
			st_client.println(message.length());
			st_client.println();
			st_client.println(message);
		}
1 Like

Dan,
Thanks. I saw that and wondered if there’s anything more than just that? It answered my question.

thanks,
Chin

Dan -
How can I reset the ESP-8266 remotely. My board along with relays etc is in a very nasty place in the attic now. For now I am reloading the sketch to accomplish reset. Its brute force but got me out of a stuck DHT22. Anyway is there a built in mechanism for doing reset?

Dan,
In the case where message is “contact1 open”, is the length “13”

thanks,
chin

Dan,
I reverted it to v2.3, re-compile, uploaded, however, it still hangs after X hrs.
Any ideas?

thanks,

Chin