Send data to MySQL DB (temperature with DHT11)

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);
      }
    }

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']-&gt;query("INSERT INTO historic VALUES(NULL,'".intval($_GET['t'])."','".intval($_GET['h'])."',CURRENT_TIMESTAMP);") or die($GLOBALS['con_db']-&gt;error . __LINE__);
 
$GLOBALS['con_db']-&gt;close();
?&gt;

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 ;

6 thoughts on “Send data to MySQL DB (temperature with DHT11)”

  1. HI

    Could you please kindly publish the entire code. I am trying to accomplish the same, however there is not much on the net with regards to using MySQL along with a DHT11 sensor.

  2. Hi,

    I updated the article with the entire Arduino code. If it doesn’t answer your question, could you be more specific?

    Don’t forget to import the library in the arduino software: Sketch/Import Library/Add Library

    Hope it helps

Leave a Reply

Your email address will not be published. Required fields are marked *