Announcing the "ST_Anything" Arduino/ThingShield Project

Thanks Dan, I appreciate you taking your time to help. I’ll keep you posted on how I make out with this project.

1 Like

Hi Everyone!

First off thank you @ogiewon and everyone else who had a part in the ST_Anything Project! Has anyone used the Sparkfun ESP32 Things board for any projects with ST_Anything?

I can’t think of any reason it wouldn’t work, since it is just another flavor of an ESP32. Just be sure to configure the Arduino IDE to compile for the correct board. The only other thing to be aware of is the pin assignments.

Thanks @ogiewon! I got everything setup in the Arduino IDE, gave out static IP’s for the ST hub and ESP32, created the device in ST IDE and then ran the RGB example and the children were created. I did get that duplicate child issue and deleted the extra one but still the example did not work. In the serial monitor I was seeing the state of the switches and could change the state with my ST phone app but the LED did not work. I tested it on an Arduino Uno to ensure the circuit was good and it worked fine? So I decided to delete the device and re-add it. Since then the children are not being created and the LED still does not work? Below is my code as well as a print out of my serial monitor. Any idea what I am doing wrong? I am using pins 36 (red), 39 (green) and 34 (blue) for my LED. I only have one RBG led so I deleted the other LED and the RGBW. So this sketch only has one child (rgbSwitch1). Any help would be greatly appreciated and let me know if I can help out with the project in anyway. I am fairly new with this stuff but will publish my projects to help others once I get a handle on it!

Thanks,
Brian

indent preformatted text by 4 spaces
//******************************************************************************************

// File: ST_Anything_RGB_ESP32WiFi.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 ESP32 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 ESP32’s WiFi.
//
// ST_Anything_RGB implements the following ST Capabilities as a demo of what is possible with a single ESP32
// - 2 x RGB LED (used to set the color of a RGB LED - Both individual RGB LED’s and RGB LED Strips)
//
//
// Change History:
//
// Date Who What
// ---- — ----
// 2017-08-14 Dan Ogorchock Original Creation - Adapted from ESP8266 to work with ESP32 board
// 2017-10-06 Allan (vseven) Modified for RGB example
// 2017-10-13 Allan (vseven) Modified for RGBW example
//
// Special thanks to Joshua Spain for his contributions in porting ST_Anything to the ESP32!
//
//******************************************************************************************
//******************************************************************************************
// SmartThings Library for ESP32WiFi
//******************************************************************************************
#include <SmartThingsESP32WiFi.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_RGB_Dim.h> //Implements an Executor (EX) for a RGB LED or strip with PWM using 3 digital output pins
//#include <EX_RGBW_Dim.h> //Implements an Executor (EX) for a RGBW strip with PWM using 4 digital output pins

//****************************************************************************************************************************
//NodeMCU-32s ESP32 Pin Definitions (just for reference from …hardware\espressif\esp32\variants\nodemcu-32s\pins_arduino.h)
//****************************************************************************************************************************
//#define LED_BUILTIN 2
//#define BUILTIN_LED 2
//
//#define A0 = 36;
//#define A3 = 39;
//#define A4 = 32;
//#define A5 = 33;
//#define A6 = 34;
//#define A7 = 35;
//#define A10 = 4;
//#define A11 = 0;
//#define A12 = 2;
//#define A13 = 15;
//#define A14 = 13;
//#define A15 = 12;
//#define A16 = 14;
//#define A17 = 27;
//#define A18 = 25;
//#define A19 = 26;

//******************************************************************************************
//Define which Arduino Pins will be used for each device
//******************************************************************************************
//“RESERVED” pins for ESP32 - best to avoid
#define PIN_0_RESERVED 0 //reserved ESP32 boot/program upload
#define PIN_1_RESERVED 1 //reserved ESP32 for TX0
#define PIN_3_RESERVED 3 //reserved ESP32 for RX0
#define PIN_6_RESERVED 6 //reserved ESP32 for flash
#define PIN_7_RESERVED 7 //reserved ESP32 for flash
#define PIN_8_RESERVED 8 //reserved ESP32 for flash
#define PIN_9_RESERVED 9 //reserved ESP32 for flash
#define PIN_10_RESERVED 10 //reserved ESP32 for flash
#define PIN_11_RESERVED 11 //reserved ESP32 for flash

//Analog Pins

//Digital Pins
#define PIN_RGB1_Red A0 //(GPIO 36) SmartThings Capability “Color Control”
#define PIN_RGB1_Green A3 //(GPIO 39) SmartThings Capability “Color Control”
#define PIN_RGB1_Blue A6 //(GPIO 34) SmartThings Capability “Color Control”

//******************************************************************************************
//ESP832 WiFi Information
//******************************************************************************************
String str_ssid = ""; // <—You must edit this line!
String str_password = ""; // <—You must edit this line!
IPAddress ip(
); //Device IP Address // <—You must edit this line!
IPAddress gateway(
); //Router gateway // <—You must edit this line!
IPAddress subnet(); //LAN subnet mask // <—You must edit this line!
IPAddress dnsserver(
**); //DNS server // <—You must edit this line!
const unsigned int serverPort = 8090; // port to run the http server on

// Smartthings Hub Information
IPAddress hubIp(*********); // 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(“Test comment!”); //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_RGB_Dim executor1(F(“rgbSwitch1”), PIN_RGB1_Red, PIN_RGB1_Green, PIN_RGB1_Blue, true, 0, 1, 2); // channels (0,1,2) must be unique per ESP32

//*****************************************************************************
// 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 ESP32WiFi Communications Object
//STATIC IP Assignment - Recommended
st::Everything::SmartThing = new st::SmartThingsESP32WiFi(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::SmartThingsESP32WiFi(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);

//*****************************************************************************
//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();
}

Serial Monitor:

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

SSID = *********
PASSWORD = *******
hubIP = ********
hubPort = 39500

SmartThingsESP32WiFI: Intialized

Everything: init ended
Everything: Free RAM = -1
Everything: adding executor named rgbSwitch1
Everything: Free RAM = -1
Everything: initDevices started
Everything: Free RAM = -1
Everything: Sending: rgbSwitch1 off
Everything: Received: Test comment!
Everything: initDevices ended
Everything: Free RAM = -1
Everything: Free Ram = -1
Everything: Free Ram = -1

Try deleting the Parent and adding it again manually. Once added, configure the settings for it with your ESP32’s IP, Port, a MAC. Make sure the MAC is all uppercase and no delimiters. Do not delete individual Child Devices, as that causes another issue due to a change in the ST Cloud backend software.

As for the RGB LED functionality, @vseven added that feature, so Allen might be able to help troubleshoot.

Alright thanks a lot! I deleted the child devices before I deleted the parent the first time so maybe that was the issue. I will go ahead and manually add the parent again.

You can simply delete the Parent as it will also delete all of the children. Earlier last year, you could delete an individual child device and everything was fine. If the Arduino still sent updates for the deleted child, it would simply be created again. But alas, something changed in the backend so now we have to delete the parent if any extra child devices exist that you want deleted. Then manually add it again so it can create children for whatever devices are defined in your sketch. You can still ADD new devices to the sketch, and they will still be auto-created. We just cannot delete an individual child without it locking up the Parent’s Child Device lookup logic. The deleted child cause an exception whenever an update comes into the parse() routine of the parent. After about 24 hours, everything starts working again if you do nothing. Must be some sort of backend clean-up routine that runs.

Alright sounds good, I read that in your documents after the fact… I deleted them last night around 7 pm EST and since I have deleted the parent one more time. I named the parent different each time so hopefully I will have the button later tonight! I will report back tomorrow if the button is created or not. Thanks again for the help and again if you need any help/support with the project let me know.

When you say “button”, what exactly do you mean? I don’t see where you’re trying to configure any “button” devices in your sketch… Are you referring to the Child RGB Switch device? If so, it should appear immediately after you configure the Parent Device’s settings in your phone, assuming all of the settings are correct.

Sorry for the confusion, I was referring to the Child RGB Switch as the “button”. Then I am still having issues since the Child RGB Switch has not been created. The Parent Device is created and setup with the ESP32’s IP, Port and MAC address but still no Child device “rgbSwitch1”. If I look at my ST live logging (see below) I can see the ESP32 communicating with the Hub at 11:50:53 PM but no Child RGB Switch is created.

11:51:21 PM: debug healthPoll()11:51:19 PM: debug sending data to AT
11:51:19 PM: debug A sensor event occurred: [name:temperature, deviceId:**** isoDate:2018-01-05T04:51:15.932Z, unit:F, value:61]

11:51:19 PM: trace Event = temperature 61 from MS - BF - A
11:51:01 PM: debug getChildDevices(false), children=1
11:50:54 PM: debug sending data to AT
11:50:54 PM: debug A sensor event occurred: [name:motion, deviceId:**** isoDate:2018-01 -05T04:50:54.070Z, unit:null, value:inactive]

11:50:54 PM: trace modeOk = false
11:50:54 PM: trace motionStopHandler(motion: inactive)
11:50:53 PM: debug Using ip: ESP32 Board and port: 8090 for device:

I got it working but I am not sure what fixed the issue. I still can not control the RGB LED but my Child Device “rgbSwitch1” was created and is now in the ST phone app.

I deleted the Parent Device, removed the device handlers, restarted the hub. Then I removed the library from Arduino IDE, downloaded the repo again and then went through the ST_Anything ReadMe. Once I uploaded my project the button was created. However, I am still unable to control the RGB led from the newly created child. I will put this in a new post, but thanks a lot for the help!

1 Like

Thank you Dan for pointing me in the right direction for integrating SmartThings with my alarm panel. Thanks for the work on ST_Anything, you’ve done all the hard work and made it almost plug and play for me. I’ve got everything 90% working, but I’m not able to send arm/disarm events back to ST_Anything from the device handler to be passed on to the RS232 port connected to the arduino. There is a way to subscribe to the alarm state with something like this:

subscribe(location, "alarmSystemStatus.off", alarmOffHandler)

def alarmOffHandler(evt) {
    log.debug "evt value: ${evt.value}"
    log.debug "alarm is off"
}

More details here: Link

But location subscriptions don’t appear to work in device handlers (at least how ST_Anything is set up - I could only get it to work in a smart app).

Do any of you ST gurus know what I can do to be able to subscribe to the alarm state? Thanks!

A device handler cannot subscribe to events from other devices. Only a SmartApp can do that. You could use WebCoRE to probably handle that. Then have an Arduino device, probably a switch, that is turned on/off as SHM is armed/disarmed. Then the Arduino can interpret the on/off to send the appropriate serial commands to your alarm system.

That worked perfectly! Thank you Dan.

1 Like

Having issues reading a DHT22 sensor. DHT Sensor works fine when using the adafruit DHT libraries @ https://github.com/adafruit/DHT-sensor-library/blob/master/DHT.cpp. However when using your code I keep getting “DHT time out error” I have tried adjusting the value of DHTLIB_TIMEOUT in dht.h but that does not help. Any help will be greatly appreciated.

Are you using the DHT library that is included in my GitHub repository? I included that specific DHT library version as it works well with Arduino and ESP8266 boards. Which board are you using?

Please remove any other DHT libraries from you libraries folder, and then add the one from my Github Repository. Then, after restarting the Arduino IDE, try to compile it and run it again.

Thank you for the quick response. I am using an ESP8266-12E board. I have removed all other DHT libraries and just using the set downloaded from https://github.com/DanielOgorchock/ST_Anything/tree/master/Arduino/libraries. Still getting the DHT TImeout error.

Apologies… Issue was on my side. you code works fine with a DHT22. The issue was I was using an AM2320 sensor which works fine with the adafruit dht lib. Once I replaced the AM2320 with a DHT22, it started working. Have you tried working with an AM2320 sensor?

I have not used an AM2320 sensor as I do not have any.

If you’d like, you could create a new ST_Anything class for the AM3230 Temperature/Humidity sensor. Just use the existing DHT class as an example to get started.

Once you get it perfected, please submit a Pull Request to my GitHub repo so we can add it for other users as well.

Not sure how to do a pull request on github. I have created a new class called PS_TemperatureHumidity_AM2320 that supports both DHT22 and AM2320 sensors. Tested it by connecting both sensors to an ESP. Definition is same as existing PS_TemperatureHumidity sensor. for example…
static st::PS_TemperatureHumidity_AM2320 sensor2(F(“temperaturehumidity”), 15, 6, PIN_TEMPERATURE_1, 22, “temperature1”, “humidity1”, false);
The 22 after the Pin definition represents the type of sensor (22=DHT22, 22=AM2320, 21=DHT21, 11=DHT11).
I am by no means a developer so excuse the hack.
link to files: https://www.dropbox.com/s/nmiyyarfn9e8mbc/ST_Anything_TemperatureHumidity-AM2320.zip?dl=0