วันศุกร์ที่ 8 กุมภาพันธ์ พ.ศ. 2562

ส่วนประกอบงานวิชา เขียนโปรแกรม ตัวอย่างงานที่ 18

ส่วนประกอบงานวิชา  เขียนโปรแกรม ตัวอย่างงานที่ 18

ใบรายงานผลการปฏิบัติงาน

วัดอุณหภูมิ ด้วย Esp8266 อัพค่าขึ้น thingspeak.com กันเถอะ

เมื่อ 2 ปีที่ผ่านมา
โดย เจ้าของร้าน

สวัสดีครับ วันนี้เราจะมา อธิบายการส่งค่า อุณหภูมิ ไปยัง thingspeak.com เพื่อเก็บข้อมูล โดยใช้ NodeMCU Esp8266 และ DHT11 เก็บค่าอุณหภูมิ เป็นกราฟ ครับ
เริ่มต้นจาก สมัคร สมาชิกเว็บไซต์ thingspeak.com อ่านบทความเพิ่มเติมได้ที่นี้
อุปกรณ์ที่ต้องใช้ มีดังต่อไปนี้
มาเริ่มต้นสร้างช่อง Channel บน thingspeak.com
1. เริ่มจาก Login เข้าระบบ แล้ว กดที่ปุ่ม New Channel

2. จากนั้น ตั้งค่าช่องของคุณตามต้องการ (ข้อมูลหลักๆ ที่ต้องการ ดังนี้)
  • Name ชื่อช่อง Channel
  • Description คำอธิบาย
  • Field 1 - Field 8 คือ ชื่อของตาราง thingspeak.com ยอมให้สร้างได้ 8 กราฟ
  • Make Public ต้องการให้แสดงเป็น สาธารณะหรือไม่
3. เมื่อสร้างช่อง Channel เสร็จคราวนี้ ก็คลิกไปยังช่องที่เราสร้างขึ้นแล้วกด ที่แถบ API Key

หลังจากนั้นให้เรา จำ ที่ Write API Key จากรูปคือ RRHS37ETW76RFAWB  เก็บค่านี้ไว้นำไปแก้ใน Code ของ Nodemcu esp8266
คราวนี้ต่อวงจรกัน ใช้งานดังนี้
NodeMCU Esp 8266Sensor DHT11
Pin VCC 5VVCC
Pin GndGnd
D4Out

 Code ตัวอย่าง
#include <ESP8266WiFi.h>
#include "DHT.h"

#define DHTPIN D4   // ขา Out ของ Sensor ต่อเข้าขา D4 ของ Esp8266

//เลือกชนิดของ Sensor
#define DHTTYPE DHT11     //DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

const char* ssid     = "SSID Wifi";    // SSID Wifi
const char* password = "Password Wifi";   // Password Wifi

const char* host = "api.thingspeak.com";    // Host ของ thingspeak ไม่ต้องแก้ไข
const char* api   = "RRHS37ETW76RFAWB";  //API Key ที่เราจำไว้ ในขั้นต้นเมื่อกี้

void setup() {
  Serial.begin(9600);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("DHTxx test!");
  dht.begin();
}

int value = 0;

void loop() {
  delay(5000);
  ++value;
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  float hif = dht.computeHeatIndex(f, h);
  float hic = dht.computeHeatIndex(t, h, false);
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");

  Serial.print("connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  
  // We now create a URI for the request
  String url = "/update?api_key=";
  url += api;
  url += "&field1=";
  url += t;
  url += "&field2=";  
  url += h;
// เราจะส่งข้อมูล https://api.thingspeak.com/update?api_key=RRHS37ETW76RFAWB&field1=(อุณหภูมิ)&field2=(ความชื่น)
  
  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  delay(10);
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("closing connection");
}
หลังจากนั้น เราก็จะได้กราฟ ค่าอุณหภูมิ
เพิ่มเติม ครับ เราสามารถนำกราฟนี้ไปใส่ยังเว็บไซต์ ที่เราสร้างขึ้นมาเอง ได้ด้วย แต่ต้องเปิดใช้ งาน Public (สาธารณะด้วย) วิธีการก็คือ กดที่ปุ่ม ดังรูปของกราฟ

ไม่มีความคิดเห็น:

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