作者:OREO | 更新時間:2020-04-09 | 瀏覽量:1301
庫鏈接: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";//設備ID,在http://www.smgyp.com/User/listDev.html中查看
String APIKEY = "apikey";//設備APIKEY,在http://www.smgyp.com/User/listDev.html中查看
const char* host = "www.smgyp.com";//貝殼服務器域名www.smgyp.com
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"命令創建一個回調函數,即ESP收到"play"命令時執行函數中的代碼
digitalWrite(LED_PIN, LOW);//開啟LED
Serial.println("Nice day ! ( > v < )");
}
void handleStop() {//為"stop"命令創建一個回調函數,即ESP收到"stop"命令時執行函數中的代碼
digitalWrite(LED_PIN, HIGH);//關閉LED
Serial.println("Good night!( = v = )");
}
void handleOffOn() {//為"offOn"命令創建一個回調函數,即ESP收到"offOn"命令時執行函數中的代碼
digitalWrite(LED_PIN, !digitalRead(LED_PIN));//反轉GPIO狀態
Serial.println(" Switch ! ( O v O )");
}
void setup() {
Serial.begin(115200);
Serial.println();
pinMode(LED_PIN, OUTPUT);//初始化LED GPIO腳為輸出腳
digitalWrite(LED_PIN, HIGH);//關閉LED 注意:在此例程中默認GPIO高電平為關燈,低電平為開燈
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);//設置WiFi模式為STA模式
WiFi.begin(ssid, password);//連接WiFi
while (WiFi.status() != WL_CONNECTED) {//程序將等待直至WiFi連接成功
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)) {//連接服務器,程序將等待直至服務器連接成功
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Host connected");
Serial.println("Iot services init");
iot.begin(client, DEVICEID, APIKEY);//初始化ArduinoBigiot,并設置設備ID和設備APIKEY
iot.checkout();//強制目標設備下線,消除滯留
iot.checkin();//設備登錄
iot.offOn(handleOffOn);//為"offOn"命令綁定"handleOffOn"函數,即上面創建的"handleOffOn"回調函數
iot.play(handlePlay);//為"play"命令綁定"handlePlay"函數,即上面創建的"handlePlay"回調函數
iot.stop(handleStop);//為"stop"命令綁定"handleStop"函數,即上面創建的"handleStop"回調函數
Serial.println("Iot services started");
}
void loop() {
iot.handleiot();//保活設備,處理連接信息
delay(50);
}
[/code]
第一眼看著很多,其實仔細看你會發現大部分代碼都是串口打印(笑
更改完信息并上傳至ESP后,嘗試在設備列表中對應的遙控面板(或小程序等)點擊play或stop或offOn,觀察板載LED燈變化,修改相應回調函數中的代碼即可改變ESP在收到命令后的動作。