//Adaptado a partir de:
/*
Adapted from WriteSingleField Example from ThingSpeak Library (Mathworks)
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-thingspeak-publish-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#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 = 1866027; //Inserir o channelNumber de sua conta no ThingSpeak
const char * myWriteAPIKey = "TIFGZD3E4UIYUFAH"; //Inserir o Write API Key de sua conta no ThingSpeak
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 10000;
// Variable to hold temperature readings
float temperatureC;
void setup() {
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.");
}
temperatureC +=1.0;
if(temperatureC>=20){
temperatureC=0.0;
}
Serial.print("Temperature (ºC): ");
Serial.println(temperatureC);
// 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, temperatureC, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
lastTime = millis();
}
}