Arduino Data Collection + DHT11

details

ETHERNET-DHT11

Description

This example consists of a UNO Arduino, having an Ethernet shield coupled to connect to the Internet and DHT11 sensor to collect temperature information and humidity environment. We can apply this example to monitor changes in temperature and humidity, in an attempt to generate alerts in adverse situations.

 

Hardware Requirements

  • Arduino UNO or compatible
  • Shield Ethernet – Chip Wiznet W5100
  • Temperature sensor DHT11 -Wires

 

Code

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

#define DHTPIN 7 //port connected to DHT11
static char apiKey[] = "xxxxxxxx-yyyy-zzzz-wwww-kkkkkkkkkkkk";
static char serial[] = "IOTWBS0001";

dht DHT;
EthernetClient client;

signed long next;
unsigned long frequencyTime = 60000; //Time will be read and sent values (1 min.).
static byte mac[] = { 0xDE, 0xAD, 0x09, 0xEF, 0xFE, 0xED }; // Mac address
static char serverName[] = "guardiao.cl";//DO NOT CHANGE THIS VALUE - URL Cloud Guardian

void setup() {
Serial.begin(9600);
Ethernet.begin(mac); //IP automatic
next = 0;
}

void loop() {
if (((signed long)(millis() - next)) > 0)
{
  Serial.println(F("Coletando...")); 
  next = millis() + frequencyTime;
  char temp[6] PROGMEM;
  char hum[6] PROGMEM;
  int chk = DHT.read11(DHTPIN); //It is possible to use other sensors DHT necessary to switch the call.
  if(chk == DHTLIB_OK){  
    dtostrf(DHT.temperature, 1, 2, temp);
    dtostrf(DHT.humidity, 1, 2, hum);
  }
  else{
    temp[0] = '0';
    hum[0] = '0';
  }
  char queryString[128];
  char outBuf[128];
  char host[64];

  if (client.connect(serverName, 80)) 
  {
    sprintf(queryString,"apiKey=%s&temperature=%s&humidity=%s",apiKey, temp,hum);
    sprintf(outBuf,"GET /collect/%s/?%s HTTP/1.1",serial,queryString);
    client.println(outBuf);
    sprintf(host,"Host: %s",serverName);
    client.println(host);
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();
    client.stop();
  }
  else
  {
    Serial.println(F("Connection failure"));
  } 
}

}

 

Tags : 201802, W5100, Ethernet, Arduino, DHT11

COMMENTS

Please Login to comment
  Subscribe  
Notify of