利用 esp8266 搭建简单物联网项目

摘要:
连接到博客,这次是关于esp8266--˃物联网I.云数据监控:DHT11+NodeMcu+Dweet.io连接到具有相同布线和相关配置的博客。Dweet.ioDweet.io是一个云平台,可以以非常简单的方式为物联网设备提供通信服务。它不需要任何设置或注册步骤。只要终端设备连接到互联网,就可以直接发布或订阅数据。Hello=world&foo=barGetDeeting获取最新发布的数据表:https://dweet.io/get/latest/dweet/for/my-thing-name使用以下名称获取所有数据表:https://dweet.io/get/dweets/for/my-thing-name$http-b“https://dweet.io/get/dweets/for/rollingstarky“{”by“:”get“,”the“:”sleeps“,”this“:”successed“,”with“:[{”content“:{”foo“:”bar“,”hello“:”world“},”created“:”2020-09-25T16:30:34.524Z“,”content”:“rollingstarky”},{”content“:”foo”:”bar”,“hello”:“world”},”created:“2020-09-20T16:10:46.694Z”,”thing“:”rollingstarsky“}]}项目代码将上一个日志的数据传输到Dweet.io云平台#包括 #include“DHT.h”//WiFiparametersconstchar*ssid=“wifi名称”;constchar*password=“wifi密码”#定义DHTPIN5#定义DHTTYPEDHT11//初始化DHT传感器DHTdht;constchar*host=“dweet.io”;voisesetup(){Serial.begin;delay;dht.begin();//连接到WiFinetworkSerial.println();Serial.printin();串行.println;串行.print;WiFi.begin()!

上一篇博客,这次还是关于 esp8266 --> 物联网

一、云端数据监控:DHT11 + NodeMcu +Dweet.io
  1. 接上一篇博客的接线及相关配置不变( DHT11 + NodeMcu )
  2. 配置 Dweet.io

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

  • Dweeting(发送数据到云端)

    • 调用URL: https://dweet.io/dweet/for/my-thing-name?hello=world&foo=bar
  • Get Dweeting

    • 获取最新发布的 dweet : https://dweet.io/get/latest/dweet/for/my-thing-name
    • 获取某个名字下所有的 dweets : 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": "2020-09-25T16:30:34.524Z",
            "thing": "rollingstarky"
        },
        {
            "content": {
                "foo": "bar",
                "hello": "world"
            },
            "created": "2020-09-25T16:10:46.694Z",
            "thing": "rollingstarky"
        }
    ]
}
  1. 项目代码
    把上篇博客的数据传送到 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
" +
                 "Host: " + host + "
" + 
                 "Connection: close

");
    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('
');
      Serial.print(line);
    }
    Serial.println();
    Serial.println("closing connection");
    Serial.println();
  
  // Repeat every 10 seconds
  delay(10000);
}
  • 访问最新更新的数据:https://dweet.io/get/latest/dweet/for/rollingstarkyesp8266

{"this":"succeeded","by":"getting","the":"dweets","with":[{"thing":"rollingstarkyesp8266","created":"2020-09-25T09:27:22.823Z","content":{"temperature":28,"humidity":61}}]}

  • 访问全部数据:https://dweet.io/get/dweets/for/rollingstarkyesp8266

{"this":"succeeded","by":"getting","the":"dweets","with":[{"thing":"rollingstarkyesp8266","created":"2020-09-25T09:17:04.292Z","content":{"temperature":27.9,"humidity":58}},{"thing":"rollingstarkyesp8266","created":"2020-09-25T09:15:08.961Z","content":{"temperature":27.9,"humidity":59}},{"thing":"rollingstarkyesp8266","created":"2020-09-25T09:13:16.383Z","content":{"temperature":27.9,"humidity":58}},{"thing":"rollingstarkyesp8266","created":"2020-09-25T09:11:30.363Z","content":{"temperature":27.9,"humidity":57}},{"thing":"rollingstarkyesp8266","created":"2020-09-25T09:09:43.309Z","content":{"temperature":27.9,"humidity":57}}]}

  • 访问可视化图表:http://dweet.io/follow/rollingstarkyesp8266
    • 这里有点见鬼,出不来,单数据应该是都到云端了,可以查到 Json 数据(假装有图有真相)
      利用 esp8266 搭建简单物联网项目第1张
  1. 链接 freeboard 平台(仪表盘)
  • 注册
  • 懂点英语,稍微摸索一下
    利用 esp8266 搭建简单物联网项目第2张
二、远程控制物联网设备:NodeMcu + PubSubClient + aREST

1.准备工具

  • aREST 库

aREST 框架可以为一些常见的嵌入式开发板提供 RESTful 接口,支持通过串口、Wi-Fi、以太网、蓝牙等硬件发送命令至开发板,激发特定的操作,并将数据以 JSON 的格式返回给控制端用户

  • PubSubClient 库
  1. 源代码
// 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);
}
  1. 运行结果

$ 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"
}

三、 esp8266 连接 OLED 屏,制作天气时钟

我一开始就想做这个了hhh,今天先做个hello world

  1. 必要组件
  • U8G2 屏幕驱动库
  • 可以去 Ardunio 下载,也可以把 U8g2.rar 解压到 libraries 文件夹
  1. 接线

| OLED | NodeMcu |
| : - : | : - : |
| GND | GND |
| VCC | 3V3/5V |
| SCL | D1 |
| SDA | D2 |

  1. 载入程序
    文件 - 示例 - U8G2 - full_buffer - 任意打开一个,去除下行代码的注释
    U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
    写入程序
    利用 esp8266 搭建简单物联网项目第3张

一切的学习,都是从 hello world 开始的[Doge]

免责声明:文章转载自《利用 esp8266 搭建简单物联网项目》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇PowerShell初探GBDT详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

esp8266物联网开发一:MicroPython初战江湖

用esp8266做的物联网开发,涉及到固件烧写,固件擦除,代码编写等方面,做一一记录。 1. 固件烧写 首先,下载固件烧写工具:https://www.espressif.com/sites/default/files/tools/flash_download_tools_v3.6.3.rar,具体烧写包我会在后面附加。 然后,下载micropython固...

microPython环境安装及使用

1.ESP8266_12E(NodeMCU1.0)(AI Thinker)板Arduino IDE环境安装 (1)方法1(自动安装,windows,mac,linux平台都可)http://arduino.esp8266.com/stable/package_esp8266com_index.json(2)方法2(手工安装库和工具,mac,linux)(a...

ESP8266-Arduino杀手?

Arduino之所以流行可能是因为它的学习曲线比较平缓,另外是支持它的第三方程序库非常多,无论在什么平台上都比较容易入门。多年前我曾和一些搞嵌入开发多年的朋友请教,他们更建议我多点尝试STM的开发,Arduino只能在实验室中玩玩很难产品化的,主要原因是由于芯片生产授权和Arduino嵌入到产品时很多时候都需要添加外围电路而导致生产成本很高。而对于我这种一...

ESP8266-模拟输出(PWM)

PWM(Pulse Width Modulation,脉宽调制),是在保持波的频率不变的同时改变脉宽的技术 首先,我们来理解一下占空比。一个脉冲周期由一个ON周期(VCCC)和一个OFF周期(GND)组成。一段时间内ON周 期占据脉冲周期的比例就叫做占空比。例如,一个10ms的脉冲保持ON 2ms,那么根据公式,占空比是20% NodeMcu PWM引...

8266station初始化和智能配网问题

代码思路是: //station //Esp8266_Station_mode_init(); wifi_set_opmode(STATION_MODE); //Station_mode //定时器任务创建 Esp8266_Timer1_init(1000,1); //1000ms, 1:重复定时 /**...

esp8266物联网开发四:MQTT再论部控

之前利用点灯科技的库来使小爱同学控制LED的过程中,我们大略提到了一下MQTT的整体流程,由于其MQTT服务器是由点灯科技提供的,所以对其中的很多连接细节,我们并不知道,本节我们准备通过搭建本地的MQTT服务器,然后通过MQTT Client向MQTT服务器发送控制命令,来控制我们的LED灯。 首先,我们需要启动MQTT服务器,启动方式我们就不需要多说了,...