#include <WiFi.h>
#include <HTTPClient.h>
#include <time.h>
// ====== WiFi Credentials for Wokwi ======
const char* ssid = "Wokwi-GUEST";
const char* password = ""; // no password
// ====== Backend URL (Render) ======
const char* serverUrl = "https://footzup.onrender.com/api/data";
// ====== NTP (for timestamps) ======
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 0;
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n✅ WiFi connected");
// Sync time from NTP
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}
String getTimestamp() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("❌ Failed to obtain time");
return "";
}
char buf[25];
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", &timeinfo);
return String(buf);
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverUrl);
http.addHeader("Content-Type", "application/json");
// ====== Fake sensor values ======
int steps = random(10, 50);
float voltage = 3.25 + random(0, 11) / 100.0; // 3.25V–3.35V
float current = random(100, 500) / 1000.0;
float power = voltage * current * 1000; // in mW
String timestamp = getTimestamp();
// ====== Build JSON manually (lighter than ArduinoJson) ======
String requestBody = "{";
requestBody += "\"steps\":" + String(steps) + ",";
requestBody += "\"voltage\":" + String(voltage, 2) + ",";
requestBody += "\"current\":" + String(current, 3) + ",";
requestBody += "\"power\":" + String(power, 1) + ",";
requestBody += "\"timestamp\":\"" + timestamp + "\"";
requestBody += "}";
// ====== Send POST ======
int httpResponseCode = http.POST(requestBody);
if (httpResponseCode > 0) {
Serial.printf("✅ POST OK: %d | Sent -> %s\n", httpResponseCode, requestBody.c_str());
} else {
Serial.printf("❌ POST failed: %s\n", http.errorToString(httpResponseCode).c_str());
}
http.end();
} else {
Serial.println("⚠️ WiFi not connected!");
}
delay(3000); // send every 3 seconds
}