Editor: how to use the functions at the bottom of the sketch

Hi,
I have a problem.I must admit that I have not yet figured out how to have what I wrote in the function "void onTemperatureChange()" executed when I change the value read by the BME680 sensor.
How to use the functions that I get automatically in the Editor.
I see the temperature change regularly in the monitor, but I don't see the Led light up nor do I see "Ok!" in the Monitor.
The sketch is this:

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/8606d840-99da-4715-86e7-e7c19f7e6876 

  Arduino IoT Cloud Variables description

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

  int altezza;
  int aria;
  int pressione;
  int temperatura;
  int umidita;

  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"
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"

//#define BME_SCK 13  // per la configurazione SPI
//#define BME_MISO 12 // per la configurazione SPI
//#define BME_MOSI 11 // per la configurazione SPI
//#define BME_CS 10 // per la configurazione SPI

#define SEALEVELòPRESSURE_HPA (1013,25)
#define Led 13

Adafruit_BME680 bme;  // per il I2C

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); 
  
  while(!Serial);
  Serial.println(F("BME680 TEST Code"));
  if(!bme.begin()){
    Serial.println("not found the BME680 sensor");
    while(1);
  }
  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_8X);
  
  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  pinMode(Led, OUTPUT);
}

void loop() {
  ArduinoCloud.update();
  if(!bme.performReading()){
    Serial.println("Failed to perform reading :(");
    return;
  }
  temperatura = bme.temperature;
  Serial.print("Temperatura = ");
  Serial.print(temperatura);
  Serial.println("°C");
}

/*
  Since Temperatura is READ_WRITE variable, onTemperaturaChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTemperaturaChange()  {
  // Add your code here to act upon Temperatura change
   digitalWrite(Led, HIGH);
   Serial.println("OK!");
}

/*
  Since Pressione is READ_WRITE variable, onPressioneChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPressioneChange()  {
  // Add your code here to act upon Pressione change
}

/*
  Since Umidita is READ_WRITE variable, onUmiditaChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onUmiditaChange()  {
  // Add your code here to act upon Umidita change
}

/*
  Since Aria is READ_WRITE variable, onAriaChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onAriaChange()  {
  // Add your code here to act upon Aria change
}

/*
  Since Altezza is READ_WRITE variable, onAltezzaChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onAltezzaChange()  {
  // Add your code here to act upon Altezza change
}

EzioGi

temperatura = bme.temperature; // original sketch
if (oldTemperatura != temperatura) { // test if temperature has changed
  oldTemperatura = temperatura; // store the new temperature as "old temperature"
  onTemperaturaChange(); // call your function
}

It is my understanding that onTemperaturaChange() is a callback function that was called automatically when the value of the associated variable changed

If so, then there needs to be confirmation that the variable is actually being changed. This could be done by printing its value periodically and/or by deliberately changing its value within the sketch

Hi @eziogi

Although you are free to call it just like any other function, as demonstrated by @xfpd, that is not how these functions are intended to be used.

As @UKHeliBob explained, onTemperatureChange is a "callback" function that is automatically called by the Arduino Cloud IoT framework of the sketch whenever the value of the temperatureChange Arduino Cloud Variable is changed through the Arduino Cloud service (e.g., via a widget on the Arduino Cloud dashboard).

I think the best way to get an understanding of how this works is by creating a minimal
Arduino Cloud Thing. A great way to get a simple Arduino Cloud Thing up and running very quickly with a minimum of difficulty is to use the "Cloud Blink" template:

https://app.arduino.cc/templates/cloud-blink

Or if you are using an ESP32 board, the "Cloud Blink ESP32" template:

https://app.arduino.cc/templates/cloud-blink-esp32

(If the above links don't work, make sure you are signed in to your arduino.cc account)

This will create a dashboard that allows you to control the LED on the Arduino board. You can study the code of the Thing sketch that was generated by the "Cloud Blink" template to understand how it works.

You'll notice that the Arduino Cloud dashboard contains a switch widget. If you use your mouse to change the setting of the switch in the dashboard, you change the value of the led variable. Changing that variable via Arduino Cloud causes the onLedChange function in the Thing sketch to be called. Since that function in the template contains a Serial.println call, you can see the effect of it being called in Serial Monitor.

Thank you for your advice.
If I use this sketch, the temperature update on the Dashboard of the cell phone works, and I also see the data in the Monitor.

#include "arduino_secrets.h"
/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/8606d840-99da-4715-86e7-e7c19f7e6876 

  Arduino IoT Cloud Variables description

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

  int altezza;
  int aria;
  int pressione;
  int temperatura;
  int umidita;

  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"
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"

//#define BME_SCK 13  // per la configurazione SPI
//#define BME_MISO 12 // per la configurazione SPI
//#define BME_MOSI 11 // per la configurazione SPI
//#define BME_CS 10 // per la configurazione SPI

#define SEALEVELòPRESSURE_HPA (1013,25)

Adafruit_BME680 bme;  // per il I2C

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); 
  
  while(!Serial);
  Serial.println(F("BME680 TEST Code"));
  if(!bme.begin()){
    Serial.println("not found the BME680 sensor");
    while(1);
  }
  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_8X);
  
  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  
}

void loop() {
  ArduinoCloud.update();
  if(!bme.performReading()){
    Serial.println("Failed to perform reading :(");
    return;
  }
  temperatura = bme.temperature;
  Serial.print("Temperatura = ");
  Serial.print(temperatura);
  Serial.println("°C");
}

/*
  Since Temperatura is READ_WRITE variable, onTemperaturaChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTemperaturaChange()  {
  temperatura = bme.temperature;
  Serial.print("Temperatura = ");
  Serial.print(temperatura);
  Serial.println("°C");
}

/*
  Since Pressione is READ_WRITE variable, onPressioneChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPressioneChange()  {
  // Add your code here to act upon Pressione change
}

/*
  Since Umidita is READ_WRITE variable, onUmiditaChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onUmiditaChange()  {
  // Add your code here to act upon Umidita change
}

/*
  Since Aria is READ_WRITE variable, onAriaChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onAriaChange()  {
  // Add your code here to act upon Aria change
}

/*
  Since Altezza is READ_WRITE variable, onAltezzaChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onAltezzaChange()  {
  // Add your code here to act upon Altezza change
}

If I use this sketch instead, it doesn't work for me anymore, I don't see either the update on the dashboard or in the Monitor.:

#include "arduino_secrets.h"
/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/8606d840-99da-4715-86e7-e7c19f7e6876 

  Arduino IoT Cloud Variables description

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

  int altezza;
  int aria;
  int pressione;
  int temperatura;
  int umidita;

  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"
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"

//#define BME_SCK 13  // per la configurazione SPI
//#define BME_MISO 12 // per la configurazione SPI
//#define BME_MOSI 11 // per la configurazione SPI
//#define BME_CS 10 // per la configurazione SPI

#define SEALEVELòPRESSURE_HPA (1013,25)

Adafruit_BME680 bme;  // per il I2C

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); 
  
  while(!Serial);
  Serial.println(F("BME680 TEST Code"));
  if(!bme.begin()){
    Serial.println("not found the BME680 sensor");
    while(1);
  }
  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_8X);
  
  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  
}

void loop() {
  ArduinoCloud.update();
  if(!bme.performReading()){
    Serial.println("Failed to perform reading :(");
    return;
  }
  
  //temperatura = bme.temperature;
  //Serial.print("Temperatura = ");
  //Serial.print(temperatura);
  //Serial.println("°C");
}

/*
  Since Temperatura is READ_WRITE variable, onTemperaturaChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTemperaturaChange()  {
  temperatura = bme.temperature;
  Serial.print("Temperatura = ");
  Serial.print(temperatura);
  Serial.println("°C");
}

/*
  Since Pressione is READ_WRITE variable, onPressioneChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPressioneChange()  {
  // Add your code here to act upon Pressione change
}

/*
  Since Umidita is READ_WRITE variable, onUmiditaChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onUmiditaChange()  {
  // Add your code here to act upon Umidita change
}

/*
  Since Aria is READ_WRITE variable, onAriaChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onAriaChange()  {
  // Add your code here to act upon Aria change
}

/*
  Since Altezza is READ_WRITE variable, onAltezzaChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onAltezzaChange()  {
  // Add your code here to act upon Altezza change
}

I figured out how the Led Blink sketch works, it is very simple however I don't know how to implement it in my sketch.

EzioGi

If you want your sketch to change the value of the temperatura Cloud Variable, you must do that in the loop function.

Right now, you only ever change the value of the Variable in the onTemperaturaChange callback function, and that function will only ever be executed if the value of the Variable is changed from the dashboard.

Did you change the value of the temperatura Variable from the dashboard? Since your sketch only prints to Serial from the onTemperaturaChange callback function, that is the only thing that will produce output in Serial Monitor.

Thank you for your advice.
I may have figured out how it works.

EzioGi

You are welcome. I'm glad if I was able to be of assistance.

Regards,
Per

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