Arduino IoT Cloud Variable Not Synchronising

I'm using an Arduino R4 Wi-Fi with a DS18B20 temperature sensor. I've created an Arduino IoT Cloud project and I'm having problems with my cloud variable not updating or syncing to the cloud. I know that the sensor is working and is reading the correct temperature as I also have an I2C LCD screen on my project and the temperature readout is accurate. I can also see that the IoT cloud is receiving data from my device 'live', just that the floating point number is also showing as zero and not updating.

Has anyone any ideas to solve this?

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include "thingProperties.h" // Include Arduino IoT Cloud Thing properties
#include <Arduino_ConnectionHandler.h>

const int SENSOR_PIN = 13; // Arduino pin connected to DS18B20 sensor's DQ pin

OneWire oneWire(SENSOR_PIN);         // setup a oneWire instance
DallasTemperature tempSensor(&oneWire); // pass oneWire to DallasTemperature library
LiquidCrystal_I2C lcd(0x3F, 16, 2);  // I2C address 0x3F (from DIYables LCD), 16 column and 2 rows

float tempCelsius;    // temperature in Celsius (floating-point)

void setup() {
  Serial.begin(9600); // initialize serial
  delay(1500);
  
   // Connect to WiFi
  Serial.println("Attempting to connect to WiFi...");
  while (!WiFi.begin(SECRET_SSID, SECRET_OPTIONAL_PASS)) {
    Serial.println("Failed to connect to WiFi. Retrying...");
    delay(1000);
  }

  // Wait for WiFi connection to be established
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi!");
  
  // Initialize 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();

  // Defined in thingProperties.h
  initProperties();
  
  tempSensor.begin();    // initialize the sensor

  lcd.init();         // initialize the lcd
  lcd.backlight();    // open the backlight 
}

void loop() {
  tempSensor.requestTemperatures();             // send the command to get temperatures
  tempCelsius = tempSensor.getTempCByIndex(0);  // read temperature in Celsius

  Serial.print(tempCelsius);    // print the temperature in Celsius
  Serial.println("°C");

  lcd.clear();
  lcd.setCursor(0, 0);       // start to print at the first row
  lcd.print("CONCRETE TEMP:");  // print concrete temp wording
  lcd.setCursor(0, 1);       // start to print at the second row
  lcd.print(tempCelsius);    // print the temperature in Celsius
  lcd.print((char)223);      // print ° character
  lcd.print("C");

  ArduinoCloud.update(); // Update Arduino IoT Cloud
  delay(10000); // read temperature once every 15 minutes which is 900000 ms
}

// Function called when Concrete_Temperature property changes
void on_Concrete_Temperature_change() {
  // You can add custom behavior here if needed
}



Hi @oliversunderland. I can see that your Cloud Variable is named tempCelcius (with a C):

But then the variable you are working with in the Thing sketch code is named tempCelsius (with an S):

So you can fix this problem by changing the name of the Cloud Variable to tempCelsius and then uploading the sketch to your board again.

Thanks for that. I'm now getting this error message

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include "thingProperties.h" // Include Arduino IoT Cloud Thing properties
#include <Arduino_ConnectionHandler.h>

const int SENSOR_PIN = 13; // Arduino pin connected to DS18B20 sensor's DQ pin

OneWire oneWire(SENSOR_PIN);         // setup a oneWire instance
DallasTemperature tempSensor(&oneWire); // pass oneWire to DallasTemperature library
LiquidCrystal_I2C lcd(0x3F, 16, 2);  // I2C address 0x3F (from DIYables LCD), 16 column and 2 rows

float tempCelsius;    // temperature in Celsius (floating-point)

void setup() {
  Serial.begin(9600); // initialize serial
  delay(1500);
  
   // Connect to WiFi
  Serial.println("Attempting to connect to WiFi...");
  while (!WiFi.begin(SECRET_SSID, SECRET_OPTIONAL_PASS)) {
    Serial.println("Failed to connect to WiFi. Retrying...");
    delay(1000);
  }

  // Wait for WiFi connection to be established
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi!");
  
  // Initialize 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();

  // Defined in thingProperties.h
  initProperties();
  
  tempSensor.begin();    // initialize the sensor

  lcd.init();         // initialize the lcd
  lcd.backlight();    // open the backlight 
}

void loop() {
  tempSensor.requestTemperatures();             // send the command to get temperatures
  tempCelsius = tempSensor.getTempCByIndex(0);  // read temperature in Celsius

  Serial.print(tempCelsius);    // print the temperature in Celsius
  Serial.println("°C");

  lcd.clear();
  lcd.setCursor(0, 0);       // start to print at the first row
  lcd.print("CONCRETE TEMP:");  // print concrete temp wording
  lcd.setCursor(0, 1);       // start to print at the second row
  lcd.print(tempCelsius);    // print the temperature in Celsius
  lcd.print((char)223);      // print ° character
  lcd.print("C");

  ArduinoCloud.update(); // Update Arduino IoT Cloud
  delay(10000); // read temperature once every 15 minutes which is 900000 ms
}

// Function called when Concrete_Temperature property changes
void on_Concrete_Temperature_change() {
  // You can add custom behavior here if needed
}

Select the "IoT_Concrete_Temperature" tab and delete this line of code:

The Arduino Cloud Thing sketch generation system automatically adds a declaration of the tempCelsius variable to the thingProperties.h file of the sketch so you don't need one in the IoT_Concrete_Temperature.ino file.

Thank you! I've managed to get some data into my IoT dashboard for the first time! I'm very pleased :slight_smile:

Quick question, are you familiar with issues with devices dropping offline for a bit and then coming back online? Any recommendations here? As you can see from the images below there was an outage for around 10 minutes.

You are welcome. I'm glad it is working now.

My area of knowledge is limited to the user interface of Arduino Cloud and sketch code. I don't have any involvement in or knowledge of the Arduino Cloud server infrastructure or even much about network communication in general.

I recommend leaving the UNO R4 WiFi board connected to your computer with a USB cable and running a serial terminal program. The Thing sketch will print some logs about the status of connectivity to the Wi-Fi access point as well as to the Arduino Cloud servers.

The logging behavior is controlled by this line of your Thing sketch:

The default level 2 is reasonable to get started on the investigation but you can increase the level in order to get more detailed information about the problem.

The obvious serial terminal to use is the Serial Monitor feature of Arduino Cloud. However, it is not really suitable for logging data over the course of days, as might be necessary if the outages don't occur frequently. In this case it would be best to use a standalone terminal. I can recommend the free open source PuTTY terminal:

https://www.putty.org/

You can configure PuTTY to write the logs to a file, which could make it easier to review them later.

I prefer PuTTY due to the fact that it is cross-platform (meaning I can use the same tool regardless of whether the computer is running Linux, macOS, or Windows), but if for some reason it isn't to your liking then there are definitely some other excellent free serial terminal programs available for whichever operating system you use.

Hi @oliversunderland , to try to fix your issues with network timeouts, you should consider refactoring the code to remove this delay:

as suggested here:
https://docs.arduino.cc/arduino-cloud/cloud-interface/variables/#cloud-updates

This is because delay is (expectingly) blocking the loop(), and the result is that after a few seconds of inactivity the Cloud considers that board as disconnected.

You should use a different approach using millis() instead of delay()

Basically something like this (warning: not tested):

void loop() {
  ArduinoCloud.update();
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= 10000) {
    previousMillis = currentMillis;
    tempSensor.requestTemperatures();
    tempCelsius = tempSensor.getTempCByIndex(0);
    [...]
}

Let me know if this helps with your connection issues.

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