無(wú)標(biāo)題文章

https://github.com/knolleary/pubsubclient

http://www.reibang.com/p/590f249ecd05

https://github.com/plapointe6/EspMQTTClient

https://github.com/Imroy/pubsubclient

esp32是樂(lè)鑫物聯(lián)網(wǎng)芯片簿透,帶wifi玫霎。

使用arduino進(jìn)行編程。MQTT的服務(wù)器代碼用NODEJS完成闯两。mqserver.js

arduino的代碼如下:

/*

Basic ESP8266 MQTT example

This sketch demonstrates the capabilities of the pubsub library in combination

with the ESP8266 board/library.

It connects to an MQTT server then:

? - publishes "hello world" to the topic "outTopic" every two seconds

? - subscribes to the topic "inTopic", printing out any messages

? ? it receives. NB - it assumes the received payloads are strings not binary

? - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,

? ? else switch it off

It will reconnect to the server if the connection is lost using a blocking

reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to

achieve the same result without blocking the main loop.

To install the ESP8266 board, (using Arduino 1.6.4+):

? - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":

? ? ? http://arduino.esp8266.com/stable/package_esp8266com_index.json

? - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"

? - Select your ESP8266 in "Tools -> Board"

*/

#include <WiFi.h>

#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "xxx";

const char* password = "xx123456";

const char* mqtt_server = "xx.xx.xx.xx";

WiFiClient espClient;

PubSubClient client(espClient);

long lastMsg = 0;

char msg[50];

int value = 0;

int dhtpin=12;

int ledpin=11;

int temp;//溫度

int humi;//濕度

void setup() {

? pinMode(ledpin,OUTPUT);

? Serial.begin(115200);

? setup_wifi();

? client.setServer(mqtt_server, 1883);

? client.setCallback(callback);

}

void setup_wifi() {? ? //連接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(".");

? }

? Serial.println("");

? Serial.println("WiFi connected");

? Serial.println("IP address: ");

? Serial.println(WiFi.localIP());

}

void callback(char* topic, byte* payload, unsigned int length) {? ? //回調(diào)函數(shù)

? 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 (length>2){

? ? if ((char)payload[length-2] == '1') {

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

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

? ? ? // it is acive low on the ESP-01)

? ? } else {

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

? ? }

? }

}

void reconnect() {????????//每次發(fā)布mqtt信息時(shí)宴霸,需檢查連接

? // Loop until we're reconnected

? while (!client.connected()) {

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

? ? // Attempt to connect

? ? if (client.connect("ID_ESP32Client")) {

? ? ? Serial.println("connected");

? ? ? // Once connected, publish an announcement...

? ? ? client.publish("hmdata", "{\"datetime\":\"2018-7-1 19:30\",\"temp\":25,\"humi\":100}");? //發(fā)布json格式

? ? ? // ... and resubscribe

? ? ? client.subscribe("hmdata");????????//訂閱

? ? } else {

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

? ? ? Serial.print(client.state());

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

? ? ? // Wait 5 seconds before retrying

? ? ? delay(5000);

? ? }

? }

}

void loop() {

? if (!client.connected()) {

? ? reconnect();

? }

? client.loop();? ? //檢查調(diào)用回調(diào)函數(shù)

? long now = millis();

? if (now - lastMsg > 2000) {

? ? lastMsg = now;

? ? ++value;

? ? wenshidu();? //讀取溫度

? ? temp=temp+random(1,5);

? ? humi=humi+random(1,10);

? ? snprintf (msg, 75, "{\"datetime\":\"2018-7-2 20:30\",\"temp\":%ld,\"humi\":%ld}", temp,humi);

? ? Serial.print("Publish message: ");

? ? Serial.println(msg);

? ? client.publish("hmdata", msg);

? }

}

void wenshidu()? //獲取溫濕度。

{

int pin=12;

int tol;//校對(duì)碼

int j;

unsigned int loopCnt;

int chr[40] = {0};//創(chuàng)建數(shù)字?jǐn)?shù)組,用來(lái)存放40個(gè)bit

unsigned long time1;

? bgn:

? delay(2000);

//設(shè)置2號(hào)接口模式為:輸出

//輸出低電平20ms(>18ms)

//輸出高電平40μs

? pinMode(pin,OUTPUT);

? digitalWrite(pin,LOW);

? delay(20);

? digitalWrite(pin,HIGH);

? delayMicroseconds(40);

? digitalWrite(pin,LOW);

//設(shè)置2號(hào)接口模式:輸入

? pinMode(pin,INPUT);

? //高電平響應(yīng)信號(hào)

? loopCnt=10000;

? while(digitalRead(pin) != HIGH)

? {

? ? if(loopCnt-- == 0)

? ? {

//如果長(zhǎng)時(shí)間不返回高電平勋又,輸出個(gè)提示,重頭開始换帜。

? ? ? Serial.println("HIGH");

? ? ? goto bgn;

? ? }

? }

? //低電平響應(yīng)信號(hào)

? loopCnt=30000;

? while(digitalRead(pin) != LOW)

? {

? ? if(loopCnt-- == 0)

? ? {

//如果長(zhǎng)時(shí)間不返回低電平楔壤,輸出個(gè)提示,重頭開始惯驼。

? ? ? Serial.println("LOW");

? ? ? goto bgn;

? ? }

? }

//開始讀取bit1-40的數(shù)值?

? ? for(int i=0;i<40;i++)

? {

? ? while(digitalRead(pin) == LOW)

? ? {}

//當(dāng)出現(xiàn)高電平時(shí)蹲嚣,記下時(shí)間“time”

? ? time1 = micros();

? ? while(digitalRead(pin) == HIGH)

? ? {}

//當(dāng)出現(xiàn)低電平,記下時(shí)間祟牲,再減去剛才儲(chǔ)存的time

//得出的值若大于50μs隙畜,則為‘1’,否則為‘0’

//并儲(chǔ)存到數(shù)組里去

? ? if (micros() - time1? >50)

? ? {

? ? ? chr[i]=1;

? ? }else{

? ? ? chr[i]=0;

? ? }

? }


//濕度说贝,8位的bit议惰,轉(zhuǎn)換為數(shù)值

humi=chr[0]*128+chr[1]*64+chr[2]*32+chr[3]*16+chr[4]*8+chr[5]*4+chr[6]*2+chr[7];


//溫度,8位的bit乡恕,轉(zhuǎn)換為數(shù)值

temp=chr[16]*128+chr[17]*64+chr[18]*32+chr[19]*16+chr[20]*8+chr[21]*4+chr[22]*2+chr[23];

? //校對(duì)碼言询,8位的bit,轉(zhuǎn)換為數(shù)值

//tol=chr[32]*128+chr[33]*64+chr[34]*32+chr[35]*16+chr[36]*8+chr[37]*4+chr[38]*2+chr[39];

//輸出:溫度傲宜、濕度运杭、校對(duì)碼

? Serial.print("temp:");

? Serial.println(temp);

? Serial.print("humi:");

? Serial.println(humi);


}

需要說(shuō)明的是:

ESP32的電壓是3.3v,導(dǎo)致帶DHT11傳感器時(shí)函卒,用dht11庫(kù)會(huì)出現(xiàn)校驗(yàn)錯(cuò)誤辆憔。因此,采用自寫代碼形式,完成溫濕度采集虱咧。還沒有仔細(xì)分析二者代碼有什么不同熊榛。

實(shí)際使用時(shí),發(fā)現(xiàn)一個(gè)問(wèn)題:發(fā)布的消息和收到的消息會(huì)不同步彤钟,發(fā)布消息后来候,要3個(gè)周期才能收到自己發(fā)布的消息。很奇怪逸雹。

---------------------

作者:wengduke

來(lái)源:CSDN

原文:https://blog.csdn.net/wengduke/article/details/80889364

版權(quán)聲明:本文為博主原創(chuàng)文章营搅,轉(zhuǎn)載請(qǐng)附上博文鏈接!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末梆砸,一起剝皮案震驚了整個(gè)濱河市转质,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌帖世,老刑警劉巖休蟹,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異日矫,居然都是意外死亡赂弓,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門哪轿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)盈魁,“玉大人,你說(shuō)我怎么就攤上這事窃诉⊙畎遥” “怎么了?”我有些...
    開封第一講書人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵飘痛,是天一觀的道長(zhǎng)珊膜。 經(jīng)常有香客問(wèn)我,道長(zhǎng)宣脉,這世上最難降的妖魔是什么车柠? 我笑而不...
    開封第一講書人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮塑猖,結(jié)果婚禮上堪遂,老公的妹妹穿的比我還像新娘。我一直安慰自己萌庆,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開白布币旧。 她就那樣靜靜地躺著践险,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上巍虫,一...
    開封第一講書人閱讀 52,246評(píng)論 1 308
  • 那天彭则,我揣著相機(jī)與錄音,去河邊找鬼占遥。 笑死俯抖,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的瓦胎。 我是一名探鬼主播芬萍,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼搔啊!你這毒婦竟也來(lái)了柬祠?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤负芋,失蹤者是張志新(化名)和其女友劉穎漫蛔,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體旧蛾,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡莽龟,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了锨天。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片毯盈。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖绍绘,靈堂內(nèi)的尸體忽然破棺而出奶镶,到底是詐尸還是另有隱情,我是刑警寧澤陪拘,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布厂镇,位于F島的核電站,受9級(jí)特大地震影響左刽,放射性物質(zhì)發(fā)生泄漏捺信。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一欠痴、第九天 我趴在偏房一處隱蔽的房頂上張望迄靠。 院中可真熱鬧,春花似錦喇辽、人聲如沸掌挚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)吠式。三九已至陡厘,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間特占,已是汗流浹背糙置。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留是目,地道東北人谤饭。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像懊纳,于是被迫代替她去往敵國(guó)和親揉抵。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容