Hi, everyone. Is it possible If someone can tell me if there is an error in my code?
I have an ESP32 with a DHT11 and DS18B20 sensor and a Relay. The DHT11 and Relay are working 100%. But I am not getting any data from the DS18B20 sensor.
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "";
char ssid[] = ""; // type your wifi name
char pass[] = ""; // type your wifi password
BlynkTimer timer;
//DHT11 LIB
#include "DHT.h"
#define DHTPIN 14
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float t, h;
//DS18B20 LIB
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 12
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float soiltemp;
//Relay Pin
#define RelayPin 13
void sendSensor()
{
//DHT11 Air Temperature and Humidity
h = dht.readHumidity();
t = dht.readTemperature();
Blynk.virtualWrite(V0, t);
Blynk.virtualWrite(V1, h);
if (isnan(h) || isnan(t)) {
Serial.println("No Reading from DHT sensor!");
return;
}
//Soil Tempreture
DS18B20.requestTemperatures();
soiltemp = DS18B20.getTempCByIndex(0);
Serial.println(soiltemp);
Blynk.virtualWrite(V2, soiltemp);
//Heat Mat relay on and off with DS18B20
pinMode(RelayPin, OUTPUT);
//Relay ON
if(soiltemp < 21){
digitalWrite(RelayPin, HIGH);
Serial.println("Heat Mat ON!");
Blynk.logEvent("heat_mat_on", "Soil Temperature is under 21°C, Heat Mat is ON!");
}
//Relay OFF
if(soiltemp > 26){
digitalWrite(RelayPin, LOW);
Serial.println("Heat Mat OFF!");
Blynk.logEvent("heat_mat_off", "Soil Temperature is over 26°C, Heat Mat is OFF!");
}
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
dht.begin();
DS18B20.begin();
delay(2000);
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates SimpleTimer
}