#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define PHOTO_RESISTOR_PIN A0
WiFiClient client;
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const char *host = "api.thingspeak.com";
String apiKey = "8RPM06NW2Y4NSTB4";
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(115200);
delay(1000);
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.display();
delay(2000);
display.clearDisplay();
display.display();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println("WiFi connected");
}
void loop() {
delay(2000);
int lightValue = analogRead(PHOTO_RESISTOR_PIN);
Serial.print("Photoresistor value: ");
Serial.println(lightValue);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Light: " + String(lightValue));
display.display();
if (client.connect(host, 80)) {
String url = "/update?api_key=";
url += apiKey;
url += "&field1=";
url += String(lightValue);
Serial.print("Requesting URL: ");
Serial.println(url);
client.print("GET ");
client.print(url);
client.println(" HTTP/1.1");
client.println("Host: " + String(host));
} else {
Serial.println("Failed");
}
delay(1000);
while (client.available()) {
char c = client.read();
Serial.write(c);
}
client.stop();
delay(20000);
}