[C]NodeMCU(ESP8266)接入阿里云物联网平台

阿里云物联网平台公共实例,18元/年起:点此购买

[C]NodeMCU(ESP8266)接入阿里云物联网平台
按用量付费6折起:点此购买

NodeMCU(ESP8266)接入阿里云物联网平台

PIR只有三个口,分别是VCC,GND和输出口。接线方法如下(NodeMCU的D7相当于arduino里的Pin 13)
[C]NodeMCU(ESP8266)接入阿里云物联网平台
[C]NodeMCU(ESP8266)接入阿里云物联网平台

物模型定义

[C]NodeMCU(ESP8266)接入阿里云物联网平台

C程序代码

esp8266.ino 代码

#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         "路由器SSID"
#define WIFI_PASSWD       "密码"


/* 设备的三元组信息*/
#define PRODUCT_KEY       "设备PRODUCT_KEY"
#define DEVICE_NAME       "替换DEVICE_NAME"
#define DEVICE_SECRET     "替换DEVICE_SECRET"
#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|"
// 算法工具: http://iot-face.oss-cn-shanghai.aliyuncs.com/tools.htm 进行加密生成password
// password教程 https://www.yuque.com/cloud-dev/iot-tech/mebm5g
#define MQTT_PASSWD       "参考上面password教程,算法工具生成"

#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);
  }

}

PubSubClient.h 修改参数

[C]NodeMCU(ESP8266)接入阿里云物联网平台

运行效果

[C]NodeMCU(ESP8266)接入阿里云物联网平台
[C]NodeMCU(ESP8266)接入阿里云物联网平台

阿里云物联网平台公共实例,18元/年起:点此购买

[C]NodeMCU(ESP8266)接入阿里云物联网平台
按用量付费6折起:点此购买

上一篇:产品内测招募:物联网平台数据分析v2.0


下一篇:AliOS Things 近期内容集锦(更新中···)