#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#define LDR_PIN 34 // GPIO pin connected to LDR
#define LED_PIN 2 // GPIO pin connected to LED
#define DHT_PIN 15 // GPIO pin connected to DHT22
#define DHT_TYPE DHT22
// Replace with your Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Replace with your Favoriot API details
const char* favoriot_endpoint = "https://apiv2.favoriot.com/v2/streams";
const char* api_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImZxaG02OSIsInJlYWRfd3JpdGUiOnRydWUsImlhdCI6MTczNjMyMzk4OX0.uYqOmsOI3MVEV18Ko-r8Ql_fqBeQO8rjr63Hb8tXDX4";
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
pinMode(LDR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to Wi-Fi");
dht.begin();
}
void loop() {
int ldrValue = analogRead(LDR_PIN);
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (ldrValue < 1000) { // Example threshold to control LED
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("LDR Value: ");
Serial.println(ldrValue);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("°C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(favoriot_endpoint);
http.addHeader("Content-Type", "application/json");
http.addHeader("apikey", api_key);
String jsonData = "{";
jsonData += "\"device_developer_id\": \"iot_1_device@fqhm69\",";
jsonData += "\"data\": {";
jsonData += "\"ldr\": " + String(ldrValue) + ",";
jsonData += "\"temperature\": " + String(temperature) + ",";
jsonData += "\"humidity\": " + String(humidity);
jsonData += "}}";
int httpResponseCode = http.POST(jsonData);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Wi-Fi not connected");
}
delay(60000); // Wait for 60 seconds before next reading
}