Week03 : Arduino IoTs – ThingSpeak Interface
ทฤษฏี
Thingspeak
เป็นเว็ปที่ให้การบริการในการเก็บข้อมูล และสามารถแสดงข้อมูลแบบ real-time
ได้ ซึ้งเราสามารถ update ข้อมูล
หรือจะเรียกดูข้อมูลได้ตลอดเวลา ที่ไหนก็ได้ เพราะทำงานบน cloud ซึ่ง thingspeak สร้างมาเพื่อต้องการให้ตอบโจทย์ของ IoT
อยู่แล้ว ส่วนข้อมูลที่เก็บอยู่บน cloud นั้นก็ขึ้นอยู่กับเราว่าจะใช้ยังไง
รูปแบบไหน ในการที่จะส่งข้อมูล data ไปไว้บน cloud นั้น ทาง thingspeak ได้มี api ในการติดต่อไว้เรียบร้อยแล้ว
การเปิดใช้งาน Thinkspeak
- อันดับแรกเลย ให้ทำการสมัครสมาชิกให้เรียบร้อย
- จากนั้นก็สร้าง channel ขึ้นมา โดยให้เรากดไปที่ My channel แล้วก็ new channel ขึ้นมา
- หลังจากที่ new channel ขึ้นมาแล้ว ไปที่ channel setting ก็ป้อนข้อมูลเข้าไป
- อย่าลืมเลือก Make Public
- เสร็จแล้วก็กด save channel
- เมื่อสร้างเสร็จแล้ว ก็จะแสดงหน้าต่าง ในหน้าต่างนี้จะแสดงข้อมูลเป็นแบบเส้นกราฟ โดยมี Field ทั้งหมด 2 Field คือ Temperature และ Humidity แล้ว กด save channel
- และขั้นตอนต่อมาคือการเขียนโค้ดลงบน nodeMCU ESP8266 ภายในโค้ดจะต้อง กำหนดคีย์ให้ตรงกับ channel ที่เราต้องการจะส่งข้อมูลไป และเมื่อเรียบร้อยแล้วจะได้ข้อมูลดังนี้

นี่คือหน้าตาที่เรารับข้อมูลเข้ามา โดยข้อมูลได้ตั้งค่าไว้ว่าจะรับข้อมูลทุกๆ 30 วินาที
การ update ข้อมูลไปยัง cloud ผ่าน api ของ thingspeak
- อันดับแรกให้ดูที่ API KEY ของเราว่ามันคืออะไร
- Write API Key 4741AGU8WGCJP0J5
- Read API Keys TYJ1J2AU2OV7Q0U8

- ในการ update ข้อมูลจะเป็นการส่ง HTTP Request ไปยัง server เพื่อ update ข้อมูลที่ต้องการตาม
https://api.thingspeak.com/update?key=4741AGU8WGCJP0J5&field1= 0 โดยที่
field1=0 คือ field ที่เราต้องการ update ในตัวอย่างคือ field ที่ 1 และ กา หนดให้มีค่าเท่ากับ 0

ตัวอย่างโค๊ด เขียนโปรแกรมให้กับ NodeMcu ให้ส่งข้อมูลไป update
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D3
const char* ssid = “testVirus”; // กำหนด SSID (อย่าลืมแก้เป็นของตัวเอง)
const char* password = “1510031510”; // กำหนด Password(อย่าลืมแก้เป็นของตัวเอง)”;
String apiKey = “4741AGU8WGCJP0J5”;
const char* server = “api.thingspeak.com”;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
WiFiClient client;
void setup()
{ Serial.begin(115200);
delay(1000);
sensors.begin();
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());}
void loop()
{ Serial.println(“————————-“);
Serial.print(“Requesting temperatures…”);
sensors.requestTemperatures(); // Send the command to get temperatures
float cTemp = sensors.getTempCByIndex(0);
Serial.print(“Temperature is: “); Serial.println(cTemp, 4); // Display Temperature
if (client.connect(server, 80)) // “184.106.153.149” or api.thingspeak.com
{ String postStr = apiKey;
postStr += “&field1=”;
postStr += String(cTemp);
postStr += “\r\n\r\n”;
client.print(“POST /update HTTP/1.1\n”);
client.print(“Host: api.thingspeak.com\n”);
client.print(“Connection: close\n”);
client.print(“X-THINGSPEAKAPIKEY: ” + apiKey + “\n”);
client.print(“Content-Type: application/x-www-form-urlencoded\n”);
client.print(“Content-Length: “);
client.print(postStr.length());
client.print(“\n\n”);
client.print(postStr);
Serial.print(“Temperature(‘C)= “);
Serial.println(cTemp, 4);
}
client.stop();
นอกจากนี้เรายังสามารถทำการทดลองการเปิด-ปิด ไฟ, การอ่านค่า input จาก switch การ อ่านค่า Humidity และ Temperature ผ่าน Internet โดยใช้ Anto เป็น Broker อีกด้วย
[อ้างอิง : https://nazt-cmmc.gitbooks.io/cmmc-nectec-iot-camp-2016/content/thingspeak.html
https://jellywaranya.wordpress.com/2017/05/07/blog-post-title/ ]
_______________________________________________________________________
ไม่มีความคิดเห็น:
แสดงความคิดเห็น