1回答

1收藏

[项目] 通过 ESP8266 就能简单完成的物联网项目

物联网 物联网 3762 人阅读 | 1 人回复 | 2019-02-18

本帖最后由 geren2014 于 2019-2-18 16:00 编辑

关于本篇文章的背景知识如 ESP8266 介绍、开发环境搭建等不做赘述。

这里使用的开发板为基于 ESP8266 芯片设计的 NodeMcu,开发环境为配置后的 Arduino IDE。

一、云端数据监控(DHT11 + NodeMcu +Dweet.io)
1. 前期准备

2. Dweet.ioDweet.io 是一个可以通过非常简易的方式为物联网设备提供通信服务(包括报警等)的云端平台。它不需要任何的设置或注册步骤,只要终端设备连接上互联网,即可直接发布或订阅数据。
通过 Dweet.io 提供的云端服务,可以很方便的将传感器数据发布到在线平台并实时地进行远程监控。
DweetingDweeting发送数据到云端,可以通过调用如下格式的 URL https://dweet.io/dweet/for/my-thing-name?hello=world&foo=bar
  1. $ http -b "https://dweet.io/dweet/for/rollingstarky?hello=world&foo=bar"
  2. {
  3.     "by": "dweeting",
  4.     "the": "dweet",
  5.     "this": "succeeded",
  6.     "with": {
  7.         "content": {
  8.             "foo": "bar",
  9.             "hello": "world"
  10.         },
  11.         "created": "2019-01-14T19:15:34.524Z",
  12.         "thing": "rollingstarky",
  13.         "transaction": "6af2b067-229f-4b40-9af9-23d22e438ecd"
  14.     }
  15. }
复制代码
注:上述代码示例中的 http 命令(类似于 curl,但更加友好)来自于 HTTPie 软件包
也可以在发送请求时通过 POST 方法提交合法的 JSON 数据。

Get Dweets获取最新发布的 dweet 可以访问如下格式的 URL:
https://dweet.io/get/latest/dweet/for/my-thing-name

而获取某个名字下所有的 dweets,则可以访问如下 URL:
https://dweet.io/get/dweets/for/my-thing-name

  1. $ http -b "https://dweet.io/get/dweets/for/rollingstarky"
  2. {
  3.     "by": "getting",
  4.     "the": "dweets",
  5.     "this": "succeeded",
  6.     "with": [
  7.         {
  8.             "content": {
  9.                 "foo": "bar",
  10.                 "hello": "world"
  11.             },
  12.             "created": "2019-01-14T19:15:34.524Z",
  13.             "thing": "rollingstarky"
  14.         },
  15.         {
  16.             "content": {
  17.                 "foo": "bar",
  18.                 "hello": "world"
  19.             },
  20.             "created": "2019-01-14T19:10:46.694Z",
  21.             "thing": "rollingstarky"
  22.         }
  23.     ]
  24. }
复制代码
好吧,发了两遍一样的内容。。。


3. 项目代码
主要是通过 DHT11 传感器获取室内的温湿度数据,再通过 ESP8266 将这些数据源源不断地发送至 Dweet.io 的云端平台。
代码如下:
  1. #include <ESP8266WiFi.h>
  2. #include "DHT.h"

  3. // WiFi parameters
  4. const char* ssid = "wifi-name";
  5. const char* password = "wifi-password";

  6. #define DHTPIN 5

  7. #define DHTTYPE DHT11

  8. // Initialize DHT sensor
  9. DHT dht(DHTPIN, DHTTYPE, 15);

  10. const char* host = "dweet.io";

  11. void setup() {
  12.   
  13.   Serial.begin(115200);
  14.   delay(10);
  15.   
  16.   dht.begin();

  17.   // Connecting to a WiFi network
  18.   Serial.println();
  19.   Serial.println();
  20.   Serial.print("Connecting to ");
  21.   Serial.println(ssid);
  22.   WiFi.begin(ssid, password);
  23.   while (WiFi.status() != WL_CONNECTED) {
  24.     delay(500);
  25.     Serial.print(".");
  26.   }

  27.   Serial.println("");
  28.   Serial.println("WiFi connected");  
  29.   Serial.println("IP address: ");
  30.   Serial.println(WiFi.localIP());
  31. }

  32. void loop() {

  33.   Serial.print("Connecting to ");
  34.   Serial.println(host);
  35.   
  36.   // Use WiFiClient class to create TCP connections
  37.   WiFiClient client;
  38.   const int httpPort = 80;
  39.   if (!client.connect(host, httpPort)) {
  40.     Serial.println("connection failed");
  41.     return;
  42.   }
  43.    
  44.   // Reading temperature and humidity
  45.   float h = dht.readHumidity();
  46.   float t = dht.readTemperature();
  47.   
  48.   while (isnan(h) || isnan(t)) {
  49.     Serial.println("Failed to read from DHT sensor!");
  50.     delay(2000);
  51.    
  52.     // Get the measurements once more
  53.     h = dht.readHumidity();
  54.     t = dht.readTemperature();
  55.   }
  56.   
  57.     Serial.println();
  58.     Serial.println("The temperature and humidity are:");
  59.     Serial.println(t);
  60.     Serial.println(h);
  61.   
  62.     // Send the request to the server
  63.     client.print(String("GET /dweet/for/rollingstarkyesp8266?temperature=") + String(t) + "&humidity=" + String(h) + " HTTP/1.1\r\n" +
  64.                  "Host: " + host + "\r\n" +
  65.                  "Connection: close\r\n\r\n");
  66.     unsigned long timeout = millis();
  67.     while (client.available() == 0) {
  68.       if (millis() - timeout > 5000) {
  69.         Serial.println(">>> Client Timeout !");
  70.         client.stop();
  71.         return;
  72.     }
  73.   }
  74.   
  75.     // Read all the lines of the reply from server and print them to Serial
  76.     while(client.available()){
  77.       String line = client.readStringUntil('\r');
  78.       Serial.print(line);
  79.     }
  80.     Serial.println();
  81.     Serial.println("closing connection");
  82.     Serial.println();
  83.   
  84.   // Repeat every 10 seconds
  85.   delay(10000);
  86. }
复制代码
根据自己的实际情况修改上述代码中的 Wi-Fi 连接信息,之后上传至 NodeMcu 并运行。
通过浏览器访问以下链接 http://dweet.io/follow/my-thing-name (代码中的 my-thing-name 为 rollingstarkyesp8266,可以自行修改),效果如下:


4. freeboardfreeboard 是一个开源的仪表盘应用,可以通过非常简单的操作,为物联网系统提供实时的、交互式的仪表盘和可视化效果。
freeboard 可以直接读取上传到 Dweet.io 上的传感器数据,并将这些数据通过“漂亮”的图表展示出来。
首先进入 freeboard 官网 创建账户并登录,新建一个仪表板。
参考下图添加位于 Dweet.io 上的数据源


添加面板和插件:


这里可以选择多种类型的插件,如 GaugeSparkline 等。实际操作并不复杂,自行摸索一下即可。最终效果如下:



呃,我又对着传感器哈气了,为了曲线好看一点。。。实际温湿度变化没有这么明显(温度一直保持在11℃。没错,这就是我的冬日卧室)。
二、远程控制物联网设备(NodeMcu + PubSubClient + aREST)
本项目源代码如下:
  1. // Import required libraries
  2. #include <ESP8266WiFi.h>
  3. #include <PubSubClient.h>
  4. #include <aREST.h>

  5. // Clients
  6. WiFiClient espClient;
  7. PubSubClient client(espClient);

  8. // Create aREST instance
  9. aREST rest = aREST(client);

  10. // Unique ID to identify the device for cloud.arest.io
  11. char* device_id = "wuwu380";

  12. // WiFi parameters
  13. const char* ssid = "wifi-name";
  14. const char* password = "wifi-password";

  15. // Callback functions
  16. void callback(char* topic, byte* payload, unsigned int length);

  17. void setup(void)
  18. {
  19.   // Start Serial
  20.   Serial.begin(115200);

  21.   // Set callback
  22.   client.setCallback(callback);

  23.   // Give name and ID to device
  24.   rest.set_id(device_id);
  25.   rest.set_name("devices_control");

  26.   // Connect to WiFi
  27.   WiFi.begin(ssid, password);
  28.   while (WiFi.status() != WL_CONNECTED) {
  29.     delay(500);
  30.     Serial.print(".");
  31.   }
  32.   Serial.println("");
  33.   Serial.println("WiFi connected");

  34.   // Set output topic
  35.   char* out_topic = rest.get_topic();
  36. }

  37. void loop() {

  38.   // Connect to the cloud
  39.   rest.handle(client);
  40. }

  41. // Handles message arrived on subscribed topic(s)
  42. void callback(char* topic, byte* payload, unsigned int length) {
  43.   rest.handle_callback(client, topic, payload, length);
  44. }
复制代码
代码编译执行前,Arduino IDE 需要先安装 aRESTPubSubClient 库。
aREST 框架可以为一些常见的嵌入式开发板提供 RESTful 接口,支持通过串口、Wi-Fi、以太网、蓝牙等硬件发送命令至开发板,激发特定的操作,并将数据以 JSON 的格式返回给控制端用户(可以参考 Arduino IDE 搭建 ESP8266 开发环境及项目演示)。
cloud.arest.io 上部署着云端版本的 aREST 框架,可以绑定用户联网设备,并通过 MQTT 协议以消息订阅和发布的模式在客户端设备和服务器之间传输数据,最终完成对远程设备的控制。
运行效果如下:

  1. $ http -b https://cloud.arest.io/wuwu380/name
  2. {
  3.     "connected": true,
  4.     "hardware": "esp8266",
  5.     "id": "wuwu380",
  6.     "name": "devices_control",
  7.     "variables": {}
  8. }

  9. $ http -b https://cloud.arest.io/wuwu380/mode/5/o
  10. {
  11.     "connected": true,
  12.     "hardware": "esp8266",
  13.     "id": "wuwu380",
  14.     "message": "Pin D5 set to output",
  15.     "name": "devices_control"
  16. }

  17. $ http -b https://cloud.arest.io/wuwu380/digital/5/1
  18. {
  19.     "connected": true,
  20.     "hardware": "esp8266",
  21.     "id": "wuwu380",
  22.     "message": "Pin D5 set to 1",
  23.     "name": "devices_control"
  24. }
复制代码
仪表盘
cloud.arest.io 还提供了一个很简易的仪表板 Dashboard (虽然在交互设计上感觉有点不友好。。),可以自己尝试下。我这里只把实际效果贴一下:
aREST Dashboard 配置
aREST Dashboard 效果



内容有点多感觉,,先告一段落了



作者:rollingstarky
來源:简书


分享到:
回复

使用道具 举报

回答|共 1 个

倒序浏览

沙发

iwha

发表于 2019-2-24 15:27:28 | 只看该作者

真是好教程, 多谢
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条