[DEPRECATED] ST_Anything - Arduino/ESP8266/ESP32

@makrupka this thread is fine for asking questions. If things get too detailed, we can move the discussion to a Private Message thread.

Here is your Arduino sketch for running 2 Dallas Semiconductor DS18B20 temperature probes connected to the same 1-wire bus (i.e. only need 3.3v, gnd, and 1 GPIO pin to run both sensors.)

When you get your hardware, reply to this post and I can assist you with wiring specifics if you need help. Be sure to follow the ST_Anything ReadMe in my GitHub repository to get your Arduino IDE environment set up correctly, as well as the SmartThings Web-based IDE.

Good luck and have fun!
Dan

//******************************************************************************************
//  File: ST_Anything_Temperatures_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_Temperatures implements the following ST Capabilities as a demo of what is possible with a single NodeMCU ESP8266
//              - 2 x Temperature Measurement devices (Temperature from Dallas Semi 1-Wire DS18B20 device)
//    
//  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
//    2017-12-31  Dan Ogorchock  Simple example for two 1-Wire DS18B20 temperature sesnors
//
//******************************************************************************************
//******************************************************************************************
// 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)
// Note:  no need to uncomment these lines as the Arduino IDE automatically defines these now
//*************************************************************************************************
//#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_TEMPERATURE_1         D7  //SmartThings Capabilty "Temperature Measurement" (Dallas Semiconductor DS18B20)

//******************************************************************************************
//ESP8266 WiFi Information
//******************************************************************************************
String str_ssid     = "Your SSID Here";                         //  <---You must edit this line!
String str_password = "Your WiFi Password Here";                //  <---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

  // st::PS_DS18B20_Temperature() constructor requires the following arguments
  //        - String &name - REQUIRED - the name of the object (If just 1 DS18B20, use "temperature1".  If more than 1 DS18B20 on the same 1-Wire bus, use "temperature" as the number will be appended automatically)
  //        - long interval - REQUIRED - the polling interval in seconds
  //        - long offset - REQUIRED - the polling interval offset in seconds - used to prevent all polling sensors from executing at the same time
  //        - byte pin - REQUIRED - the Arduino Pin to be used for the One-Wire DS18B20 sensor conenction
  //        - bool In_C - OPTIONAL - true = Report Celsius, false = Report Farenheit (Farentheit is the default)
  //        - byte resolution - OPTIONAL - DS18B20 sensor resolution in bits.  9, 10, 11, or 12.  Defaults to 10 for decent accuracy and performance
  //        - byte num_sensors - OPTIONAL - number of OneWire DS18B20 sensors attached to OneWire bus - Defaults to 1
  static st::PS_DS18B20_Temperature sensor1(F("temperature"), 30, 0, PIN_TEMPERATURE_1, false, 10, 2); 
  
  //Interrupt Sensors 

  //Special sensors/executors (uses portions of both polling and executor classes)
  
  //Executors
  
  //*****************************************************************************
  //  Configure debug print output from each main class 
  //  -Note: Set these to "false" if using Hardware Serial on pins 0 & 1
  //         to prevent communication conflicts with the ST Shield communications
  //*****************************************************************************
  st::Everything::debug=true;
  st::Executor::debug=true;
  st::Device::debug=true;
  st::PollingSensor::debug=true;
  st::InterruptSensor::debug=true;

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

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

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

Hi I am also running into a similar problem with the Wemos D1 R2.

In file included from C:\Users\Documents\Arduino\libraries\SmartThings\SmartThingsThingShield.cpp:34:0:

C:\Users\Documents\Arduino\libraries\SmartThings\SmartThingsThingShield.h:68:28: fatal error: SoftwareSerial.h: No such file or directory

 #include <SoftwareSerial.h>

                            ^

compilation terminated.

exit status 1
Error compiling for board WeMos D1 R2 & mini

Iā€™ve check all of my directories and reinstalled the Arduino IDE. I am running the ST_Anything_Multiples_ESP8266WiFi.ino sketch. Any ideas? Thanks!!

I just successfully compiled the ESP8266 Multiples sketch without any error selecting the Wemos D1 R2 and mini board. You may have your libraries folder mis-configured.

Yeah it seems soā€¦ Iā€™m normally able to compile with no issues with the Wemos.

Here is my directory breakdown:

Arduino
    -hardware
    -libraries
        -ST_Anything*
        -SmartThings*
        -other
    -scripts
    -Sketches
        -ST_Anything*

Also, the ST_Anything and SmartThings libraries show up in my library manager as installed.

Thanks for the help and happy near year!

I just compiled for your scenario as well without issue.
Just a shot in the dark here, but your sketch seems to be bombing on the SoftwareSerial library.
Mine is located at the following location. You might want to see if that library exists.
C:\Users\YOURNAME\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\SoftwareSerial

Ahhh I donā€™t even have an esp8266 folder (just arduino). Good find! So how/when is this supposed to be installed?

Take a look here if you havenā€™t already:

1 Like

Should be done by adding the ESP8266 support to the Arduino IDE. See the following guide as an example.

https://learn.sparkfun.com/tutorials/esp8266-thing-hookup-guide/installing-the-esp8266-arduino-addon

All,

In addition to RSSI reporting support for the ESP8266, I have now added it to the ESP32 and WiFi101 solutions as well. Happy signal strength monitoring!

Changes are in the SmartThingsESP32WiFi and SmartThingsWiFi101 libraries in my GitHub repo.

2 Likes

Thanks everyone for the help. I got it compiled! I did a clean install of the IDE (removed everything) and re-added all of my libraries. I had a lot of junk in there - I think there may have been some conflicts.

1 Like

Hey,

thanks so much for that. I looked at the code and noticed that it requires the smarthing hubā€™s IP address. Is there any way around it? In my current setup (itā€™s a long story) Iā€™m using a DHCP server without the ability to reserve addresses. This means, that the smartthing IP address could change over time.

Also, can you tell me briefly how to wire the two temperature probes and the resistors to the board?

Iā€™ll be receiving the hardware tomorrow and am actually excited to work on this project :slight_smile:

I do not have an easy way around hard-coding the IP address of the Hub. Most users simply configure their router to assign a static IP address via DHCP.

Here is an example of how to wire multiple DS18B20 sensors to a single ESP8266 pin. Please note that this image depicts wiring to Pin D3. However, in the example sketch I provided, I used Pin D7. Just be sure to wire the correct lead from the DS18B20 sensors to Pin D7.

image

I have installed the new Library and DH for the RSSI. Can confirm it works on Adafruit Feather MO (wifi101). Seems like a great troubleshooting tool. Also installed it on my Mega using ESP01 as wifi transceiver (wifiESP). In the Mega+ESP the tile shows up but no data nor anything in the ST IDE logger. Maybe because it used a transceiver over hardware serial?

Also for some of my projects they run on small Solar/Battery so I only poll/publish every 5min to reduce the battery draw from the wifi usage. Would be slick if in the phone app Parent device options I could set the RSSI publishing interval to 1, 3, 5, or 10min to save battery life.

Thanks

I havenā€™t updated the WiFiEsp library yet (AT commands over Serial).

Good idea about the transmit interval. Let me think about it to see what I can come up with.

Dan,

I have my siren running all the time unless I hit the ā€œSirenā€ button.

I changed the code from HIgh to Low and didnt have a difference in behavior. Is the code somewhere other than the main file? I changed the code below and they both are ā€œonā€ when the app shows ā€œoffā€

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

Thanks
~J

This is really a cool topic. Iā€™m new to SmartThings and have been doing research. Iā€™ve played with ESP8266 a couple years ago but have not done much recently. Instead of using a ESP8266 NodeMCU dev board, have you thought about hacking the Sonoff switch (which has an ESP8266 inside)? They are about $5-$6. The nice thing about this is that you donā€™t have to supply 5V for power - you can just plug them into a 110V outlet. It has a built-in relay. I just ordered some to play with. My first project will be to modify the Sonoff to control my garage door (Iā€™ve already done the garage door controller with an ESP8266-01, but this seems cleaner).

Next will be to try other things (sensors). Iā€™m hoping the GPIOs are accessible (looks like they are).

There is a whole other Sonoff thread that is dedicated to switches but I want to do more. Would be cool to combine ST_Anything with the Sonoff product.

Try changing the last argument from ā€˜trueā€™ to 'false" as that is the value that ā€œinverts the logicā€. The ā€œLOWā€ and ā€œHIGHā€ indicate the initial startup state.

All of the fields are documented in the EX_Alarm.h and .cpp files in the comments at the top.

I do not have any Sonoff devices, so I havenā€™t tried to run ST_Anything on them. It should work, once you figure out the pin assignments.

The software in the Sonoff thread has some nice features, so I typically steer these requests over to that discussion as @erocm1231 has a full-featured solution ready to run.

The problem with the sonoff devices is they arenā€™t UL approved.

Not a deal breaker to everyone but to me itā€™s much easier to use a wall socket transformer that is and then the esp8266 chipā€¦

I did the wall socket transformer thing and also built my own PCB (with 3.3V regulator, ESP8266, relay, etc. And put it in a 3D printed box. But of course my stuff was not UL certified either. https://app.box.com/s/fjdmtooabuvmz08390apwswldk8i0ewq. It was more of a teaching project but definitely not as clean and convenient and cheap as the Sonoff for general use.

Looks like the Sonoff is CE certified.