I have hardware I'm using on some remote controller units for my roller shutters. This hardware emulates a hardware button press via bridging circuit (similar to the hardware pad). I'm using the IoT Cloud functionality so I can integrate it into smart assistent & automation. This code works as expected. But when the unit it turned on or, it seems, the communication to the cloud service or watchdog resets the unit, the unit triggers the blinds to close. Looking at the functions of IoT Cloud it appears to trigger all of the defined triggers as part of the start up/end function. I've attempted to put in a timer, and this appears to work for the initial start up. But I don't think timer reset on cloud issues or watchdog resets?
What I'm after is how do I ensure that the Up or Down onChange events can only be triggered by an actual command process sent via the IoT App or intrgrated assistant (Alexa)? Or how do I stop the initialization or soft reset from triggering the events (which appear to be UP, STop & Down in that order, if I could change it to STOP being last might help)
I'm using a D1 Mini ESP8266 device, plus a custom PCB for connection to the shutter controller. Trnasistors are used for the 'switches', to bridge the buttons on the shutter controller.
/*
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
CloudSwitch k_down;
CloudSwitch k_stop;
CloudSwitch k_up;
bool led_state;
bool cloud_check;
bool up_initialize;
bool stop_initialize;
bool down_initialize;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
// Define OUTPUT pins here
#define RS_UP 5
#define RS_STOP 4
#define RS_DOWN 0
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Station mode
WiFi.mode(WIFI_STA);
// Define OUTPUT pins and set to LOW
up_initialize = true;
stop_initialize = true;
down_initialize = true;
pinMode(RS_UP, OUTPUT);
pinMode(RS_DOWN, OUTPUT);
pinMode(RS_STOP, OUTPUT);
pinMode(BUILTIN_LED, OUTPUT); //For LED flash on activity
digitalWrite(RS_UP, LOW);
digitalWrite(RS_DOWN, LOW);
digitalWrite(RS_STOP, LOW);
digitalWrite(BUILTIN_LED, led_state); //set ON/OFF state via dashboard
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
if ((ArduinoCloud.connected() == 0) && cloud_check==true) {
digitalWrite(BUILTIN_LED, !digitalRead(LED_BUILTIN));
delay(500);
}
if (millis() < 30000) {
digitalWrite(RS_STOP, HIGH);
led_state = !led_state;
digitalWrite(BUILTIN_LED, led_state);
delay(500);
digitalWrite(RS_STOP, LOW);
led_state = !led_state;
digitalWrite(BUILTIN_LED, led_state);
}
}
void onKUpChange() {
if ((ArduinoCloud.connected() == 1) && up_initialize == true) {
up_initialize = false;
}
else if ((ArduinoCloud.connected() == 1) && k_up == true && up_initialize == false && millis() > 40000) {
digitalWrite(RS_UP, HIGH);
led_state = !led_state;
digitalWrite(BUILTIN_LED, led_state);
delay(500);
digitalWrite(RS_UP, LOW);
led_state = !led_state;
digitalWrite(BUILTIN_LED, led_state);
k_up=false; //reset defined variable for UP
}
else { Serial.print("UP do nothing"); }
}
void onKDownChange() {
if ((ArduinoCloud.connected() == 1) && down_initialize == true) {
down_initialize = false;
}
else if ((ArduinoCloud.connected() == 1) && k_down == true && down_initialize == false && millis() > 40000) {
digitalWrite(RS_DOWN, HIGH);
led_state = !led_state;
digitalWrite(BUILTIN_LED, led_state);
delay(500);
digitalWrite(RS_DOWN, LOW);
led_state = !led_state;
digitalWrite(BUILTIN_LED, led_state);
k_down=false; //reset defined variable for DOWN
}
else {
digitalWrite(RS_STOP, HIGH);
led_state = !led_state;
digitalWrite(BUILTIN_LED, led_state);
delay(500);
digitalWrite(RS_STOP, LOW);
led_state = !led_state;
digitalWrite(BUILTIN_LED, led_state);
Serial.print("DOWN do nothing");
}
}
void onKStopChange() {
if ((ArduinoCloud.connected() == 1) && stop_initialize == true) {
stop_initialize = false;
}
else if ((ArduinoCloud.connected() == 1) && k_stop == true && stop_initialize == false && millis() > 40000) {
digitalWrite(RS_STOP, HIGH);
led_state = !led_state;
digitalWrite(BUILTIN_LED, led_state);
delay(500);
digitalWrite(RS_STOP, LOW);
led_state = !led_state;
digitalWrite(BUILTIN_LED, led_state);
k_stop=false; //reset defined variable for STOP
}
else { Serial.print("STOP do nothing"); }
}
void onLedStateChange() {
digitalWrite(BUILTIN_LED, led_state);
}
void onCloudCheckChange() {
digitalWrite(BUILTIN_LED, !digitalRead(LED_BUILTIN));
delay(500);
digitalWrite(BUILTIN_LED, !digitalRead(LED_BUILTIN));
delay(500);
}
void onUpInitializeChange() {
// Do something
}
void onStopInitializeChange() {
// Do something
}
void onDownInitializeChange() {
// Do something
}