[DEPRECATED] ST_Anything - Arduino/ESP8266/ESP32

There are plenty of European users here in the forums. Probably best to ask them what their recommendations are.

A quick search online turned up this site. - https://shop.zwave.eu/products/switches-and-dimmers/switches/?p=1

Again, probably best to check with folks in the https://community.smartthings.com/c/uk discussion to see what they recommend.

Hi,
Ignore this blockquote, found the issue, my mac address was lower case!! Whoops. However, my query at the bottom still stands…

Blockquote Newb question, I have used the ST_Anything.multiples_ESP8266Wifi sketch have added my wifi and ip settings in but have changed NOTHING else. I’ve compiled and uploaded and can ping the device from my windows PC, i’m in UK so have added all the child device handlers and the parent _ST_Ethernet by copying and pasting from the extracted zip file I downloaded yesterday called st_anything-master

I’ve then added a Device in Smart IDe set to be the parent self published etc.

On my Android Smarthings app I’ve added the ip. mac etc and chose 0 buttons.

my phone shows nothing aside from the parent device, , if i look at live logging back in the smart ide page i get all sort of stuff scrolling up some of which is temp data from my 1wires. I have 4 connected together, i put them in 4 different pools of water but only 1 temperature is showing.

What else do I need to amend in the sketch to show all my temp sensors (and to show in degrees C) and what do i need to do to allow my switch and relay switch to show up too,

the four 1 wire sensors are connected to D7 on the MCU board, the relay is connected to D1 and the timed relay to D8.

Thanks in advance for any help you can give!!

@Dan_Lumbard can you please post your sketch either here or in a Private Message? Please be sure to highlight it and then click the Preformatted Text button in the edit menu ( < / > button) to preserve the formatting. Once I can see the code, I should be able to help you figure out what’s wrong.

As for the Celsius instead of Fahrenheit, that is a simple setting in the Child Temperature device that you can configure on the fly (no code change needed). Just go into the Child Device’s settings (gear shaped icon) and select the conversion type you want. You can also offset the temperature to calibrate the reading.)

Thanks for your help!

Celsius is now showing, yay!!

Heres my code which results in the 5 temp sensors all reading the same, logically I guess its with them all being set to pin D7 but isn’t that how it would have to be set for 1wire devices, or is D7 not properly compatible with parallel 1wires? anyway , enough speculation.

Thanks again!!

indent preformatted text by 4 spaces//******************************************************************************************
// File: ST_Anything_Multiples_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_Multiples implements the following ST Capabilities as a demo of what is possible with a single NodeMCU ESP8266
// - 1 x Alarm device (using a simple digital output)
// - 1 x Contact Sensor devices (used to monitor magnetic door sensors)
// - 1 x Switch devices (used to turn on a digital output (e.g. LED, relay, etc…)
// - 1 x Motion devices (used to detect motion)
// - 1 x Smoke Detector devices (using simple digital input)
// - 1 x Temperature Measurement devices (Temperature from Dallas Semi 1-Wire DS18B20 device)
// - 1 x Relay Switch devices (used to turn on a digital output for a set number of cycles And On/Off times (e.g.relay, etc…))
// - 2 x Button devices (sends “pushed” if held for less than 1 second, else sends “held”
// - 1 x Water Sensor devices (using the 1 analog input pin to measure voltage from a water detector board)
//
// 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
//
//******************************************************************************************
//******************************************************************************************
// 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 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_WATER_1 A0 //NodeMCU ESP8266 only has one Analog Input Pin ‘A0’

//#define PIN_ALARM_1 D0 //SmartThings Capabilty “Alarm”
#define PIN_SWITCH_1 D1 //SmartThings Capability “Switch”
#define PIN_SWITCH_2 D3 //SmartThings Capability “Switch”
#define PIN_SWITCH_3 D4 //SmartThings Capability “Switch”
#define PIN_SWITCH_4 D6 //SmartThings Capability “Switch”
#define PIN_CONTACT_1 D2 //SmartThings Capabilty “Contact Sensor”

#define PIN_MOTION_1 D5 //SmartThings Capabilty “Motion Sensor” (HC-SR501 PIR Sensor)
#define PIN_TEMPERATURE_1 D7 //SmartThings Capabilty “Temperature Measurement” (Dallas Semiconductor DS18B20)
#define PIN_TEMPERATURE_2 D7 //SmartThings Capabilty “Temperature Measurement” (Dallas Semiconductor DS18B20)
#define PIN_TEMPERATURE_3 D7 //SmartThings Capabilty “Temperature Measurement” (Dallas Semiconductor DS18B20)
#define PIN_TEMPERATURE_4 D7 //SmartThings Capabilty “Temperature Measurement” (Dallas Semiconductor DS18B20)
#define PIN_TEMPERATURE_5 D7 //SmartThings Capabilty “Temperature Measurement” (Dallas Semiconductor DS18B20)

//******************************************************************************************
//ESP8266 WiFi Information
//******************************************************************************************
String str_ssid = “DeviceNet”; // <—You must edit this line!
String str_password = “blanked”; // <—You must edit this line!
IPAddress ip(192, 168, 1, 203); //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, 101); // 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
static st::PS_Water sensor1(F(“water1”), 60, 20, PIN_WATER_1, 200);
static st::PS_DS18B20_Temperature sensor2(F(“temperature1”), 15, 0, PIN_TEMPERATURE_1, true, 10, 1);
static st::PS_DS18B20_Temperature sensor3(F(“temperature2”), 15, 0, PIN_TEMPERATURE_1, true, 10, 1);
static st::PS_DS18B20_Temperature sensor4(F(“temperature3”), 15, 0, PIN_TEMPERATURE_1, true, 10, 1);
static st::PS_DS18B20_Temperature sensor5(F(“temperature4”), 15, 0, PIN_TEMPERATURE_1, true, 10, 1);
static st::PS_DS18B20_Temperature sensor6(F(“temperature5”), 15, 0, PIN_TEMPERATURE_1, true, 10, 1);

//Interrupt Sensors
static st::IS_Contact sensor7(F(“contact1”), PIN_CONTACT_1, LOW, true);
// static st::IS_Button sensor4(F(“button1”), PIN_BUTTON_1, 1000, LOW, true, 500);
// static st::IS_Button sensor5(F(“button2”), PIN_BUTTON_2, 1000, LOW, true, 500);
static st::IS_Motion sensor8(F(“motion1”), PIN_MOTION_1, HIGH, false);
// static st::IS_Smoke sensor7(F(“smoke1”), PIN_SMOKE_1, HIGH, true, 500);

//Special sensors/executors (uses portions of both polling and executor classes)
// static st::S_TimedRelay sensor8(F(“relaySwitch1”), PIN_TIMEDRELAY_1, LOW, false, 3000, 0, 1);

//Executors
static st::EX_Switch executor1(F(“switch1”), PIN_SWITCH_1, LOW, true); //Inverted logic for “Active Low” Relay Board
static st::EX_Switch executor2(F(“switch2”), PIN_SWITCH_2, LOW, true); //Inverted logic for “Active Low” Relay Board
static st::EX_Switch executor3(F(“switch3”), PIN_SWITCH_3, LOW, true); //Inverted logic for “Active Low” Relay Board
static st::EX_Switch executor4(F(“switch4”), PIN_SWITCH_4, LOW, true); //Inverted logic for “Active Low” 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);

//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);
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);
st::Everything::addExecutor(&executor2);
st::Everything::addExecutor(&executor3);
st::Everything::addExecutor(&executor4);

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

Dan,

You’re close! However, the DS18B20 ST_Anything library is not designed to be used exactly the way you are trying to use it. It does support multiple DS18B20 sensors attached to a single pin, however you need not create a device for each one in your sketch’s setup() routine. You simply need to create one entry and specify the number of DS18B20 attached to that pin as is documented in the PS_DS18B20_Temperature.h and .cpp files in the comments at the top of those files:

//			  Create an instance of this class in your sketch's global variable section
//			  For Example:  st::PS_DS18B20_Temperature sensor1("temperature1", 120, 0, PIN_TEMPERATURE, false, 10, 1);
//
//			  st::PS_DS18B20_Temperature() constructor requires the following arguments
//				- String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name
//				- 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

So, for your specific use case, please eliminate all of the PS_DS18B20_Temperature devices except “sensor2” and create as show below. PLEASE NOTE the name change to simply “temperature” as it becomes the root name for each of your DS18B20 sensors, numbered 1 through 5. Note the extra arguments added to the end of the following line as well. The last one, 5, is the number of DS18B20 sensors attached to the 1Wire bus.

st::PS_DS18B20_Temperature sensor2(“temperature”, 120, 0, PIN_TEMPERATURE, false, 10, 1, 5);

In my testing, these 5 DS18B20 sensors will always be reported in the same order as long as you never change the wiring. Threfore, you can rename each child device (via your phone’s ST App) that is created for them in SmartThings to be something meaningful to keep them sorted.

Hello DAN.
I’m sorry for my English, it is not my native language.
First of all: Thank you for your great work!
I’m trying to automate my home and I’ve chosen ST HUB+Arduino Mega+ST_Anything for this job. I have successfully build a test stand and found out that nothing works without the internet connection. :astonished: I’ve read on the forum that Smart Lighting app should work without the internet but it needs some sort of whitelisted devices that are allowed to work locally. So the question is: Is there a way to make lighting work without the internet connection?
I see two ways:

  1. To make HUB think that connected devices are allowed to work locally with Smart Lighting app. (I don’t know if it is possible or maybe it is done already. I didn’t find anything about it.)
  2. To make a device handler that will process the button press on the arduino side, switch a relay and send its status to ST HUB. It will need two pins, one for the button, second for the relay, like it is in the garage door device handler. May be it doesn’t meet ST’s cloud-based ideology, but it will make the system much more reliable and will eliminate the delay between button press and the lighing turns on!

The possibility to sit in the dark when internet connection is lost sound like a deal breaker to me.
Thank you.

Hi Dan,

is there any fix for the child returning “NULL” value. I just get stuck with this Issue. I’m just willing to use the ST_Anything library to connect my SOMFY RTS controlled awning to ST. By using relaySwitches to open, stop and close the awning by connecting dry relays in parallel to the RTS remote buttons.
I can connect my ESP826 it sends status relay 1,2,3 off and so, but child creation fails with: ‘‘Child device creation failed. Please make sure th “NULL” is installed and published’’ I already deleted all childs and parents installed them new and created a new device with different naming, but it still fails.
Any help would be appreciated.

Your work is great as it helps connecting things which aren’t connectable with in a moment.

best regards
Kai

@Kai_Lenk I will need to see a copy of your Arduino IDE sketch in order to assist you. I assume you have saved and published all of the Child Device Handlers without making any modifications?

@AlexBat As you have determined, ALL CUSTOM CODE runs in the SmartThings cloud. Therefore, all of ST_Anything’s groovy code will run in the cloud.

As for enabling some amount of local control within the Arduino, that is possible with some custom Arduino sketch code. One such example, called ST_Anything_Relays_Thingshield.ino, can be found in the deprecated sketch folder. Take a look and see if you can adapt this old code for your project.

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.