I wanted to capture data from the DHT11 and save it remotely into a DB. It can be used to send anything to a GET page. I didn’t put the function to get the temperature but I can if you ask me.
Some parameter need to be changed:
– YOUR_IP => XX.XX.XX.XX
– YOUR_IP_WITH_COMA => XX,XX,XX,XX
– YOUR_MAC_HEXA_WITH_COMA => 0xYY, 0xYY, 0xYY, 0x0E, 0x0C, 0xYY
#include <Ethernet.h>
#include <SPI.h>
#include "DHT.h"
#define DHTPIN 9
#define DHTTYPE DHT11
float h = 0;
float t = 0;
byte mac[] = {YOUR_MAC_HEXA_WITH_COMA};
IPAddress serverSQL(YOUR_IP_WITH_COMA);
EthernetClient sql;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("Starting...");
Ethernet.begin(mac);
dht.begin();
}
void loop() {
readTempHum();
delay(1000);
}
void get_request(float t, float h)
{
Serial.println("Connecting to Client...");
if (sql.connect(serverSQL, 80))
{
// Make a HTTP request:
sql.print("GET http://YOUR_IP/get.php?t=");
sql.print(t);
sql.print("&h=");
sql.println(h);
sql.println("HTTP/1.1");
sql.println();
Serial.println("Request sent!");
sql.stop();
}
else {
Serial.println("Client Connection Failed!");
}
}
// Show temp & humidity in the console
// & send it to the DB
void readTempHum() {
h = dht.readHumidity();
t = dht.readTemperature();
if(isnan(t) || isnan(h)) {
Serial.println( "Reading error!");
}
else {
Serial.print("Humidity :");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature :");
Serial.print(t);
Serial.println(" *C");
get_request(t,h);
}
} |
#include <Ethernet.h>
#include <SPI.h>
#include "DHT.h"
#define DHTPIN 9
#define DHTTYPE DHT11
float h = 0;
float t = 0;
byte mac[] = {YOUR_MAC_HEXA_WITH_COMA};
IPAddress serverSQL(YOUR_IP_WITH_COMA);
EthernetClient sql;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("Starting...");
Ethernet.begin(mac);
dht.begin();
}
void loop() {
readTempHum();
delay(1000);
}
void get_request(float t, float h)
{
Serial.println("Connecting to Client...");
if (sql.connect(serverSQL, 80))
{
// Make a HTTP request:
sql.print("GET http://YOUR_IP/get.php?t=");
sql.print(t);
sql.print("&h=");
sql.println(h);
sql.println("HTTP/1.1");
sql.println();
Serial.println("Request sent!");
sql.stop();
}
else {
Serial.println("Client Connection Failed!");
}
}
// Show temp & humidity in the console
// & send it to the DB
void readTempHum() {
h = dht.readHumidity();
t = dht.readTemperature();
if(isnan(t) || isnan(h)) {
Serial.println( "Reading error!");
}
else {
Serial.print("Humidity :");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature :");
Serial.print(t);
Serial.println(" *C");
get_request(t,h);
}
}
The php part
<!--?php define("DB_HOST","XXXX"); define("DB_USER","XXXX"); define("DB_PASS","XXXX"); define("DB_NAME","XXXXX"); $GLOBALS['con_db'] = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die(exit()); $GLOBALS['con_db']--->set_charset("utf8");
$GLOBALS['con_db']->query("INSERT INTO historic VALUES(NULL,'".intval($_GET['t'])."','".intval($_GET['h'])."',CURRENT_TIMESTAMP);") or die($GLOBALS['con_db']->error . __LINE__);
$GLOBALS['con_db']->close();
?> |
<!--?php define("DB_HOST","XXXX"); define("DB_USER","XXXX"); define("DB_PASS","XXXX"); define("DB_NAME","XXXXX"); $GLOBALS['con_db'] = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die(exit()); $GLOBALS['con_db']--->set_charset("utf8");
$GLOBALS['con_db']->query("INSERT INTO historic VALUES(NULL,'".intval($_GET['t'])."','".intval($_GET['h'])."',CURRENT_TIMESTAMP);") or die($GLOBALS['con_db']->error . __LINE__);
$GLOBALS['con_db']->close();
?>
The MySQL part:
CREATE TABLE IF NOT EXISTS `historic` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`temperature` INT(11) NOT NULL,
`humidity` INT(11) NOT NULL,
`date` datetime NOT NULL,
UNIQUE KEY `id_2` (`id`),
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; |
CREATE TABLE IF NOT EXISTS `historic` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`temperature` int(11) NOT NULL,
`humidity` int(11) NOT NULL,
`date` datetime NOT NULL,
UNIQUE KEY `id_2` (`id`),
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;