[DEPRECATED] ST_Anything - Arduino/ESP8266/ESP32

You are very close to being done. Yes, you really should try using the new Parent/Child device handlers and start with the ST_Anything_Multiples_EthernetW5100.ino sketch. simply comment out all of the “devices” you don’t need, except for the contact sensors. Then add in your custom pin definitions, and use them, 1 per contact sensor device, when you create all of the “contact1”, “contact2”, “contact3”, etc… devices in the setup() routine. You must use the names as I just typed them for the child devices to be automagically created. Do not name anything in the sketch “frontDoor”, for example. You must use the “contact1” nomenclature. This string is sent to the Parent DH which is parsed. If it finds a child does not exist, it auto creates it based on the name provided. For example, a string containing “contact” results in the automatic creation of a child device using the “Child Contact Sensor” DH.

After all of the children devices are created, you can rename them to meaningful names like “Front Door”. This device name will then appear in standard SmartApps like Smart Home Monitor, CoRE, WebCoRE, ActionTiles, Smart Lighting, etc… It is these applications that produce notifications (push and/or SMS text messages.)

Thank you! Are these the “devices” i should be commenting out in the ino file? (see attached)

what do i use to mess with my siren? switch relay?

Thanks again, you are the man

Here is what I mean. This is a sketch that should have everything you need based on what you’ve posted above. I haven’t tested it as I am not at home currently. Of course, you’ll need to configure the networking section for your home network. It has 13 contact sensors digital inputs, 1 motion detector digital input, and 1 alarm (a digital output that you would attached to a relay to power a siren.)

//******************************************************************************************
//  File: ST_Anything_Multiples_EthernetW5100.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 Arduino + Ethernet W5100 Shield 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 Ethernet W5100 Shield.
//
//            ST_Anything_Multiples implements the following ST Capabilities in multiples of 2 as a demo of what is possible with a single Arduino
//              - 2 x Door Control devices (used typically for Garage Doors - input pin (contact sensor) and output pin (relay switch)
//              - 2 x Contact Sensor devices (used to monitor magnetic door sensors)
//              - 2 x Switch devices (used to turn on a digital output (e.g. LED, relay, etc...)
//              - 2 x Water Sensor devices (using an analog input pin to measure voltage from a water detector baord)
//              - 2 x Illuminance Measurement devices (using a photoresitor attached to ananlog input)
//              - 2 x Voltage Measurement devices (using a photoresitor attached to ananlog input)
//              - 2 x Smoke Detector devices (using simple digital input)
//              - 2 x Carbon Monoxide Detector devices (using simple digital input)
//              - 2 x Motion devices (used to detect motion)
//              - 2 x Temperature Measurement devices (Temperature from DHT22 device)
//              - 2 x Humidity Measurement devices (Humidity from DHT22 device)
//              - 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...))
//              - 2 x Button devices (sends "pushed" if held for less than 1 second, else sends "held"
//              - 2 x Alarm devices (using a simple digital output)
//
//            During the development of this re-usable library, it became apparent that the 
//            Arduino UNO R3's very limited 2K of SRAM was very limiting in the number of 
//            devices that could be implemented simultaneously.  A tremendous amount of effort
//            has gone into reducing the SRAM usage, including siginificant improvements to
//            the SmartThings Arduino library.
//
//            Note: This sketch was fully tested on an Arduino MEGA 2560 using the Ethernet W5100 Shield.
//    
//  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-16  Dan Ogorchock  New sketch to demonstrate multiple SmartThings Capabilties of each type
//
//******************************************************************************************
//******************************************************************************************
// SmartThings Library for Arduino Ethernet W5100 Shield
//******************************************************************************************
#include <SmartThingsEthernetW5100.h>    //Library to provide API to the SmartThings Ethernet W5100 Shield

//******************************************************************************************
// 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 on an analog input pin 
#include <PS_Voltage.h>      //Implements a Polling Sensor (PS) to measure voltage on an analog input pin 
#include <PS_TemperatureHumidity.h>  //Implements a Polling Sensor (PS) to measure Temperature and Humidity via DHT library
#include <PS_Water.h>        //Implements a Polling Sensor (PS) to measure presence of water (i.e. leak detector) on an analog input pin 
#include <IS_Motion.h>       //Implements an Interrupt Sensor (IS) to detect motion via a PIR sensor on a digital input pin
#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_CarbonMonoxide.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/cycle repeat capabilities

//**********************************************************************************************************
//Define which Arduino Pins will be used for each device
//  Notes: Arduino communicates with both the W5100 and SD card using the SPI bus (through the ICSP header). 
//         This is on digital pins 10, 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega. 
//         On both boards, pin 10 is used to select the W5100 and pin 4 for the SD card. 
//         These pins cannot be used for general I/O. On the Mega, the hardware SS pin, 53, 
//         is not used to select either the W5100 or the SD card, but it must be kept as an output 
//         or the SPI interface won't work.
//         See https://www.arduino.cc/en/Main/ArduinoEthernetShieldV1 for details on the W5100 Sield
//**********************************************************************************************************
//"RESERVED" pins for W5100 Ethernet Shield - best to avoid
#define PIN_4_RESERVED            4   //reserved by W5100 Shield on both UNO and MEGA
#define PIN_1O_RESERVED           10  //reserved by W5100 Shield on both UNO and MEGA
#define PIN_11_RESERVED           11  //reserved by W5100 Shield on UNO
#define PIN_12_RESERVED           12  //reserved by W5100 Shield on UNO
#define PIN_13_RESERVED           13  //reserved by W5100 Shield on UNO
#define PIN_50_RESERVED           50  //reserved by W5100 Shield on MEGA
#define PIN_51_RESERVED           51  //reserved by W5100 Shield on MEGA
#define PIN_52_RESERVED           52  //reserved by W5100 Shield on MEGA
#define PIN_53_RESERVED           53  //reserved by W5100 Shield on MEGA


//Analog Pins

//Digital Pins
#define PIN_CONTACT_1             22  //SmartThings Capability "Contact Sensor"
#define PIN_CONTACT_2             23  //SmartThings Capability "Contact Sensor"
#define PIN_CONTACT_3             24  //SmartThings Capability "Contact Sensor"
#define PIN_CONTACT_4             25  //SmartThings Capability "Contact Sensor"
#define PIN_CONTACT_5             26  //SmartThings Capability "Contact Sensor"
#define PIN_CONTACT_6             27  //SmartThings Capability "Contact Sensor"
#define PIN_CONTACT_7             28  //SmartThings Capability "Contact Sensor"
#define PIN_CONTACT_8             29  //SmartThings Capability "Contact Sensor"
#define PIN_CONTACT_9             30  //SmartThings Capability "Contact Sensor"
#define PIN_CONTACT_10            31  //SmartThings Capability "Contact Sensor"
#define PIN_CONTACT_11            32  //SmartThings Capability "Contact Sensor"
#define PIN_CONTACT_12            33  //SmartThings Capability "Contact Sensor"
#define PIN_CONTACT_13            34  //SmartThings Capability "Contact Sensor"
#define PIN_MOTION_1		  35  //SmartThings Capability "Motion Detector"
#define PIN_ALARM_1               36  //SmartThings Capability "Alarm"


//Garage Door Pins 


//Pushbutton Pins
//#define PIN_BUTTON1               48  //SmartThings Capabilty Button / Holdable Button
//#define PIN_BUTTON2               49  //SmartThings Capabilty Button / Holdable Button

//******************************************************************************************
//W5100 Ethernet Shield Information
//****************************************************************************************** 
byte mac[] = {0x06,0x02,0x03,0x04,0x05,0x06}; //MAC address, leave first octet 0x06, change others to be unique //  <---You must edit this line!
IPAddress ip(192, 168, 1, 200);               //Arduino 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 withotu having to rely on the ST Cloud for time-critical tasks.
//******************************************************************************************
void callback(const String &msg)
{
  //Uncomment if it weould be desirable to using this function
  //Serial.print(F("ST_Anything_Miltiples 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(s) as you see fit)
  //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 
  static st::IS_Motion              sensor1(F("motion1"), PIN_MOTION_1, HIGH, false, 500);
  static st::IS_Contact             sensor2(F("contact1"), PIN_CONTACT_1, LOW, true, 500);
  static st::IS_Contact             sensor3(F("contact2"), PIN_CONTACT_2, LOW, true, 500);
  static st::IS_Contact             sensor4(F("contact3"), PIN_CONTACT_1, LOW, true, 500);
  static st::IS_Contact             sensor5(F("contact4"), PIN_CONTACT_2, LOW, true, 500);
  static st::IS_Contact             sensor6(F("contact5"), PIN_CONTACT_1, LOW, true, 500);
  static st::IS_Contact             sensor7(F("contact6"), PIN_CONTACT_2, LOW, true, 500);
  static st::IS_Contact             sensor8(F("contact7"), PIN_CONTACT_1, LOW, true, 500);
  static st::IS_Contact             sensor9(F("contact8"), PIN_CONTACT_2, LOW, true, 500);
  static st::IS_Contact             sensor10(F("contact9"), PIN_CONTACT_1, LOW, true, 500);
  static st::IS_Contact             sensor11(F("contact10"), PIN_CONTACT_2, LOW, true, 500);
  static st::IS_Contact             sensor12(F("contact11"), PIN_CONTACT_1, LOW, true, 500);
  static st::IS_Contact             sensor13(F("contact12"), PIN_CONTACT_2, LOW, true, 500);
  static st::IS_Contact             sensor14(F("contact13"), PIN_CONTACT_2, LOW, true, 500);

//  static st::IS_Button              sensor17(F("button1"), PIN_BUTTON1, 1000, LOW, true, 500);
//  static st::IS_Button              sensor18(F("button2"), PIN_BUTTON2, 1000, LOW, true, 500);

  //Executors
  static st::EX_Alarm               executor1(F("alarm1"), PIN_ALARM_1, LOW, true);

    
  //*****************************************************************************
  //  Configure debug print output from each main class 
  //*****************************************************************************
  st::Everything::debug=true;
  st::Executor::debug=true;
  st::Device::debug=true;
  st::PollingSensor::debug=true;
  st::InterruptSensor::debug=true;

  //*****************************************************************************
  //Initialize the "Everything" Class
  //*****************************************************************************
  
  //Initialize the optional local callback routine (safe to comment out if not desired)
  st::Everything::callOnMsgSend = callback;
  
  //Create the SmartThings EthernetW5100 Communications Object
  st::Everything::SmartThing = new st::SmartThingsEthernetW5100(mac, ip, gateway, subnet, dnsserver, serverPort, hubIp, hubPort, st::receiveSmartString);

  //Run the Everything class' init() routine which establishes Ethernet communications with the 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);
  st::Everything::addSensor(&sensor9); 
  st::Everything::addSensor(&sensor10); 
  st::Everything::addSensor(&sensor11);
  st::Everything::addSensor(&sensor12);
  st::Everything::addSensor(&sensor13);
  st::Everything::addSensor(&sensor14); 
    
  //*****************************************************************************
  //Add each executor to the "Everything" Class
  //*****************************************************************************
  st::Everything::addExecutor(&executor1);
  
  //*****************************************************************************
  //Initialize each of the devices which were added to the Everything Class
  //*****************************************************************************
  st::Everything::initDevices();
}

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

Duuuude. Thank youuuu!!!

You’re welcome! Did you get it working?

Haven’t tried yet…only looked over your code…which seems perfect…in ST api I have to upload each device handler, correct? Or just the parent and the sensor and smoke and alarm child’s? Also when uploading the new ino file to arduino, do I need to wipe device memory somehow? Or no. Out having beers now celebrating 15 year old contact sensors working with my arduino board, thanks to your help…so I’ll keep you posted…

Just edit the sketch with your network settings, and then upload it to the Arduino. It always wipes out the previous version of firmware.

You only need the parent + each child DH that you plan to use. I personally believe it is simpler to use the Github integration in the ST IDE and simply create all of the DH’s. Doing it this way only takes a few seconds to create them all. And, you can easily update them if/when I make changes to them.

I am trying to setup ST_Anything on an esp8266 board I have. I am in no way, shape, or form, a coder of any sort. I do however, like to tinker with stuff. Well, this was the latest thing I wanted to tinker with.

Anyways, I can’t seem to get this all going, probably just my lack of any coding ability.

Do the child device handlers need to be published for me? I know child smart apps don’t, so I figured not with handlers either?

Next, if I simply compiled and loaded your esp8266multiple sketch onto my esp8266 (without any modifications, aside from ssid/pass/ips), created the parent device, clicked on gear and entered the info in there, should it then create those 8-10 child devices? Even tho they aren’t really hooked up and wouldnt actually work.

That’s basically where I am at, I click the gear, enter device ip, port, and mac id, but no devices are ever created. Every time I reopen my ACE device, I get the warning message to click on gear to setup, even tho all the info is there.

Basically I want to use a esp8266 with 4 relays to operate my 2 garage doors and associated garage door lights. But I don’t want to tinker with any code changes until I get the default setup and working correctly.

Thanks.

Yes, you need to publish all of the device handlers.

Ok everything is published. Esp8266 is flashed with stock ino and connected to my wifi. Device is installed with the parent handler. All info entered into settings gear in ST. But I still get no Childs created, and get the message to click gear screen and enter all info and hit done.

Are you using ST Hub v2? You did get the MAC address from the Arduino IDE Serial Monitor window, correct? Did you properly set the hub’s IP address in your sketch?

What do you see in the live logging in the ST IDE?

What do you see in the Arduino IDE Serial Monitor window?

Arduino IDE Serial Monitor on boot. I have all devices except switches commented out…tried leaving all stock and same, just more devices initializing during boot.

0⸮⸮a⸮⸮⸮⸮⸮⸮Everything: init started
Everything: Free RAM = 46984
MAC Address = A0:20:A6:17:7C:D2
SmartThingsESP8266WiFI: Intialized
Everything: init ended
Everything: Free RAM = 46816
Everything: adding executor named switch1
Everything: Free RAM = 46816
Everything: adding executor named switch2
Everything: Free RAM = 46816
Everything: initDevices started
Everything: Free RAM = 46816
Everything: Sending: switch1 off
Everything: Sending: switch2 off
Everything: initDevices ended
Everything: Free RAM = 46792
Everything: Free Ram = 46816
Everything: Free Ram = 46816
Everything: Free Ram = 46816
Everything: Free Ram = 46816

I am using a packaged board, esp8266 plus relays all in one. So I’m wondering if it’s the board that’s the problem.

I wouldn’t think your board would be the issue. Can you ping the ESP8266 and the hub from a command prompt on your PC? Are both devices on the same subnet?

Did you set the hub IP address correctly in your sketch?

Yes, I can ping both the ESP and ST hub. Both are on same Subnet, and I do have the ST hub IP address in the sketch.

When you manually created the DEVICE in the ST IDE, using the published Parent DH, did you associate the device with both your hub and location? Can you please show a screen capture of what your DEVICE looks like in the ST IDE?

That was it right there. It was associated to my location, but not the hub. I see switches now.

Thanks for troubleshooting that, I would never have thought of it.

I’m sure I’ll be sending plenty more questions your way! lol

I appreciate your patience and persistence! It also helps me to know exactly what changes I need to make to the ReadMe file for the new design! The feedback is very helpful and welcome!

Wow, unbelievable how easy this is, once I got that one setting setup correctly. I thought I would need to do at least a little programming to make this work, but there is nothing to do. It just works, right out of the box.

My only question for now, is what capability would I use for a momentary button press? Is that coded in? Again, this is for multiple garage doors, momentary button press to a relay, to open or close the doors.

So, help me understand exactly how you plan on controlling your garage doors? I have a very specialized “doorControl” device which you supply two pins to in its constructor: 1 for a contact sensor to know if the door is open or closed, and 1 for a digital output which you would typically wire to a relay. The digital output for this device will only only keep the relay active a user definable duration (defaults to 1 second) to emulate a user pressing their garage door button on the wall (my relay is wired in parallel with that button!)

So, the question then becomes, what purpose would momentary pushbuttons connected to the Arduino serve? I can think of two ways to use them:

  1. You could define them as a SmartThings “Button”. In my “Multiples” example for the Arduino MEGA/W5100, I have defined two of them. These then send either a “pushed” or “held” from the Arduino/ESP8266, to the Parent Device Handler, to the rest of the ST Cloud. You can define how long they have to be “held” via a constructor argument. You could then use something like CoRE or SmartLighting to trigger your “doorControl” devices as desired (just turn them “on” like a switch and they will automagically turn themselves off after ~1sec.) This would probably be my preferred zero code option. You can also set up various rules about which ST Modes this feature would be active during. Note: You must manually define the correct number of “Buttons” in the Parent Device’s settings (gear icon) to match the number defined in your sketch. This value is used to announce to the SmartApps how many buttons are available to choose from.

  2. The other option requires further knowledge of how ST_Anything works. You would still define them as a SmartThings “Button” in your sketch. If you look in the sketch, you will see a function call “callback()” just before the “setup()” routine. This function allows you to see exactly what data is being sent from ST_Anything to the SmartThings cloud. You can “sniff” this data to see a command being sent to the cloud like “button1 pushed” and then take local action to turn on your doorControl device. The biggest advantage to this is that it will work whether or not ST is up and running. Here is an example of what I am talking about:

    void callback(const String &msg)
    {
    //Uncomment if it weould be desirable to using this function
    Serial.print(F("ST_Anything_Miltiples 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(s) as you see fit)

    if (msg == “button1 pushed”)
    {
    st::receiveSmartString(“doorControl1 on”); //use same strings that the Device Handler would send
    }
    if (msg == “button2 pushed”)
    {
    st::receiveSmartString(“doorControl2 on”); //use same strings that the Device Handler would send
    }

    }

Well, after your examples, I think I can just leave them as switches like I have now, and just make a CoRE rule, if switch1 is turns on, then wait 1 second and turn off. That would works for the garage door and lights I believe. That basically turns it to a momentary switch.

I really just want something to be able to control my doors with my phone, if I notice that they are open when I’m away. Right now I just have iris contact sensors on the doors, but no way to control the doors. I would like to put some wired sensors on them, but I’m pretty sure I would need to get a different 8266 to do the project. The one I currently have is an 8266 with 8 relays built onto the board. Don’t think there is any way that I can hook contact/motion sensors, or any temp/humidity probes to it, as there are not any pins accessible to connect to really. It’s not a normal esp2866 with pins to put into a breadboard.