[DEPRECATED] ST_Anything - Arduino/ESP8266/ESP32

Looks like you’re only using my “SmartThings Library” and not the “ST_Anything Library.” Your code looks like it should always keep ST up to date. However, you have quite a few “delay()” statements in your code. These are very unhealthy for these sketches as the delays cause problems with the WiFi communications. I recommend you remove all of the delay() statements and use other non-blocking methods to control timing.

Thanks Dan. Much appreciated. I will clean up my code and remove the delay statements.

Hi All,

Could you please help me, a newbie to the world of SmartThings and ESP8266, sort out a few basic things and move ahead?
I have 5V relay module with a 2x4 headers for mounting the ESP-01. I’m hoping to use this as a momentary switch to trigger a garage opener.
Guess this has been done before by a lot of others and I’ve been stumbling - a lot!

I want to use this module since it’s small and low-powered (5V) that doesn’t connect to 110v mains directly and I don’t want to accidentallly burn my house down! :slight_smile:

I’ve looked at the documentation and available libraries that Dan/Daniel Ogorchuk has put out (Thank you guys! What a team!).

I’m not clear if there is a specific firmware needed before I can flash the sketch for ESP-01.
The sketch I’m trying to upload is: ST_Anything_Multiples_ESP8266WiFi.ino
I have the logs from my sketch upload attempt:
https://pastebin.com/b8VCEHW3

The ESP-01 module I have came pre-loaded with some unknown firmware (don’t know if it’s a stock AI-Thinker one).

I’ve read that a sketch is a firmware so I presumed all I needed was the sketch and associated libraries.

Additionally I’ve been having trouble getting any AT commands run on the ESP-01 module since I keep getting a constant stream of junk/unreadable characters.
Not sure if I’ve blown/fried the chip. Surprisingly a second, recently acquired module also behaves in a similar way.
I’ve so far used multiple FTDI based programmers without luck. I’m having a CH340 and CP2102 programmers delivered tomorrow and will try with those.

If there is one, could you please point me to a link or resource that gives a step-by-step instructions for ESP-01 module?

Thanks so much!!
-Ashwin.
PS: This is the module I’m trying to use:
https://www.amazon.com/gp/product/B071WWMMDD/

Ashwin,

Since you are trying to use an ESP01 directly with ST_Anything running on it, please start with the example sketch designed specifically for that module, ST_Anything_ESP01WiFi.ino. As for programming the ESP01 via the Arduino IDE, you should start with a simple example, like BLINK, to make sure your programming circuit is wired up correctly.

Here is a good website that describes how to get started with programming the ESP01 via the Arduino IDE. You’ll need to wire up a special programming circuit with two buttons in order to program it.

Hi Daniel,

Thanks SO much for responding and providing the useful tips. Will definitely take another look at my circuit.

In the meantime I just got hold of another programmer that uses CH340G chip and I’m able to successfully communicate with the ESP-01 module with AT commands (Without the complicated circuit setup!). Also was able to upload the ST_Anything_Multiples_ESP01WiFi.ino sketch.

When I try to upload the ST_Anything_Multiples_ESP01WiFi.ino I get compilation errors stating:
ST_Anything_Multiples_ESP8266WiFi:86: error: ‘D2’ was not declared in this scope
#define PIN_CONTACT_1 D2 //SmartThings Capabilty “Contact Sensor”

I read somewhere else asking to replace the D1, D2 etc with the actual GPIO pin numbers like 0 and 1.
On ESP-01 the usable pins are 0 and 2. Should I just assign 0 to PIN_BUTTON_1 and 1 to PIN_BUTTON_2 ?

How can the device handler be configured to trigger individual pins 0 and 2?

Thanks.
Ashwin.

PS: By the way, the module doesn’t do anything after uploading the ESP01WiFi sketch. When I connect it to the PC in normal mode, I just get a constant stream of dots (…) in the serial monitor. Have I done something wrong?
I see faint flickering blue light. The module doesn’t appear to connect to my router. Any idea where I might have messed up? Thank you!

So, it appears you are still trying to compile the ST_Anything_Multiples_ESP8266WiFi,ino sketch and run it on the ESP01. You really need to stick the ST_Anything_Multiples_ESP01WiFi.sketch. You will not see the D1 and D2 pin references in the ESP01 specific sketch.

Please explain exactly what you’d like to accomplish with the ESP01. It sounds like you’d most likely want to use a TimedRelay device in order to control the relay output for controlling a garage door. How would you like to use the other pin on the ESP01?

I apologize for not putting out my thoughts correctly. I might have said things in the wrong order.

I did take your advice and I did upload the ST_Anything_Multiples_ESP01WiFi.sketch.
That was the very first sketch that I tried uploading. The upload went fine, but after restarting it doesn’t connect to the the ESP01 doesn’t connect to my router.
I made sure I edited the sketch correctly and updated the IP, Gateway, DNS, subnet, Hub address/port.

So after that, purely as an experiment (since I thought I had messed up the previous sketch) I tried to upload the ESP8266WiFi sketch just to see what happens. That’s when I found compilation errors.

My ultimate goal is very simple. I want to control a relay using ESP01 (The relay I’ve linked in my other post).

My apologies for any confusion and misunderstanding. I truly do appreciate all your effort in developing the sketches/handlers and also for helping others too.

Thanks.
Ashwin.

Came across this post by you ([RELEASE] ST_Anything v2.9 - Arduino/ESP8266/ESP32 to ST via ThingShield, Ethernet, or WiFi) which is what I’m trying to do.
But I still have quite a distance to cover before I get to that stage I guess. :smile:

Ashwin,

I am happy to help out. The device you are using, it has an ESP01 on it, correct? As such, you need to figure out which of the two GPIO pins is used to control the relay, and whether the relay is “Active High” or “Active Low”.

Here is an example ESP01 sketch to help you get started. This one uses 1 pin for a magnetic contact sensor (not sure if there is any way to use the second GPIO pin with the relay module you have). The other pin is used to activate the relay (it will turn on for 1 second and then turn off automatically.) Again - you may need to reverse the two pin assignments based on how your module is wired.

//******************************************************************************************
//  File: ST_Anything_Ashwin_ESP01WiFi.ino
//  Authors: Dan G Ogorchock & Daniel J Ogorchock (Father and Son)
//
//  Summary:  This Arduino Sketch, along with the ST_Anything library and the revised SmartThings 
//            library, demonstrates the ability of one ESP8266-01 (ESP-01) to 
//            implement a multi input/output custom device for integration into SmartThings.
//            The ST_Anything library takes care of all of the work to schedule device updates
//            as well as all communications with the ESP-01's WiFi.
//    
//            ST_Anything_Multiples implements the following ST Capabilities in multiples of 1 as a demo of what is possible with a single ESP-01
//              - 1 x Contact Sensor device (used to monitor magnetic door sensors)
//              - 1 x Relay Switch device (used to turn on a digital output for a set number of cycles And On/Off times (e.g.relay, etc...))
//
//  Note:  The tiny ESP-01 only has 2 GPIO pins, so this example is somewhat limited.  Use the ST_ANything_Multiples_ESP8266WiFi.ino example to see 
//         what else is possible.  As long as you only try using 2 pins, you can use them for whatever you'd like.
//
//  Change History:
//
//    Date        Who            What
//    ----        ---            ----
//    2015-01-03  Dan & Daniel   Original Creation
//    2017-02-12  Dan Ogorchock  Revised to use the new SmartThings v2.0 library
//    2017-02-21  Dan Ogorchock  New example specifically for running everythin on a ESP-01 (no Arduino required!)
//    2017-04-24  Dan Ogorchock  Updated for use with new v2.5 Parent/Child Device handlers
//
//******************************************************************************************
//******************************************************************************************
// 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_DS18B20_Temperature.h>  //Implements a Polling Sesnor (PS) to measure Temperature via DS18B20 libraries 
#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 <PS_MQ2_Smoke.h>    //Implements an Polling Sensor (PS) to monitor the status of an analog input pin from a MQ2 sensor
#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 capability with Siren and Strobe via digital outputs to relays
#include <S_TimedRelay.h>    //Implements a Sensor to control a digital output pin with timing/cycle repeat capabilities
#include <EX_Switch_Dim.h>   //Implements an Executor (EX) for a switch (on/off) and pwm output (level) uses 2 digital output pins

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

#define PIN_TIMEDRELAY_1           0
#define PIN_CONTACT_1              2

//******************************************************************************************
//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, 230);       //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 ESP-01
  //  Notes: - For each device, there is typically a corresponding "tile" defined in your 
  //           SmartThings Device Hanlder Groovy code, except when using new COMPOSITE Device Handler
  //         - For details on each device's constructor arguments below, please refer to the 
  //           corresponding header (.h) and program (.cpp) files.
  //         - The name assigned to each device (1st argument below) must match the Groovy
  //           Device Handler names.  (Note: "temphumid" below is the exception to this rule
  //           as the DHT sensors produce both "temperature" and "humidity".  Data from that
  //           particular sensor is sent to the ST Hub in two separate updates, one for 
  //           "temperature" and one for "humidity")
  //         - The new Composite Device Handler is comprised of a Parent DH and various Child
  //           DH's.  The names used below MUST not be changed for the Automatic Creation of
  //           child devices to work properly.  Simply increment the number by +1 for each duplicate
  //           device (e.g. contact1, contact2, contact3, etc...)  You can rename the Child Devices
  //           to match your specific use case in the ST Phone Application.
  //******************************************************************************************
  //Polling Sensors
 
  //Interrupt Sensors 
  static st::IS_Contact   sensor1(F("contact1"), PIN_CONTACT_1, LOW, true);

  //Special sensors/executors (uses portions of both polling and executor classes)
  static st::S_TimedRelay sensor2(F("relaySwitch1"), PIN_TIMEDRELAY_1, LOW, false, 3000, 0, 1);
  
  //Executors

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

  //*****************************************************************************
  //Initialize the "Everything" Class
  //*****************************************************************************

  //Initialize the optional local callback routine (safe to comment out if not desired)
  st::Everything::callOnMsgSend = callback;
  
  //Create the SmartThings ESP8266WiFi Communications Object
    //STATIC IP Assignment - Recommended
    st::Everything::SmartThing = new st::SmartThingsESP8266WiFi(str_ssid, str_password, ip, gateway, subnet, dnsserver, serverPort, hubIp, hubPort, st::receiveSmartString);
 
    //DHCP IP Assigment - Must set your router's DHCP server to provice a static IP address for this device's MAC address
    //st::Everything::SmartThing = new st::SmartThingsESP8266WiFi(str_ssid, str_password, serverPort, hubIp, hubPort, st::receiveSmartString);

  //Run the Everything class' init() routine which establishes WiFi communications with SmartThings Hub
  st::Everything::init();
  
  //*****************************************************************************
  //Add each sensor to the "Everything" Class
  //*****************************************************************************
    st::Everything::addSensor(&sensor1);
    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();
}

Just wanted to say this is working out great so far!! Thanks… Got 3 devices up and publishing:
Mega-ESP01, Adafruit Feather ESP, Mega-W1500 Ethernet.
On my Mega-W1500 I have a analog pressure sensor but theres no “pressure” child device… Would the best thing to do is use one of the AI type illumination or smoke detectors devices in its place?

I would use the PS_Voltage device as a starting point for a generic analog input device. You can even scale the analog input into engineering units (e.g. psi, mmHg, etc…} using th PS_Voltage’s built in map() scaling capabilities.

Hi Dan!

Yes, it’s an ESP-01 board with 2 GPIO pins (0 and 2).
Thank you very much for the sketch! I’m a step ahead I think. Here’s what I did so far:

  1. Edited, compiled and successfully uploaded the sketch.
    -> Now I can see the board in my list of wifi devices. Did a port scan and found port 80 is open (this is port I chose in the sketch).

  2. Added/Imported your code from GitHub and I can see a bunch of device handlers published for me in the ST IDE (See screenshot: https://imagebin.ca/v/3cyjDy9nT1NB)

  3. Added a new device of type Parent_ST_Anything_Ethernet and edited the parameters like MAC address, IP address and port. (https://imagebin.ca/v/3cymWmgnjxr1)
    –> At this point per your instructions the child devices should have been created, but I don’t see it anywhere on my IDE. I’ve ensured there are no duplicate devices with the same ID, but the events list still shows failure to create child device (From event log: ST_Anything Parent Device has not yet been fully configured. Click the ‘Gear’ icon, enter data for all fields, and click 'Done’).

  4. Searched for and tried to add the new device. Hub detects a “STAP_Test” device and added it.
    However there is no button to push etc.

  5. Tried to follow along with the rest of the instructions (https://github.com/DanielOgorchock/ST_Anything/blob/master/README.md) but didn’t make any progress. What am I doing wrong?

Thank you for all your time and effort.
-Ashwin.

I would:

A) Switch the default port of 8090 back and reupload to the device
B) Update your devices from the repo and make sure they are all up to date and published. There was a slight change just made to the “buttons” parameter that messes some people up. Or go into configure the parent form the SmartThings app and change the buttons to 1, hit done, then go back and change it to 0 (or however many you will have?)
C) Watch logging when you are doing this and once it’s done. You should see something like “Child device not found. Creating”. It can take a couple minutes.

You guys are amazing! I’m very grateful for all the help and advice you’re patiently giving.
I changed the port number and re-uploaded the firmware.
Also I had made a terrible mistake by mis-typing the MAC address.
I misread 5C as 5E (looks like I need a new pair of glasses), and entered in a notepad which I kept copying and pasting all the time.

Anyway, the child devices got created and the switch works.

That’s the good/great news.

Now, the relay can’t still be triggered. As I found out just yesterday on the esp8266 forums, the relay is triggered by sending a string of bytes to the serial port rather than through GPIO pins (On is A0 01 01 A2 and Off is A0 01 00 A1).

Just guessing, but could I modify S_TimedRelay.cpp or any other class to add a Serial.write() somewhere? I’ve not written a line of code in a very very long time.

I think (hope) that would conclude my seemingly never ending project.

Thank you.
Ashwin.

Glad to hear you’re making progress. Yes, you could easily modify the source code to issue the correct serial commands to turn it on and off. Just replace the digitalWrite calls.

Just a FYI for anyone that might be interested in this. I figured it out and all the code has been published into the standard ST_Anything library.

If anyone wants to try this on a ESP8266 please download a fresh copy of the library and you will see a new example sketch for RGB. I only have ESP32’s so I’d like to know that it works. It’s designed to control “analog” RGB LEDs. It can be individual LEDs (like Diffused RGB (tri-color) LED [Common Anode] : ID 159 : $2.00 : Adafruit Industries, Unique & fun DIY electronics and kits) or analog RGB strips (like Amazon.com which are only $9 for 15 feet). If it works correctly on a ESP8266, which I expect it to, I’ll add the code for RGBW also.

There is a cheap RGB controller currently, the H801, but it can only really do RGB control with one strip. The ESP8266 can do one strip plus a couple extra points and the ESP32 can do up to 4 RGB strips with a single controller again with additional points also. And since its runing ST_Anything it doesn’t just have to be a RGB controller, it can do all the other stuff ST_Anything already does. For example I plan on having a door chime hooked up to the same one doing RGB light control so I can hear when my doors are opened from the kitchen which I currently cannot. I also plan on using the single RGB LED for status of my alarm panel, changing color based on whats going on (doors open, system armed, etc).

Again if anyone has the ability to test please do and let me know.

Hi all,

Been lurking here for a while, and finally got some time to try my first st anything project!

I got an arduino mega with a 5100 shield and would like to hook it to an existing honey well security system in my house. It’s a P20 system with 2 control pads, 3 zones and a motion sensor.

I’ve been crash coursing through your readme (with a lot of trips to Wikipedia and YouTube) and have got the basics of getting sketches edited and on the arduino. Now I’m trying to figure out the hardware side and how I wire the arduino to the security panel circuit board. Is there any example you can point me to?

Little lost trying to figure out my next step.

Thanks in advance for the help and all the work you did on the st anything development.

The ST_Anything code has some examples. I used the one with all the different devices and commented out the ones I didn’t have. You mainly need one or two sensor types, so I think there is example with multiple switches that you could start with. I would use the switch example and just wire it up with two wires - connect the wires together to simulate a switch on.
Not sure how your integrating your P20, but seems like you have two easy options and one harder one:

  1. Use the output from the P20 Siren as input to the ST system - it would probably be a 9-20V input
  2. Rewire all the sensors to the ST_Anything (just multiple door and window sensor which are both just open/close). This option would probably require replacement of the controllers
  3. Integrate the ST_Anything with the P20, but this seems hard as you would need to learn a lot of technical details about the P20 and/or the controllers.
    Hope this helps, and good luck.

The first step is testing everything on your workbench before wiring anything to the old Alarm Panel wiring.

ST_Anything’s example sketches make use of the Arduino’s INTERNAL PULLUP RESISTOR feature to provide 5 VDC from the corresponding digital input pins that you choose for each “IS_Contact” sensor in your sketch’s setup() routine. This greatly simplifies wiring as you simply need to connect a jumper wire between the Arduino GND pin and a corresponding digital input pin that you’ve chosen for your contact sensors. This will complete the circuit and cause the ‘contact sensor’ to go from OPEN to CLOSED. Removing the jumper reverses the status.

Once you have this working, you can simply DISCONNECT one zone of wiring from your Alarm Panel (should be just 2 wires) and attach one wire to the Arduino GND and the other to the appropriate digital input pin. Now when a door or window from that zone opens, the circuit is broken and the status should change to “OPEN”. Closing all doors/windows for that zone will result in that contact sensor changing to the closed state. NOTE: please use a voltmeter to make sure no power is still on the pair of wires for the zone BEFORE attaching anything to the Arduino. Otherwise, you could fry your MEGA. Also, there are usually some resistors attached to the zone wiring. These can/should be removed from the circuit before attaching any wiring to the Arduino.

This is the easiest way to wiring things up. BUT, it means the old alarm panel and keypads becomes useless.

If this is not your intention, then we’ve got a few more lessons in basic electronics to review…

Hopefully this gets you started.

I am having great success with ST_anything. Great work! I am converting some arduino projects to smartthings + thingshield and running into some challenges I wasn’t prepared for in kindergarten.

Presently I use analogRead to get the temperature from a sensor and do some fancy pid math to control two relays (a heater and an AC compressor) using digitalWrite. If I want to trigger st::S_TimedRelay based on this input and have it operate exactly like a button press on the phone app, where do I put all my code? How do I write to the TimedRelay?

My workaround is to have the digitalWrite pin connect to a st::IS_Contact pin and use a SmartApp to activated TimedRelay based on the status of the Contact. This works but seems absurd. I have no idea how to do it cleanly because I’ve never cleanly done anything in Arduino.

As a simpler project, how would I with the Arduino alone read the status of a Contact and enable a TimedRelay (removing the SmartApp bridge)? This should still show contact closed and TimedRelay powered (until timer expires) within the phone app.

Thanks a ton!