Teme: Samogradnja

Arduino Temperature and Humidity Web Service

Skraćena veza: https://pedja.supurovic.net/veza/8576

I needed simple device that is able to measure temperature and make it available for collecting that info from remote places via Internet. Arduino platform was the first thought and it ended up as expected: nice and simple.

For hardware I used Arduino UNO, Ethernet Shield and DHT22 Temperature and Humidity sensor.

Ethernet Shield pinpoint matches Arduino UNO so it is simply attached on top of it. It provides RJ45 connector for ethernet connection and SD card socket which I did not need for this small project.

All pins on Ethernet Shield are pass-through which means all Arduino pins are available on the this shield too. Ethernet Shield itself uses pins 10, 11, 12 and 13 so those are not available for additional connections like temperature sensor.

For connecting sensor I used Arduino pin 2. I could use any other free pin but I found out ready made schematics image displaying usage of this pin, so I decided to comply to avoid having to draw schematics by myself 🙂

Well, schematics shows hooking DHT22 directly to Arduino but as I needed ethernet too, I simply inserted Ethernet Shield in between. As I said all Arduino pis are available via pass-through connectors so wiring is the same.

DHT22 is simple to use. It has four pins of which one is not used. Pin1 is hooked to Vin pin at Arduino board. That is the same voltage Arduino is powered from, and in mz case it was 5 V which was needed for DHT sensor too. Pin 4 is connected to GND. Pin 2 is data pin. It may be connected to arbitrary Arduino GPIO pin which is available. I used PIN 2 for my setup. DHT-22 pin 2 also has to be pulled up to 5 Volts via 10 K resistor. According to several sources 10 K resistor is not necessary. I tried using setup without it with no difference. It worked stable either way.

And that is all about hardware. As I said nice and simple. Next was to deal with software part.Ethernet library is already installed in Arduino Integrated Development Environment. DHT library is not part of IDE by default so You have to install it. DHT library is dependable of Adafruit Sensor library so that one also must be installed.

Installation is simple, when you unpack project code, copy to DHT directory and Adafruit_Sensor directory to Arduino IDE libraries. Then, you may load project sketch and upload it to Arduino.

There is one issue. For some reason, while Ethernet Shield is attached to Arduino uploading of code does not work. You have to detach Ethernet Shield from Arduino, upload code, reattach everything and it will work fine.

Sketch code is simple to read and understand so I will put up just important notes:

HTTP server is set to use DHCP to obtain IP address from network you attach device to. You can find out what IP it gets viewing serial port or looking into DHCP server that leases IPs in your network. If there is no DHCP, server will use IP address that is set in code (192.168.1.8). You may change IP to match your network. You may also change MAC address. Actually, if you use several of such devices within the same network you must change IP and MAC addresses as it is not possible to have devices with the same IP or MAC in the same network.

By default, You may access web server using it’s IP and TCP port 80, as any standard web server. It will simply respond with current temperature and humidity info. You may change access port in code.

Complete sketch code:

/*

 Arduino HTTP Temperature Sensor

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

 Version 1.03

 Circuit:
 - Ethernet shield attached to pins 10, 11, 12, 13
 - DHT-22 (AM2302) sensor attached to pin 2 for data and 5V
   and GND for power.
 - 10 K resistor connecting pin 2 of temperature sensor to 5V

 created 22 Feb 2017
 by Predrag Supurovic

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

*/

#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>

// Set a MAC address for ethernet interface.
byte mac[] = {
  0xDA, 0xA0, 0xBE, 0x1F, 0x6E, 0x08 };

// Set IP address for web server. Server will try to get
// IP address from DHCP and if failed then it will use
// this IP address
//IPAddress ip(192,168,1,8);
byte ip[] = { 192, 168, 1, 8 };
// the router's gateway address:
byte gateway[] = { 192, 168, 1, 1 };
// the subnet:
byte subnet[] = { 255, 255, 255, 0 };

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

// Define PIN DHT-22 is connected to.
#define DHT_PIN 2

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

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

//Store for humidity value
float humidity;    

//Store for temperature value
float temperature; 


// Ethernet service handler
EthernetServer server(port); 

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

  Serial.println("Querying DHCP...");

  // try to get IP address from DHCP
  if (Ethernet.begin(mac) == 0) {
    // if DHCP fails then use fixed IP
    Serial.println("No DHCP, setting static IP.");
    Ethernet.begin(mac, ip, gateway, subnet);
  }
  
  server.begin();
  Serial.print("Assigned IP address: ");
  Serial.println(Ethernet.localIP());

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

} // setup()

void loop() {

  EthernetClient client = server.available();
  if (client) {
    Serial.print("New connection: ");

    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        //char c = client.read();
        //Serial.write(c);
        //if (c == '\n' && currentLineIsBlank) {

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");       

          // refresh the page automatically every 5 sec
          //client.println("Refresh: 5");

          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");

          humidity = dht.readHumidity();
          temperature = dht.readTemperature();

          // show data to visitor
          client.print("Temperature: ");
          client.print(temperature);
          client.print(" C, ");
          client.print("Humididty: ");
          client.print(humidity);
          client.println(" %");
          client.println("</html>");

          Serial.print("temperature: ");
          Serial.print(temperature);
          Serial.print(" C, ");
          
          Serial.print("humidity: ");
          Serial.print(humidity);
          Serial.print(" %");
          
          break;
        //}
        //if (c == '\n') {
        //  currentLineIsBlank = true;
        //} else if (c != '\r') {
        //  currentLineIsBlank = false;
        //}
      }
    }
   delay(1);
   client.stop();
   Serial.println(" sent.");
  }
}

Download

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

What can you do more

This is just simple example. You may customize it for your needs.

For example, this code presents temperature and humidity info in for readable by humans. It is more likely you would like to collect this data from remote applications that would then store it in database or display on their own. You may change way data is presented to be able to collect it for that matter.

If you need to track temperature and humidity info for more items on the same location there is plenty of free GPIO pins on Arduino. You just have to attach more sensors and adjust code to read all of them.

SD card interface may be used to store temperature and humidity data in timely manner and then allow remote clients to get whole stored history, not just current data from sensor like this simple example.

Smaller form factor

If you want this device to occupy smaller space that is actually possible. You may use Arduino Nano board. There is also Ethernet Shield that matches Arduino Nano form factor. That would be significantly smaller.

Wireless form factor

If you want the same functionality but instead of Ethernet you want wireless connectivity there are some wireless shields for Arduino. However, it is simpler and costs less to use ESP8266 module. Check this other article to learn how to use that.

ESP8266 Temperature and Humidity Web Service


Podelite ovaj članak sa prijateljima


2 comments to Arduino Temperature and Humidity Web Service

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>

Popunite izraz tako da bude tačan: *