[DEPRECATED] ST_Anything - Arduino/ESP8266/ESP32

If you want old DTH’s, you’ll need to manually replace each and everyone one of them after downloading the old revision zip file from GitHub.

Take a look at the sketch in this earlier post. You’ll see the second callback function which can be used to handle/intercept data coming into the Arduino from ST. You can to declare the extra callback routine, and assign its address to a function pointer within Everything (see the change to the setup() routine.)

1 Like

Bob - The wiring diagram I provided to you earlier demonstrates how to wire the 12v power supply, through the relay, to a device (shown as an LED), and then back to the power supply.

https://rover.ebay.com/rover/0/0/0?mpre=https%3A%2F%2Fwww.ebay.com%2Fulk%2Fitm%2F183693979982

He wiring that I have pictured works for testing.
My wife would like me to clean up the wires.

But this also work to clean up the wiring.

hahah :slight_smile: thank you Dan.
As soon as i unplugged it and plug it back in it created a child device as it should, i guess i should have unplugged it once i first installed it but i bothered you with a question instead sorry :slight_smile:
However I could control it to turn it ON but i could not turn it OFF so i went and checked the code and at the very very end under void messageCallout instead of switch1 off by mistake for some odd reason we had written switch2 off when we have no switch2 so i changed that and it worked like a charm.
I am so happy and so greatful to you for ST_Anything and help this is my second project with ST_Anything now after my nodeMCU curtain automation.

One last thing though if you dont mind can you please have a look at our code and tell me how to turn this into a momentary switch (ON for 2 seconds and back to OFF again)

Thank you again

/// @file
/// @brief
///   Arduino SmartThings Ethernet ESP8266 WiFi On/Off with LED Example 
///
///   Revised by Dan Ogorchock on 2017-02-11 to work with new "SmartThings v2.0" Library
///
///   Notes: The ESP-01 communicates via WiFi to your home network router,
///          then to the ST Hub, and eventually to the ST cloud servers.
///   For Denis Grabocka use case
///
//*****************************************************************************

#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                 ****   // If i am understanding it correctly we do not need any digital Inputs or Outputs in this sketch
#define BUILTIN_LED 16                    ****   // hence the commenting out of this portion

#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   */

byte relON[] = {0xA0, 0x01, 0x01, 0xA2};  // Hex command to send to serial for open relay      Help needed here: Do I define these here or can i just serial write the hex command directly further down??
byte relOFF[] = {0xA0, 0x01, 0x00, 0xA1}; // Hex command to send to serial for close relay     Also is byte the proper parameter

//*****************************************************************************
// 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     = "grabockanet_2ghz";                           //  <---You must edit this line! // I will be putting my informations on this part 
String str_password = "4697374387";                   //  <---You must edit this line!
IPAddress ip(192, 168, 1, 222);       // 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, 192);    // 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);

// bool isDebugEnabled;    // enable or disable debug in this example


//*****************************************************************************
// 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 on()
{
  Serial.write(relON, sizeof(relON));  // turn LED on
  smartthing.send("switch1 on");        // send message to cloud
}

//*****************************************************************************
void off()
{
  Serial.write(relOFF, sizeof(relOFF));   // turn LED off
  smartthing.send("switch1 off");       // send message to cloud
}

//*****************************************************************************
// 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()
{
  // setup default state of global variables
  // isDebugEnabled = true;

  // if (isDebugEnabled)
  // { // setup debug serial port           
  Serial.begin(9600);         // setup serial with a baud rate of 9600             **** In my ESP01 case the baud rate is the same as well 9600
    // Serial.println("");
    // Serial.println("setup..");  // print out 'setup..' on start
  // }
  
  // setup hardware pins 
  // pinMode(PIN_LED, OUTPUT);     // define PIN_LED as an output    **** Should i comment this out in my case?? if Not what do i do here??
  Serial.write(relOFF, sizeof(relOFF));   // set value to HIGH (off) **** Is this right??

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

  //synch up the ST cloud
  smartthing.send("switch1 off");       // send message to cloud
}

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

//*****************************************************************************
void messageCallout(String message)
{
  // if debug is enabled print out the received message
  // if (isDebugEnabled)
  // {
    // 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("switch1 on"))
  {
    on();
  }
  else if (message.equals("switch1 off"))
  {
    off();
  }
}    ```

Hi Dan ,

Been messing with servo part of your code , and noticed that it wasnt detaching the servo once it had moved, left it for a few minutes and took another look at the D1 diode on my wemos D1 mini was glowing red , tried a different D1 … same thing … magic smoke insued, tried a different servo … same thing again , locks the servo in a constant powered state sucking more and more amps till it blows the diode on the controller … any ideas :smiley:

Sounds like a wiring problem. There is no way the code can fry your hardware if it is wired correctly. You should be powering the servo separately from the Wemos D1 Mini, not through it.

should it keep the servo held though dan when its not moving , as i have in the past used a detach to release it . but you are right , i did power the servo THROUGH the wemos , so quite likely just too much amp draw

I finally got it figured out I had to go ahead a second 12 volt power source into the mix.
So what I did was went down to my local Walmart embody 6 plug strip that splits to 12 volt wires into 6 negative and positive.

Thanks for all the help much appreciated.

A couple of tips. Please make sure you have updated all of the ST_Anything Arduino libraries from my GitHub repository (all of them, don’t be selective.). A significant change was made recently to support asynchronous servo motion, including multiple servos moving at once, and the ability to adjust the speed of the motion. These changes required some significant underlying architecture changes. Thus the need to upgrade all of it.

Second tip is to read through the comments at the top of EX_Servo.cpp to understand what all of the parameters are used for. One of them is used to disable the servo once the motion is complete. This, and other features, will only work if you upgrade all of the library files.

Hope this helps!

@Tecno-Thing - one more thing - I just released a new version of the EX)Servo.h and EX_Servo.cpp files that supports two additional/optional arguments for the constructor. You may want to grab these latest files.

//  Summary:  EX_Servo is a class which implements the SmartThings/Hubitat "Switch Level" device capability.
//			  It inherits from the st::Executor class.
//
//			  Create an instance of this class in your sketch's global variable section
//			  For Example:  st::EX_Servo executor1(F("servo1"), PIN_SERVO, 90, true, 1000, 0, 180, 2000, 544, 2400);
//
//			  st::EX_Servo() constructor requires the following arguments
//				- String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name
//				- byte pin_pwm - REQUIRED - the Arduino Pin to be used as a pwm output
//				- int startingAngle - OPTIONAL - the value desired for the initial angle of the servo motor (0 to 180, defaults to 90)
//              - bool detachAfterMove - OPTIONAL - determines if servo motor is powered down after move (defaults to false) 
//              - int servoDetachTime - OPTIONAL - determines how long after the servo is moved that the servo is powered down if detachAfterMove is true (defaults to 1000ms)
//				- int minLevelAngle - OPTIONAL - servo angle in degrees to map to level 0 (defaults to 0 degrees)
//				- int maxLevelAngle - OPTIONAL - servo angle in degrees to map to level 100 (defaults to 180 degrees)
//              - int servoRate - OPTIONAL - initial servo rate in ms/degree (defaults to 2000, used to ensure a gentle move during startup, afterwards comes from SmartThings/Hubitat with each move request)
//              - int minPulseWidth - OPTIONAL - minimum pulse width in milliseconds, defaults to 544 (see Arduino servo attach() function)
//              - int maxPulseWidth - OPTIONAL - maximum pulse width in milliseconds, defaults to 2400 (see Arduino servo attach() function)

Hey Dan our friend :slight_smile:
So like i said here

I got our script to work and it works perfectly but any thoughts where i would begining on turning it into a momentary switch (so ON for 2 sec and than OFF)

Hope you have a great week
Denis

Hey Dan our friend :slight_smile:
So like i said here

I got our script to work and it works perfectly but any thoughts where i would beginning on turning it into a momentary switch (so ON for 2 sec and than OFF)

Hope you have a great week
Denis

Sorry for the delayed response, and thanks for the reminder. It was a busy weekend with family visiting.

Making this momentary will require a little code in the sketch, assuming your want the Arduino to perform the timing. Or, the child switch DTH could turn the switch off after a few seconds.

How do you want it handled?

Wow thank you for your quick reply … I completely understand no need to apologize …it is me who should apologize for bothering you

Lets go by the way that it is easier for you and therefore by proxy me LOL i am open to both.
D

1 Like

Give this version of the sketch a try. Take a look at how I added a non-blocking 2000ms timer. If a “switch1 off” command happens to come in from ST during the 2 seconds, the timer is cancelled when the switch is turned off.

I have not tested this code, but it does successfully compile. Let me know how it works.

/// @file
/// @brief
///   Arduino SmartThings Ethernet ESP8266 WiFi On/Off with LED Example 
///
///   Revised by Dan Ogorchock on 2017-02-11 to work with new "SmartThings v2.0" Library
///
///   Notes: The ESP-01 communicates via WiFi to your home network router,
///          then to the ST Hub, and eventually to the ST cloud servers.
///   For Denis Grabocka use case
///
//*****************************************************************************

#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                 ****   // If i am understanding it correctly we do not need any digital Inputs or Outputs in this sketch
#define BUILTIN_LED 16                    ****   // hence the commenting out of this portion

#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   */

byte relON[] = {0xA0, 0x01, 0x01, 0xA2};  // Hex command to send to serial for open relay      Help needed here: Do I define these here or can i just serial write the hex command directly further down??
byte relOFF[] = {0xA0, 0x01, 0x00, 0xA1}; // Hex command to send to serial for close relay     Also is byte the proper parameter

//*****************************************************************************
// 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     = "grabockanet_2ghz";                           //  <---You must edit this line! // I will be putting my informations on this part 
String str_password = "4697374387";                   //  <---You must edit this line!
IPAddress ip(192, 168, 1, 222);       // 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, 192);    // 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);

// bool isDebugEnabled;    // enable or disable debug in this example

bool momentaryActive = false;
int period = 2000;
unsigned long time_now = 0;

//*****************************************************************************
// 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 on()
{
  Serial.write(relON, sizeof(relON));  // turn LED on
  time_now = millis();
  momentaryActive = true;
  smartthing.send("switch1 on");        // send message to cloud
}

//*****************************************************************************
void off()
{
  Serial.write(relOFF, sizeof(relOFF));   // turn LED off
  momentaryActive = false;
  smartthing.send("switch1 off");       // send message to cloud
}

//*****************************************************************************
// 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()
{
  // setup default state of global variables
  // isDebugEnabled = true;

  // if (isDebugEnabled)
  // { // setup debug serial port           
  Serial.begin(9600);         // setup serial with a baud rate of 9600             **** In my ESP01 case the baud rate is the same as well 9600
    // Serial.println("");
    // Serial.println("setup..");  // print out 'setup..' on start
  // }
  
  // setup hardware pins 
  // pinMode(PIN_LED, OUTPUT);     // define PIN_LED as an output    **** Should i comment this out in my case?? if Not what do i do here??
  Serial.write(relOFF, sizeof(relOFF));   // set value to HIGH (off) **** Is this right??

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

  //synch up the ST cloud
  smartthing.send("switch1 off");       // send message to cloud
}

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

  //check to see if we need to run off the momentary switch device
  if (momentaryActive) {
    if (millis() - time_now > period) {
      momentaryActive = false;
      off();
    }
  }
}

//*****************************************************************************
void messageCallout(String message)
{
  // if debug is enabled print out the received message
  // if (isDebugEnabled)
  // {
    // 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("switch1 on"))
  {
    on();
  }
  else if (message.equals("switch1 off"))
  {
    off();
  }
}
1 Like

Works great thank you sir very much :slight_smile:

Hi Dan: I am planning to move to Hubitat. Any comment or guidence from you? Thanks

Hi Dan @ogiewon,

Hope you’ve been well. A few questions… I’m considering DIYing a way to spin the knob on my steam radiator, so naturally I’m looking at using my favorite project, St-Anything! It’ll need to use a motor, but it’ll have to spin it at least 5 or 6 rotations to open or close the knob. Ideally I’d use a stepper motor. Do you know if that’s possible? Alternatively, if using a big continuous motion servo, do you know if I can set up an action based on time with st anything (like turn left for 16 seconds to open, turn right 16 seconds for close)? Or is the servo sketch just setup for rotation angles and not compatible with continuous rotation servos?

Currently, the EX_Servo class only works with servo angles, not continuous rotation servos.

Adding a stepper motor support device would be an interesting solution. Do you have a device in mind? I actually have a very small, inexpensive stepper motor that I experimented with on an Arduino many years ago… I might have to dig it out of the parts bin. :wink:

A stepper motor sketch would be outstanding for this and many other projects (like blinds and curtains for instance). Would need to include end stop support for many uses, so it could get it’s bearings on power on. There are lots of helpful libraries for atrium and steppers, but a fresh integration is deff above my skill level.

My backup plan is to use the timed relay function to interrupt an ssr spinning a motor mounted to a 3d printed assembly that grips the knob. Could use webcore to control it as a rudimentary thermostat.