AdSense

Tuesday, 2 July 2019

WiFi thermometer with the DHT22 and EPS8266-12E

(Deutsche Version) I should be posting more stuff about the printer but I just worked on the ESP8266-12E and realised a super easy WiFi thermometer (using an EPS8266-12E as a web client which sends to an apache server on a raspberry pi), so I wanted to share this here.


1. The ESP8266-12E
The ESP8266-12E is an Arduino like microcontroller which has an integrated WiFi controller. If you look for it on google, you'll find many sources to aquire it. The great thing about this EPS8266 is that you can program it with the Arduino software, so all your libraries work, too!

2. Set up the Arduino IDE for the ESP8266-12E
First you should install the CH340 driver. There are many tutorials for this on the internet so I will not explain this. After installing the driver, you have to switch to the Arduino settings (I only have a german Arduino IDE but I guess you should be able to figure out where to click):

Here you add the board manager URL:
http://arduino.esp8266.com/stable/package_esp8266com_index.json

Afterwards, you can download the settings for the boards in the board manager:

Look for "ESP8266" and install it:

Now, everything should be set up and we can choose the board, I used "NodeMCU 1.2 (ESP8266-12E Module)":


3. The code
The code is quite straight-forward. You have to put in your WiFi login data, afterwards the DHT22 sensor is initialised. In "loop", the connection to the WiFi is tested. If a connection is established, the sensor measures temperature and humidity (float t and float h), these values are stored in a string (requestString) and then sent as HTTP get request to the server. The code for the server is posted below.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid     = "***";
const char* password = "***";   
int wifiStatus;
    
#include "DHT.h"
#define DHTPIN D2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:

  Serial.print("Your are connecting to;");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  dht.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  wifiStatus = WiFi.status();
  
  if(wifiStatus == WL_CONNECTED){
    Serial.println("");
    Serial.println("Your ESP is connected!");  
    Serial.println("Your IP address is: ");
    Serial.println(WiFi.localIP()); 
    float t = dht.readTemperature(); 
    float h = dht.readHumidity(); 
    HTTPClient http;
    char requestString[255] = "";
    sprintf(requestString, "http://webServer.de/Send.php?name=TemperatureSensor&data=%f,%f", t, h);
    http.begin(requestString);  //Specify request destination
    int httpCode = http.GET();                                                                  //Send the request
    
    if (httpCode > 0) 
    { //Check the returning code
      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);                     //Print the response payload
    }
    
    http.end();   //Close connection
    
    delay(300000);
  }
  else{
    Serial.println("");
    Serial.println("WiFi not connected");
    delay(1000);
  }
}

4. The Sserver code
An apache server is running on my raspberry pi. In /var/www, there is a file "Send.php", which simply receives sensor data and saves this to .csv files. The sensor tells its name in the get command, this is the filename where the data is stored. The current time is added and stored together with the data at the end of the file. In this case, this is the temperature in °C and the humidity in %.

<html>
 <head>
  <title>Send Sensor Values</title>
 </head>
 <body>
  <?php

$name = htmlspecialchars($_GET["name"]);
$data = htmlspecialchars($_GET["data"]);

$date = date("Y-m-d") . "T" . date("H:i:s.u") . "Z";

$file = "data/".$name.".csv";
$Saved_File = fopen($file, 'a');
fwrite($Saved_File, $date . "," . $data . "\r\n");
fclose($Saved_File);

  ?>
 </body>
</html>

2 comments:

  1. Your blog shares good information about the WiFi thermometer. We are also dealing with these kind devices and if anyone wants to purchase these kinds of device visit us at ubibot.io

    ReplyDelete