main.cpp
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
 
// ==== TODO: fill these in for the lab ====
const char* WIFI_SSID     = "LAB_WIFI_SSID";
const char* WIFI_PASSWORD = "LAB_WIFI_PASSWORD";
const char* SERVER_URL    = "http://192.168.1.100:8080/ingest"; // replace with your server IP
// =========================================
 
const char* DEVICE_ID     = "sparrow-01";
const char* API_KEY       = "SUPER_SECRET_API_KEY_123"; // intentionally bad design
 
void connectToWiFi() {
  Serial.printf("Connecting to WiFi SSID: %s\n", WIFI_SSID);
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
 
  uint8_t retries = 0;
  while (WiFi.status() != WL_CONNECTED && retries < 30) {
    delay(500);
    Serial.print(".");
    retries++;
  }
 
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nWiFi connected!");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("\nFailed to connect to WiFi");
  }
}
 
void setup() {
  Serial.begin(115200);
  delay(2000); // give serial time to come up
 
  connectToWiFi();
  randomSeed(esp_random());
}
 
void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi disconnected, reconnecting...");
    connectToWiFi();
  }
 
  if (WiFi.status() == WL_CONNECTED) {
    // Fake sensor data
    float tempC     = 20.0 + (random(0, 1000) / 100.0f);  // 20.00 to 29.99
    float humidity  = 40.0 + (random(0, 1000) / 50.0f);   // 40.0 to 59.9
    int   battery   = random(60, 100);                    // 60-99 %
 
    // Build JSON manually to avoid extra libraries
    String payload = "{";
    payload += "\"device_id\":\"" + String(DEVICE_ID) + "\",";
    payload += "\"temp_c\":" + String(tempC, 2) + ",";
    payload += "\"humidity\":" + String(humidity, 1) + ",";
    payload += "\"battery\":" + String(battery);
    payload += "}";
 
    Serial.println("Sending HTTP POST to server...");
    Serial.println("URL: " + String(SERVER_URL));
    Serial.println("Payload: " + payload);
    Serial.println("API key (also sent!): " + String(API_KEY));
 
    HTTPClient http;
    http.begin(SERVER_URL);                       // HTTP, not HTTPS
    http.addHeader("Content-Type", "application/json");
    http.addHeader("X-API-Key", API_KEY);         // Intentionally leaked secret
 
    int httpCode = http.POST(payload);
    String response = http.getString();
 
    Serial.printf("HTTP response code: %d\n", httpCode);
    Serial.println("Response body: " + response);
    Serial.println("-----------------------------");
 
    http.end();
  }
 
  delay(5000); // send every 5 seconds
}