ESP32 showing Offline even after all the code is uploaded

Hello I am working on a project using ESP32 Dev Module I have the following code
but it is still showing Offline, so far I have tried changing Internet Sources and the code a few times but to no avail

#include <WiFi.h>
#include <HTTPClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

#define WIFI_SSID "xxx"
#define WIFI_PASSWORD "xxx"
#define FIREBASE_HOST "https://xxx-default-rtdb.firebaseio.com/"
#define FIREBASE_API_KEY "xxx"

// Time client setup
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800, 60000); // IST = UTC + 5.5 hrs = 19800s

void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi!");

  timeClient.begin();
  timeClient.update();

  randomSeed(analogRead(0));
}

String getReadableTime() {
  timeClient.update();
  time_t rawTime = timeClient.getEpochTime();

  struct tm *timeinfo = localtime(&rawTime);
  char buffer[25];
  sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
          timeinfo->tm_year + 1900,
          timeinfo->tm_mon + 1,
          timeinfo->tm_mday,
          timeinfo->tm_hour,
          timeinfo->tm_min,
          timeinfo->tm_sec);
  return String(buffer);
}

void sendDataToFirebase() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;

    String path = FIREBASE_HOST;
    path += "/helmet.json?auth=" + String(FIREBASE_API_KEY);

    float speed = random(10, 120);
    float latitude = 17.385044 + (random(-1000, 1000) / 100000.0);
    float longitude = 78.486671 + (random(-1000, 1000) / 100000.0);
    bool collisionStatus = random(0, 2);
    String timestamp = getReadableTime();
    
    // Added new variables
    float temperature = random(20, 40) + (random(0, 100) / 100.0); // Random temperature between 20-40°C
    bool crashDetected = random(0, 10) == 0; // 10% chance of crash detection
    bool helmetWorn = random(0, 10) <= 8;    // 80% chance helmet is worn

    String json = "{";
    json += "\"speed\":" + String(speed, 2) + ",";
    json += "\"latitude\":" + String(latitude, 5) + ",";
    json += "\"longitude\":" + String(longitude, 5) + ",";
    json += "\"collisionStatus\":" + String(collisionStatus ? "true" : "false") + ",";
    json += "\"temperature\":" + String(temperature, 2) + ",";
    json += "\"crashDetected\":" + String(crashDetected ? "true" : "false") + ",";
    json += "\"helmetWorn\":" + String(helmetWorn ? "true" : "false") + ",";
    json += "\"timestamp\":\"" + timestamp + "\"";
    json += "}";

    http.begin(path);
    http.addHeader("Content-Type", "application/json");

    int httpResponseCode = http.POST(json);

    if (httpResponseCode > 0) {
      String response = http.getString();
      Serial.println("Data sent: " + response);
    } else {
      Serial.println("HTTP error: " + String(httpResponseCode));
    }

    http.end();
  } else {
    Serial.println("WiFi disconnected");
  }
}

void loop() {
  sendDataToFirebase();
  delay(5000); // every 5 seconds
} 

Why not actually print the return status so you have a clue?

Need to see the error log.

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