การนำข้อมูล อุณหภูมิและความชื้นจาก DHT11 หรือ DHT22 มาเก็บลงบนฐานข้อมูล Mysql ของตัวเอง


  แนวคิด

        คือเราสามารถส่งข้อมูลจาก arduino หรือ nodemcu ผ่านทางโปรโตคอล HTTP ด้วย 

method get หรือ post ได้ ดังนั้นเราก็สามารถส่งค่าต่างๆ ลงฐานข้อมูล MYSQL ผ่านทาง php ได้

ด้วยเหมือนกัน

ขั้นตอน

  ต้องทำการติดตั้ง Xampp หรือ Wamp ก่อน

1.สร้าง ฐานข้อมูล Mysql โดยเปิด http://localhost/phpmyadmin                     และสร้าง database ชื่อ arduino 

       จากนั้น  copy code ส่วนนี้ไปวางใน phpmyadmin ส่วนของ sql แล้ว run

        CREATE TABLE `temp` (
  `id` int(11) NOT NULL auto_increment,
  `temp` int(2) default NULL,
  `humidity` int(2) default NULL,
  `date` datetime default NULL,
  PRIMARY KEY  (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
   
   จาก code นี้ จะทำให้เราได้ตางรางเก็บข้อมูล อุณหภูมิ และ ความชื้น
    


2.สร้างไฟล์ php เพื่อ insert ข้อมูลลงฐานข้อมูล

   ?php

 date_default_timezone_set('Asia/Bangkok');

$servername ="localhost";
$username = "root";
$password = " ";
$dbname = "arduino";
$now = new DateTime();

$temp = $_REQUEST['temp'];
$humidity = $_REQUEST['humidity'];

$conn = mysql_connect("localhost","root","");
if (!$conn)
{
    die('Could not connect: ' . mysql_error());
}
$con_result = mysql_select_db($dbname, $conn);
if(!$con_result)
{
die('Could not connect to specific database: ' . mysql_error());
}

$datenow = $now->format("Y-m-d H:i:s");
$hvalue = $value;

$sql ="insert  into  temp  (id,temp,humidity,date) values ( null,$temp,$humidity,'$datenow')";
// echo $sql;


$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
echo "<h1 align=center>THE DATA HAS BEEN SENT!!</h1>";
mysql_close($conn);

?>


3. ในส่วนของ arduino IDE

นำส่วนนี้ไปไว้บนสุด   

#include <ESP8266HTTPClient.h>
String message3;
String message5;

char tempF[6];


จากนั้นนำ code ข้างล่างนี้ไปไว้ใสส่วนของ void loop()


//รับค่า อุณหภูมิมาเป็น  ตัวแปร   t
//รับค่า ความชื้นมาเป็น  ตัวแปร  h

message3 = dtostrf(t, 6, 2, tempF);
message5 = dtostrf(h, 6, 2, tempF);

HTTPClient http;
http.begin("http://192.168.1.4/arduino/index.php");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.POST("temp="+ message3 +"&humidity=" + message5);
//Serial.println(httpCode);
if(httpCode == HTTP_CODE_OK)
{
  
 Serial.println("Insert to database success !!!");
}
else
{
   Serial.println("Error in HTTP request");
}

http.end();

ความคิดเห็น

  1. ขึ้น Error แบบนี้คือยังไงอ่าครับผมแก้ไม่เป็นครับ


    Arduino: 1.8.2 (Mac OS X), Board: "Arduino/Genuino Uno"

    /Users/Tustzuza/Documents/Arduino/ide/ide.ino: In function 'void loop()':
    ide:30: error: 't' was not declared in this scope
    message3 = dtostrf(t, 6, 2, tempF);
    ^
    ide:31: error: 'h' was not declared in this scope
    message5 = dtostrf(h, 6, 2, tempF);
    ^
    ide:33: error: 'HTTPClient' was not declared in this scope
    HTTPClient http;
    ^
    ide:34: error: 'http' was not declared in this scope
    http.begin("http://192.168.89.151/arduino/index.php");
    ^
    ide:38: error: 'HTTP_CODE_OK' was not declared in this scope
    if(httpCode == HTTP_CODE_OK)
    ^
    exit status 1
    't' was not declared in this scope

    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.

    ตอบลบ
  2. ใน index นี่เขียนโค๊ดยังไงอะครับ

    ตอบลบ

แสดงความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

แจ้งเตือนเข้าไลน์กลุ่ม ผ่าน Line notify เมื่อมีคน login เข้า server ของเราผ่าน SSH (linux) หรือ remote desktop เข้ามา (windows server)

การทำ cloud iot ด้วย thingsboard ไว้ใช้เองครับ

การประยุกต์ใช้ line notify ในการแจ้งปัญหาการใช้งาน สำหรับ php