作者:OREO | 更新時間:2020-04-09 | 瀏覽量:1921
庫鏈接:https://github.com/OREOCODETECH/ArduinoBigiot
用法:以Blink例程為例
[code]
#include <ESP8266WiFi.h>
#include "ArduinoBigiot.h"
#ifndef STASSID
#define STASSID "your-ssid"//你的WiFi名稱
#define STAPSK "your-password"//你的WiFi密碼
#endif
String DEVICEID = "deviceid";//設(shè)備ID,在http://m.placeboworld.cn/User/listDev.html中查看
String APIKEY = "apikey";//設(shè)備APIKEY,在http://m.placeboworld.cn/User/listDev.html中查看
const char* host = "m.placeboworld.cn";//貝殼服務(wù)器域名m.placeboworld.cn
const uint16_t port = 8181;//端口
#define LED_PIN 2//LED GPIO管腳
const char* ssid = STASSID;
const char* password = STAPSK;
WiFiClient client;
ArduinoBigiot iot;
void handlePlay() {//為"play"命令創(chuàng)建一個回調(diào)函數(shù),即ESP收到"play"命令時執(zhí)行函數(shù)中的代碼
digitalWrite(LED_PIN, LOW);//開啟LED
Serial.println("Nice day ! ( > v < )");
}
void handleStop() {//為"stop"命令創(chuàng)建一個回調(diào)函數(shù),即ESP收到"stop"命令時執(zhí)行函數(shù)中的代碼
digitalWrite(LED_PIN, HIGH);//關(guān)閉LED
Serial.println("Good night!( = v = )");
}
void handleOffOn() {//為"offOn"命令創(chuàng)建一個回調(diào)函數(shù),即ESP收到"offOn"命令時執(zhí)行函數(shù)中的代碼
digitalWrite(LED_PIN, !digitalRead(LED_PIN));//反轉(zhuǎn)GPIO狀態(tài)
Serial.println(" Switch ! ( O v O )");
}
void setup() {
Serial.begin(115200);
Serial.println();
pinMode(LED_PIN, OUTPUT);//初始化LED GPIO腳為輸出腳
digitalWrite(LED_PIN, HIGH);//關(guān)閉LED 注意:在此例程中默認(rèn)GPIO高電平為關(guān)燈,低電平為開燈
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);//設(shè)置WiFi模式為STA模式
WiFi.begin(ssid, password);//連接WiFi
while (WiFi.status() != WL_CONNECTED) {//程序?qū)⒌却敝罻iFi連接成功
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Connecting to ");
Serial.print(host);
Serial.print(":");
Serial.println(port);
while (!client.connect(host, port)) {//連接服務(wù)器,程序?qū)⒌却敝练?wù)器連接成功
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Host connected");
Serial.println("Iot services init");
iot.begin(client, DEVICEID, APIKEY);//初始化ArduinoBigiot,并設(shè)置設(shè)備ID和設(shè)備APIKEY
iot.checkout();//強(qiáng)制目標(biāo)設(shè)備下線,消除滯留
iot.checkin();//設(shè)備登錄
iot.offOn(handleOffOn);//為"offOn"命令綁定"handleOffOn"函數(shù),即上面創(chuàng)建的"handleOffOn"回調(diào)函數(shù)
iot.play(handlePlay);//為"play"命令綁定"handlePlay"函數(shù),即上面創(chuàng)建的"handlePlay"回調(diào)函數(shù)
iot.stop(handleStop);//為"stop"命令綁定"handleStop"函數(shù),即上面創(chuàng)建的"handleStop"回調(diào)函數(shù)
Serial.println("Iot services started");
}
void loop() {
iot.handleiot();//?;钤O(shè)備,處理連接信息
delay(50);
}
[/code]
第一眼看著很多,其實仔細(xì)看你會發(fā)現(xiàn)大部分代碼都是串口打?。ㄐ?/p>
更改完信息并上傳至ESP后,嘗試在設(shè)備列表中對應(yīng)的遙控面板(或小程序等)點擊play或stop或offOn,觀察板載LED燈變化,修改相應(yīng)回調(diào)函數(shù)中的代碼即可改變ESP在收到命令后的動作。