NodeMCU ESP with DS18B20 + DHT11 + Reed Sensor

Hey all, thank in advance for any help or suggestions.

I’m creating a project to keep an eye on a garden, and have run into some issues. I am using a nodemcu esp and am trying to connect 5 water temp probes, DHT11, and a reed sensor (letting me know if I have left the door open).

I have been able to get code for each sensor to work separately, but then when I try to get them all together I cannot get it to work properly. It looks as though the water probes (all on 1 pin) work just fine no matter what, but then the DHT11 seems to work intermittently and will show “NAN%” on my blynk dashboard - and seems like the temp/humidity numbers may not be correct. The reed sensor just seems to not work at all with the below code.

I’m pretty much a newbie, but can’t figure out where I’m going wrong with the code. I’m thinking something with the timers? or sequence? If you can help or point me to some help that’d be wonderful.

Thanks again!


#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <OneWire.h>

//DHT sensor
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

//water temp sensor
#include <OneWire.h>
#include <DallasTemperature.h> 
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
DeviceAddress Probe01 = { 0x28, 0xFB, 0xD7, 0x34, 0x17, 0x13, 0x01, 0x0A }; //S1
DeviceAddress Probe02 = { 0x28, 0xA0, 0x8C, 0x16, 0x17, 0x13, 0x01, 0x4E }; //S2
DeviceAddress Probe03 = { 0x28, 0xC8, 0x57, 0x33, 0x17, 0x13, 0x01, 0xF3 }; //S3
DeviceAddress Probe04 = { 0x28, 0x38, 0x8E, 0x31, 0x17, 0x13, 0x01, 0x2B }; //S4
DeviceAddress Probe05 = { 0x28, 0xAB, 0x58, 0x2D, 0x17, 0x13, 0x01, 0x2B }; //S5

//Door Sensor
const int switchPin = 4;

SimpleTimer timer;


//Blynk token
char auth[] = "xxx";

// WiFi credentials.
char ssid[] = "FiOS-xxx";
char pass[] = "xxx";

//DHT Sensor
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(true);
  Blynk.virtualWrite(V2, h);
  Blynk.virtualWrite(V3, t);
}

//water temp sensor 
void getTempData()
{
  sensors.requestTemperatures();
  Blynk.virtualWrite(V4, sensors.getTempFByIndex(0));
  Blynk.virtualWrite(V5, sensors.getTempFByIndex(1));
  Blynk.virtualWrite(V6, sensors.getTempFByIndex(2));
  Blynk.virtualWrite(V7, sensors.getTempFByIndex(3));
  Blynk.virtualWrite(V8, sensors.getTempFByIndex(4));
}

void setup()
{ 
  Blynk.begin(auth, ssid, pass);

//DHT Sensor
  dht.begin();
  
//watertemp sensor
  sensors.begin();
  sensors.setResolution(10);

//Door Sensor
  pinMode(switchPin, INPUT_PULLUP);
  digitalWrite(switchPin, LOW);
  
  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1500L, getTempData);
}

void loop()
{
  Blynk.run();
  timer.run();
}





I don’t see any code for the Reed Sensor. Additionally, when setting a pin as an input you generally don’t want to do a digitalWrite to that pin. I would remove digitalWrite(switchPin, LOW); from your setup.

As for the DHT11, these are “slow” sensors, and known to not be super accurate. I would increase your timer interval to at least 5 seconds. You can probably go even higher as I wouldn’t expect much change in temp/humidity in 5 seconds.

Thanks for the input.

I had some basic code to test the reed sensor, and that worked just fine. I’m basically only reading the output of the pin, and using Blynk app to say open/closed. I was following some other advice and I thought the digitalWrite(switchPin, LOW) was needed to go with the line previous line to pullup pinMode(switchPin, INPUT_PULLUP) Still a little confused on whether i need to do pullup/down and resistor.

I took out digitalWrite(switchPin, LOW)and it didn’t make a difference. Any suggestions?

Also, I’ve never had a problem with the DHT reading in other projects. I put it up to 5 seconds and mostly works, time will tell. Though, I don’t believe I’d had it be this inaccurate or so different than the other sensor (DS18B20).

The digitalWrite is used when you have it configured as an output. This is how you change it from HIGH to LOW; say for activating/deactivating a relay.

pinMode(switchPin, INPUT_PULLUP) is like adding a pullup resistor. For this setup you would want one wire from the reed sensor connected to the pin, and the other to ground. Take a look at THIS, and also THIS, it may help clarify. As mentioned though I do not see any code in what you posted that would monitor the reed sensors input. You could add another timed routine, say timer.setInterval(2000L, reedSensor); and then just check the pin to see if it is HIGH or LOW, for example (assuming contacts are closed when door is closed, and value display widget linked to V10):

void reedSensor()
{
if (digitalRead(switchPin) == LOW) 
    {
      // Send string
      Blynk.virtualWrite(V10, "CLOSED");
    }
   else 
   {
      Blynk.virtualWrite(V10, "OPEN");
   }
}

@Toro_Blanco thanks, this did the trick.

Now, I may need to work on the DHT some more.