概述
本文档为个人博客文档系统的备份版本、作者:小游、作者博客:点击访问
首先我们需要下载下面的依赖库
https://github.com/legenddcr/aliyun-mqtt-arduino
https://github.com/rweather/arduinolibs
这个我已经打包放到lib文件夹里面去了,我们只需要解压放到arduino的lib库里去
默认在C盘的文档里面
然后我们修改PubSubClient
库的连接参数。这里改成60
下面我们把测试代码烧进去
#include
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "aliyun_mqtt.h"
// GPIO 13, D7 on the Node MCU v3
#define SENSOR_PIN 13
#define WIFI_SSID "wifi名字"
#define WIFI_PASSWD "wifi密码"
#define PRODUCT_KEY "a1NRzHXlTTp"
#define DEVICE_NAME "Band"
#define DEVICE_SECRET "563221c763c3cbd474a4180afb775948"
#define ALINK_BODY_FORMAT "{"id":"123","version":"1.0","method":"%s","params":%s}"
#define ALINK_TOPIC_PROP_POST "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post"
#define ALINK_TOPIC_PROP_POSTRSP "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post_reply"
#define ALINK_TOPIC_PROP_SET "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/service/property/set"
#define ALINK_METHOD_PROP_POST "thing.event.property.post"
unsigned long lastMs = 0;
WiFiClient espClient;
PubSubClient mqttClient(espClient);
void initWifi(const char *ssid, const char *password)
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.println("WiFi does not connect, try again ...");
delay(3000);
}
Serial.println("Wifi is connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
payload[length] = '