NodeMCU รับค่าอุณหภูมิจาก Sensor DHT11 และดึงค่าอุณหภูมิด้วย php api จากเว็บ openweathermap.org แบบ realtime มาเปรียบเทียบกัน โดยใช้ http get






แนวคิด

    จากบทความที่แล้ว เราสามารถเก็บอุณหภูมิและความชื้นจาก  DHT11  ลงในฐานข้อมูล Mysql ได้แล้ว (อ่านบทความเก่าได้ที่ https://havespirit.blogspot.com/2017/03/esp8266-dht11-mysql-thingspeak-line.html ) บทความนี้เราจะมาต่อยอด คือ  เราเอาค่าอุณหภูมิจาก DHT11 ภายในห้อง  มาเปรียบเทียบกับอุณหภูมิภายนอก จากข้อมูล openweathermap.org  ด้วย API เขียนด้วย PHP  โดยใช้ method http get  โดยอ่านค่าแบบ real time

อุณหภูมิจาก DHT 11  -->  อุณหภูมิภายในห้อง
อุณหภูมิจาก openweathermap.org -> อุณหภูมิภายนอกห้อง (ดึงมาแบบ realtime โดยใส่ละติจูดและลองติจูดเข้าไปเพื่อความแม่นยำ)


สาเหตุที่ทำไมไม่ใช้ sensor รับอุณหภูมิอีกตัวติดตั้งภายนอกแทนที่จากดึงจากเว็บ

1.ต้องเพิ่ม sensor ที่นอกห้องเพื่อวัดอุณหภูมิ และต้องต่อสายยาวๆ เข้ามาในห้องซึ่งยุ่งยากต้องมีการเจาะช่องสายสัญญาณ ซึ่งรวมๆ แล้ว ไม่เหมาะ อาจเจอฝน ฝุ่น ทำให้ค่าอุณหภูมิไม่แม่นยำ และเนื่องจากกายภาพของห้องไม่เหมาะ
2.ต้องการใช้ประโยชน์จากเว็บที่บริการอยู่แล้ว เช่น openweathermap หรือ google weather โดยจะต่อยอดนำพยากรณ์อากาศล่วงหน้ามาใช้  เช่น อีก 3 วัน จะมีอากาศร้อนจัดเป็นเวลาหลายวัน ก็จะงดทำการซ่อมบำรุงเครื่องปรับอากาศในช่วงนั้นเป็นต้น
3.มีพื้นฐาน php อยู่แล้ว เลยเลือกใช้ php ดึงค่า
4.สามารถนำแนวคิดนี้ไปต่อยอดอื่นๆได้ เช่น ใช้ nodemcu สั่งงานเปิดแอร์ผ่าน IR โดยดึงค่าจากระบบจองห้องประชุมของที่ทำงาน ที่เขียนด้วย php ซึ่งก่อนเวลากำหนดการใช้ห้อง 15 นาที แอร์จะถูกเปิดโดยอัตโนมัติ โดย nodemcu จะไปอ่านค่าจาก php ซึ่งดึงมาจากฐานข้อมูลของระบบจองห้องประชุม มาสั่งงานเปิดแอร์โดยอัตโนมัติ



ความสามารถของโปรแกรม

1. รับค่่าอุณหภูมิและความชื้นจาก  DHT11

2.ดึงค่าอุณหภูมิจาก openweathermap.org แบบ Realtime มาเปรียบเทียบกับอุณหภูมิของเรา โดยใช้ http get

3.นำไปต่อยอดได้ครับ เช่น ถ้าค่าต่างกัน 1 องศา  จะให้ทำอะไร โดยใช้ if เข้ามาช่วย เช่น ถ้าอุณหภูมิต่างกันแค่ 1 องศา ก็ให้เปิดแอร์อีก 1 ตัว หรือแจ้งเตือน เป็นต้น



วิธีการนำข้อมูลสภาพอากาศจาก openweathermap.org มาใช้
1.เข้าไปที่ https://home.openweathermap.org/ สมัครใช้งาน เสร็จแล้ว เลือก api  เราจะนำ key มาใช้ในข้อ 3 ครับ
 
2.หาละติจูด ลองติจูด ของตำแหน่งที่เราต้องการ จากที่นี่ครับ

 โดยเราจะนำค่าที่ได้ทั้ง 2 ค่า (ละติจูดและลองติจูด) ไปใส่ในข้อ 3 ครับ

 http://www.dpt.go.th/suphanburi/toc/10/102PlaceSuphanburiGeoRSS.html



3.code php

  ***นำค่า key ที่ได้ในข้อ 1 มาใส่แทนตรง appid ครับ แทนที่คำว่าใส่ key
 ***นำค่าที่ได้ในข้อ 2 ทั้ง 2 ค่า (ละติจูดและลองติจูด) มาใส่ใน code php ครับ

ตัวอย่างค่าที่ได้จากข้อ 2
เส้นรุ้ง ละติจูด Latitude =13.78791, เส้นแวง ลองจิจูด Longitude =100.53839

  ตั้งชื่อเป็น weather_outsite.php  บันทึกใน folder   /htdoc/arduino/

<?php

$jsonurl = "http://api.openweathermap.org/data/2.5/weather?q=Bangkok&l=13.79118,100.54142&appid=ใส่ key ";
$json = file_get_contents($jsonurl);
$weather = json_decode($json);

$kelvin = $weather->main->temp;
$celcius = $kelvin - 273.15;
echo "temp = ".$celcius;

?>

4. เขียนโปรแกรม arduino ide ครับ
  โดยให้ต่อ ขา input ของ DHT11 หรือ DHT22เข้าทาง D5

////
#include "DHT.h"
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid     = "SSID"; //Set Wifi SSID
const char* password = "PASSWORD";//Set Wifi password

char tempF[6]; // buffer for temp incl. decimal point & possible minus sign


#define DHTPIN D5     // wDhat digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE); // กำหนดให้ SENSOR อุณหภูมิความชื่น เข้าทาง D5


int status = WL_IDLE_STATUS;


char serverip[]="10.10.10.10";
WiFiClient client;

unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds

const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds

int value;
int Diff;

void setup() {
    Serial.begin(115200);
   //pinMode(LED_PIN , OUTPUT);
   //digitalWrite(LED_PIN, LOW);

   WiFi.begin(ssid, password);

  //Set WiFi mode
  //You can choose between WIFI_AP, WIFI_STA, WIFI_AP_STA or WIFI_OFF
  WiFi.mode(WIFI_STA);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  }

void loop()
{

  float t = dht.readTemperature();
 
 // Serial.print("temp insite: ");
//  Serial.print(t);
 // Serial.print(" *C ");
 // Serial.println();



  if (client.available()) {

 
    Serial.println("Wait to Read data");

  }
 
  if (millis() - lastConnectionTime > postingInterval) {

    httpRequest();

    if(client.find("")){

      client.find("temp =");

      value = client.parseFloat();

      Diff = value - t;

      Serial.print("temp_insite: ");
      Serial.print(t);
      Serial.print(" *C ");
      Serial.println();

      Serial.print("temp_outsite = ");
      Serial.print(value);
      Serial.print(" *C ");
      Serial.println();
   
      Serial.print("temp_outsite - temp_insite = ");
      Serial.println(Diff);
      Serial.println();
      /*
      if( value >= 29 ){
         Serial.println(" data is too hot ");
      }else{
         Serial.println(" data is Cool ");
      }
       */
   
   
    }

  }
 
   delay(2000);

}

void httpRequest() {

  client.stop();

  if (client.connect(serverip, 80)) {

    Serial.println("Waiting  for get data ...");

    client.println("GET /arduino/weather_outsite.php HTTP/1.1");  // PATH FOR GET DATA

    client.println("Host: serverip");

    client.println("User-Agent: arduino");

    client.println("Connection: close");

    client.println();

    lastConnectionTime = millis();

  } else {

    Serial.println("connection failed");

  }

}




/////




ความคิดเห็น

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

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

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

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

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