#include "ThingSpeak.h"
#include <WiFi.h>
//Replace your wifi credentials here
const char* ssid = "Wokwi-GUEST";//Replace with your Wifi Name
const char* password = "";// Replace with your wifi Password
//change your channel number here
unsigned long channel = 2501243;//Replace with your own ThingSpeak Account Channle ID
char readAPIKey[] = "H7W9IF18YD2Z7CFW";
//1,2 and 3 are channel fields. You don't need to change if you are following this tutorial. However, you can modify it according to your application
unsigned int led1 = 33;
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(100);
pinMode(led1, OUTPUT);
digitalWrite(led1, 0);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Netmask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
ThingSpeak.begin(client);
}
void loop() {
int statusCode = ThingSpeak.readMultipleFields(channel, readAPIKey);
if ( statusCode == 200) {
//get the last data of the fields
int led_1 = ThingSpeak.getFieldAsInt(1);
if(led_1 == 1){
digitalWrite(led1, 1);
Serial.println("led1 is On..!");
}
else if(led_1 == 0){
digitalWrite(led1, 0);
Serial.println("led1 is Off..!");
}
Serial.println(led_1);
delay(100);
}
}