Wow, that is much different then what I expected. Here is what I am doing. I am using the Wemo D1 mini. Some I am using the D1 pin 5 to switch the relay and D3 = pin 0 to use a push button on the wall just in case the Wifi would die we could still turn off the lights and turn them on. Then I have some D1 mini’s that have two relays for like my living room lamps or my wall that has 3 switches on for ceiling fan, garage light and living room light. Each one of them have a push button switch as well. My wife isn’t on board with the smart home, so the buttons are important. She still walks up and turns lights on and off that way. I use Alexa. Am I barking up the wrong tree with this, or am I just so green with coding I don’t understand how this can work for me? I do want to say thank you for helping me out with this. I want to get better so I can maybe someday pass on this knowledge to my students.
ogiewon
Dan
Community Master
April 27
Bradley_Martin:
I take the opportunity to look at a functioning code to understand what is going on with it and I learn from it and really learn when I try to modify it.
I completely understand the desire to learn how things work. I am the same way. My son and I wrote ST_Anything years ago to give back to the community a much simpler, easier to use solution for integrating Arduino-type boards with SmartThings. This actually started back when SmartThings sold an Arduino Shield called the “ThingShield.” This is where I came up with the name “AnyThing” as it allowed users to connect an Arduino to ST and create ‘Any’ type of ‘Thing’ they desired.
There are two main custom sets of Arduino Libraries within our GitHub repository…
The first set are the “SmartThings…” libraries. These perform all of the Arduino to ST Hub communications over Zigbee (the old ThingShield), Ethernet, or WiFi. These libraries are specific on the hardware you’re using (e.g. Wemos D1 would use the SmartThingsESP8266WiFi library.)
The second set of libraries are the “ST_Anything…” libraries. These utilize the aforementioned ‘SmartThings…’ libraries for all communications to the ST Hub. The purpose of ST_Anything is to then create a solution that allows users to easily write a simple sketch, without having to know all of the details about Arduino programming and how to utilize the ‘SmartThings…’ libraries. GPIO pins are automagically configured and monitored for changes. Updates are automatically sent to ST. Commands from ST are automatically routed to the correct ST_Anything ‘devices’ that are defined in the sketch by the user. At the top of every one of the ST_Anything device class C++ set of files (the .h and a .cpp files), you will find a description of all of the parameters that can be passed into the constructor of each object in the setup() portion of the sketch.
The basic portions of an ST_Anything sketch are:
- define your local network connection information
- declare which ‘ST_Anything’ devices are to be used in your sketch
- declare which type of ‘SmartThings…’ communication object will be used
- Add the devices from step 2 to the ST_Anything ‘Everything’ class. This class takes care of all scheduling, and data routing between ST and the individual devices on the Arduino
(Note: I also include a copy of all of the other Arduino libraries that ST_Anything uses for its various devices. I do this to ensure users have everything they need to get started without having to hunt down an install a bunch of libraries from all over the internet. There are a few things that need to be downloaded via the Boards Manager, like support for the ESP8266, of course.)
Bradley_Martin:
You wouldn’t happen to have a version that is for one relay would you without all the other stuff?
Sure, that’s a pretty simple sketch. Here you go!
//******************************************************************************************
// File: ST_Anything_Switch_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 Switch devices (used to turn on a digital output (e.g. LED, relay, etc...)
//
// 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
// 2018-02-09 Dan Ogorchock Added support for Hubitat Elevation Hub
//
//******************************************************************************************
//******************************************************************************************
// 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
//******************************************************************************************
//Define which Arduino Pins will be used for each device
//******************************************************************************************
#define PIN_SWITCH_1 D1 //SmartThings Capability "Switch"
//******************************************************************************************
//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, 227); //Device IP Address // <---You must edit this line!
IPAddress gateway(192, 168, 1, 1); //Router gateway // <---You must edit this line!
IPAddress subnet(255, 255, 255, 0); //LAN subnet mask // <---You must edit this line!
IPAddress dnsserver(192, 168, 1, 1); //DNS server // <---You must edit this line!
const unsigned int serverPort = 8090; // port to run the http server on
// Smarthings Hub Information
IPAddress hubIp(192, 168, 1, 149); // 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_SWITCH_1, 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
//*****************************************************************************
//*****************************************************************************
//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();
}