zibuyu1995 / iot-hardware Goto Github PK
View Code? Open in Web Editor NEWInternet of Things - Hardware
License: MIT License
Internet of Things - Hardware
License: MIT License
本示例将演示如何通过 NodeMCU, DHT11 收集温湿度并通过 MQTT 协议将数据上报到 EMQX MQTT broker。
DHT11数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传感器,
Node MCU 底层集成了 ESP8266, 能提供完整且自成体系的Wi-Fi网络解决方案,
MQTT 是基于 发布(Publish)/订阅(Subscribe) 模式来进行通信及数据交换的,
EMQX 是基于 Erlang 的 MQTT broker 在开源社区中是最成熟的 MQTT 消息中间件, 它易于配置和使用,可很好地扩展。
unzip emqx-macosx-v3.2.5.zip
cd emqx
./bin/emqx start
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#define DHTPIN D4 // what pin we're connected to
#define wifi_ssid "xxxxx"
#define wifi_password "xxxxx"
#define mqtt_server "broker.emqx.io" // emqx broker url
#define humidity_topic "humidity"
#define temperature_topic "temperature"
#define DHTTYPE DHT11 // DHT 11
WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
dht.begin();
}
void setup_wifi() {
delay(10);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("nodeMcuDHT11")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
bool checkBound(float newValue, float prevValue, float maxDiff) {
return newValue < prevValue - maxDiff || newValue > prevValue + maxDiff;
}
long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 1.0;
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 30000) {
// Wait a few seconds between measurements
lastMsg = now;
float newTemp = dht.readTemperature();
float newHum = dht.readHumidity();
if (checkBound(newTemp, temp, diff)) {
temp = newTemp;
Serial.print("New temperature:");
Serial.println(String(temp).c_str());
client.publish(temperature_topic, String(temp).c_str(), true);
}
if (checkBound(newHum, hum, diff)) {
hum = newHum;
Serial.print("New humidity:");
Serial.println(String(hum).c_str());
client.publish(humidity_topic, String(hum).c_str(), true);
}
}
}
按照以下操作编辑代码以适合您自己的WIFI和MQTT设置
WIFI 设置
#define wifi_ssid ""
#define wifi_password ""
EMQX MQTT broker 服务器设置
#define mqtt_server "broker.emqx.io"
Arduion 配置
代码上传
将Node Mcu 通过 usb 连接到PC 并在Arduion IDE中选择 115200
端口,使用upload
按钮编译草图并将其上传到设备
打开 Arduino monitor window 查看数据上报
打开 EMQX Dashboard 查看设备 Node Mcu 连接情况
User: admin Password: public
利用 EMQX Websockt 工具查看DHT11 上报的温湿度数据
故障排除
为了执行故障排除,然后将 USB 适配器与 PC 连接并在 Arduino IDE 中选择USB-TTL适配器的端口。打开“串行监视器”以查看由串行输出产生的调试信息。
http://www.teomaragakis.com/hardware/electronics/how-to-connect-an-esp8266-to-an-arduino-uno/
https://www.open-homeautomation.com/2016/06/10/program-an-esp8266-from-arduino-on-macos/
https://www.instructables.com/id/NodeMCU-IoT-Project-DHT11/
http://osoyoo.com/2018/01/17/nodemcu-dht11-mqtt/
https://codebender.cc/sketch:316691#esp8266%20dht11%20mqtt%20-%20working.ino
https://www.home-assistant.io/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt/
https://thingsboard.io/docs/samples/esp8266/temperature/
https://www.instructables.com/id/Wireless-Temperature-and-Humidity-Monitor-With-ESP/
https://create.arduino.cc/projecthub/thingsboard/temperature-dashboard-using-arduino-uno-esp8266-and-mqtt-5e26eb
https://codebender.cc/sketch:316691#esp8266%20dht11%20mqtt%20-%20working.ino
ActorCloud 是由杭州映云科技有限公司开发的物联网平台,基于EMQ X企业级消息中间件,是面向企业物联网的一站式支撑服务系统,能够协助企业在几天内实现对物联网设备与网络的全面管控,并建立起与企业IT流程融合的运维与运营体系。ActorCloud提供企业内部的私有部署能力,具有更强的可控性和安全性。
注册账户(注册地址) :
选择设备认证方式: 这里我们选择token验证;
填写设备信息: 填入设备编号,用户名和密码(用于树莓派连接actorcloud平台), 可以不填系统会自动生成36位的字符串
git clone https://github.com/zibuyu1995/RaspberryPi
cd humiture
pip install -r pip install -r requirements.txt -i https://pypi.douban.com/simple/
树莓派与DHT11连接
修改配置文件:
需要修改设备id 用户名 密码 温湿度上传间隔, gpio口(默认4), topic(数据流主题)
vi config.py
python mqtt_publish.py
ActorCloud 是由杭州映云科技有限公司开发的物联网平台,基于EMQ X企业级消息中间件,是面向企业物联网的一站式支撑服务系统,能够协助企业在几天内实现对物联网设备与网络的全面管控,并建立起与企业IT流程融合的运维与运营体系。ActorCloud提供企业内部的私有部署能力,具有更强的可控性和安全性。
注册账户(注册地址) :
选择设备认证方式: 这里我们选择token验证;
填写设备信息: 填入设备编号,用户名和密码(用于树莓派连接actorcloud平台), 可以不填系统会自动生成36位的字符串
git clone https://github.com/zibuyu1995/RaspberryPi
cd led
pip install -r pip install -r requirements.txt -i https://pypi.douban.com/simple/
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.