[DEPRECATED] ST_Anything - Arduino/ESP8266/ESP32

Thank you Ogiewon for all of your effort on this project! I got my LinkNodeR8 up and running based on the LinkNode R4 code you included in the package. I had a **** of a time getting the children to populate but after leaving the boot jumper off and/or rebooting the ST hub, she chooched!

Here’s the final LinkNode R8 code I used if anyone else wants to play along. Relays correspond to switch numbers 1 - 8, top to bottom, left to right. I used NodeMCU 1.0 (ESP-12E Module) as the board in the IDE.

//******************************************************************************************
//  File: ST_Anything_LinkNodeR8_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 LinkNodeR8 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 LinkNodeR8 ESP8266's WiFi.
//
//            ST_Anything_LinkNodeR8 implements the following Capabilities as a demo of what 
//            is possible with a single LinkSprite LinkNode R8 board
//              - 8 x Switch devices (used to turn on a digital output (e.g. LED, relay, etc...)
//
//  Board Programming details available at: http://www.linksprite.com/wiki/index.php?title=File:1-262-a.jpg
//    
//  Change History:
//
//    Date        Who            What
//    ----        ---            ----
//    2019-03-26  Dan Ogorchock  Original Creation
//    2019-05-18  John Crusan    Modified LinkNode R4 Code to support LinkNode R8
//
//******************************************************************************************
//******************************************************************************************
// 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 <EX_Switch.h>       //Implements an Executor (EX) via a digital output to a relay
#include <S_TimedRelay.h>    //Implements a Sensor to control a digital output pin with timing capabilities


//******************************************************************************************
//Define which Arduino Pins will be used for each device
//******************************************************************************************
#define PIN_S1              14  //LinkNode R8 Board Marking D8
#define PIN_S2              13  //LinkNode R8 Board Marking D7
#define PIN_S3               5  //LinkNode R8 Board Marking D10
#define PIN_S4              15  //LinkNode R8 Board Marking D11
#define PIN_S5              12  //LinkNode R8 Board Marking D6
#define PIN_S6              16  //LinkNode R8 Board Marking D9
#define PIN_S7               4  //LinkNode R8 Board Marking D17
#define PIN_S8              10  //LinkNode R8 Board Marking D12

//******************************************************************************************
//ESP8266 WiFi Information
//******************************************************************************************
String str_ssid     = "yourSSIDhere";                           //  <---You must edit this line!
String str_password = "yourWiFiPasswordhere";                   //  <---You must edit this line!
IPAddress ip(192, 168, 1, 127);       //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, 104);  // smartthings hub ip       //  <---You must edit this line!
const unsigned int hubPort = 39500; // smartthings hub port

// Hubitat Hub Information
//IPAddress hubIp(192, 168, 1, 143);    // 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.
//******************************************************************************************
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
  
  //Interrupt Sensors 

  //Special sensors/executors (uses portions of both polling and executor classes)
  
  //Executors
  static st::EX_Switch executor1(F("switch1"), PIN_S1, LOW, false);  //Non-Inverted logic for "Active High" Relay Board
  static st::EX_Switch executor2(F("switch2"), PIN_S2, LOW, false);  //Non-Inverted logic for "Active High" Relay Board
  static st::EX_Switch executor3(F("switch3"), PIN_S3, LOW, false);  //Non-Inverted logic for "Active High" Relay Board
  static st::EX_Switch executor4(F("switch4"), PIN_S4, LOW, false);  //Non-Inverted logic for "Active High" Relay Board
  static st::EX_Switch executor5(F("switch5"), PIN_S5, LOW, false);  //Non-Inverted logic for "Active High" Relay Board
  static st::EX_Switch executor6(F("switch6"), PIN_S6, LOW, false);  //Non-Inverted logic for "Active High" Relay Board
  static st::EX_Switch executor7(F("switch7"), PIN_S7, LOW, false);  //Non-Inverted logic for "Active High" Relay Board
  static st::EX_Switch executor8(F("switch8"), PIN_S8, LOW, false);  //Non-Inverted logic for "Active High" Relay Board
  
  //*****************************************************************************
  //  Configure debug print output from each main class 
  //  -Note: Set these to "false" if using Hardware Serial on pins 0 & 1
  //         to prevent communication conflicts with the ST Shield communications
  //*****************************************************************************
  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, "LinkNodeR8");
 
    //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
  //*****************************************************************************
      
  //*****************************************************************************
  //Add each executor to the "Everything" Class
  //*****************************************************************************
  st::Everything::addExecutor(&executor1);
  st::Everything::addExecutor(&executor2);
  st::Everything::addExecutor(&executor3);
  st::Everything::addExecutor(&executor4);
  st::Everything::addExecutor(&executor5);
  st::Everything::addExecutor(&executor6);
  st::Everything::addExecutor(&executor7);
  st::Everything::addExecutor(&executor8);
    
  //*****************************************************************************
  //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

@ogiewon am I reading correctly that I can use a distance sensor in place of the contact sensor for the garage door function? If so, is there a specific sensor that I must use? I got my DS18B20 temp sensor to read in the example sketch, but before I order I thought I’d ask. Thanks

Some users have hacked together a solution like that. Personally, I use inexpensive wired contact sensors on my pair of doors. They would every time without fail. Pretty simple device, when you think about it - a reed switch and a magnet. Not much to fail.

I remember one user used an Ultrasonic sensor to know whether or not his cars were present in the garage. I think we treated this as a presence sensor. Looks like the code for that is still in the following child DTH. The reported value is compared against a threshold to determine present or away.

First of all thanks for this library and all the support you provide.

I am trying to get the LED_on/off demo working and I keep getting “post request timed out”

Here is the output from the serial monitor

setup..
Disabling ESP32 WiFi Access Point

[WiFi-event] event: 0
[WiFi-event] event: 2
ESP32 station start

Initializing ESP32 WiFi network.  Please be patient...
Attempting to connect to WPA SSID: BLAH
..[WiFi-event] event: 7
WiFi connected
IP address: 
192.168.2.232
[WiFi-event] event: 7
WiFi connected
IP address: 
192.168.2.232


Enter the following three lines of data into ST App on your phone!
localIP = 192.168.2.232
serverPort = 8090
MAC Address = MACADDRESS

SSID = BLAH
PASSWORD = PASSWORD
hubIP = 192.168.2.88
hubPort = 39500
RSSI = -55

SmartThingsESP32WiFI: Intialized

Ready! Open http://esp32-webupdate.local in your browser
IP address: 192.168.2.232
MAC address: CC:50:E3:80:9B:D4
Post request timed out

Post request timed out

Received message: 'off' 
Post request timed out

Post request timed out

And here is the output from the live debug

8:22:32 PM: debug Using ip: 192.168.2.232 and port: 8090 for device: DEVIC_ADDRESS
8:22:32 PM: debug Executing 'sendEthernet' off
8:22:32 PM: debug Sending 'off'
8:22:32 PM: debug Using ip: 192.168.2.232 and port: 8090 for device: DEVICE_ADDRESS
8:22:32 PM: debug Executing 'sendEthernet' off
8:22:32 PM: debug Sending 'off'

Hmmmm… I don’t have an ESP32 version of the very old “On/Off” demo sketch in my repository? Did you write your own?

What is your goal? Most users start with the ST_Anything_Multiples_ESP32WiFi.ino sketch as a starting point. This sketch is designed to work with the ST_Anything Groovy Device Type Handlers.

The On/Off Sketches, and their corresponding DTH, are simply a very basic example that emulates the old example sketch for now-obsolete SmartThings ThingShield. This can be a useful starting point for someone that wants to role their own sketch from scratch and not use the rest of ST_Anything.

I was planning on using it to trigger a stepper motor to run for about 10 seconds when “ON” then run 10 seconds in the other direction when “OFF”

I will take a look at the multiples example and see if I can get that working

thanks!

If a servo motor would work for your application, there is already support for those built into the library using the EX_Servo class.

i don’t think it will, i need multiple revolutions, and as I understand it the servo class is only 180 correct?

That is correct. Are you an Arduino programmer? If so, you could possibly use the EX_Servo class files as an example for adding a stepper motor device class.

@ogiewon, I’ve successfully loaded the Node MCU multi onto my chip and played with everything i think I have interest in. which leads me to the following questions:

  1. I assume we now customize the pin definitions to match our needs and load that sketch onto our board?
  2. It appears switch induces 5v when off and ground when on?
  3. Relay switch induces 3.3v when off and ground when on for its duration?
  4. How can I reverse 2 & 3 if needed?
  5. I am showing 2 temperature children one shows accurate and one is way off - I am using a DS18B20 temp sensor .

Thanks in advance, im ready to start building :smile:

Correct. You can customize the pin definitions as you see fit, but be aware that the ESP8266 boards have limitations. Please see my ReadMe to see some of these limitations.

You’ll want to also define the list of devices as you see fit. Be sure to follow the naming convention as documented in the ReadMe. The names (first parameter of each device) are critical to their proper identification and creation on the ST side of things.

5V ??? Should be 3.3V on a NodeMCU ESP board.

The behavior is user selectable. When the EX_Switch device is created in the sketch, there are numerous parameters that are passed in which control the behavior of the digital output pin. Read the comments at the top of the EX_Switch.cpp or .h file for the documentation of these parameters.

Same as the EX_Switch above.

See answer for EX_Switch above.

Sometimes ST creates two child devices instead of just one (read the recent posts from the last few weeks on this issue.) Unfortunately, I haven’t found the root cause, nor a workaround for it. You have to delete the Parent Device (not the DTH!) and manually create it again until you get just one child device for each device you’ve defined in the sketch.

Is there support for Arduino due( sam processor)
Arduino\libraries\SmartThings/SmartThingsEthernet.h:37:3: error: ‘IPAddress’ does not name a type

IPAddress st_hubIP;

Hendre,
Did you ever get a modified version of this dth to work? I have the garage door working but not the open closed sensor.

I have never tried building for an Arduino Due. Most folks these days just grab an $8 NodeMCU ESP8266 off of Amazon.

What are you using this on?

@Glock43 - Thank you for the LinkNode R8 example sketch. I have added it to my ST_Anything GitHub Repository.

It’s just that I had the boards and all the connections for the Due to my garage door and lights, controlled with a couple keyfobs that I had.

Either way, got it working. Part of the issue was I downloaded anything v2.1, and the other was the redefinition of ringbuffer class. Just renamed the class and all the calls to it and it worked.

Now Esp-01 does not connect to my WiFi (WAP2) . FW is too old seems like 0.91. Will flash with latest FW and verify again.

Thanks for the good work.

1 Like

I am looking to add an OLED to my ESP8266WiFi temperature sensor. I followed the ESP8266WiFi Temperature sketch and got everything to work within smartthings. The OLED is to display the temperature on the senesor it self for a quick check. I can use ST to control the devices in the background. I was able to get it to work by adding the sensor, libraries, etc… to the ESP8266WiFi sketch. This seems redundent as I am sure this is done on the PS_DS18B20_Temperature.h file. Is there a way to call the sensor data to the ESP8266WiFi sketch to use for a display?

Thank you,

Chris

I recommend uncommenting the serial.print statements in the “callback()” routine in the example sketch. This will allow you to see the data that is being sent to ST. By tapping into this data stream, you can then add whatever calls you want to update your OLED display. Just be sure to never add any delay() or blocking calls that prevent ST_Anything from servicing the GPIO pins and the Network communications.

Just a FYI for anyone using the new Samsung app. I put in a bunch of tiny code changes to add metadata to some of the DTH’s so that they will work with the new app. I didn’t do all of them, just some of the main ones like temperature, humidity, contact, Etc. I’ll look into doing the other ones in the future but these are the ones I had time for. @ogiewon merged my changes in a couple days ago so you’ll have to go into the IDE and update your device handlers if you haven’t.

Unfortunately that’s not all you’ll have to do to make the new app show them correctly. It seems there is a ongoing bug where the new app holds onto the device Handler the first time it sees a device and doesn’t like to update it. The fix on Android phones is to go into settings than apps than the new SmartThings app, force close it, then clear its cashe (storage). Once you do that go into your IDE and switch the device Handler for each devices to something else, honestly it doesn’t matter, and save it then go back into the device and switch it back to the correct device Handler. Once that’s done start the new app backup and it will pull the device Handler information again which will now be correct. I have no idea how to do this on an Apple but I’m assuming you just uninstall the app, do the device Handler change thing, then reinstall the app.

Although it takes getting used to the history feature on the new app is kind of nice. Unfortunately I’m not sure why it only is working on humidity and not temperature but I’m looking into that too.

1 Like