#include <WiFi.h>
#include "ThingSpeak.h"
const char* ssid = "Wokwi-GUEST"; // your network SSID (name)
const char* password = ""; // your network password
WiFiClient client;
unsigned long myChannelNumber = 1896462;
const char * myWriteAPIKey = "XI198KRHOOVD58JS";
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 3000;
// Variable to hold temperature readings
float analogInput;
//uncomment if you want to get temperature in Fahrenheit
//float temperatureF;
void setup() {
pinMode(33,INPUT);
Serial.begin(115200); //Initialize serial
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("\nConnected.");
}
// Get a new reading
analogInput = analogRead(33)*0.00805;
Serial.print("Analog Input(ºC): ");
Serial.println(analogInput);
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
int x = ThingSpeak.writeField(myChannelNumber, 1, analogInput, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
lastTime = millis();
}
}