Arduino Cloud Variable not synchronising

I'm using an ESP32 Wrover Module with a LM35 temperature sensor, MAX30100 pulse sensor, and SIM800C module. I've created an Arduino IoT Cloud project and I'm having problems with my cloud variable not updating or syncing to the cloud. Moreover, in Sketch, the board is connected but in Setup, the associated device is offline.
Anyone has any ideas? Please help

#include <WiFi.h>
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#include <TinyGPS++.h>
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"

// WiFi Credentials
const char* SSID = "VGU_Student_Guest";
const char* PASSWORD = "";

// Arduino Cloud Credentials
const char* DEVICE_ID = "c533a5be-9e36-4894-ac41-4be08274744a";
const char* SECRET_KEY = "I!B?piCxCIsNlJ2XV2Rixy7bp";

// SIM800C Module (Connected via Hardware Serial)
HardwareSerial sim800(2);  // TX: 17, RX: 16

// GPS Module (NEO-6M)
TinyGPSPlus gps;
HardwareSerial gpsSerial(1);  // TX: 19, RX: 18

// MAX30100 (Heart Rate Sensor)
PulseOximeter pox;
#define REPORTING_PERIOD_MS 1000
uint32_t lastReportTime = 0;

// LM35 Temperature Sensor
#define LM35_PIN 34

// Cloud Variables
float heart_rate;
float length;  
String location;  // Store GPS as a string (latitude,longitude)
float temperature;
float velocity;
bool emergency_button = false;  // Emergency button on Arduino Cloud

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASSWORD);

// Callback for Emergency Button
void onEmergencyButtonChange() {
  if (emergency_button) {  
    Serial.println("🚨 Emergency Button Activated!");
    sendEmergencyMessage();
    emergency_button = false;  // Reset after sending
  }
}

// Callback for Heartbeat Detection
void onBeatDetected() {
  Serial.println("♥ Heartbeat Detected!");
}

void setup() {
  Serial.begin(115200);
  gpsSerial.begin(9600, SERIAL_8N1, 18, 19);
  sim800.begin(115200, SERIAL_8N1, 16, 17);  // SIM800C
  Wire.begin();

  // Initialize MAX30100
  if (!pox.begin()) {
    Serial.println("MAX30100 not found!");
  } else {
    pox.setOnBeatDetectedCallback(onBeatDetected);
  }

  // Connect to Arduino Cloud
  ArduinoCloud.begin(DEVICE_ID, SECRET_KEY);
}

void loop() {
  ArduinoCloud.update();  // Update Cloud Variables

  // Read LM35 Temperature Sensor
  int adcValue = analogRead(LM35_PIN);
  temperature = adcValue * (3.3 / 4095.0) * 100.0;
  Serial.printf("Temperature: %.2f°C\n", temperature);

  // Read Heart Rate from MAX30100
  pox.update();
  if (millis() - lastReportTime > REPORTING_PERIOD_MS) {
    heart_rate = pox.getHeartRate();
    Serial.printf("Heart Rate: %.1f bpm\n", heart_rate);

    // Check for high heart rate
    if (heart_rate > 100) {
      Serial.println("⚠️ WARNING: High Heart Rate! Please take a rest.");
      sendWarningMessage();
    }

    lastReportTime = millis();
  }

  // Read GPS Data
  while (gpsSerial.available()) {
    gps.encode(gpsSerial.read());
    if (gps.location.isUpdated()) {
      float lat = gps.location.lat();
      float lng = gps.location.lng();
      location = String(lat, 6) + "," + String(lng, 6);  // Store as String
      Serial.printf("GPS: %s\n", location.c_str());
    }
  }

  // Simulate Distance and Velocity (Modify as needed)
  length += 0.5;
  velocity = length / (float)millis() * 1000;  // Fix division issue

  delay(2000);
}

// Function to Send Emergency SMS
void sendEmergencyMessage() {
  String message = "🚨 EMERGENCY ALERT! \n";
  message += "📍 Location: " + location + "\n";
  message += "💓 Heart Rate: " + String(heart_rate) + " bpm\n";
  message += "🌡 Temperature: " + String(temperature) + "°C\n";
  
  Serial.println("Sending SMS...");
  Serial.println(message);

  sim800.println("AT+CMGF=1");  // Set SMS mode to text
  delay(1000);
  sim800.println("AT+CMGS=\"+84943364938\"");  // Replace with emergency contact
  delay(1000);
  sim800.println(message);
  delay(100);
  sim800.write(26);  // Send SMS (CTRL+Z)
  delay(5000);
  
  Serial.println("🚀 Emergency SMS Sent!");
}

// Function to Send Warning for High Heart Rate
void sendWarningMessage() {
  Serial.println("⚠️ High Heart Rate Detected! Sending Warning SMS...");

  sim800.println("AT+CMGF=1");
  delay(1000);
  sim800.println("AT+CMGS=\"+943364938\"");  // Replace with user's phone number
  delay(1000);
  sim800.println("⚠️ ALERT: High Heart Rate Detected! Please take a rest.");
  delay(100);
  sim800.write(26);
  delay(5000);
  
  Serial.println("⚠️ Warning SMS Sent!");
}

/*
  Since HeartRate is READ_WRITE variable, onHeartRateChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onHeartRateChange()  {
  // Add your code here to act upon HeartRate change
}
/*
  Since Temperature is READ_WRITE variable, onTemperatureChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTemperatureChange()  {
  // Add your code here to act upon Temperature change
}
/*
  Since Length is READ_WRITE variable, onLengthChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLengthChange()  {
  // Add your code here to act upon Length change
}
/*
  Since Location is READ_WRITE variable, onLocationChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLocationChange()  {
  // Add your code here to act upon Location change
}



Hello @tracminhluan,

I see that your device has never connected.
Do you see some logs in the "Serial Monitor" that show why the connection to the Cloud fails?

Thanks,

Stefano

1 Like