[DEPRECATED] ST_Anything - Arduino/ESP8266/ESP32

These libraries look really great! I’m wondering if you’ve considered porting any for use with the Particle Photon (or Spark)?

I love the photon and the whole cloud infrastructure built around it, and it would be nice to have other integration options for smartthings.

Thanks for the quick reply. I now have the parsed message displaying on my LCD

1 Like

Thanks for the feedback, Robert. I have not looked at the Photon/Spark platform. This project grew from our desire to make using the Arduino ThingShield easier for everyone. It has since grown to include LAN connected devices. My son is pondering expanding it to additional platforms (e.g. RaspberryPi, Linux, MS Windows, etc…) Mostly just concepts at this time, with no active programming currently. We’ll keep the Particle platform in mind should these efforts progress beyond the drawing board stage.

Anyone know if there is a way to change a single DS18B20 temp probe and subsequent tile to report in Celsius. I have lots working nicely in F… thanks…

Nigel,

The DS18B20 ST_Anything class does support both Fahrenheit and Celsius reporting, assuming you have only one DS18B20 attached to an Arduino pin (i.e. not daisy chained together.) In the constructor arguments within your sketch’s setup() routine, simply set the “In_C” field to “true” instead of “false”.

//			  For Example:  st::PS_DS18B20_Temperature sensor1("temperature1", 120, 0, PIN_TEMPERATURE, true);
//
//			  st::PS_DS18B20_Temperature() constructor requires the following arguments
//				- String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name
//				- long interval - REQUIRED - the polling interval in seconds
//				- long offset - REQUIRED - the polling interval offset in seconds - used to prevent all polling sensors from executing at the same time
//				- byte pin - REQUIRED - the Arduino Pin to be used for the One-Wire DS18B20 sensor conenction
//				- bool In_C - OPTIONAL - true = Report Celsius, false = Report Farenheit (Farentheit is the default)
//				- byte resolution - OPTIONAL - DS18B20 sensor resolution in bits.  9, 10, 11, or 12.  Defaults to 10 for decent accuracy and performance
//				- byte num_sensors - OPTIONAL - number of OneWire DS18B20 sensors attached to OneWire bus - Defaults to 1

@renesis would you mind creating another project post on the community with details on what you did to accomplish this? There have been several posts over the years on integrating dumb doorbells using reed switches, vibration sensors, etc but your approach is interesting. I could never get mine to work and would be interested in learning more details about it. Suggesting a separate thread so we keep this one on topic.

Thank you in advance!

1 Like

Hey @ritchierich,

I’m actually using a reed switch (wired) to trip the transistors.

I started trying to get it to just signal off the doorbell switch, but the coil in the doorbell lets too much current leak through to trigger it without adding a relay. The transistors turn on the ESP-01 to send the signal to ST which turns off again a few seconds.

Main benefit is no batteries (vs using a wireless sensor), and very little standby current.

If you (or anyone else) are still interested in the full circuit, i can certainly create a project post.

2 Likes

Thanks. I knew i had seen it somewhere. I have a single probe with 2 sensors connected to the same pin. Named …1 and …2 and one set to false and one to true. This gives me a C tile and F tile on my app.

1 Like

@ogiewon Hello Dan! I have just started to begin trying to use your fantastic code to control an ESP-8266. I have uploaded the sketch ST_Anything_Multiples_ESP01WiFi.ino to the board and have set up the device in the SmartThings app, but I keep getting an error saying, “Child device creation failed. Please make sure that the ‘null’ is installed and published.” Any help would be appreciated. Thanks!

V There’s is a bug introduced by ST in the last month or so causing the error you’re seeing. Just delete the parent device and recreate it, remembering to configure its settings via your phone app. Afterwards, the child devices should be created.

If you manually delete a child device, the problem will return. It actually clears itself up after about 24 hours…

OR

It could be that you tried renaming the devices in the Arduino sketch. Are you starting with an unaltered version of my example sketch? Of course, you you have to edit the network section to work with your router.

Awesome Awesome Awesome, so many thanks for this project! Can’t tell you how many hours I invested into trying to get ESP8266 sensors integrated into ST!

I’m having a slight issue with adding contact sensors. When I first install this, usually neither show up. If I then add the first contact sensor, recompile and flash, then add the second, recompile and flash, I can get them added one by one. Any thoughts?

My code is below:

//******************************************************************************************
//  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_CONTACT_1             D1  //SmartThings Capabilty "Contact Sensor"
#define PIN_CONTACT_2             D2  //SmartThings Capabilty "Contact Sensor"
#define PIN_TEMPERATURE_1         D6  //SmartThings Capabilty "Temperature Measurement" (Dallas Semiconductor DS18B20)
 #define PIN_TEMPERATUREHUMIDITY1    D7  //SmartThings Capabilty "Temperature Measurement" (DHT Temp and Humidity Sensor)
  
//******************************************************************************************
//ESP8266 WiFi Information
//******************************************************************************************
String str_ssid     = "xxxx";                           //  <---You must edit this line!
String str_password = "xxxx";                   //  <---You must edit this line!
IPAddress ip(192, 168, 1, 105);       //Device IP Address       //  <---You must edit this line!
IPAddress gateway(192, 168, 1, 2);    //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, 2);  //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, 56);    // 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)
  //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
  static st::PS_DS18B20_Temperature sensor1(F("temperature0"), 15, 0, PIN_TEMPERATURE_1, false, 10, 1); 
  
  //Interrupt Sensors 
  static st::IS_Contact             sensor3(F("contact1"), PIN_CONTACT_1, LOW, true);
  static st::IS_Contact             sensor4(F("contact2"), PIN_CONTACT_2, LOW, true);  

  //DHT Sensors
  static st::PS_TemperatureHumidity sensor2(F("temphumid1"), 15, 0, PIN_TEMPERATUREHUMIDITY1, st::PS_TemperatureHumidity::DHT11,"temperature1","humidity1");
  
  //*****************************************************************************
  //  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);
    
  //*****************************************************************************
  //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();
}

You really shouldn’t have to add them one at a time. Maybe some sort of race condition on the ST Cloud Servers? You can always just press the REFRESH tile on the parent device. This causes all of the devices to send a status update to ST. Any missing children will be created. Also, sometimes you need to refresh the ST app to pick up the new children. On iOS, I just drag down on the device page until I see the spinning wheel icon appear, then let go. This causes the app to refresh its data.

Thanks Dan!

Another question. I noticed after I unplug my ESP8266 that my temperature data remains at the last value. I’m planning to use this device as a fridge/freezer monitor, so if my board stops working I’d rather the values go stale, rather than remain at their last value.

Any thoughts on this? Maybe some time of keep alive?

Matt,

That is how all ST devices work, unfortunately. You can very easily install a community developed SmartApp called “Device Monitor” which will notify you if any of your devices stop updating values for an extended period of time versus normal.

Check it out here. I use it and it works very well!

Thanks Dan, two more questions regarding DHT22 setup:

  1. Can you clarify what the “120, 10” are used for in the line of code below for the DHT22?
    static st::PS_TemperatureHumidity sensor2(F(“temphumid”), 120, 10, PIN_TEMPERATUREHUMIDITY, st::PS_TemperatureHumidity::DHT22)

  2. Is it possible to setup multiple DHT22 sensors? It appears that PS_TemperatureHumidity.h automatically names the children temperture1 and humidity1. Would I have to create an additional PS_TemperatureHumidity.h for each DHT22?

On question 2 I have 5 DHT21’s in my home/outside.

You just have to define additional PINs etc… like this:

#define PIN_TEMPERATUREHUMIDITY_1 23
#define PIN_TEMPERATUREHUMIDITY_2 25
#define PIN_TEMPERATUREHUMIDITY_3 27
#define PIN_TEMPERATUREHUMIDITY_4 29
#define PIN_TEMPERATUREHUMIDITY_5 33

and then the sensors of course you are using DHT22:
static st::PS_TemperatureHumidity sensor1(F(“temphumid1”), 60, 40, PIN_TEMPERATUREHUMIDITY_1, st::PS_TemperatureHumidity::DHT21,“temperature1”,“humidity1”);
static st::PS_TemperatureHumidity sensor2(F(“temphumid2”), 60, 50, PIN_TEMPERATUREHUMIDITY_2, st::PS_TemperatureHumidity::DHT21,“temperature2”,“humidity2”);
static st::PS_TemperatureHumidity sensor3(F(“temphumid3”), 60, 50, PIN_TEMPERATUREHUMIDITY_3, st::PS_TemperatureHumidity::DHT21,“temperature3”,“humidity3”);
static st::PS_TemperatureHumidity sensor4(F(“temphumid4”), 60, 50, PIN_TEMPERATUREHUMIDITY_4, st::PS_TemperatureHumidity::DHT21,“temperature4”,“humidity4”);
static st::PS_TemperatureHumidity sensor5(F(“temphumid5”), 60, 50, PIN_TEMPERATUREHUMIDITY_5, st::PS_TemperatureHumidity::DHT21,“temperature5”,“humidity5”);

Finally don’t forget the addSensor section:
st::Everything::addSensor(&sensor1);
st::Everything::addSensor(&sensor2);
st::Everything::addSensor(&sensor3);
st::Everything::addSensor(&sensor4);
st::Everything::addSensor(&sensor5);

The child objects are created in the main sensor list and you can rename them to whatever you want them called with settings for each child object.

1 Like

Thanks David. Do you know what the “60, 50” nomenclature is for?

At the top of each device’s class .h and .cpp file you will find a detailed explanation of what each of the arguments is used for. The comments/documentation for the DHT Temperature & Humidity sensors is below.

So, the “60, 50” nomenclature means that the device will be scanned every 60 seconds, with a “50” second offset from the top of a 60 second window of time.

For multiple sensors, I would probably go with 120 seconds for the poling interval. For the offset, I would set the first one to be 5 seconds, the second one to be 10 seconds, and so on (i.e. 5, 10, 15, 20, 25, 30, etc…). This prevents trying to collect and transfer all the data at once. It also provides a more balanced workload for the ST cloud to handle. No reason to slam it all at once.

//			  Create an instance of this class in your sketch's global variable section
//			  For Example:  st::PS_TemperatureHumidity sensor2("temphumid1", 120, 7, PIN_TEMPERATUREHUMIDITY, st::PS_TemperatureHumidity::DHT22, "temperature1", "humidity1", false);
//
//			  st::PS_TemperatureHumidity() constructor requires the following arguments
//				- String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name
//				- long interval - REQUIRED - the polling interval in seconds
//				- long offset - REQUIRED - the polling interval offset in seconds - used to prevent all polling sensors from executing at the same time
//				- byte pin - REQUIRED - the Arduino Pin to be used as a digital output
//				- DHT_SENSOR DHTSensorType - REQUIRED - the type of DHT sensor (DHT11, DHT21, DHT22, DHT33, or DHT44)
//				- String strTemp - OPTIONAL - name of temperature sensor to send to ST Cloud (defaults to "temperature")
//				- String strHumid - OPTIONAL - name of humidity sensor to send to ST Cloud (defaults to "humidity")
//				- bool In_C - OPTIONAL - true = Report Celsius, false = Report Farenheit (Farentheit is the default)

hi, thank you for this code. My project is to control my garage by wiring my garage remote to a relay (my garage opener has a fancy control panel that I can’t simply wire the relay into). I previously tried using a raspberry pi with webiopi, but that was very flaky. Your code, along with the ESP8266-12e works very well.

I have a problem, though. The relay seems to engage for approximately 10 or more seconds no matter what I set the onTime and offTime to when calling the relaySwitch constructor. I even changed the value in the relayswitch.h header file with no effect.

Please note that my relay board is a normally on type so I set the reverse logic variable to true when calling the constructor. Also, I only have two relays connected at this time, nothing else.

#define PIN_TIMEDRELAY_1          D5  //SmartThings Capability "Relay Switch"
#define PIN_TIMEDRELAY_2          D6  //SmartThings Capability "Relay Switch"
  
static st::S_TimedRelay           sensor1(F("relaySwitch1"), PIN_TIMEDRELAY_1, LOW, true, 1, 1, 1);
static st::S_TimedRelay           sensor2(F("relaySwitch2"), PIN_TIMEDRELAY_2, LOW, true, 1, 1, 1);

Happy to try to help troubleshooting. Can you please post your entire sketch either here, or in a private message? That will help me to debug your actual code.

I have never seen the TimedRelay device not work as designed. The only thing that I can think which would mess up the timing is if there was a lot of extra code added to you loop() function in the sketch. Any delays or looping code inside of loop() will screw up all of the timing.