IoT Cloud On Change functionality

When I create an IoT Cloud sketch, it adds a onChange function for each variable (such as onTemperatureChange).

I have added a Serial.printing to them but the don’t seem to be triggered.

Is there documentation on these? How can they be used?

i doubt anyone will know which sketch you may have downloaded or written yourself nor any libraries it may be using without more information.

Fair enough.
Here is the code:

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/42e09ae2-5d37-46e7-a9ad-e51ddf875472 

  Arduino IoT Cloud Variables description

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

  CloudPercentage moisture;
  CloudElectricPotention battery;
  float temperature;
  CloudIlluminance illumination;

  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"

void setup() {
  // set up analog pins
  pinMode(A0, INPUT); //temperature
  pinMode(A1, INPUT); //illumination
  pinMode(A2, INPUT); //moisture

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

  // 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();
  // Your code here 
  
  
  float temperatureRaw = analogRead(A0);
  float temperaturemV = 3300.0 * (temperatureRaw / 1023.0);
  temperature=temperatureRaw;
  
  Serial.print("Temperature C = ");
  Serial.println(temperature);
  
  float illuminationRaw = analogRead(A1);
  illumination = (illuminationRaw/1023.0)*100.0; //measure in %. Bright resistance @ 10 lux = 50-100 kOhm; 0 lux=5 MOhm
  
  Serial.print("Illumination % = ");
  Serial.println(illumination);
  
  float moistureRaw = analogRead(A2);
  moisture = (moistureRaw/1023.0)*100.0;
  
  Serial.print("Moisture = ");
  Serial.println(moisture);
  
  Serial.println("--");
  int batteryValue = analogRead(ADC_BATTERY);
  float voltage = batteryValue * (4.3 / 1023.0);
  // print out the value you read:
  //Serial.print(battery);
  //Serial.println("V");

  delay(1000);
}

void onMoistureChange() {
  // Do something
  Serial.println("Moisture Change");
}

void onTemperatureChange() {
  // Do something
  Serial.println("Temperature Change");
}

[b]void onIlluminationChange()[/b] {
  // Do something
  Serial.println("Illumination Change");
}

The "on...Change" routines were added by the IoT Cloud IDE.

I figured it out. When a variable is Read/Write, it triggers the routine if a new value is being written to the Thing.

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