通过 ESP8266 搭建简单的物联网项目

关于本篇文章的背景知识如 ESP8266 介绍、开发环境搭建等可以参考之前写的 Arduino IDE 搭建 ESP8266 开发环境及项目演示,或者浏览网络上的其他文章,不做赘述。
这里使用的开发板为基于 ESP8266 芯片设计的 NodeMcu,开发环境为配置后的 Arduino IDE

一、云端数据监控(DHT11 + NodeMcu +Dweet.io)

1. 前期准备
6875152-992e0bc53660d51c.png
NodeMcu & DHT11 线路连接
2. Dweet.io

Dweet.io 是一个可以通过非常简易的方式为物联网设备提供通信服务(包括报警等)的云端平台。它不需要任何的设置或注册步骤,只要终端设备连接上互联网,即可直接发布或订阅数据。
通过 Dweet.io 提供的云端服务,可以很方便的将传感器数据发布到在线平台并实时地进行远程监控。

Dweeting

Dweeting发送数据到云端,可以通过调用如下格式的 URL https://dweet.io/dweet/for/my-thing-name?hello=world&foo=bar

$ http -b "https://dweet.io/dweet/for/rollingstarky?hello=world&foo=bar"
{
    "by": "dweeting",
    "the": "dweet",
    "this": "succeeded",
    "with": {
        "content": {
            "foo": "bar",
            "hello": "world"
        },
        "created": "2019-01-14T19:15:34.524Z",
        "thing": "rollingstarky",
        "transaction": "6af2b067-229f-4b40-9af9-23d22e438ecd"
    }
}

注:上述代码示例中的 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

$ http -b "https://dweet.io/get/dweets/for/rollingstarky"
{
    "by": "getting",
    "the": "dweets",
    "this": "succeeded",
    "with": [
        {
            "content": {
                "foo": "bar",
                "hello": "world"
            },
            "created": "2019-01-14T19:15:34.524Z",
            "thing": "rollingstarky"
        },
        {
            "content": {
                "foo": "bar",
                "hello": "world"
            },
            "created": "2019-01-14T19:10:46.694Z",
            "thing": "rollingstarky"
        }
    ]
}

好吧,发了两遍一样的内容。。。

3. 项目代码

主要是通过 DHT11 传感器获取室内的温湿度数据,再通过 ESP8266 将这些数据源源不断地发送至 Dweet.io 的云端平台。

代码如下:

#include <ESP8266WiFi.h>
#include "DHT.h"

// WiFi parameters
const char* ssid = "wifi-name";
const char* password = "wifi-password";

#define DHTPIN 5

#define DHTTYPE DHT11

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

const char* host = "dweet.io";

void setup() {
  
  Serial.begin(115200);
  delay(10);
  
  dht.begin();

  // Connecting to a WiFi network
  Serial.println();
  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 loop() {

  Serial.print("Connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
    
  // Reading temperature and humidity
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  while (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    delay(2000);
    
    // Get the measurements once more
    h = dht.readHumidity(); 
    t = dht.readTemperature();
  }
  
    Serial.println();
    Serial.println("The temperature and humidity are:");
    Serial.println(t);
    Serial.println(h);
  
    // Send the request to the server
    client.print(String("GET /dweet/for/rollingstarkyesp8266?temperature=") + String(t) + "&humidity=" + String(h) + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" + 
                 "Connection: close\r\n\r\n");
    unsigned long timeout = millis();
    while (client.available() == 0) {
      if (millis() - timeout > 5000) {
        Serial.println(">>> Client Timeout !");
        client.stop();
        return;
    }
  }
  
    // Read all the lines of the reply from server and print them to Serial
    while(client.available()){
      String line = client.readStringUntil('\r');
      Serial.print(line);
    }
    Serial.println();
    Serial.println("closing connection");
    Serial.println();
  
  // Repeat every 10 seconds
  delay(10000);
}

根据自己的实际情况修改上述代码中的 Wi-Fi 连接信息,之后上传至 NodeMcu 并运行。

通过浏览器访问以下链接 http://dweet.io/follow/my-thing-name (代码中的 my-thing-namerollingstarkyesp8266,可以自行修改),效果如下:

6875152-5333b977fddea17d.png
Dweet.io

4. freeboard

freeboard 是一个开源的仪表盘应用,可以通过非常简单的操作,为物联网系统提供实时的、交互式的仪表盘和可视化效果。
freeboard 可以直接读取上传到 Dweet.io 上的传感器数据,并将这些数据通过“漂亮”的图表展示出来。

首先进入 freeboard 官网 创建账户并登录,新建一个仪表板。
参考下图添加位于 Dweet.io 上的数据源

6875152-5bb461a98e4b4359.png
datasource

添加面板和插件:


6875152-2af8886bb6c96bb0.png
Widget

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

6875152-77c3c715224a1c7b.png
freeboard

呃,我又对着传感器哈气了,为了曲线好看一点。。。实际温湿度变化没有这么明显(温度一直保持在11℃。没错,这就是我的冬日卧室)。

二、远程控制物联网设备(NodeMcu + PubSubClient + aREST)

本项目源代码如下:

// Import required libraries
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>

// Clients
WiFiClient espClient;
PubSubClient client(espClient);

// Create aREST instance
aREST rest = aREST(client);

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

// WiFi parameters
const char* ssid = "wifi-name";
const char* password = "wifi-password";

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

void setup(void)
{
  // Start Serial
  Serial.begin(115200);

  // Set callback
  client.setCallback(callback);

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

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

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

void loop() {

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

// Handles message arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {
  rest.handle_callback(client, topic, payload, length);
}

代码编译执行前,Arduino IDE 需要先安装 aRESTPubSubClient 库。

aREST 框架可以为一些常见的嵌入式开发板提供 RESTful 接口,支持通过串口、Wi-Fi、以太网、蓝牙等硬件发送命令至开发板,激发特定的操作,并将数据以 JSON 的格式返回给控制端用户(可以参考 Arduino IDE 搭建 ESP8266 开发环境及项目演示)。

cloud.arest.io 上部署着云端版本的 aREST 框架,可以绑定用户联网设备,并通过 MQTT 协议以消息订阅和发布的模式在客户端设备和服务器之间传输数据,最终完成对远程设备的控制。

运行效果如下:

$ http -b https://cloud.arest.io/wuwu380/name
{
    "connected": true,
    "hardware": "esp8266",
    "id": "wuwu380",
    "name": "devices_control",
    "variables": {}
}

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

$ http -b https://cloud.arest.io/wuwu380/digital/5/1
{
    "connected": true,
    "hardware": "esp8266",
    "id": "wuwu380",
    "message": "Pin D5 set to 1",
    "name": "devices_control"
}
仪表盘

cloud.arest.io 还提供了一个很简易的仪表板 Dashboard (虽然在交互设计上感觉有点不友好。。),可以自己尝试下。我这里只把实际效果贴一下:

6875152-a7e189cc6ef2d602.png
aREST Dashboard 配置

6875152-8ce672764df520c2.png
aREST Dashboard 效果

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

参考资源

Internet of Things with ESP8266 (English Edition)
ESP8266 Internet of Things Cookbook

猜你喜欢

转载自blog.csdn.net/weixin_33752045/article/details/86949369