(1)先安装ESP8266支持
文件->首选项->附加开发板管理器地址
添加http://arduino.esp8266.com/stable/package_esp8266com_index.json
(2)点击工具 - 开发板 - 开发板管理器,进入开发板管理器界面:找到 esp8266 并安装;
(3)工具->开发板-。选择如下图
(4)下载外库包
地址: https://github.com/JINYU-ZHOU/MQTT-DHT11.git(如果有其他问题欢迎留言)
点项目->加载库->添加.zip库->选择你下载好的压缩包,下面窗口回提示你是否安装成果!
看了很多配网的教程,我觉得PubSubClient库实例特别兼容,. 此库可以在Arduino IDE的库管理器中找到。工具->管理库->找到PubSubClient根据自己想要的版本下载,之后打开实例
你只需加上自己要连接网的名字和密码以及你要连接MQTT服务器的地址
const char* ssid = "........";//你的WIFI名称
const char* password = "........";//你的WIFI密码
const char* mqtt_server = "broker.mqtt-dashboard.com";
//你要连接的MQTT服务器地址如const char* mqtt_server = "123.57.133.11";
如果你不间接使用Arduino,直接连接ESP8266,需要提前下载CH340或者CP2102的驱动,
我用的CP2102,如果需要可以留言我发邮箱。
前提下载DHT11相应的库包,还是上面那个地址。这是源代码,只是单单用来测试温湿度模块,后续订阅还需要进行组装。
#include "DHT.h"
#define DHTPIN 5
#define DHTTYPE DHT11
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE, 15);
void setup() {
// Start Serial
Serial.begin(115200);
// Init DHT
dht.begin();
}
void loop() {
// Reading temperature and humidity
float h = dht.readHumidity();
float t = dht.readTemperature();
// Display data
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
// Wait a few seconds between measurements.
delay(2000);
}
我之前的博文有提过订阅加发布代码,如果想了解更多可以点击MQTT+ardunio+ESP8266开发
简单发布信息的部分代码如下,全部代码参考我之前的博文。
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 20000) {
//延时
lastMsg = now;
++value;
snprintf (msg, 75, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}
这次需要将snprintf (msg, 75, "hello world #%ld", value);
中间的hello world #%ld
替换成我们的温湿度数据
能直接这样吗?
void loop() {
// Reading temperature and humidity
float h = dht.readHumidity();
float t = dht.readTemperature();
void loop() {
...
snprintf (msg, 75, h, value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}
// Wait a few seconds between measurements.
delay(2000);
}
如果这样直接上传就会报错!!!可以了解snprintf这个函数,第三个变量为const char
那么有个好方法,把你的温湿度数据变成字符串
String data = "{\"H\":" + String(h) +",\"T\":" + String(t)+"}";
我们利用string函数将字符串转换为const char 类型
strcpy(c,data.c_str());//char c[50];
最后附上总代码
#include
#include
#include "DHT.h"
#define DHTPIN 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE, 15);
// Update these with values suitable for your network.
const char* ssid = "JINYU";
const char* password = "150";
const char* mqtt_server = "123.57.133.11";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
dht.begin();
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
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(".");
}
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 acive low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
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 loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
char c[50];
// Display data
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
// Wait a few seconds between measurements.
delay(2000);
String data =String(h) +"," + String(t) ;
strcpy(c,data.c_str());
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 75, c, value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}
}
欢迎大家批评指正,我也是初学者,记录每一次简单的进步。
特此感谢为我提供帮助的学长们和同学!
希望早日成为大佬的小跟班~