#include <WiFi.h>
#include "ThingSpeak.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
const char* ssid = "Wokwi-GUEST"; // your network SSID (name)
const char* password = "";
WiFiClient client;
unsigned long myChannelNumber = 2220214;
const char * myWriteAPIKey = "RLSTSZ06GBSF03I9";
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
void setup() {
Serial.begin(115200);
// Start the DS18B20 sensor
sensors.begin();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(Client);
//initialize the LCD
LCD.init();
LCD.backlight();
LCD.clear();
}
void loop() {
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
float tempF = sensors.getTempFByIndex(0);
if(tempC != DEVICE_DISCONNECTED_C || tempF != DEVICE_DISCONNECTED_F)
{
Serial.print(tempC);
Serial.println("ºC");
Serial.print(tempF);
Serial.println("ºF");
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Temperature:");
LCD.setCursor(0, 1);
LCD.print(tempC);
LCD.print("C ");
LCD.print(tempF);
LCD.print("F");
}
else
{
Serial.println("Error: Could not read temperature data");
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Read Error!");
}
delay(5000);
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.");
}
}
Loading
ds18b20
ds18b20