-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathcode.cpp
90 lines (81 loc) · 2.08 KB
/
code.cpp
1
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
#include <Arduino.h>
#include "DHT.h"
// DHT11 DATA 引脚连接的数字引脚编号
#define DHT_DATA_PIN 8
#define UPDATE_INTERVAL 10*1000
DHT dht11(DHT_DATA_PIN, DHT11, 1);
float t, h;
unsigned long start;
String get_data()
{
char c[100];
h = dht11.readHumidity();
t = dht11.readTemperature();
// sprintf 在 Arduino 中无法转换浮点数
dtostrf(t, 2, 2, c);
dtostrf(h, 2, 2, c+5);
return String(c);
}
boolean at_exec(char *data, char *keyword, unsigned long time_out)
{
Serial.println(data);
Serial.flush();
delay(100); // 等待响应
unsigned long start = millis();
while (Serial.available() < strlen(keyword))
{
if (millis() - start > time_out)
return false;
}
if (Serial.find(keyword))
return true;
else
return false;
while (Serial.available())
Serial.read(); //清空串口缓存
}
void setup()
{
Serial.begin(115200);
dht11.begin();
pinMode(LED_BUILTIN, OUTPUT);
while (!at_exec("AT+RST", "OK", 1000));
while (!at_exec("AT+CWMODE=1", "OK", 1000));
while (!at_exec("AT+CWQAP", "OK", 1000));
while (!at_exec("AT+CWJAP=\"HelloGithub\",\"PassWord\"", "WIFI CONNECTED", 2000));
while (!at_exec("AT+CIPSTART=\"TCP\",\"183.230.40.40\",1811", "CONNECT", 1000));
while (!at_exec("AT+CIPMODE=1", "OK", 500));
while (!at_exec("AT+CIPSEND", "OK", 500));
Serial.println("*产品ID#ILoveHelloGitHub#HG*");
start = millis();
}
// 根据从串口收到的 字符串 执行相应的指令
bool command_parse(String command){
command.trim();
command.toLowerCase();
if (command == "open")
{
digitalWrite(LED_BUILTIN, HIGH);
}else if (command == "close")
{
digitalWrite(LED_BUILTIN, LOW);
}
else if (command == "received");
}
void loop()
{
// 定时上报消息
if (millis() - start > UPDATE_INTERVAL)
{
String data = get_data();
Serial.println(data);
start = millis();
}
// 收到消息进行解析
if (Serial.available()){
delay(10); // 等待全部数据接收完毕
command_parse(Serial.readString());
while (Serial.available())
Serial.read(); //清空串口缓存
}
}