[RELEASE] ST_Anything - Arduino/ESP8266/ESP32

I’m not sure if I’m blind or what. The only references I can find are in the read me. which says:

 - Control: "doorControl1", "doorControl1", "doorControl1", ...

Which tells me the naming convention, and in the SmartThingsESP8266wifi sketch it says:

 #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

However there is no demo of this command in the sketch, and nothing that I found that has an explanation of the arguments to pass. Thanks for your time!

I see the confusion… Sorry about the typo in the ReadMe file - I have fixed the naming convention section for the doorControl.

Also, the ESP8266WiFi example sketch is one of the few sketches that does not have the IS_DoorControl object shown as an example. I have modified a version of it below for you as an example. It uses D1 and D2 for the Reed Switch (Contact Sensor) and Relay respectively (which means I commented out the other devices in the example sketch that were using those pins.)

As I mentioned before, depending on your relay board (Active Low Logic versus Active High Logic), you may need to tweak the constructor arguments in setup() routine where the IS_DoorControl object is created. It is set up for a typical Arduino Relay board (i.e. SainSMart) which uses Active Low logic (i.e. pull the pin to ground to activate the relay.) If you can let me know what relay you’re using, I might be able to help figure out what the proper settings are for it.

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

//Garage Door Pins 
#define PIN_DOORCONTROL_CONTACT_1 D1  //SmartThings Capabilty "Door Control" - the Contact Sensor (reed switch)
#define PIN_DOORCONTROL_RELAY_1   D2  //SmartThings Capabilty "Door Control" - the relay output

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

// Smartthings 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)
//  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_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             sensor3(F("contact1"), PIN_CONTACT_1, LOW, true);
  static st::IS_DoorControl         sensor3(F("doorControl1"), PIN_DOORCONTROL_CONTACT_1, LOW, true, PIN_DOORCONTROL_RELAY_1, LOW, true, 1000); //configured for Active Low Relay Board
  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);
  static st::EX_Alarm executor1(F("alarm1"), PIN_ALARM_1, LOW, true);
  //static st::EX_Switch executor2(F("switch1"), PIN_SWITCH_1, 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

  //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
  //Add each sensor to the "Everything" Class
  //Add each executor to the "Everything" Class
  //Initialize each of the devices which were added to the Everything Class

//Arduino Loop() routine
void loop()
  //Execute the Everything run method which takes care of "Everything"

Sweet, thanks! Makes much more sense now! Yes my relay pulls low to trigger. which is an issue since I plan on using this for the garage door and we occasionally have power outages. I have tried with the relay attached an it hangs on boot and also triggers the relays. No good… I plan on adding a Mosfet that on boot will be trigged after a short pause. Still in the works.

Also you have spent a bit of time on this. Do you have a donation page or something?


No donation page. I enjoy giving back to the community. Your success is my reward. (Although, I wouldn’t so “no” to new free hardware to integrate into ST_Anything! :slight_smile: If you have a new microcontroller or sensor that you’d like to see integrated, I am willing to listen!)

I am using a 2-channel relay board like the following on from Amazon. Using the exact same code I posted above, I do NOT have any cycling of the relays during an NodeMCU ESP8266 power on or reset cycle. I am powering the relay module with a constant 5V and triggering it with the GPIO pin D2 of the NodeMCU ESP8266. If you’re having issues during power on, I’d recommend trying different NodeMCU ESP8266 GPIO pins for the relay output to see if you can find one that works best for your application

I have done some research (as noted in the ReadMe) regarding the various limitations of the GPIO pins of the NodeMCU ESP8266 based boards (LoLin v3 and Amica boards.) There are definitely GPIO pins you should avoid to prevent spurious state changes during the reset/power on cycle. I believe D1, D2, D5, D6, and D7 are the safest pins to use based on my research and experimental findings. YMMV, of course.

Well thanks again for all your time. This is exactly what ST needs. I have the exact same relay board, I think I actually used a link in one of your other threads to buy it. After looking at the GPIO list I think I might be using D8, which would explain my problem.

My other projects after I get this completed are: ESP8266 with accelerometer on the washer and dryer to notify me when they are done. I have already datalogged both my washer and dryer with an Arduino UNO, next hurdle is I2C with the ESP8266. Other project is DIY motorized curtains.

Yep, D8 is not the best choice for a Active Low Logic relay board.

For my Washer and Dryer, I am using an Aeon Labs 1st Gen Power Meter with a custom Device Handler. I was able to buy a few of these for <$15 each during a sale a few months back.

And here is the custom Device Handler which sends “button pushed” events to CoRE to send my wife and me text messages each time either the washer or dryer is complete. It works very well. I tried using a SmartThings Multisensor’s accelerometer, but it wasn’t reliable. The power meter has been working great for me.

1 Like

:smiley: Dunno about the others in here, but I’d be up for reading a blog post one day about all the quirky automations you’ve got running in your house Dan.

1 Like

Interesting idea, although I really don’t have too many quirky automations actually running. I do still run an Arduino UNO + Thingshield (using very old code still) to act as a Temperature + Humidity, sensor Motion sensor, 4 door sensors, and 2 garage doors controllers. That was the project that started it all. I hope to replace the UNO/Thingshield with a NodeMCU ESP8266 in the near future as the performance is much quicker. I am hoping it is as reliable!

The washer/dryer monitoring system was something others in the community had come up with. I was planning on using an ESP8266 to monitor the power usage of each of these devices, and I had even bought a pair of clamp-on current transformers for the project. But then there was that awesome deal on the Aeon v1 power monitoring z-wave devices. It was too good a deal to pass up, and was nicely and neatly packaged already.

I have considered building a whole home power monitoring system, with each breaker in my panel being monitored independently. I just haven’t found the energy for that project :wink:

The rest of my SmartThings devices are pretty normal - GE Z-Wave switches and dimmers, a few GE Link bulbs, ST Multi sensors and motion sensors here and there throughout the house, a water sensor monitoring the Air Conditioning drip pan, a siren, etc…

I really enjoy putting the puzzle pieces together with respect to the Arduino C++ world and the physical devices, sensors, and actuators. That’s why I am always intrigued by use cases other come up with. Here are some of the things that I’ve added to ST_Anything as user requests:

  • RCSwitch (inexpensive 433MHz RF devices)
  • Adafruit Thermocouple
  • Dallas Semiconductor DS18B20
  • A paddle wheel pulse counter to measure water flow using Arduino Hardware Interrupts
  • A dimmable switch (using Arduino PWM output)

One of my other fun projects was tearing apart a Cree Connected light bulb to get its Zigbee module. I then connected this to an Arduino Nano and am able discern On/Off and 100 dim levels, each of which can be used to execute a routine on the Arduino as the user sees fit. Amazing how the NodeMCU ESP8266 modules are now so much cheaper than the light bulb + arduino hack!


Was thinking ahead to the motorized curtains, what would be the best way to go about this?
I would like to pass a value to the ESP8266 (0-100% like a dimmer?) to denote a position. The only two executor classes you have are the Alarm, and Switch, can others be added? Can you point me in the right direction?

There is a dimmer switch class as well. Look in the ST_Anything library folder and you’ll find it. Read the comments at the top of the .h or .cpp file. You will need to tweak it to run the motor.

Note: I do not believe I have created a Child Dimmer Switch Device Handler yet… if not, I should be able to create one this weekend.

1 Like

Hey Dan have you looked into the MQ2 Smoke, Gas, & CO2 sensor?

I’ll send you one for testing if needed

Got it all set up thanks!

I have a question, can you group/hide child things, like multi sensor do. (ie lux,temp,humidity only show when opening parent sensor?, not crowing up things list )

FYI I’m very novice at this stuff, but with your great instructions i was able to get this far.

Thanks again,

@Nathan909 - Nathan,

Glad to see you got it up and running! In answer to your question, YES, you can very easily hide the child devices, however you will not be able to rename them, move them to separate ‘rooms’, or delete them individually.

In order to change the behavior, simply edit the following line of code in the Parent_ST_Anything_Ethernet device handler. Modify the “isComponent: false” to be “isComponent: true” instead. Save and Publish-For Self again.

 				addChildDevice(deviceHandlerName, "${device.deviceNetworkId}-${deviceName}${deviceNumber}", null,
                	[completedSetup: true, label: "${device.displayName} ${deviceName}${deviceNumber}", 
                	isComponent: false, componentName: "${deviceName}${deviceNumber}", componentLabel: "${deviceName}${deviceNumber}"])

You will need to delete the current parent device (which will delete all of the children as well) and then manually recreate it again. Afterwards, you will only have a single device in the Things view.

1 Like


I have not looked into adding the MQ2 Smoke, Gas, & CO2 sensor. I will happily add support for it if you send me one for testing. Please private message me so we can work out the details.


@ogiewon - Hey Dan,

I am beyond thankful for all the work you have done with ST Anything! I’m just now getting the time to implement everything around the house and it has been amazing and so much easier to do everything that I’ve wanted to do now that you have written all this code!

I do have a question that is related to the Alarm Panel, but it’s not specific to just the Alarm Panel.
I’m using NodeMCU and running the contact sensors through the digital pins and its working perfectly, but I have roughly 24 sensors to attach. The previous home owners when they built the house had all windows and doors wired, but only the main doors actually hooked up to the original Alarm Panel. So lucky for me I will have every conceivable entry into my home armed and monitored!

The question is, can I use a Shift Register with my set up and get all 24 sensors running on the NodeMCU or is there going to be a limitation with the RAM on the device?

Example of a Shift Register: https://www.arduino.cc/en/Tutorial/ShiftOut

I know I can just add more arduino’s, but if possible I always try to trim down my projects to the absolute minimum needed for it to function, but for aesthetics and practical purposes…I try to keep it as clean as possible.

I will be running some tests here shortly, but just thought I’d ask for some feedback that could possibly prevent some headache in the future.

Thank you again for developing all this!

P.S. - Alot of this is over my head, but I’m learning!

I haven’t tried this my self but you may be able to use this.

If you need more analog inputs.

What am I doing wrong?

I have had 2 thingshields for almost a year and not really done anything with them. Every project I came up with other than full window open/close monitor I found hardware for sale already.

Now I am working on triggering switches in my bathroom for vent fans based on humidity (showers) and have returned to my thingshield and arduino + DHT22 sensor collection.

I have followed the directions and added the GitHub repo in to the arduino IDE I opened the sample project for ST_Anything_Temperatures.ino as that is closest to what I want to replicate.

I compiled and uploaded the project to the UNO. I seem to have forgotten something like I can’t flash with the thingshield attached or I have to move the hard switch on the shield and reset to flash? Anyway I have gotten the project loaded on to the UNO and paired the thingshield up with smartthings. I changed its device handler to the parent ST_Anything type. I have let it go for a while and I see nothing when I open the item in my smartthings “Thing” collection other than Refresh and Configure. Neither of which seem to do anyting save one of the serial comms LED’s flashed on the arduino. Their are no sensors connected to the arduino as I only have the 3 DHT 22’s and not the thermocouples anyway.

As I understand it child objects should automatically be added by what is defined in the arduino project but I don’t see that happening. No tiles, I added buttons, nothing happens. I am wondering what i am missing?

The ST_Anything_Temperatures.ino sketch is located in the deprecated folder for a reason… :slight_smile: Those sketches are old examples that pre-date the new Parent/Child Device handlers.

Please start with the new ST_Anything_Multiples_Thingshield.ino sketch. Just comment out all of the extra sensors/devices in the setup() routine other than the DHT22 sensor.

This sketch will then work as expected and auto create the child Temperature and Humidity devices.

I understand the request, however I do not have any plans currently to extend the I/O capabilities of the NodeMCU ESP8266 boards beyond what they have onboard. Adding in an I/O expander might be a good future enhancement. Or perhaps, the newer ESP32 will provide a higher GPIO count?

For now, I’d recommend you wire the various window sensors in series to create a traditional alarm zone. If any of the sensors wired together in series are “opened” then that zone will report as such. This allows you to dramatically reduce your I/O pin requirements.

Another option would be to add multiple ESP8266’s to handle everything. They are inexpensive, but they could get a little crowded.

Or, if you could hard wire some Cat5 Ethernet to your panel, an Arduino MEGA + W5100 or W5500 shield would provide you with 54 Digital and 16 Analog IO pins to choose from.

Lol and there you go… Im doing it wrong :smiley: Will test out in a bit

1 Like