【TinkerNode NB-IoT 物联网开发板】测评(三)MQTT控制LED灯开关
【TinkerNode NB-IoT 物联网开发板】测评(三)MQTT控制LED灯开关【TinkerNode NB-IoT 物联网开发板】测评(一)开箱验机
【TinkerNode NB-IoT 物联网开发板】测评(二)运行出厂默认程序
【TinkerNode NB-IoT 物联网开发板】测评(三)MQTT控制板载LED灯开关
【TinkerNode NB-IoT 物联网开发板】测评(四)移动应用开发
这次测评不太顺利,阿里云MQTT一直连接不成功,和赵工沟通多次,最后给出的解决方案还是没有解决。直到后来固件更新到1.03才算是解决问题。但是前面浪费了太多时间,后来一直巨忙,所以后续的测评就搁置了。
今天的测评是建立阿里云MQTT连接并控制板载LED灯开关。
一、新建产品和设备
1. 创建产品
[*]新建产品
[*]输入相关参数
2. 添加设备
[*]在产品中点击“添加设备”
[*]点击“添加设备”
[*]输入设备名称:
[*]添加成功:
3. 查看设备
[*]选择设备
注意此时设备是“未激活”状态,只有当MQTT连接成功后设备才会被激活
[*]点击设备,选中设备,在DeviceSecret点“查看”,将ProductKey,SecrectKey和DeviceName保存并妥善保管
4. 添加自定义功能
[*]切换到产品,点击“编辑草稿”
[*]点击“添加自定义功能”,如图进行设置
[*]草稿编辑成功,需要发布
[*]点击下方的“发布”按钮
[*]发布成功
二、配置并运行Arduino程序
1. 点击设备-->物模型通信Topice,复制set和post
2. 运行IDE,打开文件TinkerNode_Bedroom_Light.ino
3. 将对应的信息复制到文件中:
/*!
* @file Bedroom_Light.ino
*
* @brief Simulate esp32 as a bedroom light and use Aliyun as a cloud platform.
* @n Subscribe to the switch status theme to enable remote switch control bedroom lights
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author (xiao.wu@dfrobot.com)
* @versionV1.0
* @date2019-02-10
* @get from https://www.dfrobot.com
*/
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "DFRobot_Iot.h"
#define BEDROOD_LIGHTD4
/*Set WIFI name and password*/
const char * WIFI_SSID = "WIFI_SSID";
const char * WIFI_PASSWORD = "WIFI_PASSWORD";
/*Configure device certificate information*/
String ProductKey = "you_Product_Key";
String ClientId = "12345";/*Custom id*/
String DeviceName = "you_Device_Name";
String DeviceSecret = "you_Device_Secret";
/*Configure the domain name and port number*/
String ALIYUN_SERVER = "iot-as-mqtt.cn-shanghai.aliyuncs.com";
uint16_t PORT = 1883;
/*Product identifier that needs to be operated*/
String Identifier = "you_Identifier";
/*TOPIC that need to be published and subscribed*/
const char * subTopic = "you_sub_Topic";//****set
const char * pubTopic = "you_pub_Topic";//******post
DFRobot_Iot myIot;
WiFiClient espClient;
PubSubClient client(espClient);
static void openLight(){
digitalWrite(BEDROOD_LIGHT, HIGH);
}
static void closeLight(){
digitalWrite(BEDROOD_LIGHT, LOW);
}
void connectWiFi(){
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID,WIFI_PASSWORD);
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("IP Adderss: ");
Serial.println(WiFi.localIP());
}
void callback(char * topic, byte * payload, unsigned int len){
Serial.print("Recevice [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < len; i++){
Serial.print((char)payload);
}
Serial.println();
StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject((const char *)payload);
if(!root.success()){
Serial.println("parseObject() failed");
return;
}
const uint16_t LightStatus = root["params"];
if(LightStatus == 1){
openLight();
}else{
closeLight();
}
String tempMseg = "{\"id\":"+ClientId+",\"params\":{\""+Identifier+"\":"+(String)LightStatus+"},\"method\":\"thing.event.property.post\"}";
char sendMseg;
strcpy(sendMseg,tempMseg.c_str());
client.publish(pubTopic,sendMseg);
}
void ConnectCloud(){
while(!client.connected()){
Serial.print("Attempting MQTT connection...");
/*A device connected to the cloud platform based on an automatically calculated username and password*/
if(client.connect(myIot._clientId,myIot._username,myIot._password)){
Serial.println("connected");
client.subscribe(subTopic);
}else{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup(){
Serial.begin(115200);
pinMode(BEDROOD_LIGHT,OUTPUT);
/*Connect to WIFI*/
connectWiFi();
/*Initialize the configuration of Aliyun*/
myIot.init(ALIYUN_SERVER,ProductKey,ClientId,DeviceName,DeviceSecret);
client.setServer(myIot._mqttServer,PORT);
/*Set the callback function to execute the callback function when receiving the subscription information*/
client.setCallback(callback);
/*Connect to the cloud platform*/
ConnectCloud();
/*Turn off the lights first*/
closeLight();
/*Publish information about turning off the lights*/
client.publish(pubTopic,("{\"id\":"+ClientId+",\"params\":{\""+Identifier+"\":0},\"method\":\"thing.event.property.post\"}").c_str());
}
void loop(){
if(!client.connected()){
ConnectCloud();
}
client.loop();
}
4. 建立MQTT连接
[*]选择板子型号和端口,打开串口监视器。程序上传成功后,串口监视器显示如下:
[*]MQTT连接成功
[*]此时回到阿里云控制台,可以看到设备已经激活并显示在线:
三、在线调试
1. 在控制台中按下图对设备进行调试
2. 上图中设置的值为1时,开灯
{
"NightLightSwitch": 1
}
3. 设置的值为0时,关灯
{
"NightLightSwitch": 0
}
file:///C:\Users\Djust\AppData\Roaming\Tencent\Users\3260546337\QQ\WinTemp\RichOle\}Z[]K(5LM2878)]
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
按照例程走,最后碰到这个,奇怪 svw 发表于 2020-9-8 12:40
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
按照例程走,最后碰到这个,奇怪 ...
https://mc.dfrobot.com.cn/data/attachment/forum/201905/25/001732fdjeeqz7dgjllrqu.png
图片中有错误代码类型,-2是连接失败。我前面也一直是这个问题,你可以更新一下固件试试。 szjuliet 发表于 2020-9-9 15:37
图片中有错误代码类型,-2是连接失败。我前面也一直是这个问题,你可以更新一下固件试试。 ...
请问你的固件是几点几版本?
svw 发表于 2020-9-10 15:45
请问你的固件是几点几版本?
V1.0.3 1.04 和1.03 版本 都是这样哈,rc=2 try again in 5 seconds
估计被aliyun屏蔽了。
不知道你的现在还OK吗?
页:
[1]