|
本帖最后由 vany5921 于 2019-9-4 09:41 编辑
相信使用天猫精灵、小爱同学的玩家不在少数,随着HomeAssistant越来越普及,家中的设备越来越多的接入到了控制中枢,通常我们用树莓派或者OPENWRT来作为中控实现远程监控和控制功能,对于小白用户来讲门槛稍微有点高,其实M5Stack就扮演了这样一种角色,既可以作为终端又可以作为服务端。你可以利用UIFlow的MQTT假设自己的小型应用,当然你也可以连接服务器作为控制器。今天就带着大家来实现最简单的天猫精灵应用,接入贝壳物联的API通过语音命令来控制M5Stack的IO,达到远程控制目的。
先到贝壳物联的网站注册账号。注册好以后进入控制面板添加设备
添加完成以后回到控制面板首页,这里会出现你刚添加的设备。设备ID和APIKEY在我们的程序中用来验证身份。
接下来在天猫精灵智能音箱里绑定贝壳物联账号
绑定好以后进行最后一步操作,修改代码。贝壳物联给出了参考模板,只需简单修改几个参数就能实现。当然,借助于M5Stack的这块屏幕,我们可以打印日志或者加入本地控制
一起来看一下代码
[mw_shl_code=arduino,true]#include <M5Stack.h>
#include <WiFi.h>
#include <aJSON.h>
//============= 此处必须修该============
String DEVICEID="ID"; // 你的设备编号 ==
String APIKEY = "APIKEY"; // 设备密码==
//=======================================
unsigned long lastCheckInTime = 0; //记录上次报到时间
const unsigned long postingInterval = 40000; // 每隔40秒向服务器报到一次
const char* ssid = "SSID";//无线名称
const char* password = "PASSWORD";//无线密码
const char* host = "www.bigiot.net";
const int httpPort = 8181;
int pins[2] = {32, 33}; //GPIO端口
int state[2] = {LOW}; //电平
int arr_len = sizeof(pins)/sizeof(pins[0]); //数据长度
void setup() {
M5.begin();
// Serial.begin(115200);
delay(1000);
WiFi.begin(ssid, password);
//默认输出关闭电频
for(int i=0;i<arr_len;i++){
pinMode(pins, OUTPUT);
digitalWrite(pins, state);
}
}
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"){
for(int i=0;i<arr_len;i++){
state = HIGH;
digitalWrite(pins, state);
}
sayToClient(F_C_ID,"LED All on!");
}else if(C == "stop"){
for(int i=0;i<arr_len;i++){
state = LOW;
digitalWrite(pins, state);
}
sayToClient(F_C_ID,"LED All off!");
}else{
int pin = C.toInt();
if(pin > 0 && pin <= arr_len){
pin--;
state[pin] = !state[pin];
digitalWrite(pins[pin], state[pin]);
}
sayToClient(F_C_ID,"LED pin:"+pin);
}
}
}
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();
}[/mw_shl_code]
|
|