|
5金币
本帖最后由 jpolo 于 2019-6-24 09:45 编辑
手上有个nodemcu v3 的板子,在阿里云的教程下想着试试连接阿里云网址如下:https://help.aliyun.com/document_detail/104070.html?spm=5176.2020520165.120.d104070.64a87029lohfhn
我就照着做啊......
然后嘞...
就没有结果了...
感觉自己废了,官方教程都看不懂...
附上我的出错图...
(这个是连线图)
....................................................................................................................................................................................................................
(这个是我注册的阿里云)
....................................................................................................................................................................................................................
(这个是根据教程打开的密码小工具...不知道该怎么填了...,后来咨询说随便填...没有格式么?)
....................................................................................................................................................................................................................
(这个是我copy的代码和上传结果)
....................................................................................................................................................................................................................
总之就是连不上啊...
有做成的朋友么?指导一下...谢谢了:handshake
最后附上源代码:
[pre]#include <ESP8266WiFi.h>
/* 依赖 PubSubClient 2.4.0 */
#include <PubSubClient.h>
/* 依赖 ArduinoJson 5.13.4 */
#include <ArduinoJson.h>
#define SENSOR_PIN 13
/* 连接您的WIFI SSID和密码 */
#define WIFI_SSID "******"//替换自己的WIFI
#define WIFI_PASSWD "******"//替换自己的WIFI
/* 设备证书信息*/
#define PRODUCT_KEY "a1oT1xm6rwe"
#define DEVICE_NAME "nodemcu"
#define DEVICE_SECRET "dOzKxncXBy4UglQL4*******zek4p"
#define REGION_ID "cn-shanghai"
/* 线上环境域名和端口号,不需要改 */
#define MQTT_SERVER PRODUCT_KEY ".iot-as-mqtt." REGION_ID ".aliyuncs.com"
#define MQTT_PORT 1883
#define MQTT_USRNAME DEVICE_NAME "&" PRODUCT_KEY
#define CLIENT_ID "esp8266|securemode=3,timestamp=1234567890,signmethod=hmacsha1|"
// 请使用以上说明中的加密工具或参见MQTT-TCP连接通信文档加密生成password。
// 加密明文是参数和对应的值(clientIdesp8266deviceName${deviceName}productKey${productKey}timestamp1234567890)按字典顺序拼接
// 密钥是设备的DeviceSecret
#define MQTT_PASSWD "D9D586CAF0E7E0D7C478950CA9CC050341AD8977" //"f1ef80ee8add322c519a5a6bec326dc499f854b8"
#define ALINK_BODY_FORMAT "{\"id\":\"123\",\"version\":\"1.0\",\"method\":\"thing.event.property.post\",\"params\":%s}"
#define ALINK_TOPIC_PROP_POST "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post"
unsigned long lastMs = 0;
WiFiClient espClient;
PubSubClient client(espClient);
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);
}
void wifiInit()
{
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWD);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("WiFi not Connect");
}
Serial.println("Connected to AP");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("espClient [");
client.setServer(MQTT_SERVER, MQTT_PORT); /* 连接WiFi之后,连接MQTT服务器 */
client.setCallback(callback);
}
void mqttCheckConnect()
{
while (!client.connected())
{
Serial.println("Connecting to MQTT Server ...");
if (client.connect(CLIENT_ID, MQTT_USRNAME, MQTT_PASSWD))
{
Serial.println("MQTT Connected!");
}
else
{
Serial.print("MQTT Connect err:");
Serial.println(client.state());
delay(5000);
}
}
}
void mqttIntervalPost()
{
char param[32];
char jsonBuf[128];
sprintf(param, "{\"idle\":%d}", digitalRead(13));
sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
Serial.println(jsonBuf);
boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
Serial.print("publish:0 失败;1成功");
Serial.println(d);
}
void setup()
{
pinMode(SENSOR_PIN, INPUT);
/* initialize serial for debugging */
Serial.begin(115200);
Serial.println("Demo Start");
wifiInit();
}
// the loop function runs over and over again forever
void loop()
{
if (millis() - lastMs >= 5000)
{
lastMs = millis();
mqttCheckConnect();
/* 上报消息心跳周期 */
mqttIntervalPost();
}
client.loop();
if (digitalRead(SENSOR_PIN) == HIGH){
Serial.println("Motion detected!");
delay(2000);
}
else {
Serial.println("Motion absent!");
delay(2000);
}
}[/pre] |
最佳答案
查看完整内容
你这个看返回信息就知道是Mqtt的密码错了,就是用工具加密后生成的密码不对。
我也是在这个地方被坑了,阿里的文档说得不够清楚。
加密工具中的:
timestamp填1234567890;
clientId填esp8266;
实际上上面两个变量都是可以自定义的,它们来源于代码中的:
#define CLIENT_ID "esp8266|securemode=3,timestamp=1234567890,signmethod=hmacsha1|"这里的esp8266和1234567890可以自己修改,使用加密工具时用改过的字符串就行了。
...
|