Alexa Variables on change not calling in Code

Hi All,

first time project here I have got a loop running which will switch on and off a pump via a transistor. So Far So Good.

Then I thought I would like to get my Alexa to switch it on or off rather than using a timer. I am using a nano 33 IOT

I configured a couple of variables pump and drink that I can switch on and off via Alexa.

BUT

The on change functions do not seem to get called when I change them. All I am trying to do at the mo is to write a message to the serial monitor when they change so I can see how it works but it is not triggering

Code is below

/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/3169c6fa-c0bb-4121-9869-2c0331cdf095

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  CloudSwitch drink;
  CloudSwitch pump;

  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 ASWITCH 4
bool PumpStatus = false;
bool DrinkStatus = false;
int i;
void setup() {

  //---set pin direction
  pinMode(ASWITCH,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  Serial.println(PumpStatus);
  digitalWrite(ASWITCH, PumpStatus);
  Serial.println("ASWITCH IS");
  Serial.println(PumpStatus);
  delay(10000);
  PumpStatus = HIGH;
  digitalWrite(ASWITCH, PumpStatus);
  Serial.println("ASWITCH IS");
  Serial.println(PumpStatus);
  delay(10000);
  PumpStatus = LOW;
  Serial.println("Drink is ");
  Serial.println(drink);
  Serial.println("EndLoop");
}
void onPumpChange() {
  // Do something
  Serial.println("Isla is the best!");
  Serial.println(pump);
  //PumpStatus = pump;
}
void onDrinkChange() {
  // Do something
  Serial.println(drink);
  //DrinkStatus = drink;
  Serial.println("Jim");
}

ANy Pointers would be apreciated

thanks

James

Taking one function as an example, what should cause onPumpChange() to be called ?

It should get called when I say Alexa switch pump on. And pump was off.

I added the variable as an Alexa switch and it added that function for me. I can see the pump switch from my Alexa app and switch it on and off, but it doesn't flow through to my code

I know nothing about Alexa or the library that you are using but where in the sketch does the code connect to your network to receive commands ?

There is an thingprperties.h file that is called upfront which has the connections. you'll see I got fed up with it deleted the old variables and created a new one called Water to see if that helped. It didnt :frowning:

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

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


const char THING_ID[] = "3169c6fa-c0bb-4121-9869-2c0331cdf095";

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 onWaterChange();

CloudSwitch water;

void initProperties(){

  ArduinoCloud.setThingId(THING_ID);
  ArduinoCloud.addProperty(water, READWRITE, 1 * SECONDS, onWaterChange);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

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