1234
1234
1234
h>
/* 依赖 PubSubClient 2.4.0 */
#include <PubSubClient.h>
/* 依赖 ArduinoJson 5.13.4 */
#include <ArduinoJson.h>
/* 修改 1 ------------------------------------------ */
/* 修改 1 end--------------------------------------- */
/* 修改 2 ------------------------------------------ */
/* 设备证书信息*/
/* 修改 2 end--------------------------------------- */
/* 线上环境域名和端口号,不需要改 */
/* 修改 3 ------------------------------------------ */
// 加 密 明 文 是 参 数 和 对 应 的 值 ( clientIdesp8266deviceName${deviceName}productKey$
{productKey}timestamp1234567890)按字典顺序拼接
// 密钥是设备的 DeviceSecret
/* 修改 3 end--------------------------------------- */
#define ALINK_BODY_FORMAT
"{\"id\":\"123\",\"version\":\"1.0\",\"method\":\"thing.event.property.post\",\"params\":%s}"
WiFiClient espClient;
PubSubClient client(espClient);
void callback(char *topic, byte *payload, unsigned int length)
Serial.print(topic);
Serial.print("] ");
payload[length] = '\0';
Serial.println((char *)payload);
void wifiInit()
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWD);
delay(1000);
Serial.println("Connected to AP");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("espClient [");
client.setCallback(callback);
void mqttCheckConnect()
{
while (!client.connected())
Serial.println("MQTT Connected!");
}
else
Serial.println(client.state());
delay(5000);
void mqttIntervalPost()
char param[32];
char jsonBuf[128];
/* 修改 4 ------------------------------------------ */
/* 修改 4 end--------------------------------------- */
Serial.println(jsonBuf);
Serial.println(d);
delay(1000);
void setup()
pinMode(SENSOR_PIN, INPUT);
Serial.begin(115200);
Serial.println("Demo Start");
wifiInit();
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);