查看: 309|回复: 0

[用户分享] 【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. 创建产品
  • 新建产品

01阿里云创建产品.png
  • 输入相关参数

02阿里云保存设备模型.png

2. 添加设备
  • 在产品中点击“添加设备”

03阿里云添加设备a.png

  • 点击“添加设备”
03阿里云添加设备b.png

  • 输入设备名称:

03阿里云添加设备c.png
  • 添加成功:

03阿里云添加设备d.png
3. 查看设备
  • 选择设备
注意此时设备是“未激活”状态,只有当MQTT连接成功后设备才会被激活
03阿里云添加设备e.png

  • 点击设备,选中设备,在DeviceSecret点“查看”,将ProductKey,SecrectKey和DeviceName保存并妥善保管

03阿里云添加设备e0.png


03阿里云添加设备f.png

4. 添加自定义功能

  • 切换到产品,点击“编辑草稿”

03阿里云添加设备g.png

  • 点击“添加自定义功能”,如图进行设置

03阿里云添加设备h.png

  • 草稿编辑成功,需要发布

03阿里云添加设备i.png

  • 点击下方的“发布”按钮

releaseModule.png

  • 发布成功

releaseModule_a.png

二、配置并运行Arduino程序

1. 点击设备-->物模型通信Topice,复制set和post
03阿里云添加设备j.png

2. 运行IDE,打开文件TinkerNode_Bedroom_Light.ino
openExample.png

3. 将对应的信息复制到文件中:
04修改测试程序.png
[C++] 纯文本查看 复制代码
/*!
 * @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 ([url=http://www.dfrobot.com]http://www.dfrobot.com[/url])
 * @licence     The MIT License (MIT)
 * @author      [Wuxiao]([url=mailto:xiao.wu@dfrobot.com]xiao.wu@dfrobot.com[/url])
 * @version  V1.0
 * @date  2019-02-10
 * @get from [url=https://www.dfrobot.com]https://www.dfrobot.com[/url]
 */

#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "DFRobot_Iot.h"

#define BEDROOD_LIGHT  D4

/*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[i]);[/i]
[i]  }[/i]
[i]  Serial.println();[/i]
[i]  StaticJsonBuffer<300> jsonBuffer;[/i]
[i]  JsonObject& root = jsonBuffer.parseObject((const char *)payload);[/i]
[i]  if(!root.success()){[/i]
[i]    Serial.println("parseObject() failed");[/i]
[i]    return;[/i]
[i]  }[/i]
[i]  const uint16_t LightStatus = root["params"][Identifier];[/i]
[i]  if(LightStatus == 1){[/i]
[i]    openLight();[/i]
[i]  }else{[/i]
[i]    closeLight();[/i]
[i]  }[/i]
[i]  String tempMseg = "{\"id\":"+ClientId+",\"params\":{\""+Identifier+"\":"+(String)LightStatus+"},\"method\":\"thing.event.property.post\"}";[/i]
[i]  char sendMseg[tempMseg.length()];[/i]
[i]  strcpy(sendMseg,tempMseg.c_str());[/i]
[i]  client.publish(pubTopic,sendMseg);[/i]
[i]}[/i]

[i]void ConnectCloud(){[/i]
[i]  while(!client.connected()){[/i]
[i]    Serial.print("Attempting MQTT connection...");[/i]
[i]    /*A device connected to the cloud platform based on an automatically calculated username and password*/[/i]
[i]    if(client.connect(myIot._clientId,myIot._username,myIot._password)){[/i]
[i]      Serial.println("connected");[/i]
[i]      client.subscribe(subTopic);[/i]
[i]    }else{[/i]
[i]      Serial.print("failed, rc=");[/i]
[i]      Serial.print(client.state());[/i]
[i]      Serial.println(" try again in 5 seconds");[/i]
[i]      delay(5000);[/i]
[i]    }[/i]
[i]  }[/i]
[i]}[/i]
[i]void setup(){[/i]
[i]  Serial.begin(115200);[/i]
[i]  pinMode(BEDROOD_LIGHT,OUTPUT);[/i]

[i]  /*Connect to WIFI*/[/i]
[i]  connectWiFi();[/i]

[i]  /*Initialize the configuration of Aliyun*/[/i]
[i]  myIot.init(ALIYUN_SERVER,ProductKey,ClientId,DeviceName,DeviceSecret);[/i]

[i]  client.setServer(myIot._mqttServer,PORT);[/i]

[i]  /*Set the callback function to execute the callback function when receiving the subscription information*/[/i]
[i]  client.setCallback(callback);[/i]

[i]  /*Connect to the cloud platform*/[/i]
[i]  ConnectCloud();[/i]

[i]  /*Turn off the lights first*/[/i]
[i]  closeLight();[/i]

[i]  /*Publish information about turning off the lights*/[/i]
[i]  client.publish(pubTopic,("{\"id\":"+ClientId+",\"params\":{\""+Identifier+"\":0},\"method\":\"thing.event.property.post\"}").c_str());[/i]
[i]}[/i]

[i]void loop(){[/i]
[i]  if(!client.connected()){[/i]
[i]    ConnectCloud();[/i]
[i]  }[/i]
[i]  client.loop();[/i]
[i]}


4. 建立MQTT连接
  • 选择板子型号和端口,打开串口监视器。程序上传成功后,串口监视器显示如下:

MQTTconnected.png
  • MQTT连接成功

MQTTconnected.gif

  • 此时回到阿里云控制台,可以看到设备已经激活并显示在线:

deviceOnline.png


三、在线调试

1. 在控制台中按下图对设备进行调试
OnlineDebug.png

2. 上图中设置的值为1时,开灯
{
  "NightLightSwitch": 1
}

LightOn.jpg

3. 设置的值为0时,关灯
{
  "NightLightSwitch": 0
}
LightOff.jpg




高级模式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2020 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail