#include <WiFi.h>
#include <ThingSpeak.h>
#define LDR 34
#define SECRET_SSID "Wokwi-GUEST"
#define SECRET_PASS ""
#define CHANNEL_ID 2804140
#define WRITE_APIKEY "J0GIT08F4T8I6G9V"
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
WiFiClient client;
unsigned long myChannelNumber = CHANNEL_ID;
const char * myWriteAPIKey = WRITE_APIKEY;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
// put your main code here, to run repeatedly:
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("Connected.");
}
int analogValue = analogRead(LDR);
int lux = map(analogValue, 4063, 32, 0 , 100000);
delay(10);
Serial.println(analogValue);
Serial.println(lux);
Serial.println("---");
ThingSpeak.setField(5, analogValue);
ThingSpeak.setField(6, lux);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200)
{
Serial.println("Channel update successful.");
}
else
{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(15000);
}