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
}
@Jean.K21 Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
And a related Gotcha. If using a DSP18B20 in normal (not parasitic) mode, as shown above, which requires the pullup resistor from the data line to VCC and an ESP32 processor do not use GPIO12. Pulling GPIO12 High can inhibit the firmware load and throw an error along the lines of “rst:0x10 (RTCWDT_RTC_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT) | flash read err, 1000”
Others to avoid are GPIO6 through to GPIO11.
Forewarned etc
Cheers
@Argus.Tuft have you noticed anything odd about your posts compared to those from other people?
Triple backticks (that’s the ``` characters that you are starting and ending every post with) are used to make C++ code display correctly, and things like serial output and compiler error messages more readable.
They are not required for normal conversational posts, and as you can see, they make your posts much harder to read.
I’d suggest that you just use triple backticks where they are needed.