老哥们,我是想VSCode+PlatformIo IDE(or hacklab方式)实现Wemos D1 Mini(ESP8266)接入阿里云物联网平台。这是原文链接:
https://blog.csdn.net/happyjoey217/article/details/90199308?ops_request_misc=%7B%22request%5Fid%22%3A%22160333187319726892428148%22%2C%22scm%22%3A%2220140713.130102334.pc%5Fblog.%22%7D
我和这个博主老哥操作是一样的,但是会报错。
报错的类型
报错地方的代码。
下面是整个代码:
#include
#include
#include
// Wifi名称和密码,你WemosD1Mini上网时需要接入的Wifi账号和密码
#define WIFI_SSID “xxx”
#define WIFI_PASSWD “xxx”
// LP的设备三元组(PK/DN/DS)根据你云端创建的设备三码进行更改
#define PRODUCT_KEY “xxx”
#define DEVICE_NAME “xxx”
#define DEVICE_SECRET “xxx”
// Alink协议格式和Topic定义 这个部分是固定格式不用进行修改
#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”
// 创建WiFiClient实例
WiFiClient espClient;
//创建MqttClient实例
PubSubClient mqttClient(espClient);
//连接Wifi
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] = ‘\0’;
Serial.println((char *)payload);
if (strstr(topic, ALINK_TOPIC_PROP_SET))
{
StaticJsonBuffer<200> jsonBuffer;
JsonObject &root = jsonBuffer.parseObject(payload);
if (!root.success())
{
Serial.println("parseObject() failed");
} else {
Serial.println("parseObject() success");
//下一步对收到的云端指令进行处理
}
}
}
//连接Mqtt
void mqttCheckConnect()
{
bool connected = connectAliyunMQTT(mqttClient, PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET);
if (connected) {
Serial.println(“MQTT connect succeed!”);
// client.subscribe(ALINK_TOPIC_PROP_POSTRSP);
mqttClient.subscribe(ALINK_TOPIC_PROP_SET); // 订阅属性设置Topic
Serial.println(“subscribe done”);
}
}
void setup() {
Serial.begin(115200);
initWifi(WIFI_SSID, WIFI_PASSWD); // 连接Wifi
mqttClient.setCallback(callback); // 设置回调监听云端下发的指令
}
void loop() {
mqttCheckConnect(); // MQTT上云
mqttClient.loop();
delay(3000); // 每1秒连接一次
}