[RELEASE] ST_Anything - Arduino/ESP8266/ESP32

That information is all in the ReadMe in my GitHub repo…

here is the original code:
we had it in the setup function so it would only run once because it was the aurdino was being turned on and off my a smart light bulbs board.
MyKeurig

Compiled 11/27/18

This Sketch serves the basic function of cycling a Keurig k35 model's brew cycle Autonomously without the need
to open the lid. It presses the power button, cycles the lid, brews a (medium size in my case) cup of coffee,
then safely shuts down the Keurig after completing the brew. This can all be achieved by modifying the existing
button panel harness by means of elongating the ground, power, lid, and brew circuits, and attaching them as follows:
Ground to D1
Power Button to D2
Lid Switch to D3
Desired Brew Button to D4

Thanks for checking out my first sketch and Happy Brewing

*/
const int power = 2;
const int lid = 3;
const int brew = 4;

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin D2 as an output for “POWER BUTTON”
pinMode(power, OUTPUT);
// initialize digital pin D3 as an output for “LID POSITION SENSOR”
pinMode(lid, OUTPUT);
// initialize digital pin D4 as an output for “10oz BREW BUTTON”
pinMode(brew, OUTPUT);

//BEGIN BREW CYCLE
//SET ALL OUTPUTS TO OFF
digitalWrite(power, HIGH);
digitalWrite(lid, HIGH);
digitalWrite(brew, HIGH);
//TOGGLE POWER SWITCH
digitalWrite(power, LOW);
delay(1000);
digitalWrite(power, HIGH);
//WAIT TO POWER UP, CYCLE LID POSITION
delay(5000);
digitalWrite(lid,HIGH);
delay(1000);
digitalWrite(lid, LOW);
//WAIT, TOGGLE BREW BUTTON
delay(5000);
digitalWrite(brew, LOW);
delay(1000);
digitalWrite(brew, HIGH);
//WAIT POWER OFF 15MIN
delay(900000);
digitalWrite(power, LOW);
delay(1000);
digitalWrite(power, HIGH);

Serial.begin(9600);
}

// the loop function runs over and over again forever
void loop(){

thank you for some reason the it was only showing me the esp32 setup get hub on this page

So, that original sketch simply toggles three digital outputs. I am confused as to the purpose of this. Is the goal to have SmartThings start a brewing cycle at a certain time each day? Is that what the purpose of the smart bulb’s zigbee radio board? Were you using the zigbee board out of an old Cree bulb by any chance? What was the previous home automation system, if not SmartThings (out of curiosity!) ?

My guess is the bulb’s zigbee board received a command from a home automation system, which then caused its output to somehow reset/power on the Arduino. The Arduino then simply toggled the digital outputs per a hard-coded schedule.

Does that about sum it up?

If so, you can use an ESP8266 or an ESP32 board to replace the smart-bulb’s zigbee board, as well as the Arduino UNO (assuming the Keurig can be manipulated in the same manner using a 3.3v board like ESP8266 or ESP32, as opposed to the 5v board of the UNO.

Do you have some relays wired between the UNO and the Keurig?

A wiring diagram will be very helpful… :wink:

so the goal, which i did succeed for a couple months until the keurig malfunctioned, was to wake up naturally (not any specific time) say alexa good morning. and while still in bed my coffee would brew and my bathroom heater would turn on and my bathroom lights would slowly lighten up. honestly the hands down best way to wake up in the world. lol. give me a min and ill draw one up… also no relays

1 Like

So, it would probably be easiest to simply create this as a specialized Arduino sketch, that uses my WiFi communications library to integrate with SmartThings. Basically, you just need an “Alexa to SmartThings to Arduino Switch” device. Then, when the Arduino receives the command to turn on the ‘switch’ device, the sketch will run your sequence of digital output commands. At the end of the cycle, the Arduino will tell SmartThings that the ‘switch’ has been turned off, so everything is ready for the next cycle.

When I get some free time, I’ll whip up a very simple example sketch that you can use and then add your custom code to. One thing that will need to happen is that all of your hard-coded ‘delay()’ statements will need to be replaced with ‘millis()’-based timing logic instead. The WiFi communications library does not like long delay() statements as they block the Arduino board from servicing the network stack.

Ok so here is a youtube video. Cant really give you a wiring diagram. Also the button board inside the keurig is 3v3. https://youtu.be/t3OI08AXrq0.

also i did the aurdino esp32 setup… but unsure if should change board choice. i got this https://www.amazon.com/gp/product/B07DBNHJW2/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1
and it says there to use node32 and i used wifi scan example sketch to verify it could find my wifi which it can… im sorry im such a noob.

Some of the delays may not be neccesary since i can name each function as a different thing right? So rather than activating 1 thing (the uno i had with smart light) ill be using esp to call the power button a thing the lid switch a thing and the brew button a thing…

Some of the delays may not be neccesary since i can name each function as a different thing right? So rather than activating 1 thing (the uno i had with smart light) ill be using esp to call the power button a thing the lid switch a thing and the brew button a thing… or we could do it your way because i know nothing lol.

[
EDIT: not sure if I should have added to this post as a reply or create a new topic. I just added this new topic incase that is the correct forum protocol.


]

1) Give a description of the problem
Hi All,

I am using a NodeMCU 12E to automate opening and closing of a pet door. The following hardware list and diagram below will provide an idea what is being done.
2 SPST switches for manual operation (open/close) of the door.
2 SPST switches for automatic motor shutoff at limit of open & close.
1 SPST Relay 3V coil for switching 12v motor power supply.
2 DPDT Relays 3V coil for reversing polarity on motor for open and close direction.

I got the arduino sketch operating properly on the Node MCU, without ST_Anything first, and response to manually activating switches is as expected and nearly instantaneous as monitored in Arduino IDE serial monitor and via LED activation. Boolean logic for activating motor below (will need some modifications, but fine for testing):

if (digitalRead(closeLimitPin) == HIGH || digitalRead(openLimitPin)==HIGH) {
digitalWrite(motorStatePin, LOW); // Set pin to 0V for motor power OFF.
}
else if (digitalRead(closeButtonPin) == HIGH || digitalRead(remoteControl) == HIGH) {
digitalWrite(motorStatePin, HIGH); // Set pin to 1V for motor power on.
digitalWrite(motorDirPin, LOW); // Set pin to 0V for CLOSE Direction.
}
else if (digitalRead(openButtonPin) == HIGH) || digitalRead(PIN_remoteControl) == LOW) {
digitalWrite(motorStatePin, HIGH); // Set pin to 1V for motor power on.
digitalWrite(motorDirPin, HIGH); // Set pin to 1V for OPEN direction.
}
else {
digitalWrite(motorStatePin, LOW); // Set pin to 0V for motor power OFF.

I added ST_Anything with 4 contact sensors and one switch defined. The contact sensors are for monitoring status of the physical switches and the virtual switch is for activating a Digital Output pin (Using D0-onboard LED pin) which is used in the boolean logic for remotely opening and closing the door.
Successfully recognized my ST_Anything Parent and 5 Child devices in Smartthings Classic App.
I have two problems:
A) when I attempt to toggle the switch from the app I just get a “TurningOn” message indefinitely and no messages in the serial monitor.
B) response times to physical interactions (switch activations) is delayed 1-30 seconds (sometimes longer) as evidenced both in the serial monitor and direct observation of test LEDs.

2) What is the expected behaviour?
A) Toggling switch device in Smartthings App will toggle HIGH/LOW value of NodeMCU pin.
B) no delay in response to activation of switches.

3) What is happening/not happening?
I have two problems:
A) when I attempt to toggle the switch from the app I just get a “TurningOn” message indefinately.
B) response times to physical interactions (switch activations) is delayed 1-30 seconds as evidenced both in the serial monitor and direct observation of test LEDs.

4) Wiring Diagram

Welcome to the community. The first thing I’ll need to see is a copy of your sketch. You can either post it here (be sure to use the Preformatted Text ‘</>’ menu option on the sketch after pasting it into this forum software) or you can Private Message me if you’d like.

Have you tried using one of the existing ST_Anything example sketches for the ESP8266 to make sure you have everything set up correctly on both the ST side and your home network side of things?

Operating a ST_Anything Switch device should be nearly instantaneous. You should see the data appear in the Arduino IDE Serial Monitor window almost as fast as you can change it in the ST Classic App on your phone. If that is not happening using my example sketch, then you may have something going on with your network.

Since you’re trying to use a custom sketch, I cannot help troubleshoot it without seeing the code. Any type of blocking call in your sketch can result in delays of processing network traffic between the ESP8266 and the ST Hub. Perhaps this would explain what is happening?

Thanks for the reply @ogiewon and thank you VERY much for the ST_Anything integration. I am pasting the sketch below (hope it works).
-Dan

/*
  Cat Door 

  Opens and closes automated door using Node MCU 12E.
  2 SPST switches for manual operation of the door.
  2 SPST switches for automatic motor shutoff at limit of open & close.
  1 SPST Relay 3V coil for switching 12v motor power supply.
  2 DPDT Relay 3V coil for reversing polarity on motor for open and close direction.
  
*/





//******************************************************************************************
// 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 <IS_Button.h>       //Implements an Interrupt Sensor (IS) to monitor the status of a digital input pin for button presses
#include <S_TimedRelay.h>    //Implements a Sensor to control a digital output pin with timing capabilities

#include <PS_TemperatureHumidity.h>  //Implements a Polling Sensor (PS) to measure Temperature and Humidity via DHT library. 
//#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 <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



  int i = 0;

//*************************************************************************************************
//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 ESP8266 12E/NodeMCU 12E pins will be used for each device
//******************************************************************************************

  #define PIN_openSwitch D7            // MCU D7 – Manual open switch
  #define PIN_openLimitSwitch D2       // MCU D2 – Stop power when door open
  #define PIN_closeSwitch D6           // MCU D6 – Manual close switch
  #define PIN_closeLimitSwitch D1      // MCU D1 – Stop power when door closed
  #define PIN_motorState D8            // MCU D8 – Power on/off    1=on, 0=off
  #define PIN_motorDir D5              // MCU D5 – Motor Direction  1=Open (NO), 0=Close (NC)
  #define PIN_remoteControl D0         // MCU D0 – Web Based control  1=Open, 0=Close

//******************************************************************************************
//ESP8266 WiFi Information
//******************************************************************************************
String str_ssid     = "xxxxxxxx";           //  <---You must edit this line!
String str_password = "xxxxxxx";           //  <---You must edit this line!
IPAddress ip(10, 0, 0, 105);                  // Device IP Address       //  <---You must edit this line!
IPAddress gateway(10, 0, 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(75, 75, 75, 75);          // DNS server              //  <---You must edit this line!
const unsigned int serverPort = 8090;         // port to run the http server on
IPAddress hubIp(10, 0, 0, 100);               // smartthings hub ip //  <---You must edit this line!
const unsigned int hubPort = 39500;           // smartthings hub port


//------------------------------------------------------------------------------------------
// the setup function runs once when you press reset or power the board
//------------------------------------------------------------------------------------------
void setup() 
{
//  Serial.begin(9600);

  pinMode(PIN_openSwitch, INPUT);               // initialize digital pins as an INPUT.
  pinMode(PIN_openLimitSwitch, INPUT);                // initialize digital pins as an INPUT.
  pinMode(PIN_closeSwitch, INPUT);              // initialize digital pins as an INPUT.
  pinMode(PIN_closeLimitSwitch, INPUT);               // initialize digital pins as an INPUT.
  pinMode(PIN_motorState, OUTPUT);              // initialize digital pins as an output.
  pinMode(PIN_motorDir, OUTPUT);                // initialize digital pins as an output.
  pinMode(PIN_remoteControl, OUTPUT);              // initialize digital pins as an output. 



  //******************************************************************************************
  //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.
  //******************************************************************************************
  
  
  //Interrupt Sensors 
    static st::IS_Contact             sensor1(F("contact1"), PIN_openLimitSwitch, HIGH, false, 0);
    static st::IS_Contact             sensor2(F("contact2"), PIN_closeLimitSwitch, HIGH, false, 0);
    static st::IS_Contact             sensor3(F("contact3"), PIN_openSwitch, HIGH, false, 0);
    static st::IS_Contact             sensor4(F("contact4"), PIN_closeSwitch, HIGH, false, 0); 
  
  //Executors
     static st::EX_Switch executor1(F("switch1"), PIN_remoteControl, LOW); 

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




}

// the loop function runs over and over again forever
void loop() {
  //*****************************************************************************
  //Execute the Everything run method which takes care of "Everything"
  //*****************************************************************************
  st::Everything::run();

  
//  Serial.print("\t\t"); Serial.print("Close Switch");
//  Serial.print("\t\t"); Serial.print("Open Switch");
//  Serial.print("\t\t"); Serial.print("On/Off");
//  Serial.print("\t\t"); Serial.print("Direction"); Serial.print("\n");

  
  if (digitalRead(PIN_closeLimitSwitch) == HIGH || digitalRead(PIN_openLimitSwitch)==HIGH)  // This test will need to be separated distributed to next two if statements 
    {
      digitalWrite(PIN_motorState, LOW);   // Set pin to 0V for motor power OFF.
    }

 else if (digitalRead(PIN_closeSwitch) == HIGH || digitalRead(PIN_remoteControl) == HIGH) 
    {
        digitalWrite(PIN_motorState, HIGH);   // Set pin to 1V for motor power on.
        digitalWrite(PIN_motorDir, LOW);      // Set pin to 0V for CLOSE Direction.
    }

  else if (digitalRead(PIN_openSwitch) == HIGH || digitalRead(PIN_remoteControl) == LOW) 
    {
        digitalWrite(PIN_motorState, HIGH);    // Set pin to 1V for motor power on.
        digitalWrite(PIN_motorDir, HIGH);      // Set pin to 1V for OPEN direction.
    }
  else
    {
        digitalWrite(PIN_motorState, LOW);   // Set pin to 0V for motor power OFF.
    }
  
//  Serial.print(i++);
//  Serial.print("\t\t\t"); Serial.print(digitalRead(PIN_closeSwitch));
//  Serial.print("\t\t\t"); Serial.print(digitalRead(PIN_openSwitch));
//  Serial.print("\t\t");   Serial.print(digitalRead(PIN_motorState));
//  Serial.print("\t\t\t"); Serial.println(digitalRead(PIN_motorDir)); Serial.print("\n");
   
//  delay(250);                       // wait for a 1/4 second
}

Dan,

I am not seeing anything that stands out as being problematic with your sketch. Glad to see that you’re not trying to write to the “PIN_remoteControl” directly within your loop() routine. Also, no blocking calls in your loop(). You are writing to the “PIN_motorState” and “PIN_motorDir” constantly, but that should not cause any issues that I can think of, especially since these pins are not being used by ST_Anything.

You could temporarily comment out everything in the loop() routine except the ‘st::Everything::run();’ statement. Then try operating the Switch device again from the ST Classic App on your phone. Since I assume that the 4 contact sensors and the switch device were automatically created on the ST side, that indicates that the communications from the ESP8266 to the ST Hub is functioning.

Note: If you manually deleted any Child Devices on the ST side, you will need to delete the Parent Device and manually add it again. Deleting child devices causes issues on the ST platform (it used to work, but then something changed.) Try starting with a new, clean Parent device to see if all 5 ST_Anything devices are created automatically. Then test your contact sensors to see if those states update nearly instantly in the ST Classic App. If that works, we know ESP8266 to ST Hub to ST Coud traffic is flowing as expected. Then try changing the state of the switch device via the ST Classic App. You should see the command on the Arduino Serial Monitor window instantly. If not, something is wrong. You could try a full power cycle on your ST hub (remove batteries if a v2 hub) to see if that helps at all.

Hope you figure it out!
Dan

Thanks very much for the sanity check Dan. I will try your suggestion to comment out all my custom lines to see how the sketch performs.

I’ll report back if I find anything interesting.
Dan

1 Like

One more thing… It appears that you removed the callback() function from the sketch. Was that intentional? I believe it is optional, but you might find it useful to execute code based on what is being sent from ST_Anything to SmartThings.

It was not intentional. I am not sure how the callback() function works or what it facilitates. I’ll look at a fresh version of the example I started with and add it back in.

Posted in other thread… ST_Anything slows NodeMCU response times

Reset of hub has corrected delay symptom, but not activation of switch from ST app. There is a slight, but reasonable lag in updating contact sensor state in the app, but no different than my other home contact sensors.

When I attempt to toggle within the ST classic app it just hangs at “turning on”

I did delete the parent & all child devices individually from the app and re-discovered, but that hasn’t helped.

Any more suggestions?

Thanks again,
Dan

When you attempt to change the swicth’s state via the ST Classic Mobile App, what do you see in the SmartThings Web IDE Live Logs? How about in the Arduino IDE Serial Monitor Window?

Sorry. I didn’t think to look at the SmartThing Web IDE logs. That immediately showed me the problem. I had fat fingered the IP address in the settings for the parent on the ST Classic Mobile App.

Corrected the IP address and everything is now working as expected. Thanks for all your help and patience with a newbie.

Problem resolutions

  1. Hard reset of the SmartThings hub resolved the delayed processing observed in the NodeMCU serial monitor.

  2. Review of SmartThings Web IDE Live Log revealed misconfiguration of parent device IP address in SmartThings Classic Mobile App preventing communication from app to NodeMCU.

1 Like

Excellent! Have fun with your project!