#include <WiFi.h>
#define Led 2
#define SECRET_SSID "Wokwi-GUEST" // replace with your WiFi network name
#define SECRET_PASS ""
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
// Weather station channel details
unsigned long weatherStationChannelNumber = 2244972;
unsigned int temperatureFieldNumber = 3;
// Counting channel details
unsigned long counterChannelNumber = 2244972;
const char * myCounterReadAPIKey = "OONLE05DI9R8TXQS";
unsigned int counterFieldNumber = 4;
void setup() {
Serial.begin(115200); //Initialize serial
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
pinMode(Led,OUTPUT);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
int statusCode = 0;
// 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("\nConnected");
}
// Read in field 4 of the public channel recording the temperature
//int temperatureInF = ThingSpeak.readIntField(weatherStationChannelNumber, temperatureFieldNumber);
long temperatureInF = ThingSpeak.readLongField(counterChannelNumber, temperatureFieldNumber, myCounterReadAPIKey);
// Check the status of the read operation to see if it was successful
statusCode = ThingSpeak.getLastReadStatus();
if(statusCode == 200){
Serial.println("Temperature at MathWorks HQ: " + String(temperatureInF) + " deg F");
if (temperatureInF==1)
{
digitalWrite(Led,HIGH);
}
else{
digitalWrite(Led,LOW);
}
}
else{
Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
}
delay(15000); // No need to read the temperature too often.
// Read in field 1 of the private channel which is a counter
// long count = ThingSpeak.readLongField(counterChannelNumber, counterFieldNumber, myCounterReadAPIKey);
// // Check the status of the read operation to see if it was successful
// statusCode = ThingSpeak.getLastReadStatus();
// if(statusCode == 200){
// Serial.println("Counter: " + String(count));
// }
// else{
// Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
// }
// delay(15000); // No need to read the counter too often.
}