[DEPRECATED] ST_Anything - Arduino/ESP8266/ESP32

The examples that Dan’s put together are really complete. There’s an example for just about everything. And a little tip I learned the hard way, comment out lines you don’t need with the “//” rather than delete them. You may decide you need it again and then you have to go back and grab it again. Also, if you’re working the Arduino IDE, save the sketch with a new name before you compile…otherwise the example will be overwritten.

1 Like

I have your sketch up and running and will keep an eye on the free ram to see if it drops here as well.

Sorry, the sketch is a little messy as i was just adding the blinking onboard led for debugging.

These are for the onboard led blinking:

int ledState = LOW;
long previousMillis = 0;
int interval = 1000;
pinMode(2, OUTPUT);

The problem was happening before i added the blink code. The polling time didn’t change from the original ST Anything code. I was planning to change that once i got everything up and running.

The contact sensor is just connected to VCC (3.3V) for now by a wire. Once i get this running i’ll add more contact sensors and use a magnetic door contact sensor.

My plan for this project is to monitor 3 garage doors, monitor temp and humidity, and control the garage doors as well.

@Trevorb1

Looks like you’re not using a DHT22 that is pre-attached to a breakout board. I think you may need to add a 10K resistor to your circuit as follows:

My free ram is holding fairly constant…

Everything: Free Ram = 40512
Everything: Sending: temperature1 75.56
Everything: Sending: humidity1 38.00
Everything: Sending: temperature1 75.56
Everything: Sending: humidity1 37.90
Everything: Sending: temperature1 75.56
Everything: Sending: humidity1 38.10
Everything: Sending: temperature1 75.56
Everything: Sending: humidity1 38.30
Everything: Free Ram = 40672
Everything: Sending: temperature1 75.56
Everything: Sending: humidity1 38.30
Everything: Sending: temperature1 75.56
Everything: Sending: humidity1 38.40
Everything: Sending: temperature1 75.56
Everything: Sending: humidity1 38.50
Everything: Sending: temperature1 75.56
Everything: Sending: humidity1 38.50
Everything: Free Ram = 40592
Everything: Sending: temperature1 75.56
Everything: Sending: humidity1 39.00
Everything: Sending: temperature1 78.08
Everything: Sending: humidity1 42.50
Everything: Sending: temperature1 77.90
Everything: Sending: humidity1 37.40
Everything: Sending: temperature1 77.54
Everything: Sending: humidity1 36.60
Everything: Free Ram = 40736
Everything: Sending: temperature1 77.18
Everything: Sending: humidity1 36.50
Everything: Sending: temperature1 77.18
Everything: Sending: humidity1 36.80
Everything: Sending: temperature1 77.00
Everything: Sending: humidity1 37.00
Everything: Sending: temperature1 76.82
Everything: Sending: humidity1 37.00
Everything: Free Ram = 40656
Everything: Sending: temperature1 76.64
Everything: Sending: humidity1 36.80
Everything: Sending: contact1 open
Everything: Sending: temperature1 76.64
Everything: Sending: humidity1 36.80
Everything: Sending: temperature1 76.64
Everything: Sending: humidity1 37.00
Everything: Sending: temperature1 76.46
Everything: Sending: humidity1 37.00
Everything: Free Ram = 40736
Everything: Sending: temperature1 76.46
Everything: Sending: humidity1 37.00
Everything: Sending: temperature1 76.46
Everything: Sending: humidity1 37.10
Everything: Sending: temperature1 76.28
Everything: Sending: humidity1 37.10
Everything: Sending: temperature1 76.28
Everything: Sending: humidity1 37.30
Everything: Free Ram = 40672
Everything: Sending: temperature1 76.28
Everything: Sending: humidity1 37.10
Everything: Sending: temperature1 76.28
Everything: Sending: humidity1 37.10
Everything: Sending: temperature1 76.28
Everything: Sending: humidity1 37.30
Everything: Sending: temperature1 76.28
Everything: Sending: humidity1 37.20
Everything: Free Ram = 40840
Everything: Sending: temperature1 76.10
Everything: Sending: humidity1 37.20
Everything: Sending: temperature1 76.28
Everything: Sending: humidity1 37.30
Everything: Sending: temperature1 76.10
Everything: Sending: humidity1 37.40
1 Like

Here my my sketch that monitors and controls 2 garage doors, monitors 4 other doors, measures Temp/Humidity, and detects motion in my garage. This is for an Arduino MEGA + W5500 shield, but the device definitions may prove useful to your project. Note this version uses the correct hub port for a Hubitat hub. Make sure you keep using the SmartThings Hub Port.

//******************************************************************************************
//  File: HubDuino_Door_EthernetW5500.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 W5500 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 W5500 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)
//              - 4 x Contact Sensor devices (used to monitor magnetic door sensors)
//              - 1 x Motion devices (used to detect motion)
//              - 1 x Temperature Measurement devices (Temperature from DHT22 device)
//              - 1 x Humidity Measurement devices (Humidity from DHT22 device)
//
//            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 Ethernet2 W5500 Shield.
//    
//  Change History:
//
//    Date        Who            What
//    ----        ---            ----
//    2018-02-11  Dan Ogorchock  Original Creation
//
//******************************************************************************************
//******************************************************************************************
// SmartThings Library for Arduino Ethernet W5500 Shield
//******************************************************************************************
#include <SmartThingsEthernetW5500.h>    //Library to provide API to the SmartThings Ethernet W5500 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 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

//**********************************************************************************************************
//Define which Arduino Pins will be used for each device
//  Notes: Arduino communicates with both the W5500 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 W5500 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 W5500 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/ArduinoEthernetShield for details on the W5500 Sield
//**********************************************************************************************************
//"RESERVED" pins for W5500 Ethernet Shield - best to avoid
#define PIN_4_RESERVED            4   //reserved by W5500 Shield on both UNO and MEGA
#define PIN_1O_RESERVED           10  //reserved by W5500 Shield on both UNO and MEGA
#define PIN_11_RESERVED           11  //reserved by W5500 Shield on UNO
#define PIN_12_RESERVED           12  //reserved by W5500 Shield on UNO
#define PIN_13_RESERVED           13  //reserved by W5500 Shield on UNO
#define PIN_50_RESERVED           50  //reserved by W5500 Shield on MEGA
#define PIN_51_RESERVED           51  //reserved by W5500 Shield on MEGA
#define PIN_52_RESERVED           52  //reserved by W5500 Shield on MEGA
#define PIN_53_RESERVED           53  //reserved by W5500 Shield on MEGA


//Analog Pins

//Digital Pins
#define PIN_TEMPERATUREHUMIDITY_1 22  //SmartThings Capabilities "Temperature Measurement" and "Relative Humidity Measurement"
#define PIN_MOTION_1              24  //SmartThings Capability "Motion Sensor"
#define PIN_CONTACT_1             26  //SmartThings Capability "Contact Sensor"    KITCHEN DOOR
#define PIN_CONTACT_2             27  //SmartThings Capability "Contact Sensor"    FRONT DOOR
#define PIN_CONTACT_3             28  //SmartThings Capability "Contact Sensor"    BACK DOOR
#define PIN_CONTACT_4             29  //SmartThings Capability "Contact Sensor"    GARAGE BACK DOOR

//Garage Door Pins 
#define PIN_DOORCONTROL_CONTACT_1 34  //SmartThings Capabilty "Door Control"       DAN'S GARAGE DOOR
#define PIN_DOORCONTROL_RELAY_1   35  //SmartThings Capabilty "Door Control" 
#define PIN_DOORCONTROL_CONTACT_2 36  //SmartThings Capabilty "Door Control"       TRICIA'S GARAGE DOOR
#define PIN_DOORCONTROL_RELAY_2   37  //SmartThings Capabilty "Door Control" 

//Pushbutton Pins

//******************************************************************************************
//W5500 Ethernet Shield Information
//****************************************************************************************** 
byte mac[] = {0x90,0xA2,0xDA,0x11,0x06,0x5F}; //MAC address                                 //  <---You must edit this line using the MAC address provided with your W5500 Shield!
IPAddress ip(192, 168, 1, 130);               //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

// Hubitat hub information
IPAddress hubIp(192,168,1,144);               // Hubitat hub ip                             //  <---You must edit this line!
const unsigned int hubPort = 39501;           // Hubitat 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 
  static st::PS_TemperatureHumidity sensor1(F("temphumid1"), 15, 0, PIN_TEMPERATUREHUMIDITY_1, st::PS_TemperatureHumidity::DHT22,"temperature1","humidity1");
  
  //Interrupt Sensors 
  static st::IS_Motion              sensor2(F("motion1"), PIN_MOTION_1, HIGH, true, 500);
  static st::IS_Contact             sensor3(F("contact1"), PIN_CONTACT_1, LOW, true, 500);
  static st::IS_Contact             sensor4(F("contact2"), PIN_CONTACT_2, LOW, true, 500);
  static st::IS_Contact             sensor5(F("contact3"), PIN_CONTACT_3, LOW, true, 500);
  static st::IS_Contact             sensor6(F("contact4"), PIN_CONTACT_4, LOW, true, 500);
  static st::IS_DoorControl         sensor7(F("doorControl1"), PIN_DOORCONTROL_CONTACT_1, LOW, true, PIN_DOORCONTROL_RELAY_1, LOW, true, 1000);
  static st::IS_DoorControl         sensor8(F("doorControl2"), PIN_DOORCONTROL_CONTACT_2, LOW, true, PIN_DOORCONTROL_RELAY_2, LOW, true, 1000);

  //Special sensors/executors (uses portions of both polling and executor classes)

  //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 EthernetW5500 Communications Object
    //STATIC IP Assignment - Recommended
    //st::Everything::SmartThing = new st::SmartThingsEthernetW5500(mac, 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::SmartThingsEthernetW5500(mac, 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);
    
  //*****************************************************************************
  //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();
}

Not sure but this might be related to your free ram issue https://github.com/esp8266/Arduino/issues/4497

3 Likes

Thanks Jason, I think that fixed it. I downgraded the ESP8266 library from 2.4.1 to 2.3 and the memory has been stable ever since. Thank you everyone for your help!

1 Like

Nice find Jason! Thanks!

I appear to be running ESP8266 2.4.0 without any memory leak… Good know about this issue!

@ogiewon & @Ryan780 - Do you guys know if I can carry 1.2 amps over 2-strands of CAT 5E wire (50 ft cable) (12V) for a couple seconds? And then 120mA on standby?

It would be nice if I can tap into my old CCTV cameras as a source of power for my electric drop bolt? And I don’t think the D1 Wemos will have an issue being powered with 12V to 5V (DC-DC converter).

I can upgrade the current 12V power source for the IP camera so that won’t be an issue…just want to make sure if 2-strands of CAT 5E can carry the amps…if not I will see if I can use 4 strands. Worse case…I will need to find an electrician to maybe tap into one of my outdoor light fixtures or use power from my doorbell.

Here is a test I did with a voltmeter…(50 ft wire - slightly better CAT6A) but connections are poor :
Results for a single strand:

Results for two strands:

Not sure how to interpret these results.

I can also run 48 volts and use a 48V to 12V converter to prevent voltage drop…but hopefully the resistants isn’t too high.

24 gauge copper stranded is rated for 360ma only. And btw… It’s called Google. Try it sometime. :grin:

1 Like

Hi,

Sorry if this has been brought up before!

I have a Crazepony-UK 2pcs ESP8266 Module ESP-12E NodeMcu with a PIR sensor connected to D5. The unit works great for about 1 - 2 hrs but then it seems to go offline. I have to turn the power ON/OFF to get it to start reporting back to ST.

I am not an expert and only tried a few things with ST, so any help would really be appreciated.

Regards

Dale

Sounds like you may have found the same bug another user found a day or two ago…

The discussion is just a few post above this one.

HI Thanks for the reply,

looking at the serial monitor it does look the RAM is going down very fast. I only want to use the device as a motion sensor. What is using all the data?

I will have a read through the link you sent.

thanks

Dale

I am not sure where the memory leaks is precisely. It is not in any of the ST_Anything code. It seems to be a problem in the ESP866 Arduino Board package. Try using an earlier version, like 2.3, to see if it resolves the memory leak.

I tried 2.3 and the problem has gone.

thnaks

Dale

1 Like

This a good Idea as I have several different modified DH’s for my purposes using the contact sensor. For instance Tank “Full” (Green) or “Low” Red. Would it be possible to also add a color choice for each open and close state. Like allow user to not only select the name but also select color for each case; Red, Green, Blue, or Orange etc…?

The short answer is yes its possible. The long answer is you would need to have a entry for each states color which would get completely unmanageable if they were select-able options. It’s doable but for each color choice you’d allow the user to select you’d be increasing the DTH code by 5% - 8%.

With that said you can modify my DTH yourself. if you look at the code you will see a section under the tile definition called “Regular” and “Inverted”. This is where I’m reversing the colors. The colors are based on the SmartThings Tiles Documentation so #e86d13 is orange and #00a0dc is blue. Just change all of those to the colors you want. #44b621 is green and #bc2323 is red for example.

If you think you’ll use it I can add the “Full” and “Low” options in pretty easily also. I tried to make it a single DTH for any contact closure situation that I personally would have but I’m sure their are others.

Has any work been done with esp.deepsleep? I’m following this project, but it seems like it’d require the esp8266/arduino/etc device to be on the entire time, or am I misunderstanding it?

In my case, it’s a simple wifi/wireless humidity sensor. It’s running off a 18650 battery so being able to sleep is very important… Right now I’m using a combination of homeassistant/mqtt-smartbridge bridge… Would love to simplify using this, but not sure if it’s realistic given that this project won’t let the device sleep? Basically it wakes up every 5 minutes to report the temp/humidity through mqtt to homeassistant then goes back to sleep. I’m still working on the smartthings-mqtt bridge part of it. Would love to switch to this project if it can handle the device napping…

(Or am I totally missing something here?)

-mark

Mark,

You are in luck! I had another ST_Anything user attempt the exact same thing, and it works!

Basically, ST_Anything is already designed to perform the WiFi connection and an initial status update to SmartThings as part of the sketch’s setup() routine. This means if you deep sleep at the end of the setup() routine, and tell it to wake back up in 5 minutes, you’ll be all set.

When an ESP8266 wakes up from deep sleep, it just restarts the board from scratch, thereby running the setup() routine again. And thus the process is sustained.

In this case, you do not need to have any code in the loop() routine as it will never get called.

This also means that your device can only send data. It will never be able to receive data, which is probably fine for your use case.

Dan

awesome, thanks. I’ll have to try it tonight.

Which sketch would you thing is the closest out of your examples for a simple humidity sensor? (I’m using bme280 which I didn’t see… I guess the dht22 is the closest?)

-mark