[DEPRECATED] ST_Anything - Arduino/ESP8266/ESP32

This should do it. And you can still control the switch1 device as you see fit from SmartThings. Of course, opening or closing the door will override any prior commands from ST in favor of the logic below.

//******************************************************************************************
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
  //NOTE:  you may need to play with the logic below to get the behavior you desire!!!
  if (msg == "contact1 closed") {
    st::receiveSmartString("switch1 on");
  }
  if (msg == "contact1 open") { 
    st::receiveSmartString("switch1 off");
  }
  
}

Thank you, sir!
Ill try tomorrow.

1 Like

I did have some weird issues prior to updating sketches & DHs a few weeks ago. I do get delays quite often, but I notice it more when I’m switching multiple devices on/off. I always thought it was because the board was busy with sending status from other switches on the same board.

I also use webCoRE positions to turn on multiple devices at the same time. (ie if spa heater is switched to on, turn on switches 3,4,5 & 6)

Dan, as always thanks for your time and patience with less talented users as myself. I will try this sometime soon, it’ just a pain disconnecting everything from the board and it seems every time I do, the jumper cables get looser.

If you ever find yourself bench testing your code and you have time, load one with a bunch of switches and thermostats and see if you experience the same issue while monitoring the serial output, curious if it’s just me.

Thanks again,
Nathan

Nathan, yours delay just a second or two. Mine is anything between 1 min and never!
Let me ask you a question - What exactly do you update in the sketch? As far as I understand, you just have to download updated libraries into Arduino and re-flush the board with those new libraries, but nothing changes in the .ino file, correct?

Deleted, as the issue not acurate

If I had a dollar for every time someone asked me that exact same question! :stuck_out_tongue_winking_eye:

I have compiled and loaded an ESP8266 today using the new ST_Anything library. So, my changes to Everything.cpp seem fine.

As usual, your sketch needs the correct Hub LAN IP address and Port 39500. Your Parent Device needs the ESP8266’s IP address, Port (usually 8090), and MAC address. It also needs the Location and Hub defined via the Web IDE.

My apology, In IDE it took time to create the child devices. Now, I tested all the logic and working fine. Big thank you to you. :smile:

1 Like

Awesome! Glad to hear the Pre and Post callbacks are properly implementing the required logic for your device!

When you send the On command from ST on your phone’s app, and it changes to “Turning On”, does it immediately change back to “Off” once the Arduino logic prevents it from happening due to the status of your other switch device? It should, but I just wanted to make sure


Dan:
When I click a button, the action on the device takes place immediately with no delay, but takes time to update the status on the button in the phone apps.

Great. Thanks for the feedback. Glad to hear it works as designed.

Ariel - sorry for the very slow response. I just found this buried in my email. I have ordered a SHT30 sensor and will try your code once I receive the device (coming from China, so it will take a little while.)

Thank you for your contribution to ST_Anything!

Unfortunately, no changes here. Local logic didn’t work. I have downloaded freshly updated everything. cpp and .h files
I can see a serial port monitor mentioned about logic, but the switch does nothing.

Can you please post your sketch? Use the Preformatted Text button after highlighting all of it so it will be easy to read.

Hre it is, Sir!

//******************************************************************************************
//  File: ST_Anything_Multiples_ESP01WiFi.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 ESP8266-01 (ESP-01) 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 ESP-01's WiFi.
//    
//            ST_Anything_Multiples implements the following ST Capabilities in multiples of 1 as a demo of what is possible with a single ESP-01
//              - 1 x Contact Sensor device (used to monitor magnetic door sensors)
//              - 1 x Motion devices (used to detect motion)
//
//  Note:  The tiny ESP-01 only has 2 GPIO pins, so this example is somewhat limited.  Use the ST_ANything_Multiples_ESP8266WiFi.ino example to see 
//         what else is possible.  As long as you only try using 2 pins, you can use them for whatever you'd like.
//
//  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-02-21  Dan Ogorchock  New example specifically for running everythin on a ESP-01 (no Arduino required!)
//    2017-04-24  Dan Ogorchock  Updated for use with new v2.5 Parent/Child Device handlers
//    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_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 <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

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

#define PIN_SWITCH               0
#define PIN_CONTACT              2

//******************************************************************************************
//ESP8266 WiFi Information
//******************************************************************************************
String str_ssid     = "XXXXXXXXXXXX";                           //  <---You must edit this line!
String str_password = "00000000000";                   //  <---You must edit this line!
IPAddress ip(192, 168, 10, 85);       //Device IP Address       //  <---You must edit this line!
IPAddress gateway(192, 168, 10, 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, 10, 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, 10, 200);    // 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 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
  //NOTE:  you may need to play with the logic below to get the behavior you desire!!!
  if (msg == "contact1 closed") {
    st::receiveSmartString("switch1 on");
  }
  if (msg == "contact1 open") { 
    st::receiveSmartString("switch1 off");
  }
  
}

//******************************************************************************************
//Arduino Setup() routine
//******************************************************************************************
void setup()
{
  //******************************************************************************************
  //Declare each Device that is attached to the ESP-01
  //  Notes: - For each device, there is typically a corresponding "tile" defined in your 
  //           SmartThings Device Hanlder 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
 
  //Interrupt Sensors 
  
  static st::IS_Contact sensor1(F("contact1"), PIN_CONTACT, LOW, true);
  
  //Executors
 
  static st::EX_Switch executor1(F("switch1"), PIN_SWITCH, LOW, true);  //Inverted logic for "Active Low" Relay Board
  
  //*****************************************************************************
  //  Configure debug print output from each main class 
  //*****************************************************************************
  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 Assigment - Must set your router's DHCP server to provice 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);
    
  //*****************************************************************************
  //Add each executor to the "Everything" Class
  //*****************************************************************************
    st::Everything::addExecutor(&executor1);
  //*****************************************************************************
  //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();
}

Here is the Serial monitor output:

19:11:25.167 -> Initializing ESP8266 WiFi network.  Please be patient...
19:11:25.167 -> Attempting to connect to WPA SSID: xxxxxxx
19:11:25.167 -> .
19:11:25.665 -> 
19:11:25.665 -> Enter the following three lines of data into ST App on your phone!
19:11:25.665 -> localIP = 192.168.10.85
19:11:25.665 -> serverPort = 8090
19:11:25.665 -> MAC Address = xxxxxxxxxx
19:11:25.665 -> 
19:11:25.665 -> SSID = xxxxxxxxxxxxx
19:11:25.665 -> PASSWORD =xxxxxxxxxxxxx
19:11:25.665 -> hubIP = 192.168.10.200
19:11:25.665 -> hubPort = 39500
19:11:25.698 -> RSSI = -60
19:11:25.698 -> hostName = ESP8266_xxxxxxxxxxxx
19:11:25.698 -> 
19:11:25.698 -> SmartThingsESP8266WiFI: Intialized
19:11:25.698 -> 
19:11:25.698 -> Disabling ESP8266 WiFi Access Point
19:11:25.698 -> 
19:11:25.698 -> ArduinoOTA Ready
19:11:25.698 -> IP address: 192.168.10.85
19:11:25.698 -> ArduionOTA Host Name: ESP8266_xxxxxxxxxxxxxxx
19:11:25.698 -> 
19:11:25.698 -> Everything: init ended
19:11:25.698 -> Everything: Free RAM = 47480
19:11:25.698 -> Everything: adding sensor named contact1
19:11:25.698 -> Everything: Free RAM = 47480
19:11:25.698 -> Everything: adding executor named switch1
19:11:25.731 -> Everything: Free RAM = 47480
19:11:25.731 -> Everything: initDevices started
19:11:25.731 -> Everything: Free RAM = 47480
19:11:25.731 -> Everything: Sending: contact1 open
19:12:59.927 -> ST_Anything Callback: Sniffed data = contact1 open
19:12:59.927 -> Everything: Received: switch1 off
19:12:59.927 -> EX_Switch::beSmart s = off
19:12:59.927 -> Everything: Sending: switch1 off
19:13:00.225 -> ST_Anything Callback: Sniffed data = switch1 off
19:13:00.225 -> Everything: Sending: switch1 off
19:13:00.459 -> ST_Anything Callback: Sniffed data = switch1 off
19:13:00.459 -> Everything: initDevices ended
19:13:00.493 -> Everything: Free RAM = 46904

I just tested your sketch and it works perfectly. I only changed the network connection information. I am running this on a NodeMCU ESP8266, using pins D3 (switch1) and D4 (contact1) as these correspond to pins 0 and 2 that the sketch is using. The callback() routine works exactly as designed, automatically turning on switch1 when contact1 is closed

The only issue I had was the first time the child devices were created, there were 2 switch1 devices. You cannot just delete one. You must delete the parent device and then manually create it again. Keep repeating that process until on one of every child is created. This is a bug on the ST Cloud side, not in my groovy code. I even tried recently to add some logic to prevent creating duplicate devices to no avail.

You do realize that if either pins 0 or 2 are held low during the power on cycle, the ESP01 will not power up correctly, right? Pin 0 is used to enter Flash Programming mode.

I did try to use an ESP01, but my ESP01 refused to run properly after the sketch was loaded. It would just reset itself continuously. No clue what is wrong with it. I haven’t touched it in a few years.

Here is the output from the Arduino Serial Monitor window which I annotated with explanations for what you’re seeing.

20:17:13.341 -> 
20:17:13.341 -> Everything: init ended
20:17:13.341 -> Everything: Free RAM = 47464
20:17:13.341 -> Everything: adding sensor named contact1
20:17:13.341 -> Everything: Free RAM = 47464
20:17:13.341 -> Everything: adding executor named switch1
20:17:13.341 -> Everything: Free RAM = 47464
20:17:13.341 -> Everything: initDevices started
20:17:13.341 -> Everything: Free RAM = 47464
20:17:13.341 -> Everything: Sending: contact1 open                                  FROM THE INIT ROUTINE
20:17:13.479 -> ST_Anything Callback: Sniffed data = contact1 open    Callback Called
20:17:13.479 -> Everything: Received: switch1 off                                      Callback turns switch1 off
20:17:13.479 -> EX_Switch::beSmart s = off                                                switch1 starts turning off
20:17:13.479 -> Everything: Sending: switch1 off                                       switch1 now off, let ST know
20:17:13.721 -> ST_Anything Callback: Sniffed data = switch1 off         Callback Called, nothing to do
20:17:13.721 -> Everything: Sending: switch1 off                                       FROM INIT ROUTINE
20:17:13.930 -> ST_Anything Callback: Sniffed data = switch1 off         Callback Called, nothing to do
20:17:13.930 -> Everything: initDevices ended                                            End of INIT routine
20:17:13.930 -> Everything: Free RAM = 47504
20:17:21.211 -> Everything: Sending: contact1 closed                               I added a jumper from pin to gnd
20:17:21.316 -> ST_Anything Callback: Sniffed data = contact1 closed Callback called
20:17:21.316 -> Everything: Received: switch1 on                                       Callback turns on switch1
20:17:21.316 -> EX_Switch::beSmart s = on                                                 switch1 starts turning on
20:17:21.316 -> Everything: Sending: switch1 on                                        switch1 now on, let ST know
20:17:21.559 -> ST_Anything Callback: Sniffed data = switch1 on          Callback called, nothing to do
20:17:24.577 -> Everything: Sending: contact1 open                                 I removed jumper from pin to gnd
20:17:24.716 -> ST_Anything Callback: Sniffed data = contact1 open   Callback called
20:17:24.716 -> Everything: Received: switch1 off                                     Callback turns switch1 off
20:17:24.716 -> EX_Switch::beSmart s = off                                               switch1 starts turning off
20:17:24.716 -> Everything: Sending: switch1 off                                      switch1 now off, let ST know
20:17:24.958 -> ST_Anything Callback: Sniffed data = switch1 off        Callback Called, nothing to do
20:18:00.931 -> Everything: Received: switch1 on                                    I used ST App on phone to turn on
20:18:00.931 -> EX_Switch::beSmart s = on                                               switch1 starts turning on
20:18:00.931 -> Everything: Sending: switch1 on                                      switch1 now on, let ST know
20:18:01.001 -> ST_Anything Callback: Sniffed data = switch1 on        Callback called, nothing to do
20:18:02.565 -> Everything: Received: switch1 off                                   I used ST App on phone to turn off
20:18:02.565 -> EX_Switch::beSmart s = off                                             switch1 starts turning off
20:18:02.565 -> Everything: Sending: switch1 off                                    switch1 now off, let ST know
20:18:02.668 -> ST_Anything Callback: Sniffed data = switch1 off      Callback Called, nothing to do

Dan, Thank you for your time!
I was under an impression that you have to avoid D3 and D4 (get this from Dan’s sketches. Oh, wait a second, it is you! :smile:)
//#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’

Anyway, I must be doing something wrong. No matter what I do I cannot get a nodemcu or ESP01 acting right. Both of them seems to go to nirvana and are coming back after some time or ever!
Here is an example when I start up the board with a closed contact.
I will try to find out why the board goes to a nonresponse mode even though the WIFI portion is functioning

22:13:56.295 -> ArduinoOTA Ready
22:13:56.295 -> IP address: 192.168.10.201
22:13:56.295 -> ArduionOTA Host Name: ESP8266_BD4E620425
22:13:56.328 -> 
22:13:56.328 -> Everything: init ended
22:13:56.328 -> Everything: Free RAM = 47392
22:13:56.328 -> Everything: adding sensor named contact1
22:13:56.328 -> Everything: Free RAM = 47392
22:13:56.328 -> Everything: adding executor named switch1
22:13:56.328 -> Everything: Free RAM = 47392
22:13:56.328 -> Everything: initDevices started
22:13:56.328 -> Everything: Free RAM = 47392
22:13:56.328 -> Everything: Sending: contact1 closed
22:13:56.493 -> ST_Anything Callback: Sniffed data = contact1 closed  <<Initial boot up
22:13:56.493 -> Everything: Received: switch1 on
22:13:56.493 -> EX_Switch::beSmart s = on
22:13:56.493 -> Everything: Sending: switch1 on
22:15:30.211 -> ST_Anything Callback: Sniffed data = switch1 on
22:15:30.211 -> Everything: Sending: switch1 on
22:15:30.508 -> ST_Anything Callback: Sniffed data = switch1 on
22:15:30.508 -> Everything: initDevices ended
22:15:30.508 -> Everything: Free RAM = 46816
22:15:30.508 -> Everything: Sending: contact1 open                                      << Took 96 sec to react
22:17:04.703 -> ST_Anything Callback: Sniffed data = contact1 open
22:17:04.703 -> Everything: Received: switch1 off
22:17:04.703 -> EX_Switch::beSmart s = off
22:17:04.703 -> Everything: Sending: switch1 off
22:18:38.960 -> ST_Anything Callback: Sniffed data = switch1 off
22:18:39.092 -> Everything: Sending: contact1 closed
22:18:39.257 -> ST_Anything Callback: Sniffed data = contact1 closed
22:18:39.257 -> Everything: Received: switch1 on                                          << 
22:18:39.257 -> EX_Switch::beSmart s = on
22:18:39.257 -> Everything: Sending: switch1 on
22:20:13.448 -> ST_Anything Callback: Sniffed data = switch1 on
22:21:47.690 -> Everything: Sending: switch1 on
22:23:22.071 -> ST_Anything Callback: Sniffed data = switch1 on
22:23:22.071 -> Everything: Sending: contact1 closed
22:23:22.303 -> ST_Anything Callback: Sniffed data = contact1 closed
22:23:22.303 -> Everything: Received: switch1 on
22:23:22.303 -> EX_Switch::beSmart s = on
22:23:22.336 -> Everything: Sending: switch1 on
22:24:56.448 -> ST_Anything Callback: Sniffed data = switch1 on
22:24:56.448 -> Everything: Sending: contact1 open               << in reality opened on 22:19.01
22:26:30.782 -> ST_Anything Callback: Sniffed data = contact1 open
22:26:30.782 -> Everything: Received: switch1 off
22:26:30.782 -> EX_Switch::beSmart s = off
22:26:30.782 -> Everything: Sending: switch1 off                      << Took SEVEN MINS  to react
22:26:31.047 -> ST_Anything Callback: Sniffed data = switch1 off

That’s correct. But the ESP01 doesn’t give you any choice but to use those two pins.

It sure seems like something must be wrong
I just don’t have any idea of what that might be.

Hello there Dan,
I finally got the above relay and got it flashed with our custom code that we concocted here

But i am having a hard time connecting it to ST to test it
what do i use for device type handler do i still use Parent_ST_Anything_Ethernet??
When i try that it fails to create a child device here is what i get on the IDE

and here is what i get on the ST app 
no Switch1 as a relay inside of the parent

What do we hae wrong on the code that we have flashed 
or what am i doing wrong :frowning: ??

Always a big thank you
Denis

Can you please show me the Live Logs for the ST_Anything Parent Device (instead of the event history)? The Live Logs have a tone more debug in them to help uncover the problem. My guess is that you may have forgotten to either install the Child Switch DTH, or forgot to Publish it.

As long as the sketch is sending the exact strings “switch1 on” and “switch1 off” to SmartThings it will work. The strings are case-sensitive, so don’t get creative
 :wink: