#include <WiFi.h>
#include <ThingSpeak.h> // always include thingspeak header file after other header files and custom macros
// Replace with your Wi-Fi credentials
const char* ssid = "kanha";
const char* password = "00000000";
// ThingSpeak settings
const char* apikey = "BXD9WTTSJLQC7DY5";
const unsigned long channelID = 2217229;
const int ledPin = T2; // Assuming you're using NodeMCU with built-in LED
WiFiClient client;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(115200);
delay(100);
//void setup()
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
// Connect to Wi-Fi
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("not conncted\n");
}
Serial.println(connected
);
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}
void loop()
{
// Blink LED
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
// Send data to ThingSpeak
int value = digitalRead(ledPin);
ThingSpeak.writeField(channelID, 1, value, apikey);
delay(20000); // Send data every 20 seconds
}