[DEPRECATED] ST_Anything - Arduino/ESP8266/ESP32

The requirements are very low I need the vent to open and close and the and the SmartThings app will use the dimmer switch functionality. Here’s what I want the vent to do let’s say the room is 75 degrees but AC is set at 72 open the vent to cool down the room. If someone walks into the room it will activate the vent also. The application that I have already bought tell SmartThings when to open and close. Basically I just need a device Handler to make the Hardware open and close and use the dimmer funks nationality. It will be using 1.5 volts. Some people have suggested pwm pulse-width modulation. From what I understand you said one pin High the other pin low to go One Direction and reverse it to go the opposite direction. If you guys have any other suggestions on how to get this done let me know. I have a gentleman at the library supposed to be helping me Friday but he’s thinking about using the SmartThings anyting relay functionality.

An h-bridge uses two pins to drive the motor,
The first is to choose the spin direction, and the other to activate the motor.
In most cases you can use PWM to control the second pin - to adjust the speed.
Caution, as a general rule - try not to swap the direction without stopping the motor first.

It will be powered by 110 to usb and battery backup.
It will also need to keep track of what position the motor is in.
that is app i’m using ScheduleTstatZones, refer to http://thingsthataresmart.wiki/index.php?title=ScheduleTstatZones

int M1_Left = 12; //Direccion
int M1_Right = 11; //Direccion

void setup()
{
  pinMode(M1_Left, OUTPUT);
  pinMode(M1_Right , OUTPUT);
}

void loop(){
  turn (1);
  delay(1000); //1 sg
 
  stop();
  delay(250); //250ms

  turn(2);
  delay(1000); //1 sg
 
  stop();
  delay(250); //250ms
}


void turn(int direction)
{
  boolean inPin1 = LOW;
  boolean inPin2 = HIGH;

  if(direction== 1){
    inPin1 = HIGH;
    inPin2 = LOW;
  }
    digitalWrite(M1_Left, inPin1);
    digitalWrite(M1_Right , inPin2);
}

void stop(){
    digitalWrite(M1_Left, LOW);
    digitalWrite(M1_Right , LOW);
}

So, it appears that the delay(1000) commands are what is allowing the motor to turn in way direction for 1 second.

The problem with this design is that nothing is keeping track of the actual position of the vent (whereas a servo motor does keep track of its position internally.)

Would you be alright with just having the vent 100% open or 100% closed? That would be the easiest to implement.

If you really want to be able to open the vent partially, I am not sure if a simple DC motor with no feedback is the best approach.

Let’s just start with a simple on (open) and off (closed) design and see how that goes. Sound good?

Yes that would work. Keep in mind all the requirements for this project are also in that Wiki that I linked to. The developer can answer any of your questions Dan. The developer was the one that said his app also uses the dimmer switch functionality. I’m also using a nest thermostat so if you would like to write a different SmartThings app you’re more than welcome to or make a device Handler that works with app. That code was just example that I found on the internet. To get started with.

Bob,

I am NOT going to write a DTH that specifically works with that particular SmartApp that you linked. Not sure if you realize it or not, but that author’s Device Handlers and SmartApps are available for purchase. I do not have access to his code to understand what his requirements are, nor am I willing to engage in a detailed conversation with him on your behalf.

What I am willing to do is provide you with a very basic Arduino Sketch that implements both the ST “Switch” and “Switch Level” capabilities. You will be able to then do whatever you want to that sketch to integrate it with any motion control solution you desire.

Okay that would work.

Bob,

Here is your sketch. It still uses all of the exact same ST_Anything software on the SmartThings side. So you will still need to manually create a Parent Device using the ST Web IDE.

The big difference in this sketch versus a typical ST_Anything sketch is that all of the code (except network communications) in included in this one sketch. This should make it much simpler for you to ADD your motor control commands.

Before you start trying to edit this file, PLEASE GET IT UP AND RUNNING by only adding in your WiFi credentials and assigning the TCP/IP settings. Do not change anything else until you can get this working with SmartThings.

//*****************************************************************************
///   Arduino SmartThings Ethernet ESP8266 WiFi On/Off/Dim with LED Example 
///
///   Created by Dan Ogorchock on 2018-06-27 for bob5731
///
///   Notes: The NodeMCU ESP communicates via WiFi to your home network router,
///          then to the ST Hub, and eventually to the ST cloud servers.
///
///   This sketch is very simple and implements the ST "Switch" and "Switch Level"
///   Capabilities.  How these are used if left up to the user.
///
///
//*****************************************************************************

#include <SmartThingsESP8266WiFi.h>

//*****************************************************************************
// Pin Definitions    | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
//                    V V V V V V V V V V V V V V V V V V V V V V V V V V V V V
//*****************************************************************************
//******************************************************************************************
//NodeMCU ESP8266 Pin Definitions (makes it much easier as these match the board markings)
//******************************************************************************************
//#define LED_BUILTIN 16
//#define BUILTIN_LED 16
//
//#define D0 16
//#define D1  5
//#define D2  4
//#define D3  0
//#define D4  2
//#define D5 14
//#define D6 12
//#define D7 13
//#define D8 15
//#define D9  3
//#define D10 1


#define PIN_LED LED_BUILTIN  //Onboard LED (THIS LINE IS JUST AN EXAMPLE!)

//*****************************************************************************
// Global Variables   | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
//                    V V V V V V V V V V V V V V V V V V V V V V V V V V V V V
//*****************************************************************************
SmartThingsCallout_t messageCallout;    // call out function forward decalaration

//******************************************************************************************
//ESP8266 WiFi Information    CHANGE THIS INFORMATION ACCORDINGLY FOR YOUR NETWORK!
//******************************************************************************************
String str_ssid     = "your_ssid_here";                                    //  <---You must edit this line!
String str_password = "your_wifi_password_here";               //  <---You must edit this line!
IPAddress ip(192, 168, 1, 221);       // 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, 149);    // smartthings hub ip     //  <---You must edit this line!
const unsigned int hubPort = 39500;   // smartthings hub port


//Create a SmartThings Ethernet ESP8266WiFi object
st::SmartThingsESP8266WiFi smartthing(str_ssid, str_password, ip, gateway, subnet, dnsserver, serverPort, hubIp, hubPort, messageCallout);

int switchLevel;        // current switch dim level (usually 0 - 99)
String switchStatus;    // current switch status ("on" or "off")

//*****************************************************************************
// Local Functions  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
//                  V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V
//*****************************************************************************
void refresh()
{
  //Update the ST cloud with current data from the MCU
  String msg = "dimmerSwitch1 " + switchStatus;
  smartthing.send(msg);       // send message to cloud
  Serial.println("sending: " + msg);

  msg = "dimmerSwitch1 " + String(switchLevel);  
  smartthing.send(msg);       // send message to cloud
  Serial.println("sending: " + msg);
}

//*****************************************************************************
void on()
{

  //TODO: Fill in whatever user code is required here

  digitalWrite(PIN_LED, LOW);  // turn LED on (THIS LINE IS JUST AN EXAMPLE!)
  
  switchStatus = "on";        
  refresh();  //update ST cloud
}

//*****************************************************************************
void off()
{

  //TODO: Fill in whatever user code is required here

  digitalWrite(PIN_LED, HIGH);   // turn LED off (THIS LINE IS JUST AN EXAMPLE!)
  
  switchStatus = "off";        
  refresh();  //update ST cloud
}

//*****************************************************************************
void setLevel(int level)
{

  //TODO: Fill in whatever user code is required here

  switchLevel = level;
  refresh();  //update ST cloud  
}

//*****************************************************************************
void messageCallout(String message)
{
  // if debug is enabled print out the received message

  Serial.print("Received message: '");
  Serial.print(message);
  Serial.println("' ");

  // if message contents equals to 'on' then call on() function
  // else if message contents equals to 'off' then call off() function
  if (message.equals("dimmerSwitch1 on"))
  {
    on();
  }
  else if (message.equals("dimmerSwitch1 off"))
  {
    off();
  }
  else if (message.equals("refresh"))
  {
    refresh();
  }
  else //must be a level command
  {
    String s = message.substring(message.indexOf(' ') + 1);
    s.trim();
    switchLevel = int(s.toInt());
    setLevel(switchLevel);
  }
}


//*****************************************************************************
// API Functions    | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
//                  V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V
//*****************************************************************************
void setup()
{

  Serial.begin(115200);         // setup serial with a baud rate of 115200
  Serial.println("");
  Serial.println("setup..");    // print out 'setup..' on start

  // setup hardware pins 
  pinMode(PIN_LED, OUTPUT);     // define PIN_LED as an output (THIS LINE IS JUST AN EXAMPLE!)

  //Run the SmartThings init() routine to make sure the ThingShield is connected to the ST Hub
  smartthing.init();

  //initialize switch level to 0
  switchLevel = 0;
  
  //synch up the ST cloud intially (set switch to off)
  off();

}

//*****************************************************************************
void loop()
{
  // run smartthing logic
  smartthing.run();
}
2 Likes

Thanks for this Dan, it worked just as advertised. I have actually edited the DHT a bit more as I am also bringing it into Action tiles and according to those guys they don’t look at the unit parameter, so the code is now spoofing as a battery device so that I get a proper readout in Action tiles!

Fun and games all round. Thanks heaps for your help, FYI my Arduino has been quite stable for the last week or so reading/writing serial as well as running ST with about 15 Executors and a few sensors :slight_smile:

I need to add DHT11.

How do I add PS DS18B20?

Bob,

Sigh… That would have been pretty simple if the sketch was still based on the ST_Anything library. But alas, the sketch I wrote for you is very simple and it would take a little bit of effort to add a temperature sensor at this point. I clearly asked you for all of the requirements, which I fulfilled with the sketch I provided to you to use as a starting point.

If you can get a temperature sensor added to your sketch, that only updates data about every 60 seconds (DO NOT FLOOD ST with continuous temperature updates!!!) you can use the following lines of code to send the data (where ‘currentTemperature’ is a variable you will need to populate with temperature data from the DHT11, DHT22 or DS18B20 sensor).

  String msgTemp = "temperature1 " + currentTemperature;
  smartthing.send(msgTemp);       // send message to cloud
  Serial.println("sending: " + msgTemp);

Having an issue with this. I implemented 2 esp32’s earlier in the year and I am trying to add a esp8266 now and cannot get it to talk to the app on my phone. After uploading the code to my device I can serial monitor and the contact updates correctly but the app does not. Any ideas why it would be doing this?

Usually means your MAC address was typed in wrong in the Parent Device on your phone’s ST Classic Mobile App. Also, did you remember to edit the sketch to put in the correct IP address of your hub?

MAC is correct (checked 3 times) and the ip address of the hub is correct too. Just doesnt want to work.

Did you remember to specify the Location and Hub when manually creating the Parent Device in the ST Classic Web IDE?

Can you ping the ESP8266?

yep, only have 1 hub. I did this at the beginning of the year and worked flawlessly. Now I am following the same process it is not working. Yes I can ping it.

Do you get any data in the ST Classic Web IDE Live Logging page?

Can you show me a screen shot from the Web IDE of the PARENT DEVICE’s settings?

yes. But the app is not updating. Just rebooted the phone to make sure thats not it.

The phone has nothing to do with it, actually. It is purely used to configure the settings of the Parent Device. Afterwards, you could turn your phone off and it wouldn’t matter.

//******************************************************************************************
// 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
// 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 <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_CONTACT_1 D7 //SmartThings Capabilty “Contact Sensor”
//#define PIN_BUTTON_1 D3 //SmartThings Capabilty Button / Holdable Button (Normally Open!)
//#define PIN_BUTTON_2 D4 //SmartThings Capabilty Button / Holdable Button (Normally Open!)
//#define PIN_MOTION_1 D5 //SmartThings Capabilty “Motion Sensor” (HC-SR501 PIR Sensor)
//#define PIN_SMOKE_1 D6 //SmartThings Capabilty “Smoke Detector”
//#define PIN_TEMPERATURE_1 D7 //SmartThings Capabilty “Temperature Measurement” (Dallas Semiconductor DS18B20)
//#define PIN_TIMEDRELAY_1 D8 //SmartThings Capability “Relay Switch”

//******************************************************************************************
//ESP8266 WiFi Information
//******************************************************************************************
String str_ssid = “"; // <—You must edit this line!
String str_password = "
”; // <—You must edit this line!
IPAddress ip(170, 34, 185, 65); //Device IP Address // <—You must edit this line!
IPAddress gateway(170, 34, 185, 1); //Router gateway // <—You must edit this line!
IPAddress subnet(255, 255, 255, 0); //LAN subnet mask // <—You must edit this line!
IPAddress dnsserver(8, 8, 8, 8); //DNS server // <—You must edit this line!
const unsigned int serverPort = 8090; // port to run the http server on

// Smartthings / Hubitat Hub TCP/IP Address
IPAddress hubIp(170, 34, 185, 206); // smartthings/hubitat hub ip // <—You must edit this line!

// SmartThings / Hubitat Hub TCP/IP Address: UNCOMMENT line that corresponds to your hub, COMMENT the other
const unsigned int hubPort = 39500; // smartthings hub port
//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
//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, false, 10, 1);

//Interrupt Sensors
static st::IS_Contact sensor3(F(“contact1”), PIN_CONTACT_1, HIGH, 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 sensor6(F(“motion1”), PIN_MOTION_1, HIGH, false);
//static st::IS_Smoke sensor7(F(“smoke1”), PIN_SMOKE_1, HIGH, true, 500);

//static st::IS_DoorControl sensor17(F(“doorControl1”), PIN_DOORCONTROL_CONTACT_1, HIGH, true, PIN_DOORCONTROL_RELAY_1, LOW, true, 1000);

//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_Alarm executor1(F(“alarm1”), PIN_ALARM_1, LOW, true);
//static st::EX_Switch executor2(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
//*****************************************************************************
//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);
//st::Everything::addSensor(&sensor17);

//*****************************************************************************
//Add each executor to the “Everything” Class
//*****************************************************************************
//st::Everything::addExecutor(&executor1);
//st::Everything::addExecutor(&executor2);

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

Hiding your private network addresses only make it harder to troubleshoot. I can’t see how your network is configured…

It makes sense to blank out your WiFi credentials, but everything else is pretty useless to a hacker.

I would not use Google’s DNS server address. Just use your router’s address for DNS.