Skip to content

Homemade mini Weather Stations

2014-07-12 11:34:06

What you will have to buy.

Extra library

2 steps to connect everything

  • Connect arduino with arduino ethernet shield
  • Connect DHT22 to arduino ethernet shield. (3.3V / 5V, GND, PIN2)

Arduino Code

//Ethernet
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "www.SERVER_URL.com";
IPAddress ip(192,168,0,177);

//DHT
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE);

char rhPct[6]; // buffer for humidity incl. decimal point
char tempF[6]; // buffer for temp incl. decimal point

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {

  //DHT22
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  dtostrf(h, 5, 2, rhPct);
  dtostrf(t, 5, 2, tempF);

  //data for send
  String data = "GET /";
  data += "add.php?h=";
  data += rhPct;
  data += "&t=";
  data += tempF;
  data += " HTTP/1.1";
  Serial.println(data);

  //ETHERNET
  sendDataToServerGET(data);
  delay(60L * 1000L);
}

void sendDataToServerGET(String data) {

  Serial.println("init function sendDataToServer");

  EthernetClient client;

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }

  delay(1000);
  Serial.println("connecting..."); 

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println(data);
    client.println("Host: louis.btbw.pl");
    client.println("Connection: close");
    client.println();
  } 
  else {
    Serial.println("connection failed");
  }

}

Php script

to adding information about humidity and temperature to database

<?php
    $h = $_GET['h'];
    $t = $_GET['t'];

    $db_user = 'USER_NAME';
    $db_pass = 'PASSWORD';
    $db = new PDO('mysql:host=DB_URL;dbname=DB_NAME', $db_user, $db_pass);

    $sql = "INSERT INTO dht22 ( humidity, temperature) VALUES ( :humidity, :temperature)";

    $query = $db->prepare($sql);
    $query->execute(array(':humidity' => $h, ':temperature' => $t));
?>

Database

CREATE TABLE IF NOT EXISTS `dht22` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `humidity` varchar(10) NOT NULL,
  `temperature` varchar(10) NOT NULL,
  `createat` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin2 AUTO_INCREMENT=41 ;