美文网首页
ESPduino-01上传温湿度数据到OneNet平台(MQTT

ESPduino-01上传温湿度数据到OneNet平台(MQTT

作者: 耳火木杉 | 来源:发表于2020-05-15 21:09 被阅读0次


    一.概述

    本文章主要讲述利用ESPduino开发板(兼容NodeMcu)获取DHT11采集的温湿度信息,并且通过MQTT协议实时上传到ONENET物联网云平台。开发过程采用ArduinoIDE开发,代码我现在已经整理成一个模版(只要是ONENET上传数据,都可以基于这套模版代码,代码根据ESP8266 库中实例修改而成),分享在下方,供各位小伙伴二次修改开发。

    一.准备

    1.ESPduino(兼容NodeMcu等一些列ESP8266开发板)

        ESPDuino将Arduino和ESP8266有机地结合起来,使开发高效稳定的物联网变得容易。

    2.DHT11 温湿度传感器模块

    很常见且便宜的温湿度传感器,精度不是很准,但可以玩玩,后期可以更换其他高精度传感器。

    3.面包板及其若干杜邦线

    三.接线

    VCC----------------------5v

    GND---------------------GND

    DATA---------------------D0

    四.代码(ONENET上传模版)

    /**********************************************************************************************

    *项目:Espduino采集DHT11温湿度上传ONENET云平台

    *作者:耿彬

    *日期:2020-05-04

    *版本:V1.O

     **********************************************************************************************/

    #include <ESP8266WiFi.h>

    #include <PubSubClient.h>

    #include <dht11.h>

    //定义LED灯:PIN_LED所对应的引脚为16号

    #define BUILTIN_LED 16

    //定义DHT11数据引脚

    #define DHT11PIN 0

    //此处定义为你自己的路由器名称(SSID)

    const char* ssid = "此处定义为你自己的路由器名称";

    //此处定义为你自己的路由器密码

    const char* password = "此处定义为你自己的路由器密码";

    const char* mqtt_server = "183.230.40.39";//固定 ONENET云平台IP地址

    const char* DeviceID = "你自己onenet上的设备号"; //设备号

    const char* ProductID = "你自己onenet上的产品号";  //产品号

    const char* AuthInfo = "你自己onenet上的APIKey"; //鉴权信息

    WiFiClient espClient;

    PubSubClient client(espClient);

    long lastMsg = 0;

    char msg[50];

    int value = 0;

    char tmp[28];

    char d[3];

    dht11 DHT11;

    /**

    *连接WIFI

     */

    void setup_wifi() {

      delay(10);

      // We start by connecting to a WiFi network

      Serial.println();

      Serial.print("Connecting to ");

      Serial.println(ssid);

      WiFi.begin(ssid, password);

      while (WiFi.status() != WL_CONNECTED) {

        delay(500);

        Serial.print("-)");

      }

      randomSeed(micros());

      Serial.println("");

      Serial.println("WiFi 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("] ");

      for (int i = 0; i < length; i++) {

        Serial.print((char)payload[i]);

      }

      Serial.println();

      // Switch on the LED if an 1 was received as first character

      if ((char)payload[0] == '1') {

        digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level

        // but actually the LED is on; this is because

        // it is active low on the ESP-01)

      } else {

        digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH

      }

    }

    /**

    * MQTT连接ONENET

     */

    void reconnect() {

      // Loop until we're reconnected

      while (!client.connected()) {

        Serial.print("Attempting MQTT connection...");

    //默认创建一个随机的 client ID

        String clientId = "ESP8266Client-";

        clientId += String(random(0xffff), HEX);

    //尝试连接

        if (client.connect(DeviceID, ProductID, AuthInfo)) {

          Serial.println("connected");

    //发布主题和内容

          client.publish("outTopic", "hello world");

    //订阅主题

          client.subscribe("inTopic");

        } else {

          Serial.print("failed, rc=");

          Serial.print(client.state());

          Serial.println(" try again in 5 seconds");

          // Wait 5 seconds before retrying

          delay(5000);

        }

      }

    }

    /**

    *初始化函数

     */

    void setup() {

      pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output

      Serial.begin(115200);

      setup_wifi();

      client.setServer(mqtt_server, 6002);

      client.setCallback(callback);

    }

    /**

    *循环函数

     */

    void loop() {

    //读取温湿度数据

      DHT11.read(DHT11PIN);

    Serial.print("湿度:");

    Serial.println(DHT11.humidity * 100); //湿度*100以辨别温度

    Serial.print("温度:");

      int temperature = DHT11.temperature;

    Serial.println(temperature);//输出温度

      if (!client.connected()) {

        reconnect();

      }

      client.loop();

      long now = millis();

      if (now - lastMsg > 2000) {

        lastMsg = now;

        ++value;

        /*snprintf (msg, 50, "hello world #%ld", value);

          Serial.print("Publish message: ");

          Serial.println(msg);

          client.publish("outTopic", msg);*/

    //拼接所要发送的json串(重要,这里最终发送的数据)

        snprintf(tmp, sizeof(tmp), "{\"temperature\":%d}", temperature);

        Serial.print("Publish message: ");

        Serial.println(tmp);

        uint16_t streamLen = strlen(tmp);

        d[0] = '\x03';

        d[1] = (streamLen >> 8);

        d[2] = (streamLen & 0xFF);

        snprintf(msg, sizeof(msg), "%c%c%c%s", d[0], d[1], d[2], tmp);

        client.publish("$dp", (uint8_t*)msg, streamLen + 3, false);

      }

    }

    五.修改方法

    1. 修改成为自家的路由器名称,密码

    2. 修改自己的onenet设备ID,产品ID,和鉴权信息(APIkey)

    (1)设备ID:设备列表中

    (2)产品ID:产品概况

    (3)鉴权信息(APIKEY):产品概况

    3. 修改JSON串

    Json可以自行百度学习,这里就不说多少了.

    格式为{“数据流1”:数据,”数据流2”:数据}

    六.资源分享

    1. ArduinoIDE内置ESP8266库文件 可直接使用 以及 ESPduino相关说明

    链接:https://pan.baidu.com/s/1MDBVi09Mc-TUWeZbiFh7-g

    提取码:840q

    2. DHT11库文件

    链接:https://pan.baidu.com/s/1o2HifBpIMX95wmE4SetYLQ

    提取码:vrl8

    相关文章

      网友评论

          本文标题:ESPduino-01上传温湿度数据到OneNet平台(MQTT

          本文链接:https://www.haomeiwen.com/subject/bgvhohtx.html