Unintended Arduino/Alexa variable state change upon new connection to Arduino IoT Cloud

Hello community!

I have this Arduino Nano 33 IoT project that basically makes Alexa able to activate/deactive my home alarm by sending radio signals (Nano 33 IoT wired to an alarm remote).

Unfortunately, when a new connection is established between The Nano 33 IoT and Arduino IoT Cloud (eg: the Thing is restarted , or the router is restared, or - for any reason - the wifi connection is restored) a new "IoT Cloud ID" is issued and all my CloudSwitch variables state changes to FALSE, like if some sort of initialization is taking place.

Let me detail this a little bit more in detail: In the first 30 seconds after a new connection is established between my Thing and Arduino IoT Cloud, all the 3 CloudSwitch variables are set (from Arduino IoT Cloud :thinking: :thinking:) to "false", this unforseen change of state in the variable translates in my "PINdisable" pin state output going to HIGH, wich corresponds to pushing the remote alarm_disable button... too bad! :money_with_wings: :money_with_wings: :money_with_wings:

My remote has got 4 standard alarm buttons, but I figured out that my Alexa project only needs 3 variables (and 4 pin outputs) to control the full setup:

  1. TOTAL ARM

  2. PARTIAL ARM

  3. SOS ARM

Then, to DISARM the alarm I chose not to have a specific variable which would cause the Alexa activation command to be "Alexa, turn on the DISARM alarm". Instead I can simply say "Alexa, turn off the TOTAL/PARTIAL/SOS alarm" wich makes more sense from a logical perspective. Probably reading my code will explain better what I'm trying to achieve:

Alexa_Alarm.ino

/*
  Project: Alexa_Alarm
  Author: txomin
  Date: 14/07/2022
  Revision: 0.1
  License: Public Domain
*/

#define DEBUG 1
#include "thingProperties.h"

const int PINalarm = 2;
const int PINpartial = 4;
const int PINpanic = 6;
const int PINdisable = 8;
int whois = 0;

void setup() {
#ifdef DEBUG
  Serial.begin(9600);  //Initialize serial and wait for port to open
  delay(1500);
#endif

  initProperties();   // Defined in thingProperties.h

  ArduinoCloud.begin(ArduinoIoTPreferredConnection); //Connect to Arduino IoT Cloud

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

  pinMode(PINalarm, OUTPUT);
  pinMode(PINpartial, OUTPUT);
  pinMode(PINpanic, OUTPUT);
  pinMode(PINdisable, OUTPUT);
}

void loop() {
  ArduinoCloud.update();
}

void onAlarmChange()  {
  switch (alarm) {
    case true:
      #ifdef DEBUG
        Serial.println("Sending command: ALARM");
      #endif
      digitalWrite(PINalarm, HIGH); //Virtually pushing TOTAL ARMING button on the remote
      delay(1000);
      digitalWrite(PINalarm, LOW);
      delay(1000);
      alarm = false; //Not really needed, just for aesthetics inside the Alexa app
      break;
    case false:
      whois = 1;
      DisableAlarm();
      break;
  }
}

void onPartialChange()  {
  switch (partial) {
    case true:
      #ifdef DEBUG
        Serial.println("Sending command: PARTIAL");
      #endif
      digitalWrite(PINpartial, HIGH); //Virtually pushing PARTIAL ARMING button on the remote
      delay(1000);
      digitalWrite(PINpartial, LOW);
      delay(1000);
      partial = false; //Not really needed, just for aesthetics inside the Alexa app
      break;
    case false:
      whois = 2;
      DisableAlarm();
      break;
  }
}

void onPanicChange()  {
  switch (panic) {
    case true:
      #ifdef DEBUG
        Serial.println("Sending command: PANIC");
      #endif
      digitalWrite(PINpanic, HIGH); //Virtually pushing SOS button on the remote
      delay(1000);
      digitalWrite(PINpanic, LOW);
      delay(1000);
      panic = false; //Not really needed, just for aesthetics inside the Alexa app
      break;
    case false:
      whois = 3;
      DisableAlarm();
      break;
  }
}

void DisableAlarm()  {
  #ifdef DEBUG
    Serial.print("Sending command: DISABLE ");
    Serial.println(whois);
  #endif
  digitalWrite(PINdisable, HIGH); //Virtually pushing DISARM button on the remote
  delay(1000);
  digitalWrite(PINdisable, LOW);
  delay(1000);
}

And for those that are wondering, this is my ThingProperties.h

// Code generated by Arduino IoT Cloud, DO NOT EDIT.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char SSID[]     = SECRET_SSID;    // Network SSID (name)
const char PASS[]     = SECRET_PASS;    // Network password (use for WPA, or use as key for WEP)

void onAlarmChange();
void onPanicChange();
void onPartialChange();

CloudSwitch alarm;
CloudSwitch panic;
CloudSwitch partial;

void initProperties(){

  ArduinoCloud.addProperty(alarm, READWRITE, ON_CHANGE, onAlarmChange);
  ArduinoCloud.addProperty(panic, READWRITE, ON_CHANGE, onPanicChange);
  ArduinoCloud.addProperty(partial, READWRITE, ON_CHANGE, onPartialChange);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

Just to have an idea, this is how it looks right now (note: while I solve this issue, instead of a remote I have 4 leds):

Right now, the only work around I can think of is to check ArduinoCloud.update() output and if the connection state goes from disconnected to connected, then I can us Millis() to disregard for 30 seconds any variables state change going to "false" (so my "white" PINdisable pin doesn't go HIGH and my alarm doesn't turn off).

Anybody here who run into the same issue?
Cheers.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.