Giter VIP home page Giter VIP logo

iot-hardware's People

Contributors

zibuyu1995 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

ossend

iot-hardware's Issues

How to Use MQTT With the Raspberry Pi and ESP8266

使用 NodeMCU 和 DHT11 传感器通过 MQTT 上传温湿度数据

简介

本示例将演示如何通过 NodeMCU, DHT11 收集温湿度并通过 MQTT 协议将数据上报到 EMQX MQTT broker。
DHT11数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传感器,
Node MCU 底层集成了 ESP8266, 能提供完整且自成体系的Wi-Fi网络解决方案,
MQTT 是基于 发布(Publish)/订阅(Subscribe) 模式来进行通信及数据交换的,
EMQX 是基于 Erlang 的 MQTT broker 在开源社区中是最成熟的 MQTT 消息中间件, 它易于配置和使用,可很好地扩展。

配置

硬件配置
  • NodeMCU board x 1
  • DHT11 temperature/humidity sensor x 1
  • Breadboard x 1
  • jumper wires
  • Connection Graph:
    image
Arduino 配置
  • 下载并安装 CH340G USB 驱动
  • 安装 Esp8266模块
  • 安装 PubSubClient 库(by Nick O'Leary)
    Sketch -> Include Library -> Manage Libraries... -> Type PubSub in Search field -> Install
MQTT broker 配置
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 配置

    image

运行

  • 代码上传

    将Node Mcu 通过 usb 连接到PC 并在Arduion IDE中选择 115200 端口,使用upload 按钮编译草图并将其上传到设备

  • 打开 Arduino monitor window 查看数据上报

    image

  • 打开 EMQX Dashboard 查看设备 Node Mcu 连接情况

    User: admin Password: public

    image

  • 利用 EMQX Websockt 工具查看DHT11 上报的温湿度数据

    image

  • 故障排除

    为了执行故障排除,然后将 USB 适配器与 PC 连接并在 Arduino IDE 中选择USB-TTL适配器的端口。打开“串行监视器”以查看由串行输出产生的调试信息。

refer

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

ActorCloud 简介

ActorCloud 是由杭州映云科技有限公司开发的物联网平台,基于EMQ X企业级消息中间件,是面向企业物联网的一站式支撑服务系统,能够协助企业在几天内实现对物联网设备与网络的全面管控,并建立起与企业IT流程融合的运维与运营体系。ActorCloud提供企业内部的私有部署能力,具有更强的可控性和安全性。

Actorcloud 使用:

  • 注册账户(注册地址) :
    image

  • 创建产品:
    填入产品名称, 以及选择协议,这里我们选择MQTT协议来做测试
    产品

  • 创建设备:
    image

  • 填入设备名称, 选择所属产品(其他选项可以依据情况填写),
    image

  • 选择设备认证方式: 这里我们选择token验证;
    填写设备信息: 填入设备编号,用户名和密码(用于树莓派连接actorcloud平台), 可以不填系统会自动生成36位的字符串

填写设备编号密码

  • 获取注册设备编号,登录名,密码(树莓派连接用到):
    设备编号密码

  • 创建温湿度功能点
    image

  • 创建数据流
    image

树莓派连接 Actorcloud 并上报温湿度数据:

  • 获取运行代码:
git clone https://github.com/zibuyu1995/RaspberryPi
  • 进入项目目录:
cd humiture
  • 安装包:
pip install -r pip install -r requirements.txt -i https://pypi.douban.com/simple/ 
  • 树莓派与DHT11连接

    树莓派GPIO图
    image
    树莓派GPIO口与DHT11连接示意图:
    image

    树莓派与DHT11连接图实物图:
    image

  • 修改配置文件:
    需要修改设备id 用户名 密码 温湿度上传间隔, gpio口(默认4), topic(数据流主题)

vi config.py
  • 运行:
python mqtt_publish.py
  • actorcloud 查看温度和湿度图:

    设备事件
    image
    温湿度折线图
    image
    仪表图
    image

Driving LED on a Raspberry Pi via actorcloud

通过actorcloud 驱动树莓派LED

ActorCloud 简介

ActorCloud 是由杭州映云科技有限公司开发的物联网平台,基于EMQ X企业级消息中间件,是面向企业物联网的一站式支撑服务系统,能够协助企业在几天内实现对物联网设备与网络的全面管控,并建立起与企业IT流程融合的运维与运营体系。ActorCloud提供企业内部的私有部署能力,具有更强的可控性和安全性。

Actorcloud 使用:

  • 注册账户(注册地址) :
    image

  • 创建产品:
    填入产品名称, 以及选择协议,这里我们选择MQTT协议来做测试
    产品

  • 创建设备:
    image

  • 填入设备名称, 选择所属产品(其他选项可以依据情况填写),
    image

  • 选择设备认证方式: 这里我们选择token验证;
    填写设备信息: 填入设备编号,用户名和密码(用于树莓派连接actorcloud平台), 可以不填系统会自动生成36位的字符串

填写设备编号密码

  • 获取注册设备编号,登录名,密码(树莓派连接用到):
    设备编号密码

actorcloud 驱动树莓派LED

  • 获取运行代码:
git clone https://github.com/zibuyu1995/RaspberryPi
  • 进入项目目录:
cd led
  • 安装包:
pip install -r pip install -r requirements.txt -i https://pypi.douban.com/simple/ 
  • 树莓派与DHT11连接

    树莓派GPIO图
    GPIO
    树莓派GPIO口与LED连接示意图:
    Building

    树莓派与LED连接图实物图:
    image

  • 修改配置文件:
    需要修改设备id 用户名 密码 温湿度上传间隔, gpio口(默认4), topic(数据流主题)

     vi config.py
    
  • 运行:

    python mqtt_subscribe.py
    
  • actorcloud 控制树莓派LED turn or off:

    登录actorcloud -> 设备详情 -> 设备控制 -> 添加指令 -> 自定义指令 输入如下指令

    # led 灯开
    { "led": true }
     # led 灯关
    { "led": false }
  • 查看下发记录:
    image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.