0% found this document useful (0 votes)
35 views4 pages

123

1

Uploaded by

zcr987650
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

123

1

Uploaded by

zcr987650
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <ESP8266WiFi.

h>

/* 依赖 PubSubClient 2.4.0 */

#include <PubSubClient.h>

/* 依赖 ArduinoJson 5.13.4 */

#include <ArduinoJson.h>

#define SENSOR_PIN 13 //D5

/* 修改 1 ------------------------------------------ */

/* 连接您的 WIFI SSID 和密码 */

#define WIFI_SSID "Honorjia"

#define WIFI_PASSWD "12345678"

/* 修改 1 end--------------------------------------- */

/* 修改 2 ------------------------------------------ */

/* 设备证书信息*/

#define PRODUCT_KEY "a1KgQbgEjpg"

#define DEVICE_NAME "tem_sensor"

#define DEVICE_SECRET "ef213c87af1ed8f19e0cf0714093be9c"

#define REGION_ID "cn-shanghai"

/* 修改 2 end--------------------------------------- */
/* 线上环境域名和端口号,不需要改 */

#define MQTT_SERVER PRODUCT_KEY ".iot-as-mqtt." REGION_ID ".aliyuncs.com"

#define MQTT_PORT 1883

#define MQTT_USRNAME DEVICE_NAME "&" PRODUCT_KEY

/* 修改 3 ------------------------------------------ */

#define CLIENT_ID "esp8266|securemode=3,signmethod=hmacsha1,timestamp=1234567890|"

// 请使用以上说明中的加密工具或参见 MQTT-TCP 连接通信文档加密生成 password。

// 加 密 明 文 是 参 数 和 对 应 的 值 ( clientIdesp8266deviceName${deviceName}productKey$
{productKey}timestamp1234567890)按字典顺序拼接

// 密钥是设备的 DeviceSecret

#define MQTT_PASSWD "079B0383E3CCC9000BB97BEAF48A66C840D64DA2"

/* 修改 3 end--------------------------------------- */

#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()

You might also like