IoT Code for Temperature Logger

I have code for a temperature logger that I've created using the Arduino IDE software that works well. I now want to add this code to the IoT arduino cloud so that the Ardunio R4 Wifi sends the temperature data to the cloud. The IoT cloud has created me the start of a sketch that includes the basic code I need to float the temperature data I'm trying to collect but when I try and merge it with my existing code I encounter problems. Can anyone fix this for me?

my existing code that works



#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include <SD.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
float tempFahrenheit; // temperature in Fahrenheit

void setup()
{
  Serial.begin(9600); // initialize serial
  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");

  delay(1000); // read temperature once every 15 minutes which is 900000 ms
}

IoT generated sketch

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/fd1441dc-f3f9-4a6d-8c75-a3277dceaedc 

  Arduino IoT Cloud Variables description

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

  float Concrete_Temperature;

  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() {
  // 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 
  
  
}

Hi @oliversunderland.

Please add a forum reply here that provides a detailed explanation of what you mean by "problems", including:

  • What did you do?
  • What were the results you expected from doing that thing?
  • What were the results you observed that did not match your expectations?

Make sure to include the full and exact text of any error or warning messages you might have encountered.

Hello,

I've used ChatGPT to fix my code and make it work with the Arduino IoT cloud. I can see that the Arduino R4 board is sending data over the wifi and is updating the cloud every 5 seconds as expected, but the floating point variable isn't updating. The last value that is always being returned is '0'. I know that this must be an issue with software/code as opposed to hardware as when I open the serial manager I can see the temperature values are reading correctly and also my LCD screen is outputting an accurate temperature.

Below is my code. How do I fix the issue of the last updated value not returning the correct floating point value? Do Arduino Team have a customer service helpline or chat function? I think this might be an easy fix for someone who's more experienced than me.

#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
  tempSensor.begin();    // initialize the sensor

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

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

void loop() {
  tempSensor.requestTemperatures();             // send the command to get temperatures
  float 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(1000); // 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
}

type or paste code here

type or paste code here

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