#include "WiFi.h"
#include <HTTPClient.h>
#include <ThingSpeak.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* thingSpeakServer = "api.thingspeak.com/update";
const char* writeAPIKey = "39SNENCWNDY86C1Z";
void setup()
{
Serial.begin(115200);
pinMode(21, OUTPUT);
pinMode(3, INPUT);
digitalWrite(21, LOW);
while(WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid,password,6);
delay(500);
Serial.print(". ");
}
Serial.print("Connected");
}
void loop()
{
int a=digitalRead(3);//ead(3);
int an=analogRead(15);
Serial.print("LDR Value: ");
Serial.println(a);
if(an>1120)
{
digitalWrite(21, HIGH);
}
else{
digitalWrite(21,LOW);
}
int s =digitalRead(21);
Serial.println(s);
delay(5000);
sendDataToThingSpeak(an,s);
}
void sendDataToThingSpeak(int lux, int a)
{
HTTPClient http;
// Construct the URL
String url = "http://" + String(thingSpeakServer) + "?api_key=" + String(writeAPIKey) + "&field1=" + String(lux) + "&field2=" + String(a);
http.begin(url); // Start the HTTP request
int httpCode = http.GET(); // Send GET request and get the HTTP response code
if (httpCode > 0)
{
String payload = http.getString(); // Get the response payload
Serial.println("ThingSpeak response: " + payload);
}
else
{
Serial.print("Error in HTTP request: ");
Serial.println(httpCode);
}
http.end(); // End the HTTP request
}