ArduinoBigiot库

作者:OREO | 更新时间:2020-04-09 | 浏览量:1423

库链接: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,在https://www.bigiot.net/User/listDev.html中查看
String APIKEY = "apikey";//设备APIKEY,在https://www.bigiot.net/User/listDev.html中查看
const char* host = "www.bigiot.net";//贝壳服务器域名www.bigiot.net
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在收到命令后的动作。


评论:共1条

18346175183 评论于:2020-04-12 12:26:03
ajson.h在哪下载?
返回顶部