#include #include #include #include #include LTR308 light; unsigned char ID; // Replace with your network credentials const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* password = "REPLACE_WITH_YOUR_PASSWORD"; // Create AsyncWebServer object on port 80 AsyncWebServer server(80); void initLight(){ light.begin(); // To start taking measurements, power up the sensor light.setPowerUp(); // Allow for a slight delay in power-up sequence (typ. 5ms from the datasheet) delay(10); light.setGain(0); light.setMeasurementRate(0, 3); } String readLight(){ unsigned long rawData; double lux; // Resulting lux value boolean good; // True if sensor is not saturated light.getData(rawData); good = light.getLux(0, 0, rawData, lux); if(good) return String(lux); else { Serial.println("Failed to read light sensor!"); return "-1"; } } void setup(){ // Serial port for debugging purposes Serial.begin(115200); light.begin(); if (light.getPartID(ID)) { Serial.print("Got Sensor Part ID: 0X"); Serial.print(ID, HEX); Serial.println(); } else { Serial.println("Could not initialize LTR308 sensor"); while(1); } initLight(); // Initialize SPIFFS if(!SPIFFS.begin()){ Serial.println("An Error has occurred while mounting SPIFFS"); return; } // Connect to Wi-Fi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi.."); } // Print ESP32 Local IP Address Serial.println(WiFi.localIP()); // Route for root / web page server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(SPIFFS, "/index.html"); }); server.on("/light", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/plain", readLight().c_str()); }); // Start server server.begin(); } void loop(){ }