RestAPI读取温湿度
# PinToo之RestAPI读取温湿度
# 1. 说明
范例采用RestAPI通讯,来读取连接到NodeMCU的DHT22温湿度传感器的数据。NodeMCU是以ESP8266(ESP12)晶片为基础,包含了WiFi,GPIO,PWM,ADC,I2C等功能的主控板,执行效率高,非常适合物联网应用开发,因为它内建了WiFi功能,与Arduino相容,Arduino中可用的传感器基本都可用于NodeMCU。范例中使用的DHT22传感器正极(长脚)连接接至NodeMCU Vin针脚,负极连接至NodeMCU GND针脚,信号针脚连接至NodeMCU D5针脚。
范例中使用的NodeMCU的ESP8266无线网络,先从路由器获取IP,连接网络成功后,启动Rest服务,通过访问对应的地址获取温湿度数据。
通过范例学习,可以掌握Rest通讯的基本通讯原理,并结合NodeMCU开发板进行LED的控制功能。
# 2. 零件连接图

# 3. 使用零件
序 | 零件名称 | 数量 |
---|---|---|
1 | NodeMCU开发板 | 1 |
2 | USB数据线 | 1 |
3 | 面包板 | 1 |
4 | 杜邦线 | 若干 |
6 | DHT22温湿度传感器 | 1 |
# 4. Arduino流程图

# 5. Arduino程序
使用Arduino IDE 编译并上传以下Arduino程序。
// 使用 NodeMCU 开发板
// aREST
//https://github.com/marcoschwartz/aREST
//aRESTUI
//https://github.com/marcoschwartz/aREST_UI
//DHT Library
//https://github.com/adafruit/DHT-sensor-library
#include <ESP8266WiFi.h>
#include <aREST.h>
#include <aREST_UI.h>
#include <DHT.h>
#define DHT_PIN 14 //定义LED针脚D5
#define DHTTYPE DHT22 // 定义温湿度传感器类型 DHT22
DHT dht(DHT_PIN, DHTTYPE);//初始化传感器
// 创建aREST实例
aREST_UI rest = aREST_UI();
// WiFi连接参数
const char* ssid = "WIFI_SSID";
const char* password = "WIFI_PASSWORD";
// 监听TCP连接
#define LISTEN_PORT 80
// 创建WiFi服务器
WiFiServer server(LISTEN_PORT);
// 定义传递给API的变量
float temperature;
float humidity;
void setup(void) {
// 开启串口
Serial.begin(9600);
// 设定标题
rest.title("aREST DHT Sensor");
// 初始化 DHT
dht.begin();
// 初始化变量,并将其传递给REST API
rest.variable("temperature", &temperature);
rest.variable("humidity", &humidity);
// 设定标签
rest.label("temperature");
rest.label("humidity");
// Give name and ID to device
rest.set_id("1");
rest.set_name("esp8266");
// 给设备定义ID与名称
rest.set_id("1");
rest.set_name("esp8266");
//指定IP位址,请自行在此加入WiFi.config()叙述。
WiFi.config(IPAddress(192,168,0,171), // IP地址
IPAddress(192,168,0,1), // 网关地址
IPAddress(255,255,255,0)); // 子网掩码
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// 启动服务器
server.begin();
Serial.println("Server started");
// 输出IP地址
Serial.println(WiFi.localIP());
}
void loop() {
//读取温湿度
humidity = dht.readHumidity();
temperature = dht.readTemperature();
// 响应aREST
WiFiClient client = server.available();
if (!client) {
return;
}
while (!client.available()) {
delay(1);
}
rest.handle(client);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 6. 设计明细
开启PinToo设计器,分别加入下插图之控件。或者点击左上角的[打开模板Lib文件]
选择模板文件来打开对应模板。

①:TfxWidgetLCDLabel组件,控件名称为fxWidgetLCDLabel1
。
②:TfxWidgetLCDLabel组件,控件名称为fxWidgetLCDLabel2
。
③:TfxSwitch组件,控件名称为fxSwitch1
。
fxRunFrame属性设置
Height
:设置页面高度=800
。Width
:设置页面宽度=400
。
fxTimer1属性设置
Interval
:设置计时器触发的时间间隔,单位为ms,设置为5000
。Enabled
:设置是否启用,设置为False
。
①fxWidgetLCDLabel1属性设置
Height
:设置控件高度=95
。Width
:设置控件宽度=285
。Color
:设置背景颜色=Black
。
②fxWidgetLCDLabel2属性设置
Height
:设置控件高度=95
。Width
:设置控件宽度=285
。Color
:设置背景颜色=Black
。
# 7. 程序设计
点击程序设计界面右下角的按钮,切换至单元选择界面,勾选需要使用的单元。该程序需要引用ufxFunctions
、JSON
单元。
# 7.1. 程序初始设置
该程序无初始设置。
# 7.2. 事件设置
- fxTimer1-OnTimer事件
计时器定时触发,每隔五秒读取温湿度数据并显示。
Procedure fxTimer1OnTimer(Sender: TObject);
//计时器定时读取温湿度
var
hum :String;
tmp :String;
JSONStringHUM: String;
JSONStringTMP: String;
J: TJSONObject;
ValueHUM: TJSONValue;
ValueTMP: TJSONValue;
Begin
J := TJSONObject.Create;
Try
JSONStringTMP := fxfun.UrlDecode(fxfun.NetHttpGet('http://192.168.0.171/temperature'));
JSONStringHUM := fxfun.UrlDecode(fxfun.NetHttpGet('http://192.168.0.171/humidity'));
ValueTMP := fxfun.ParseJSONObject(JSONStringTMP);
ValueHUM := fxfun.ParseJSONObject(JSONStringHUM);
tmp := TJSONObject(ValueTMP).Get(0).JsonValue.Value;
hum := TJSONObject(ValueHUM).Get(0).JsonValue.Value;
fxWidgetLCDLabel1.Caption.Value := StrToFloat(tmp);
fxWidgetLCDLabel2.Caption.Value := StrToFloat(hum);
Finally
//Except {ErrorMsg / RaiseMsg(Const Error:String)}
J.Free;
End;
End;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- ③fxSwitch1-OnSwitch事件
点击开关,打开/关闭计时器。
Procedure fxSwitch1OnSwitch(Sender: TObject);
//开启、关闭温湿度读取
Begin
fxTimer1.Enabled := fxSwitch1.IsChecked;
if fxSwitch1.IsChecked = False Then
begin
fxWidgetLCDLabel1.Caption.Value := 0;
fxWidgetLCDLabel2.Caption.Value := 0;
End;
End;
Begin
fxWidgetLCDLabel1.Caption.FillOff.Color := NULL;
fxWidgetLCDLabel2.Caption.FillOff.Color := NULL;
fxWidgetLCDLabel1.Caption.Format := '000.00';
fxWidgetLCDLabel2.Caption.Format := '000.00';
End.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 8. 运行结果
使用鼠标在 PinToo 菜单,点击[保存至数据库]
按钮,将其保存至数据库,点击[调试运行]
确认能够正常打开。

通过同步中心,将程序上传至手机PinToo运行;同步时,请确保手机已经运行PinToo,并且已经登陆。

打开开关,程序每隔五秒读取温湿度数据并显示在仪表中。
