WEEK 3 Arduino IoTs – ThingSpeak Interface


สมัครเข้าใช้งาน ThingSpeak >> https://thingspeak.com/

ถ้าใครไม่มีก็ให้สมัครใหม่ แต่ตามนี้คือมีอยู่แล้ว ทำการเข้าสู่ระบบ



ไปสร้างช่องใหม่ตามรูป


ทำการใส่ name และ field


เสร็จแล้วเราจะได้ test_pea ของเรามา



จากนั้นทำการหาkey ของเรามาใส่ในโค้ช


นี่คือโค้ช Arduino ที่เราใช้

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <Wire.h>
#include "ClosedCube_HDC1080.h"
ClosedCube_HDC1080 hdc1080;
int StepCount = 1;
String thinkKey = "1FTHIKG2UN4FPNUS";
const char* ssid = "kk";
const char* password = "12345678";
const char* host = "api.thingspeak.com";
const int httpsPort = 443;
// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C";
void setup() {
Serial.begin(115200);
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());
// Use WiFiClientSecure class to create TLS connection
hdc1080.begin(0x40);
}
void loop() {
//https://api.thingspeak.com/update…
if (StepCount % 30 == 0) {
WiFiClientSecure client;
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
if (client.verify(fingerprint, host)) {
Serial.println("certificate matches");
} else {
Serial.println("certificate doesn't match");
}
String url = "/update?api_key=" + thinkKey + "&field1=";
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + hdc1080.readTemperature() + "&field2=" + hdc1080.readHumidity() + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
if (line.toInt()>0) {
Serial.println("Push Sucess !!");
} else {
Serial.println("Fail!!!");
}
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
}
delay(100);
Serial.print("Time: ");
Serial.println(StepCount++);
}


สุดท้ายเราจะได้โปรแกรมที่เสร็จมา

























ความคิดเห็น

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

WEEK 1 Arduino – Basic I/O (Digital Input/Output, Shields, Sensors)

Raspberry Pi III คืออะไร

WEEK 9 NI labview 2014