Monitoring Humidity, Temperature in the Hen House: DHT11, DS18B20, ESP8266

by diy_bloke in Circuits > Microcontrollers

10477 Views, 42 Favorites, 0 Comments

Monitoring Humidity, Temperature in the Hen House: DHT11, DS18B20, ESP8266

DSC_0937kl.JPG

Note, I have now updated this project with an OLED, BMP180 and 2 DS18B20 sensors

In order to keep track off the temperature in the night quarters of my chicken coop I needed a wireless connection to some sensors.
As I had an ESP8266-01 lying around that seemed like the perfect medium to do that with.

BOM
ESP8266-01 (or other version)
DHT11
DS18B20
2x 4k7 resistor
1x 1n4001 diode
LM1117-33
10uF 10V capacitor
2x4 female headers
some veroboard

The ESP8266 has 4 I/O pins. However, GPIO 0 and the Tx pin are a bit fussy in their use (The GPIO0 pin is also used to jump in the program mode) so I decided to use GPIO 2 and the Rx pin. (Mind you, it is NOT impossible to use GPIO0 as an I/O pin, I just took the path of least resistance)
The DHT11 can only report temperatures down to 0 degrees. I am not sure if it can stand subzero temperatures but I guess I will find out. I am hoping that temperatures in the night quarters will not go below zero.
DS18B20 is added to keep an eye in the outside temperature
The 4k7 resistors are pull up resistors for the sensors. They will work without, but it is good to use the resistors anyway.
The 1N4001 diode is to prevent accidental reversed polarity. If you think that is not going to happen to you leave it out.
LM1117-33 as the entire device needs 3.3 Volt. The Minimum input of the LM1117-33 is 4.75 Volt so if you plan to feed it with a 5 Volt supply, the voltage drip over the 1N4001 diode might just be a wee bit too much

The Circuit

kippenhok.JPG

The circuit is quite straightforward.
There is a powersupply built around an LM1117-33 with a diode and one capacitor.
There is a 2x4 point female header in which the ESP8266-01 will slot. The DHT11 is connected to pin2 and the DS18B20 to pin 3. Both have a 4k7 pull up resistor

The Program

#include <DHT.h>
#include <OneWire.h> //http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <DallasTemperature.h> //  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <ESP8266WiFi.h>

#define DHTPIN 2
#define DHTTYPE  DHT11
#define ONE_WIRE_BUS 3  // that is the Rx pin

const char* ssid     = "MyHouse";//<-- put your SSID here
const char* password = "secret123";<// put your password here
const char* host = "api.thingspeak.com";
const char* writeAPIKey="W59AEELE0N2EHJA6";// <-- put your API Key here
float temperature_buiten;
DHT dht(DHTPIN, DHTTYPE, 15);
OneWire oneWire(ONE_WIRE_BUS); //oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire);// Pass address of our oneWire instance to Dallas Temperature.
DeviceAddress Probe01 = { 0x28, 0x0F, 0x2A, 0x28, 0x00, 0x00, 0x80, 0x9F}; 
//DeviceAddress Probe02={0x28, 0x10, 0xA4, 0x57, 0x04, 0x00, 0x00, 0xA9};// if you want more sensors



void setup() {
  // Initialize sensor
    sensors.begin();//ds18b20
  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 10);
  //sensors.setResolution(Probe02, 10);
  dht.begin();
  delay(1000);

//  Connect to WiFi network
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

void loop() {
  //ds18b20stuff-------------------
  sensors.requestTemperatures(); // Send the command to get temperatures
  temperature_buiten = sensors.getTempC(Probe01);//
  //dht11 stuff--------------------
 float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  if (isnan(humidity) || isnan(temperature)) {
    return;
  }

// make TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    return;
  }

  String url = "/update?key=";
  url+=writeAPIKey;
  url+="&field1=";
  url+=String(temperature);
  url+="&field2=";
  url+=String(humidity);
  url+= "&field3=";
  url+=String(temperature_buiten);
  url+="\r\n";
  
  // Send request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");

    delay(1000);
}

The program is fairly straightforward. As I wanted to keep the possibility open to add more DS18B20 sensors (might as well be informed what the temperature of my pond or soil is) I am using a protocol that read outs the unique ID of the sensor. If you do not know how to get that, check the next step.

The Unique Address of Your DS18B20

To find out what the unique number of your DS18B20 is, use this code, preferably on an Arduino

/* YourDuino Example: Find Address of a DS18B20 Temperature Sensor
 Cut and paste the address to a text file for later use.
 V1.1 01/17/2013
 Questions: terry@yourduino.com 
 
 Connections:
 DS18B20 Pinout (Left to Right, pins down, flat side toward you)
 - Left   = Ground
 - Center = Signal (Pin 2):  (with 3.3K to 4.7K resistor to +5 or 3.3 )
 - Right  = +5 or +3.3 V   
 This sketch looks for 1-wire devices and  prints their addresses (serial number)
 to the Serial Monitor in a format that is useful in Arduino sketches.
 Based on example at: 
 http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
 */

/*-----( Import needed libraries )-----*/
#include <OneWire.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define SENSOR_PIN 2  // Any pin 2 to 12 (not 13) and A0 to A5

/*-----( Declare objects )-----*/
OneWire  ourBus(SENSOR_PIN);  // Create a 1-wire object

void setup()  /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(115200);
 
  discoverOneWireDevices();  // Everything happens here!
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  // Nothing happening here
}

/*-----( Declare User-written Functions )-----*/
void discoverOneWireDevices(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];

  Serial.print("Looking for 1-Wire devices...\n\r");// "\n\r" is NewLine 
  while(ourBus.search(addr)) {
    Serial.print("\n\r\n\rFound \'1-Wire\' device with address:\n\r");
    for( i = 0; i < 8; i++) {
      Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');
      }
      Serial.print(addr[i], HEX);
      if (i < 7) {
        Serial.print(", ");
      }
    }
    if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n\r");
      return;
    }
  }
  Serial.println();
  Serial.print("Done");
  ourBus.reset_search();
  return;
}

//*********( THE END )***********

Thingspeak

kippenhokthingspeak.png

I decided to upload the data to Thingspeak, but one may just as well upload them to a personal webserver.
I presume the use of Thingspeak is well known buy now. In short.. one needs to set up a channel and create 3 fields: 1 for the temperature of the DHT11, one for the humidity and one for the temperature of the DS18B20

Construction

DSC_0937kl.JPG

After I tested it I put it all on a small piece of veroboard. Whether that is the easiest in your case depends.
The ESP8266-01 is notoriously breadboard unfriendly I had made a small adapter that I also used to program the board. Afterwards I just put it in the headers on my veroboard

Programming the ESP8266

esp8266-ftdi33.png
esp8266-ftdi5v.png

It is not my intention to give a detailed description of the programming process of an ESP8266 as there are excellent instructables or HowTo'sHowTo's on how to do that, but I will briefly go through the steps and for most people that should be sufficiently clear:

  1. Make sure you have the ESP8266 cores installed in your arduino IDE and select the board you are using (Generic ESP8266 Module will suffice in most cases)
  2. Connect the ESP8266 with an USB-ttl module as in the picture. USE 3.3 VOLT LEVELS ONLY
    1. Vcc to Vcc
    2. GPIO0 to Ground
    3. Tx to Rx
    4. Rx to Tx
    5. Ground to Ground
    6. CH_PD to Vcc
  3. Upload your sketch

Using a 3.3 Volt USB-FTDI converter is the easiest. If you only have a 5 Volt then give the ESP8266 its own 3.3 Volt supply and use a voltage converter between the Tx of the converter and the Rx of the ESP8266.
I repeat: DO NOT USE ANY 5 VOLT LEVELS ON ANY PIN OF THE ESP8266