[DEPRECATED] ST_Anything - Arduino/ESP8266/ESP32

@jameslee522

James,

Here is an example sketch that implements the logic you requested for your remote start functionality. Let me know if you have any questions. Be sure to take a look at the top-of-file comments inside the S_TimedRelay.h or .cpp files (inside your …\libraries\ST_Anything folder) to see what all of the arguments do. This will let you tweak the timing and logic as needed for your application and hardware. Let me know if you have any questions.

 //******************************************************************************************
//  File: ST_Anything_RemoteStart_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_RemoteStart implements the following ST Capabilities as a demo of what is possible with a single NodeMCU ESP8266
//              - 2 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...))
//    
//  Change History:
//
//    Date        Who            What
//    ----        ---            ----
//    2018-01-22  Dan Ogorchock  Example sketch chaining two timed relays to run in sequence
//
//******************************************************************************************
//******************************************************************************************
// 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 Sesnor (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)
// (no need to uncomment these are they are now defined in the Arduino IDE automatically based on 
//  the board type you select.)
//*************************************************************************************************
//#define LED_BUILTIN 16
//#define BUILTIN_LED 16
//
//#define D0 16  //no internal pullup 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 Pins will be used for each device
//******************************************************************************************
#define PIN_TIMEDRELAY_1          D5  //SmartThings Capability "Relay Switch"
#define PIN_TIMEDRELAY_2          D6  //SmartThings Capability "Relay Switch"

//******************************************************************************************
//ESP8266 WiFi Information
//******************************************************************************************
String str_ssid     = "yourSSIDhere";                           //  <---You must edit this line!
String str_password = "yourPASSWORDhere";                   //  <---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 Hub Information
IPAddress hubIp(192, 168, 1, 149);    // smartthings hub ip     //  <---You must edit this line!
const unsigned int hubPort = 39500;   // smartthings 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)
{
  static unsigned long lastOn = 0;  //last time relaySwitch1 was turned on (used prevent misfires of relaySwitch2)
  
  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
  if (msg == "relaySwitch1 on")
  {
    lastOn = millis();
  }
  else if ((msg == "relaySwitch1 off") && (millis() - lastOn < 6000))
  {
    st::receiveSmartString("relaySwitch2 on");
  }
  
  //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 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 

  //Special sensors/executors (uses portions of both polling and executor classes)
  static st::S_TimedRelay           sensor1(F("relaySwitch1"), PIN_TIMEDRELAY_1, LOW, true, 1000, 1000, 2);
  static st::S_TimedRelay           sensor2(F("relaySwitch2"), PIN_TIMEDRELAY_2, LOW, true, 5000, 0, 1);
  
  //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 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);
  st::Everything::addSensor(&sensor2);
      
  //*****************************************************************************
  //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();
}

@lackr007

Dave,

That is an interesting idea… Basically it sounds like you want to have an optional default state for outputs in the event of loss of connectivity, correct?

I am guessing that the system might be able to detect a loss of local WiFi/LAN connectivity… But I cannot think of a simple way to know that the ST Cloud Services are not functioning properly. I believe we’d have to implement some sort of bi-directional watchdog signal between the microcontroller and the Parent Device. I think we’d want to have ST_Anything initiate a handshake. If it times out waiting for a response, then we would know something is not correct and then take action accordingly. By performing a handshake between ST_Anything and SmartThings, we test not only WiFi/LAN & WAN connectivity, but also whether or not the ST cloud is functioning as well.

I don’t have a lot of time right now to work on this, and it is going to take some design work to consider all of the scenarios. So it could take a while. Is this something you think you could tackle on your own? Perhaps you could issue a Pull Request in the ST_Anything GitHub repository once you get something working?

Please let me know what your thoughts are on the above proposal.

You could always implement a quick and dirty version of this by creating a millis() based timer in your loop() function of the sketch (remember, no delay() statements allowed!) Perhaps every 60 seconds you could send a special “watchdogStart” string to SmartThings. You could trap the reply in the sketch using the new callOnMsgRcvd callback function (you need to create a new function that looks like the current callback() function, and use your new one (different name, of course) to initialize the st::Everything::callOnMsgRcvd variable. Use this new function to sniff messages from ST to ST_Anything and determine if you have a timeout waiting for the reply in your loop() function. You also need to modify the Parent Device Handler to intercept the “watchdogStart” messages and respond with a unique string like “watchdogReply”.

I may have more time later this week, or over the weekend, to look into this concept and whip something up.

Let me know how you’d like to proceed.

Dan

I don’t think he wants a default output state based on ST/internet, from how I read it he wants to be able to maintain local control if ST/internet is out.

Just thinking out loud but if the intent is to turn on a output when a input is detected couldn’t you just bypass ST completely and have the output call pushed when the input is turned on/off using the callOnMsgRcvd function? I.e. Input 1 on → push output 1 on? If ST is up and running then worst case your output gets commanded twice (once locally, once through the cloud), if ST or internet is down then it still works as it should locally.

Dan and Allan,
Allan is correct, I have 2 contacts and 2 switches configured in the ESP8266 (One switch controls a light, the other switch controls a ceiling fan). I have installed a 2 CH relay to take the place of 2 wall switches in a 2 gang box. The ESP8266 contact inputs are wired directly to the existing light switches. I wrote a bit of code in the ESP that if the light switch changes position it will turn off or on the light. And obviously ST can control it as well. But this also means that when the internet goes down lights/fan don’t work AND what ever state they are in when the internet goes down, they stay in that state. I thought about a double command (st::receiveSmartString(“switch1 on”); digitalWrite(PIN_SWITCH_1, LOW); ) when the contact is changed as Allan mentioned, but have not tested it as I don’t know if they will conflict or create other issues. Do you think this will create any strange behavior? Originally I was thinking about attempting to ping an IP address (example 8.8.8.8) and if TRUE then run as currently programmed, if FALSE then switch to “Local” mode and read and write from local (ESP) GPIO’s. Thoughts?

If it helps here is my sketch…

//******************************************************************************************
//  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
//
//******************************************************************************************
//******************************************************************************************
// 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 Sesnor (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)
//*************************************************************************************************
//#define LED_BUILTIN 16
//#define BUILTIN_LED 16
//
//#define D0 16  //no internal pullup 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_WATER_1               A0  //NodeMCU ESP8266 only has one Analog Input Pin 'A0'

//#define PIN_ALARM_1               D2  //SmartThings Capabilty "Alarm"
#define PIN_SWITCH_1              D0  //SmartThings Capability "Switch"
#define PIN_SWITCH_2              D5  //SmartThings Capability "Switch"
//#define PIN_SWITCH_3              D6  //SmartThings Capability "Switch"
//#define PIN_SWITCH_4              D7  //SmartThings Capability "Switch"
#define PIN_CONTACT_1             D1  //SmartThings Capabilty "Contact Sensor"
#define PIN_CONTACT_2             D2  //SmartThings Capabilty "Contact Sensor"
#define PIN_BUTTON_1              D3  //SmartThings Capabilty Button / Holdable Button (Normally Open!)
#define PIN_BUTTON_2              D4  //SmartThings Capabilty Button / Holdable Button (Normally Open!)
#define PIN_SMOKE_1               D6  //SmartThings Capabilty "Smoke Detector"
#define PIN_TEMPERATURE_1         D7  //SmartThings Capabilty "Temperature Measurement" (Dallas Semiconductor DS18B20)
#define PIN_TIMEDRELAY_1          D8  //SmartThings Capability "Relay Switch"

//*************************************************************************************************
//Daves Custom Section
//*************************************************************************************************


int oldState1;
int oldState2;

//******************************************************************************************
//ESP8266 WiFi Information
//******************************************************************************************
String str_ssid     = "xxxxxxxxxxxx";                           //  <---You must edit this line!
String str_password = "xxx.xxx.x.xxx";                   //  <---You must edit this line!
IPAddress ip(192, 168, 1, 182);       //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 Hub Information
IPAddress hubIp(xxxxxxxxxx);    // smartthings hub ip     //  <---You must edit this line!
const unsigned int hubPort = 39500;   // smartthings 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

  //Masquerade as the ThingShield to send data to the Arduino, as if from the ST Cloud (uncomment and edit following line)




  //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 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
 // static st::PS_Water               sensor1(F("water1"), 60, 20, PIN_WATER_1, 200);
 // static st::PS_DS18B20_Temperature sensor2(F("temperature1"), 15, 0, PIN_TEMPERATURE_1, false, 10, 1); 
  
  //Interrupt Sensors 
static st::IS_Contact             sensor1(F("contact1"), PIN_CONTACT_1, LOW, true);
static st::IS_Contact             sensor2(F("contact2"), PIN_CONTACT_2, LOW, true);
//  static st::IS_Button              sensor4(F("button1"), PIN_BUTTON_1, 1000, LOW, true, 500);
//  static st::IS_Button              sensor5(F("button2"), PIN_BUTTON_2, 1000, LOW, true, 500);
//  static st::IS_Motion              sensor6(F("motion1"), PIN_MOTION_1, HIGH, false);
//  static st::IS_Smoke               sensor7(F("smoke1"), PIN_SMOKE_1, HIGH, true, 500);

  //Special sensors/executors (uses portions of both polling and executor classes)
//  static st::S_TimedRelay           sensor8(F("relaySwitch1"), PIN_TIMEDRELAY_1, LOW, false, 3000, 0, 1);
  
  //Executors
  static st::EX_Switch executor1(F("switch1"), PIN_SWITCH_1, LOW, true);  //Inverted logic for "Active Low" Relay Board
  static st::EX_Switch executor2(F("switch2"), PIN_SWITCH_2, LOW, true);  //Inverted logic for "Active Low" Relay Board
 // static st::EX_Switch executor3(F("switch3"), PIN_SWITCH_3, LOW, true);  //Inverted logic for "Active Low" Relay Board
 // static st::EX_Switch executor4(F("switch4"), PIN_SWITCH_4, LOW, true);  //Inverted logic for "Active Low" Relay Board
  
  //*****************************************************************************
  //  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 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);
st::Everything::addSensor(&sensor2);
//  st::Everything::addSensor(&sensor3);
//  st::Everything::addSensor(&sensor4); 
//  st::Everything::addSensor(&sensor5); 
//  st::Everything::addSensor(&sensor6); 
//  st::Everything::addSensor(&sensor7);  
//  st::Everything::addSensor(&sensor8);  
      
  //*****************************************************************************
  //Add each executor to the "Everything" Class
  //*****************************************************************************
  st::Everything::addExecutor(&executor1);
  st::Everything::addExecutor(&executor2);
 // st::Everything::addExecutor(&executor3);
 // st::Everything::addExecutor(&executor4);
    
  //*****************************************************************************
  //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();

  //*****************************************************************************
  //Daves Custom Code
  //*****************************************************************************
  int newState1 = digitalRead(PIN_CONTACT_1);
  int newState2 = digitalRead(PIN_CONTACT_2);
  int outputState1 = digitalRead(PIN_SWITCH_1);
  int outputState2 = digitalRead(PIN_SWITCH_2);
  
    if(newState1 != oldState1)
    {
         if((newState1 == LOW) && (outputState1 == HIGH))
        {
           st::receiveSmartString("switch1 on");
           goto end_code;
        }
        else if((newState1 == HIGH) && (outputState1 == HIGH))
        {
           st::receiveSmartString("switch1 on");
           goto end_code;
        }
        else if((newState1 == LOW) && (outputState1 == LOW))
        {
           st::receiveSmartString("switch1 off");
           goto end_code;
        }   
        else if((newState1 == HIGH) && (outputState1 == LOW))
        {
           st::receiveSmartString("switch1 off");
           goto end_code;
        }       
    
    
        delay(10); // wait for the switch to stop bouncing.
    }

    if(newState2 != oldState2)
    {
         if((newState2 == LOW) && (outputState2 == HIGH))
        {
           st::receiveSmartString("switch2 on");
           goto end_code;
        }
        else if((newState2 == HIGH) && (outputState2 == HIGH))
        {
           st::receiveSmartString("switch2 on");
           goto end_code;
        }
        else if((newState2 == LOW) && (outputState2 == LOW))
        {
           st::receiveSmartString("switch2 off");
           goto end_code;
        }   
        else if((newState2 == HIGH) && (outputState2 == LOW))
        {
           st::receiveSmartString("switch2 off");
           goto end_code;
        }       
    
    
        delay(10); // wait for the switch to stop bouncing.
    }
    end_code:
    oldState1 = newState1;    
    oldState2 = newState2;   

}

Thank you Dan, once I get some free time later this week I will try it out. Thanks you really are awesome!

1 Like

Those are great thoughts and I will attempt to give that a try. Tonight I attempted the easy stuff like a double command that Allan mentioned in the thread he recommended “callOnMsgRcvd function? I.e. Input 1 on -> push output 1 on? If ST is up and running then worst case your output gets commanded twice (once locally, once through the cloud), if ST or internet is down then it still works as it should locally.”. This didn’t work for me, the ESP didn’t respond to contact state changes once I unplugged my DSL connection. I also tried a some code that ping an eternal IP address, it didn’t work either. Although I did have a lot of time tonight to debug, but this will not be as strait forward as I was hoping for. Again I like your ideas and will give them a try and maybe I will learn a thing or two along the way! Let me know if you have any other ideas to try as the requirement for a constant internet connection is the ONE thing I don’t like about smartthings. Thanks for your insight! Many thanks!

Dave

I have a question if anyone can help me.
I have an 8266 board with one button and one switch.
The button is reporting back to ST but in the app I do not get any child device.
The live login is

6e3383e9-8176-4ed9-ad5e-755fc66d5e47  1:27:48 PM: debug [name:switch1, value:off, isStateChange:false, displayed:false, linkText:Butt_Grafeio, descriptionText:Butt_Grafeio switch1 is off]
6e3383e9-8176-4ed9-ad5e-755fc66d5e47  1:27:48 PM: debug isChild = true, but no child found - Auto Add it!
6e3383e9-8176-4ed9-ad5e-755fc66d5e47  1:27:48 PM: debug Parsing: switch1 off
6e3383e9-8176-4ed9-ad5e-755fc66d5e47  1:27:47 PM: debug Using ip: 192.168.1.100 and port: 8090 for device: 6e3383e9-8176-4ed9-ad5e-755fc66d5e47
6e3383e9-8176-4ed9-ad5e-755fc66d5e47  1:27:47 PM: debug Executing 'sendEthernet' refresh
6e3383e9-8176-4ed9-ad5e-755fc66d5e47  1:27:47 PM: debug Executing 'refresh()'

Any ideas?

Periklis,

Sounds like everything is working as designed. There are NO Child Devices created for “Button” devices. The Parent Device handles the “button pushed” and “button held” messages directly from the ST_Anything microcontroller.

In order to use these buttons, simply use a SmartApp (like Smart Lighting or webCoRE) to subscribe to these events from the Parent Device as a trigger for an action. You must properly configure the ST_Anything Parent Device, in its settings page, with correct number of buttons that you have defined in the Arduino Sketch. This allows the Parent Device to broadcast the correct number of “Buttons” to any SmartApp that uses them.

I modeled this after the stock Aeon Minimote button controller Device Handler, which has no individual tiles for each button, and no settings for anything other than an icon and name. When you think about it, it makes sense since there is nothing to configure for a Button Controller. It just generates “pushed” and “held” events that SmartApps must subscribe to in order for anything to happen.

Hope this helps explain it.

Dan

Thank you for the answer
I know that the button does not create a child devise but I have also a switch.
I had tested the code to a ESP32 module some time ago and it created a child devise for the switch witch I could turn on and off from the app.

I was confused by your wording above… Sounded like you were wondering where child device was for the button, not the switch.

OK, let’s troubleshoot the missing Child Switch device… By any chance, did you recently manually delete any child devices from your current ST_Anything Parent Device? If so, that may be the problem. You may need to completely delete the Parent Device using the ST App on your phone (note: do not remove any Device Handler source code.) Once the Parent Device is gone (which also removes any child devices with it), manually create a ST_Anything Parent Device in the IDE again, and then go in and configure the settings for it on your phone.

Also, it is important that you not modify the ST_Anything Parent or Child Device Handlers as they are a matched set. In particular, the definition (name: “Child xxxxx”, namespace: “ogiewon”, author: “Dan Ogorchock”) portion at the top of every file must match, or else the Parent will not be able to find the correct Child Device Handler code. In your Live Logging output above, where is the “Butt_Grafeio” text coming from?

Hope this helps,

Dan

The “Butt_Grafeio” is how I had named the parent device.
In my first attempt I had added manually the parent device. Then I waited for the child device to appear in the app. After a while I tried to add it manually (that was wrong). Finally I removed the child and then the parent device and created a new parent device. This is when I posted my question.
Now I took a new esp8266 module ( I have 5 off them) I assigned a new ip to this one named it butt_2 but still I keep getting a message "ST_Anything Parent Device has not yet been fully configured. Click the ‘Gear’ icon, enter data for all fields, and click ‘Done’ "
I have configured all of the fields (IP, PORT, MAC addr ,Number of Buttons), but I did this from my pc not the app.
I have not modify anything in the DH.
I live in Europe and I can not add a DH from GitHub so I added it manually about a month ago when I made my first tests with an ESP32 module. Is there a different DH for ESP32 and ESP8266?

I just took one more ESP8266 board and configured this to an other IP and a new name. This time I have configured all of the fields (IP, PORT, MAC addr ,Number of Buttons) from my phone App not from the IDE on the PC and the child device appeared almost immediately. I will now try this with the other 2.
Just tried one of the boards that had a problem. I removed it and then created a new device. I then configured it from the mobile app and now it working! I have a child device!

1 Like

Glad to hear it is working for you!

The error message you mentioned (Device Not Fully Configured) will occur the very first time you open the ST App on your phone after manually creating the parent, and then for about a day if you visit the “Recently” tab of the Parent Device. If you get the error pop-up when visiting the “Recently” tab, it can be ignored. If you get it after saving the settings on the Parent (IP, MAC, PORT, # Buttons) then something is still not fully configured.

1 Like

thanks Dan, every thing works great!

1 Like

Thank you Dan for all the work!

1 Like

Confused about upgrading? I have an Arduino MEGA2560 with a ThingShield. I have installed AD2SmartThings v4_4_7 DTH and the associated Alarm Handler 2.3 SmartApp. Is it appropriate for me to upgrade to ST_Anything v2.9 DTH with the Parent/Child? If so, do I need to/should I remove the virtual devices that Alarm Handler 2.3 created before I upgrade? Does this ST_Anything V2.9 replace AD2SmartThings v4_4_7? Does it provide the same functionality with showing sensor and alarm status and being able to arm/disarm my Visa 20P alarm system? Or, does is just expose those sensors to SmartThings? I want to make sure I don’t mess up my working system by upgrading to something that is not appropriate to gain a few more sensors that I am currently lacking.

Thanks for your help.

Do Not upgrade. ST_Anything does not control your old alarm panel. If what you have is working, leave it alone.

ST_Anything could be used as a replacement for your old panel, assuming wired door and window sensors. But then you’d be using a cloud based alarm system via Smart Home Monitor. If your old panel is still working, leave well enough alone.

Was there a reason for wanting to upgrade?

Thank you. I was beginning to think that was the case. The only reason I was considering upgrading was to expose a Honeywell 5821 sensor (Flood/Freeze) that I have that I cannot see now. It would be nice to be able to interface with that through SmartThings, but it is certainly not a requirement. My system is working well and trouble free now so I think I will follow your advice and leave it alone.

Appreciate you expert advice. Thanks again.

1 Like

Hi Dan,

First and foremost, thank you for all that you’ve done to make ST integration not only possible but straightforward. I’ve implemented your code for multiple projects, and, thanks to the quality of your commenting and documentation, it’s been a great learning experience.

The Problem:
I’m attempting to use ST_Anything to control a single-channel 5V relay (Phone APP for ESP8266 5V WiFi Relay Module Home Smart Remote Control Switch) via an ESP-01. I can’t trigger the relay using ST_Anything.

image

Background:
As noted by one or two other users in this forum, this particular relay board has a built-in adapter for the ESP-01. Unfortunately, it requires serial commands from the ESP-01 at a baud rate of 9600 to trigger the relay. To my knowledge, digital communication with the relay board isn’t possible without hardware modifications.

The relay board currently takes a 5V input which, in my prototyping setup, is provided by a regulated breadboard power supply (9V input). Sufficient power to the board and ESP-01 has been confirmed via tests of the relays funciontality independent of the ST_Anything sketches.

Steps Taken:
I’ve validated the ESP-01’s functionality from two angles:
1-Using the ST_Anything sketch for the ESP8266, It can succcessfully communicate with the hub, creates a child relay device, and can trigger digital pins high/low appropriately via the app
2-Using a basic on/off loop sketch, it can activate the relay using a set of serial commands

I believe the issue is a combination of specificying the right parameters in “Constants” and the proper coding in “S_TimedRelay” to make sure that the baud rate is appropriate, that other serial communications are suppressed, that the right TX/RX pins are properly identified and used, and that the serial commands are properly formatted.

Specifically, the ESP-01 needs to be able to transmit the following on/off commands via serial instead of (or in addition to) the digitalwrite() command that is issued in the writetopin() function in S_TimedRelay.cpp:

byte relON = {0xA0, 0x01, 0x01, 0xA2}; //Hex command to send to serial for open relay
byte relOFF = {0xA0, 0x01, 0x00, 0xA1}; //Hex command to send to serial for close relay

In the basic on/off loop sketch that I’ve confirmed activates the relay, I’ve called these as follows:

Serial.write(relON, sizeof(relON)); // turns the relay ON
Serial.write(relOFF, sizeof(relOFF)); // turns the relay OFF

I’ve tried altering the baud rate in Constants, implementing a Serial.begin(9600) command in void Setup of the ST_Anything sketch, and adding the above definitions and Serial.write commands to different sections of the S_TimedRelay.cpp file. No luck getting it to actually trigger the relay once it is plugged into the relay board, though!

The question:
What’s the simplest way to modify the libraries so that they’ll issue these commands over the proper pin, but still play nice with the other code (i.e. turn on then off after a defined interval to simulate a momentary button press)?