作者:kevin_Lv | 更新时间:2019-01-21 | 浏览量:2774
掌控板【esp32】连接贝壳物联服务,实现远程控制LED灯亮灭,并将结果反馈至控制界面。
掌控板 ×1
掌控板扩展板 x1
杜邦线(公对母) ×1
LED灯 ×1
所有公开代码托管于码云,方便大家使用和共同参与完善,地址:https://gitee.com/hejinlv/ESP32/tree/master
点击上方链接,进入如下界面:
1.点击 ESP32_bigiot_LED 进入:
2.点击下载。
1.下载代码后解压
2、用Arduino IDE打开
ESP32_bigiot_LED/ESP32_bigiot_LED.ino
修改其中的DEVICEID、APIKEY两个参数,将代码上传至Arduino开发板。
之后的详细操作步骤可参照https://www.bigiot.net/help/2.html
#include <WiFi.h>
#include <aJSON.h>
#include <MPython.h>
const char* ssid = "WLJY";
const char* password = "steam666";
const char* host = "www.bigiot.net";
const int httpPort = 8181;
unsigned long lastCheckInTime = 0; //记录上次报到时间
const unsigned long postingInterval = 40000; // 每隔40秒向服务器报到一次
//============= 此处必须修该============
String DEVICEID="9159"; // 你的设备编号 ==
String APIKEY = "58710677c"; // 设备密码==
//=======================================
void setup()
{
Serial.begin(115200);
delay(10);
mPython.begin();
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
display.setCursorXY(36,22);
display.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
delay(3000);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
pinMode(P0, OUTPUT);
// pinMode(P1, OUTPUT);
display.fillScreen(0);
display.setCursorXY(16, 22);
display.print("connected: OK");
delay(1000);
}
WiFiClient client;
void loop()
{
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
// Use WiFiClient class to create TCP connections
if (!client.connected()) {
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
delay(5000);
return;
}
}
if(millis() - lastCheckInTime > postingInterval || lastCheckInTime==0) {
checkIn();
}
// Read all the lines of the reply from server and print them to Serial
if (client.available()) {
String inputString = client.readStringUntil('\n');
inputString.trim();
Serial.println(inputString);
int len = inputString.length()+1;
if(inputString.startsWith("{") && inputString.endsWith("}")){
char jsonString[len];
inputString.toCharArray(jsonString,len);
aJsonObject *msg = aJson.parse(jsonString);
processMessage(msg);
aJson.deleteItem(msg);
}
}
}
void processMessage(aJsonObject *msg){
aJsonObject* method = aJson.getObjectItem(msg, "M");
aJsonObject* content = aJson.getObjectItem(msg, "C");
aJsonObject* client_id = aJson.getObjectItem(msg, "ID");
if (!method) {
return;
}
String M = method->valuestring;
if(M == "say"){
String C = content->valuestring;
String F_C_ID = client_id->valuestring;
if(C == "play"){
digitalWrite(P0, HIGH);
//digitalWrite(P1, HIGH);
display.fillScreen(0);
display.setCursorXY(24, 22);
display.print("LED: On");
sayToClient(F_C_ID,"LED All on!");
}
if(C == "stop"){
digitalWrite(P0, LOW);
//digitalWrite(P1, LOW);
display.setCursorXY(24, 22);
display.print("LED: OFF");
sayToClient(F_C_ID,"LED All off!");
}
}
}
void checkIn() {
String msg = "{\"M\":\"checkin\",\"ID\":\"" + DEVICEID + "\",\"K\":\"" + APIKEY + "\"}\n";
client.print(msg);
lastCheckInTime = millis();
}
void sayToClient(String client_id, String content){
String msg = "{\"M\":\"say\",\"ID\":\"" + client_id + "\",\"C\":\"" + content + "\"}\n";
client.print(msg);
lastCheckInTime = millis();
}