上一篇介紹硬體的溫斯度讀取,看是沒有相關其實今天要來介紹真正上雲端的部分!實際把家裡的溫度濕度傳送到雲端。
今天使用簡單的開放平台 ThingSpeak 來作為上傳的雲端。
當然先輸入電子郵件位置注冊。
ThingSpeak 注冊
ThingSpeak 注冊信確認
登入後首先看到的是 My Channels(我的頻道),他的概念是每個項目就是一個頻道。
我們還可以看到右邊有一些相關資訊與範例連結。
ThingSpeak 我的頻道
點擊 New Channels (新增頻道)
ThingSpeak 新增頻道
ThingSpeak 新增頻道 表格
新增後看到 整個頻道控制台/儀表板。
ThingSpeak 頻道控制台/儀表板
這邊我們先看一下重要資訊 API Keys 這是等等我們要發送資料的重點。那除了一般 Http API(REST API.)他也有額外提供 MQTT API.
ThingSpeak API Keys
點擊 Account 的 Profile 這頁面才有提供 MQTT API 需要的 Key.
ThingSpeak My Profile
現在介紹實際發送資料部分,我們先讓 ESP8266 連上網路 WIFI。
#include <Arduino.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#ifndef STASSID
#define STASSID "sdpower"
#define STAPSK "**********"
#endif
const char *ssid = STASSID;
const char *password = STAPSK;
const char *host = "api.thingspeak.com";
void setup()
{
Serial.begin(115200);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
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());
}
// the loop function runs over and over again forever
void loop()
{
delay(1000);
}
看到下圖表示你成功連線並取的 IP。
esp8266 連上網路
這邊實際使用 PlatformIO 安裝 ThingSpeak 程式庫加速。
ThingSpeak 程式庫
THINGSPEAK_DELAY 是因為資料最小發送時間為 15 秒內只能一筆。
#include <Arduino.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
#include "secrets.h"
#ifndef STASSID
#define STASSID "sdpower"
#define STAPSK "********"
#endif
#define THINGSPEAK_DELAY 15000
#define DEFAULT_TIMEOUT 300
#define AHT1X_CMD_SIZE 3
#define AHT1X_DATA_SIZE 6
#define AHT1X_T_MEASUREMENT_DELAY 60
#define AHT1X_RH_MEASUREMENT_DELAY 20
#define AHT1X_RESET_DURATION 20
#define AHT1X_READ 0xAC /* t and RH Calibrated Measurement 1010 1100 */
#define AHT1X_READ_DATA0 0x33
#define AHT1X_READ_DATA1 0x00
#define AHT1X_INIT 0xE1 /* Init: 1110 0001 */
#define AHT1X_INIT_DATA0 0x08
#define AHT1X_INIT_DATA1 0x00
#define AHT1X_RESET 0xBA /* Reset: 1011 1010 */
uint8_t readBuffer[17] = {0};
uint8_t writeBuffer[3] = {0};
uint32_t timeout, loop_timeout;
float hum = 0;
float temp = 0;
const char *ssid = STASSID;
const char *password = STAPSK;
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char *myWriteAPIKey = SECRET_WRITE_APIKEY;
void setup()
{
Serial.begin(115200);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
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());
//AHT10 setup
Wire.begin();
Wire.beginTransmission(0x38);
Wire.write(AHT1X_RESET);
Wire.endTransmission();
delay(AHT1X_RESET_DURATION);
Wire.beginTransmission(0x38);
writeBuffer[0] = AHT1X_INIT;
writeBuffer[1] = AHT1X_INIT_DATA0;
writeBuffer[2] = AHT1X_INIT_DATA1;
for (int i = 0; i < AHT1X_CMD_SIZE; i++)
{
Wire.write(writeBuffer[i]);
}
Wire.endTransmission();
loop_timeout = millis();
}
void getTempHum()
{
uint32_t xresult;
Wire.beginTransmission(0x38);
writeBuffer[0] = AHT1X_READ;
writeBuffer[1] = AHT1X_READ_DATA0;
writeBuffer[2] = AHT1X_READ_DATA1;
for (int i = 0; i < AHT1X_CMD_SIZE; i++)
{
Wire.write(writeBuffer[i]);
}
Wire.endTransmission();
delay(AHT1X_RH_MEASUREMENT_DELAY);
Wire.requestFrom((uint8_t)0x38, (uint8_t)AHT1X_DATA_SIZE);
timeout = millis() + DEFAULT_TIMEOUT;
while (millis() < timeout)
{
if (Wire.available() < AHT1X_DATA_SIZE)
{
delay(AHT1X_RH_MEASUREMENT_DELAY / 4);
}
else
{
for (int i = 0; i < AHT1X_DATA_SIZE; i++)
{
readBuffer[i] = Wire.read();
}
xresult = (((uint32_t)readBuffer[1] << 16) | ((uint32_t)readBuffer[2] << 8) | (uint32_t)readBuffer[3]) >> 4;
hum = (float)xresult;
hum *= 100;
hum /= 1048576;
xresult = (((uint32_t)readBuffer[3] & 0x0F) << 16) | ((uint32_t)readBuffer[4] << 8) | (uint32_t)readBuffer[5];
temp = (float)xresult;
temp *= 200;
temp /= 1048576;
temp -= 50;
// Serial.println(String("") + "Humidity(%RH):\t\t" + hum + "%");
// Serial.println(String("") + "Temperature(C):\t" + temp + " C");
}
}
}
// the loop function runs over and over again forever
void loop()
{
//斷線重新連線
if (WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, password); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(3000);
}
Serial.println("\n WIFI Connected.");
}
if (millis() > loop_timeout)
{
getTempHum();
if ((temp > -50) && (hum > 0))
{
Serial.println(String("") + "Humidity(%RH):\t\t" + hum + "%");
Serial.println(String("") + "Temperature(C):\t" + temp + " C");
ThingSpeak.setField(1, temp);
ThingSpeak.setField(2, hum);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (x == 200)
{
Serial.println("Channel update successful.");
}
else
{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
}
loop_timeout = millis() + THINGSPEAK_DELAY;
}
}
esp8266 發送資料中
這時回到我們的 Channels 就可以看到線圖!是不是很棒!
channel 圖表!
等等手機也可以看呢!
app
app 圖表!
今天主要介紹實際開始發送資料到雲端,是不是很棒!在公司可以看家裡的溫度!如果家裡有養寵物需要注意就可以監控是不是寵物白天在家會不會中暑。
這就是 IoT 魅力!
今天就到這了感謝收看