การประยุกต์ใช้ arduino ESP8266 วัดอุณหภูมิห้อง server พร้อมเก็บข้อมูลบน cloud thingspeak เพื่อทำกราฟ และแจ้งเตือนผ่านไลน์




แนวคิด

   นำ arduino มาวัดอุณหภูมิห้อง server พร้อมกับ ส่งข้อมูลไปยัง cloud thingspeak เผื่อจัดเก็บข้อมูลไว้ดูย้อนหลังพร้อมทำกราฟออกมาแบบสวยงาม  และแจ้งเตือนหากห้อง server มีอุณหภูมิสูงเกินกว่าที่กำหนด  ในตัวอย่างใส่ไว้ 24.50 องศาเซลเซียส ถ้าต่ำกว่านั้นจะแจ้งเตือนผ่าน line group






ก่อนใช้ thingspeak ต้องสมัครใช้งานก่อนครับ ฟรี https://thingspeak.com/
thingspeak เป็น cloud server ที่ยอมให้รับเก็บข้อมูลดูสามารถ ทำกราฟดูย้อนหลังได้ด้วย ซึ่งในบทความนี้เราจะมาใช้เก็บข้อมูลอุณหภูมิห้อง ซึ่งรับค่าจาก sensor temperatory จาก arduino  ESP8266 ขา D5





ส่วน Linenotify  ไปสมัครใช้ฟรีได้ที่ https://notify-bot.line.me/th/  ส่วนขั้นตอน สามารถหาอ่านบทความเก่าที่ผมเขียนได้ที่ https://havespirit.blogspot.com/2017/02/line-notify-php.html


หน้าจอกราฟบน thingspeak  แบบ REALTIME  โดยเข้าไปที่ https://thingspeak.com/  ไปสมัครใช้งานได้ฟรีๆ แล้้วสร้าง new channel แล้วเอา API KEY มาใช้

 https://thingspeak.com/channels/227532






/////////////////////////////////////   sourcode  arduino  ide   /////////////////////////////////////////


#include "ThingSpeak.h"
#include <ESP8266WiFi.h>

#include <OneWire.h>
#include <DallasTemperature.h>
#include <stdlib.h>

#define ONE_WIRE_BUS D5

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

const char* ssid     = "ap_wifi"; //Set Wifi SSID
const char* password = "11234";//Set Wifi password
WiFiClient client;

unsigned long myChannelNumber = 227532;  /// thingspeak channel id จาก thingspeak
const char * myWriteAPIKey = "thingspeak api key";  // ได้มาจาก thingspeak

#define LINE_TOKEN "linetoken"  //  line token ได้มาจาก line notify

String message3;
String message4;
float M;
float t ;
char tempF[6]; // buffer for temp incl. decimal point & possible minus sign

void setup() {
  Serial.begin(115200);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin(); // การ start Sensors

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  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());
  ThingSpeak.begin(client);

}

void loop() {

 Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  // After we got the temperatures, we can print them here.
  // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(sensors.getTempCByIndex(0));  // C คือ เซลเ


   ThingSpeak.writeField(myChannelNumber, 1, sensors.getTempCByIndex(0), myWriteAPIKey);
  delay(20000); // ThingSpeak will only accept updates every 15 seconds.

message3 = dtostrf(sensors.getTempCByIndex(0), 6, 2, tempF);
if(sensors.getTempCByIndex(0) < 24.50){
message4 = "%E0%B8%AD%E0%B8%B8%E0%B8%93%E0%B8%AB%E0%B8%A0%E0%B8%B9%E0%B8%A1%E0%B8%B4%E0%B8%AB%E0%B9%89%E0%B8%AD%E0%B8%87%E0%B8%AD%E0%B8%9A%E0%B8%A3%E0%B8%A1%20(%E0%B9%80%E0%B8%8B%E0%B8%A5%E0%B9%80%E0%B8%8B%E0%B8%B5%E0%B8%A2%E0%B8%AA) "+ message3;
//Line_Notify(dtostrf(sensors.getTempCByIndex(0), 6, 2, tempF));
Line_Notify(message4);
}else{
   Serial.println(sensors.getTempCByIndex(0));
}

}

void Line_Notify(String message) {
 WiFiClientSecure client;

if (!client.connect("notify-api.line.me", 443)) {
 Serial.println("connection failed");
 return;
 }

String req = "";
 req += "POST /api/notify HTTP/1.1\r\n";
 req += "Host: notify-api.line.me\r\n";
 req += "Authorization: Bearer " + String(LINE_TOKEN) + "\r\n";
 req += "Cache-Control: no-cache\r\n";
 req += "User-Agent: ESP8266\r\n";
 req += "Content-Type: application/x-www-form-urlencoded\r\n";
 req += "Content-Length: " + String(String("message=" + message).length()) + "\r\n";
 req += "\r\n";
 req += "message=" + message;
 Serial.println(req);
 client.print(req);
 delay(2000);

Serial.println("-------------");
 while (client.connected()) {
 String line = client.readStringUntil('\n');
 if (line == "\r") {
 break;
 }
 Serial.println(line);
 }
 Serial.println("-------------");
}



//////////////////////////////////////////////////////////////////

ความคิดเห็น

  1. รบกวนสอบถามครับ ผมต่อสาย data เข้าที่ขา D5 แล้วแต่ว่ามันวัดออกมาได้ -127 ตลอดเลย ลองเปลี่ยนเป็นขาอื่นๆ และแก้ไขโค้ดตามก็เหมือนเดิมเป็น -127 ตลอดเลยครับ มีวิธีแก้ไขไหม ขอบคุณครับ

    ตอบลบ
  2. ขอบคุณมากๆครับ กำลังหาวิธีพอดีเลยครับ

    ตอบลบ
  3. ไม่ทราบแก้ยังไง ติดลบ อะ ครับ

    ตอบลบ
  4. Error #include "ThingSpeak.h" ทำไงดีครับผม

    ตอบลบ
  5. ขอบพระคุณสำหรับความรู้นะครับ ผมทำสำเร็จเรียบร้อย บบรจุในกล่อง
    ท่านใดสนใจ 0879481314 วชิระ
    ราคาพร้อมชุด 5,000 บาท พร้อมใช้ครับ
    *******************
    ขอบคุณครับ

    ตอบลบ
  6. ถ้า​ จะเกาะ​ ssid wifi แบบยืนยันตัวตนด้วย​ login เยจะใช้​macaddress wifi ต้องเขียน​ codeยังไงครับ

    ตอบลบ
  7. สอบถามหน่อยครับ ถ้าทำตามนี้ อุปกรณ์ที่ต้องใช้มีอะไรบ้างครับ แนะนำหน่อย มือใหม่อยากทำเอง

    ตอบลบ
  8. มีวงจรการต่อเปล่าครับ

    ตอบลบ
  9. ขอวงจร และ มันไม่แจ้งผ่าน Line ครับ

    ตอบลบ

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

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

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

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

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