[RELEASE] ST_Anything - Arduino/ESP8266/ESP32

I’m glad that there is such code in your project. I’ll give it a try. Can you please give me a short plan of how to integrate it better. Can I combine it with existing EthernetW5500 sketch, or is it better to use it on a separate arduino board to avoid conflicts?
Thank you.

You’ll need to combine the two sketches together, as the old Relays sketch was designed for use with the old thingshield. You just want to use it as a reference/example as one possible method for adding Arduino local control of the outputs.

Note: It is very important that you do not add any “delay()” statements in the sketch as those will interfere with the Ethernet communications to the ST hub.

@ogiewon, please find below the Markise.ino code. Base code was taken from ST_Anything_Multiples_ESP8266WiFi and mofiied to relaySwitches only.

I’ve tested it with all child devices installed and published as well as only the relay switch child device without modification. Both test returned the same failure.

Thank you

//******************************************************************************************
// File: Markise.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_Markise implements the following ST Capabilities as a demo of what is possible with a single NodeMCU ESP8266
//
// - 3 x Relay Switch devices (used to open Markise , stop markise and close Markise)
// This Thing will connect to SOMFY RTS
//
//
// 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-11-27 Kai Lenk Modified to 3 relaySwitch
//
//******************************************************************************************
//******************************************************************************************
// 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)
//*************************************************************************************************
#define LED_BUILTIN 16
#define BUILTIN_LED 16

#define D5 14
#define D6 12
#define D7 13

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

#define PIN_TIMEDRELAY_1 D5 //SmartThings Capability “Relay Switch”
#define PIN_TIMEDRELAY_2 D5 //SmartThings Capability “Relay Switch”
#define PIN_TIMEDRELAY_3 D7 //SmartThings Capability “Relay Switch”

//******************************************************************************************
//ESP8266 WiFi Information
//******************************************************************************************
String str_ssid = “404 Network unavailable”; // <—You must edit this line!
String str_password = “0602198106021”; // <—You must edit this line!
IPAddress ip(192, 168, 0, 200); //Device IP Address // <—You must edit this line!
IPAddress gateway(192, 168, 0, 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, 0, 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, 0, 132); // 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.
//******************************************************************************************

//Special sensors/executors (uses portions of both polling and executor classes)
static st::S_TimedRelay sensor1(F(“relaySwitch1”), PIN_TIMEDRELAY_1, LOW, true, 3000, 0, 1);
static st::S_TimedRelay sensor2(F(“relaySwitch2”), PIN_TIMEDRELAY_2, LOW, true, 3000, 0, 1);
static st::S_TimedRelay sensor3(F(“relaySwitch3”), PIN_TIMEDRELAY_3, LOW, true, 3000, 0, 1);

//*****************************************************************************
// 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);
st::Everything::addSensor(&sensor2);
st::Everything::addSensor(&sensor3);

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

Thanks thanks and thanks again. That now all works exactly as it should…

But …

now having seen the post beneath mine about local control I realise that would be amazing. I was thinking in some of my rooms, remove the mains 240V from the 2 way switches I have and connect these 2 way light switches instead to a switch pin on the nodemcu and have the local running code then trigger a relay to send the power to the lights. this way I can detect the state of the light in smarthings and control accurately. I could turn a light on in smartthings that someone else could turn off by flicking the light switch on the wall or vice versa. because the code will run direct in the nodemcu the effect should be almost as fast as if the 2 way light switches were wired to the power. That really would be slick!!

I look forward to seeing if you can get it going.

Also, do you have a beer fund I can contribute to? Previously this functionality was realised using a zwave module designed to go behind the wall plate at a cost of 45GBP!!

All the best,
Dan.

@Kai_Lenk Here is a version of your sketch, slightly cleaned up, but tested to work 100% reliably. If this does not resolve your issues, I believe you probably have an issue on the ST IDE Groovy side of things, not the Arduino/ESP8266 side.

Please make sure you do NOT change any of the groovy code, as the Parent and Child Device Handlers (source code) are a matched set. Also, if you’re still having the issue, be sure to delete the Parent Device (not the source code) and then manually recreate a new Device. Be sure to pay attention to entering the correct Arduino IP address, Port, and MAC Address (all UPPERCASE and no delimiters) when you configure the device via your phone’s ST App.

@Dan_Lumbard and @AlexBat

First, glad to hear you got things sorted. Second, I strongly recommend you buy the properly safety-rated and mains power-rated Z-Wave in-wall switches for safe control of your home’s lighting and power outlets. I really do not want to hear that you, or anyone, burned down their home by hacking together a solution. If z-wave is too pricey, please take a look at the Itead Sonoff and Touch devices (SONOFF Smart Home - SONOFF Official Online Store to provides smart swiches, plug, bulb, zigbee devices) which many ST users are happy with. @erocm1231 has custom firmware and ST Device Handlers for these very low cost ESP8266-based devices that are designed properly for mains-voltage applications. See his very active thread at

With those disclaimers/recommendations out of the way… Here is you example sketch that demonstrates local control using momentary buttons between the NodeMCU ESP8266’s GND pin and corresponding pins D1, D2, and D3. Each time you press a button, the corresponding output at D5, D6, and D7 will toggle and update SmartThings. This is essentially exactly how one of the in-wall Z-wave switches work.

1 Like

Would this work with two 3 way toggle switches rather than all momentary switches? I intend to use it for lighting in a shed where I can easily remove the mains power so I use my existing 3 way switch (2 light switches) purely to provide the input state change and will link my power and light via the relay side of this project. This way my existing wall switches stay in place and can be operated as “normal”.

The complication would come with me turning a light on via ST, as I’d need my toggle switches to toggle the output state (light on or off) regardless of going from closed to open or open to closed.

Or…, I could just toggle a switch twice if the coding was too tricky.

Not sure if I’ve explained that very well!

@ogiewon Thank you very much, really appreciate your fast support.
I cleaned up everything in the IDE and started from zero by adding each device handler, adding the device manualy, flashed the NodeMCU with your modified sketch and now its working like a charme.
I need to check if the timing of the “on” period can be shorter. But before checking this one, it’s time to design a 3D printable docking station for the SOMFY RTS remote, attaching it to the relay switches. So it can still be used standalone and when connected to the docking, it can be controlled with ST. As soon as I’ve finished everything, I’ll post an update and links to the 3D files. If you agree, I’ll make it a separate post to keep this one purely for the ST_Anything discussions.

Thx again
Kai

Kai,

Great to hear you’ve got it working. You can easily change the “on-time” by adjusting the 3000 ms value in the declaration of each Timed Relay device. Read the comment section in the .h or .cpp file for this device to see what all of the values are used for.

Dan

Dan,

You’re free to modify the example however you see fit. You’ll need to be creative in figuring out the wiring to use the three way switches.

Dan

Thinking out loud all a three way switch setup is doing is changing the output no matter which switch is “switched”. So could you poll the normal output of your 3-way setup (the load side) and simply say if it changes in any way (on -> off or off -> on) then reverse the status of the light output? In this way it would be independent of ST control. If the lights are on and you flick either switch it just sees a change and then turns the lights off. On the same token you can turn them on from ST and the switch position wouldn’t matter.

Hi, trying to convert my Adafruit Feather MO board from Adafruit IO MQTT over to ST_Anything. The board has Cortex M0+ processor (same as Adruino Zero) and WINC1500 wifi. I’m using the <SmartThingsWiFi101.h> library. So the issue is how to get the ST Library to use the MO wifi pins over the SPI.

Here’s the pins that the previous working connection with Adafruit MQTT, Wifi101, and SPI librarys.

#2 - used as the ENable pin for the WiFi module, by default pulled down low, set HIGH to enable WiFi
#4 - used as the Reset pin for the WiFi module, controlled by the library
#7 - used as the IRQ interrupt request pin for the WiFi module, controlled by the library
#8 - used as the Chip Select pin for the WiFi module, used to select it for SPI data transfer

#23 - MOSI
#22 - MISO
#24 - SCK

@kampto

You should be able to simply add the “WiFi.setPins()” function in your Arduino sketch’s setup() function. Just add teh call before the following line:

  //Run the Everything class' init() routine which establishes Ethernet communications with the SmartThings Hub
  st::Everything::init();

https://www.arduino.cc/en/Reference/WiFi101SetPins

ST_Anything is designed to work with the WiFi101 shield’s default pins, as well as with the MKR1000’s default settings.

I was using “WiFi.setPins()” but had it outside the set-up loop. It compiles now and I can ping the board with static IP. Problem now is none of the child sensors are reaching the smart app. I have the parent device configured and showing up on the app. Serial monitor shows connection failure. But also say “SmartThingsWiFi101: Intialized” Does initialized mean is connected and ready to send data?

****** Attempting to resend missed data *******


ST_Anything Callback: Sniffed data = temperature1 -196.60
Everything: Sending: voltage1 2862.46failure. What in this tells me that its


***** SmartThings.send() - Ethernet Connection Failed *****


hubIP = 192.168.0.151 hubPort = 39500


****** Attempting to restart network *******


Initializing WiFi 101 network. Please be patient…
Attempting to connect to WPA SSID: ATT444

Enter the following three lines of data into ST App on your phone!
localIP = 192.168xxxxxxxxx.
serverPort = 8090
MAC Address = F8:F0:xxxxxxxxx.
SSID = ATT444
PASSWORD = xxxxxxxxxxxxxx
hubIP = 192.168.0.151
hubPort = 39500

SmartThingsWiFi101: Intialized

Are you sure you have the hub’s is address correct? Can you ping the hub with a computer on the same network?

Personally, I’d recommend picking up a NodeMCU ESP8266 board. They are very robust and reliable, and on.y cost about $10.

I have never gotten the WiFi101 based boards to run reliably for more than a day or two before they lock up. I even recently upgraded to the latest WINC1500 firmware and WiFi101 library hoping they would have fixed the issues, but my MKR1000 still locks up after a while.

I chose this board cause it has a uFl antenna connector so I could put an external antenna outside the metal enclosure and it has PWM. I was having some lock issues as well so I added some code to do a SW reset at night and that seemed to improve that situation. That was a year ago, now the ESP32 might be a better cost effective choice if I could find one with the uFl connector.

Anyway I got the child devices up by removing the device in ST IDE and phone then re-entering it.

1 Like

Now that the board is talking to ST, back to the topic I had a month a so ago. This controller has a Adafruit INA219 i2c current sensor. I need to get that current reading “current_mA” which resides in the sketch over to ST. The value is picked up in the Loop via current_mA = ina219_B.getCurrent_mA();

I played around with the “static st::PS_Generic sensor1(F(“generic1”), 60, 0);” option but could get it to work. Is there an example sketch using the Generic Library and polling sensor?

Thanks…

I wrote up the generic sensor to do exactly what you are trying to do, extend ST_Anything to other sensors that might not fit the mold of a current library. It should be pretty well commented in the library (PS_generic.h and .cpp). What couldn’t you get to work? Did you try adding a generic sensor without making any changes and see the child device get created with a default value of 15?

@vseven Good point, I’m completely overthinking this, I guess my question is simply how to code the toggle switch rather than momentary then. I have virtually no coding skills, I’m more of a smartthings/arduino script (kiddy) oldie

:slight_smile:
Thanks everyone.

I’m not familiar with every part of ST anything so I will let Dan answer this better from that side but on the physical side if you have a 3 way set up going to a input on the microcontroller then it would work like normal: you turn one switch on and the input would be closed, turn the other switch on and the input would be open. You’d be using a regular 3 way switch set up with no main power going through it. Then based on a change in that input you turn on or off the output. You wouldn’t care whether the input is actually on or off, just that it has changed states. This way you can also control it through smart things. The only goofy part would be you could technically have both your 3 way switchs turned on and the light would be off one day or could be on another day since the position of the switchs no longer really matters.