#include <WiFi.h>
#include "secrets.h"
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
#include <OneWire.h>
#include <DallasTemperature.h>
char ssid[] = "Wokwi-GUEST"; // your network SSID (name)
char pass[] = ""; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = 2966573;
const char * myWriteAPIKey = "7M28RYM21PIEI3JZ";
int number = 0;
// GPIO onde o DS18B20 está conectado
const int OneWireBus = 5;
// Configure uma instância oneWire para se comunicar com qualquer dispositivo One-Wire
OneWire oneWire(OneWireBus);
// Passe nossa referência oneWire para o sensor de temperatura Dallas
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200); //Initialize serial
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
// Inicia o DS18B20 sensor
sensors.begin();
}
void loop() {
// 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.");
}
//Faz a leitura de temperatura do DS18B20:
sensors.requestTemperatures();
float temperaturaC = sensors.getTempCByIndex(0);
// 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, temperaturaC, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(20000); // Wait 20 seconds to update the channel again
}EXERCICIO_4_CANAL_THINGESPEAK
Rodrigo Francisco Pettinati Mikaro
RM: 37974
Sensor de
Temperatura