一.概述
本文章主要講述利用ESPduino開發(fā)板(兼容NodeMcu)獲取DHT11采集的溫濕度信息涛酗,并且通過MQTT協(xié)議實(shí)時(shí)上傳到ONENET物聯(lián)網(wǎng)云平臺(tái)。開發(fā)過程采用ArduinoIDE開發(fā)埠巨,代碼我現(xiàn)在已經(jīng)整理成一個(gè)模版(只要是ONENET上傳數(shù)據(jù)巴粪,都可以基于這套模版代碼,代碼根據(jù)ESP8266 庫中實(shí)例修改而成)沛厨,分享在下方刚照,供各位小伙伴二次修改開發(fā)刑巧。
一.準(zhǔn)備
1.ESPduino(兼容NodeMcu等一些列ESP8266開發(fā)板)
????ESPDuino將Arduino和ESP8266有機(jī)地結(jié)合起來,使開發(fā)高效穩(wěn)定的物聯(lián)網(wǎng)變得容易无畔。
2.DHT11 溫濕度傳感器模塊
很常見且便宜的溫濕度傳感器啊楚,精度不是很準(zhǔn),但可以玩玩浑彰,后期可以更換其他高精度傳感器恭理。
3.面包板及其若干杜邦線
三.接線
VCC----------------------5v
GND---------------------GND
DATA---------------------D0
四.代碼(ONENET上傳模版)
/**********************************************************************************************
*項(xiàng)目:Espduino采集DHT11溫濕度上傳ONENET云平臺(tái)
*作者:耿彬
*日期:2020-05-04
*版本:V1.O
?**********************************************************************************************/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <dht11.h>
//定義LED燈:PIN_LED所對(duì)應(yīng)的引腳為16號(hào)
#define BUILTIN_LED 16
//定義DHT11數(shù)據(jù)引腳
#define DHT11PIN 0
//此處定義為你自己的路由器名稱(SSID)
const char* ssid = "此處定義為你自己的路由器名稱";
//此處定義為你自己的路由器密碼
const char* password = "此處定義為你自己的路由器密碼";
const char* mqtt_server = "183.230.40.39";//固定 ONENET云平臺(tái)IP地址
const char* DeviceID = "你自己onenet上的設(shè)備號(hào)"; //設(shè)備號(hào)
const char* ProductID = "你自己onenet上的產(chǎn)品號(hào)"; ?//產(chǎn)品號(hào)
const char* AuthInfo = "你自己onenet上的APIKey"; //鑒權(quán)信息
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());
}
/*
*訂閱主題后收到信息的回調(diào)函數(shù)(用于進(jìn)行遠(yuǎn)程控制)
?*/
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...");
//默認(rèn)創(chuàng)建一個(gè)隨機(jī)的 client ID
????String clientId = "ESP8266Client-";
????clientId += String(random(0xffff), HEX);
//嘗試連接
????if (client.connect(DeviceID, ProductID, AuthInfo)) {
??????Serial.println("connected");
//發(fā)布主題和內(nèi)容
??????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);
????}
??}
}
/**
*初始化函數(shù)
?*/
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);
}
/**
*循環(huán)函數(shù)
?*/
void loop() {
//讀取溫濕度數(shù)據(jù)
??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);*/
//拼接所要發(fā)送的json串(重要,這里最終發(fā)送的數(shù)據(jù))
????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設(shè)備ID颜价,產(chǎn)品ID,和鑒權(quán)信息(APIkey)
(1)設(shè)備ID:設(shè)備列表中
(2)產(chǎn)品ID:產(chǎn)品概況
(3)鑒權(quán)信息(APIKEY):產(chǎn)品概況
3. 修改JSON串
Json可以自行百度學(xué)習(xí)诉濒,這里就不說多少了.
格式為{“數(shù)據(jù)流1”:數(shù)據(jù)周伦,”數(shù)據(jù)流2”:數(shù)據(jù)}
六.資源分享
1. ArduinoIDE內(nèi)置ESP8266庫文件 可直接使用 以及 ESPduino相關(guān)說明
鏈接:https://pan.baidu.com/s/1MDBVi09Mc-TUWeZbiFh7-g
提取碼:840q
2. DHT11庫文件
鏈接:https://pan.baidu.com/s/1o2HifBpIMX95wmE4SetYLQ
提取碼:vrl8