Теме: Самоградња

ESP8266 Temperature and Humidity Web Service

Скраћена веза: https://pedja.supurovic.net/veza/8605

After I successfuly tested HTTP server using Arduino and Ethernet Shield, I decided to try to accomplish the same thing using wireless connectivity. I decided to use ESP8266 on NodeMCU board. That is microcontroler with wireless interface all in one package, and quite low priced which makes it interesting for various purposes. I used the same temperature and humidity sensor DHT-22.

ESP8266 is supported within Arduino IDE which is big plus, as one can use the same development environment.

Soon I realized that popularity of this platform is well earned. It was very easy to set it up. Most of the trouble is setting up Arduino IDE to compile and upload to board. Everything else was apiece of cake – just connected DHT sensor to get power from board (Vin and GND pins, same as with Arduino) and attached DHT data pin to pin D1 of the NodeMCU board whuch is actually GPIO5 on ESP8266.

After that all it was needed was to load simple code:

/*

 ESP8266 HTTP Temperature and Humidity Sensor

 A simple web server that displays current temperature and
 humidity read from DHT-22 (AM2302) sensor.

 Version 1.01

 Circuit:
 - NodeMCU (ESP12E) module for ESP8266
  - DHT-22 (AM2302) sensor attached to pin 2 for data and 5V
   and GND for power.
 
 created 22 Feb 2017
 by Predrag Supurovic

 http://pedja.supurovic.net/esp8266-temperature-and-humidity-web-service

*/

// WiFi access parameters
const char* ssid = "MYWIFI";
const char* password = "mypassword";


// Include the ESP8266 WiFi library
#include <ESP8266WiFi.h>
// Include the DHT sensor library
#include "DHT.h"

// Uncomment line to set what DHT sensor zou are using
//#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
#define DHTTYPE DHT22     // DHT 22  (AM2302), AM2321

// TCP port for http server
int port = 80;

// Define PIN DHT sensor is connected to.
#define DHT_PIN 5

// Web Server on port 80
WiFiServer server(port);

// Initialize DHT sensor.
DHT dht(DHT_PIN, DHTTYPE);

// Temporary variables
static char celsiusTemp[7];
static char humidityTemp[7];

//Store for humidity value
float humidity;    

//Store for temperature value
float temperature; 

// only runs once on boot
void setup() {
  // prepare serial connection to provide some logging
  Serial.begin(9600);
   while (!Serial) {
    ;
  }

  dht.begin();
  
  // Connecting to WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.print(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  
  // Starting the web server
  server.begin();
  Serial.println("Querying DHCP...");
  delay(10000);
  
  // Printing IP address
  Serial.print("Assigned IP address: ");
  Serial.println(WiFi.localIP());

  Serial.print("Access sensor at http://");
  Serial.print(WiFi.localIP());
  Serial.print(":");
  Serial.print(port);
  Serial.println("/");  
}

// runs over and over again
void loop() {
  // Listenning for new clients
  WiFiClient client = server.available();
  
  if (client) {
   Serial.print("New connection: ");
    while (client.connected()) {
      if (client.available()) {
        float humidity = dht.readHumidity();
        float temperature = dht.readTemperature();

        Serial.print("temperature: ");
        Serial.print(temperature);
        Serial.print(" C, ");
          
        Serial.print("humidity: ");
        Serial.print(humidity);
        Serial.print(" %");            
 
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println("Connection: close");
        client.println();
        
        // Create and send web page that displays temperature and humidity
        client.println("<!DOCTYPE HTML>");
        client.println("<html>");
        client.println("<head></head><body>");
        
        client.println("<div>Temperature: ");
        client.println(temperature);
        client.println(" C, ");
        
        client.println("Humidity: ");
        client.println(humidity);
        client.println(" %</div>");
        client.println("</body></html>");     
        break;
      }
    }  
 
    delay(1);
    client.stop();
    Serial.println(" sent.");
  }
}   

After code is executed on board it started wireless client, connected to my home wireless, got IP form DHCP and HTTP server was ready. Al I had to do is access controllers IP address using web browser and it displayed current temperature and humidity values.

Superb!

Download

You may Download whole project from: ESP8266 Temperature and Humidity Sensor (17). It contains Sketch code, schematic images and DHT and Adafruit_Sensor library.


Поделите овај чланак са пријатељима


Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Попуните израз тако да буде тачан: *