-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhumtemp.ino
305 lines (255 loc) · 9.65 KB
/
humtemp.ino
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "edp.c"
#include "DHT11.h"
DHT11 dht11;
#define DHT11PIN 4
#define KEY "QOkcfMBvmD8noLNeAMbPY1SBoB8=" //APIkey [需要修改]!!!
#define ID "757998631" //设备ID [需要修改]!!!
//#define PUSH_ID "680788"
#define PUSH_ID NULL
String comdata = "";
// 定义串口初始化
#define _baudrate 115200
#define WIFI_UART Serial
int DHT11 = 0;
//定义数码管端口
const int stbPin = 7; //the segment display module STB pin connected to digital pin 7
const int clkPin = 9; //the segment display module CLK pin connected to digital pin 9
const int dioPin = 8; //the segment display module DIO pin connected to digital pin 8
//定义数组代表0-9显示
uint8_t digits[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f };
edp_pkt *pkt;
/** doCmdOk 判断回复内容是否符合 * 发送命令至模块,从回复中获取期待的关键字 * keyword: 所期待的关键字 * 成功找到关键字返回true,否则返回false */
bool doCmdOk(String data, char *keyword)
{
bool result = false;
if (data != "") //对于tcp连接命令,直接等待第二次回复
{
WIFI_UART.println(data); //发送AT指令
}
if (data == "AT") //检查模块存在
delay(2000);
else
while (!WIFI_UART.available());
// 等待模块回复
delay(200);
if (WIFI_UART.find(keyword))
//返回值判断
{
result = true;
}
else
{
result = false;
}
while (WIFI_UART.available())
WIFI_UART.read();
//清空串口接收缓存
delay(500);
//指令时间间隔
return result;
}
//数码管初始化
void sendCommand(uint8_t value)
{
digitalWrite(stbPin, LOW);
shiftOut(dioPin, clkPin, LSBFIRST, value);
digitalWrite(stbPin, HIGH);
}
/*此部分为ESP8266连接WiFi */
void setup()
{
char buf[100] = {0};
int tmp;
pinMode(13, OUTPUT);
pinMode(8, OUTPUT); //用于连接EDP控制的发光二极管
WIFI_UART.begin( _baudrate );
pinMode(stbPin, OUTPUT);
pinMode(clkPin, OUTPUT);
pinMode(dioPin, OUTPUT);
sendCommand(0x8f); //activate
WIFI_UART.setTimeout(3000);
delay(3000);
Serial.setTimeout(100);
delay(2000);
while (!doCmdOk("AT", "OK"));
digitalWrite(13, HIGH);
while (!doCmdOk("AT+CWMODE=3", "OK"));
while (!doCmdOk("AT+CWJAP=\"awwwwman\",\"20050512\"", "OK"));//WiFi账号与 WiFi密码 [需要修改]!!!
while (!doCmdOk("AT+CIPSTART=\"TCP\",\"jjfaedp.hedevice.com\",876", "OK")); //连接onenet TPC接口
while (!doCmdOk("AT+CIPMODE=1", "OK")); //透传模式
while (!doCmdOk("AT+CIPSEND", ">")); //开始发送
}
int dht_flag = 1;
/** packetSend * 将待发数据发送至串口,并释放到动态分配的内存 */
void packetSend(edp_pkt* pkt)
{
if (pkt != NULL)
{
WIFI_UART.write(pkt->data, pkt->len);
//串口发送
WIFI_UART.flush();
free(pkt);
//回收内存
} }
bool readEdpPkt(edp_pkt *p)
{ int tmp;
if ((tmp = WIFI_UART.readBytes(p->data + p->len, sizeof(p->data))) > 0 )
{ rcvDebug(p->data + p->len, tmp);
p->len += tmp;
}
return true;
}
void rcvDebug(unsigned char *rcv, int len)
{ int i; }
void loop()
{
static int edp_connect = 0;
bool trigger = false;
edp_pkt rcv_pkt;
unsigned char pkt_type;
int i = 0, tmp;
char num[10];
int wd,sd;
char wd1[20],sd1[20];
/* EDP 连接 */
if (!edp_connect)
{
while (WIFI_UART.available()) WIFI_UART.read();//清空串口接收缓存
packetSend(packetConnect(ID, KEY)); //发送EPD连接包
while (!WIFI_UART.available()); //等待EDP连接应答
if ((tmp = WIFI_UART.readBytes(rcv_pkt.data, sizeof(rcv_pkt.data))) > 0 )
{ rcvDebug(rcv_pkt.data, tmp);
//平台连接成功会返回特殊数据,判断,否则清空
if (rcv_pkt.data[0] == 0x20 && rcv_pkt.data[2] == 0x00 && rcv_pkt.data[3] == 0x00)
{
edp_connect = 1;
digitalWrite(13, LOW);
// 使Led灭
}
else ;
}
packetClear(&rcv_pkt);
//该函数上传温湿度信息
}
if(dht_flag == 1)
{
dht_flag = 0;
dht11.read(DHT11PIN);
wd = dht11.temperature;
sd = dht11.humidity;
sprintf(wd1,"%d",wd);
//int型转换char型
sprintf(sd1,"%d",sd); //int型转换char型
DHT11 = 0;
delay(500);
packetSend(packetDataSaveTrans(NULL, "WD", wd1));
//将新数据值上传至数据流 WD是 上传的地方 wd1是上传的数据
delay(500);
packetSend(packetDataSaveTrans(NULL, "SD", sd1));
//将新数据值上传至数据流
delay(500);
}
DHT11++;
if(DHT11 > 150&&edp_connect)
{
dht11.read(DHT11PIN);
wd = dht11.temperature;
sd = dht11.humidity;
sprintf(wd1,"%d",wd);
sprintf(sd1,"%d",sd);
DHT11 = 0;
delay(500);
packetSend(packetDataSaveTrans(NULL, "WD", wd1));
delay(500);
packetSend(packetDataSaveTrans(NULL, "SD", sd1));
delay(500);
}
while (WIFI_UART.available())
{
readEdpPkt(&rcv_pkt);
if (isEdpPkt(&rcv_pkt))
{
pkt_type = rcv_pkt.data[0];
switch (pkt_type)
{
case CMDREQ:
char edp_command[50];
char edp_cmd_id[40];
long id_len, cmd_len, rm_len;
char datastr[20];
char val[10];
memset(edp_command, 0, sizeof(edp_command));
memset(edp_cmd_id, 0, sizeof(edp_cmd_id));
edpCommandReqParse(&rcv_pkt, edp_cmd_id, edp_command, &rm_len, &id_len, &cmd_len);
//数据处理与应用中EDP命令内容对应
//本例中格式为 datastream:[1/0]
sscanf(edp_command, "%[^:]:%s", datastr, val);
if (atoi(val) == 1)
// 使Led亮
digitalWrite(13, HIGH);
//digitalWrite(8, HIGH);
else
digitalWrite(13, LOW);
// 使Led灭
//digitalWrite(8, HIGH);
if(atoi(val) > 1)
{
sendCommand(0x40);
//setting the Write Data Command,using automatic address genuine.
digitalWrite(stbPin, LOW);
//pin low. To begin receiving data
shiftOut(dioPin, clkPin, LSBFIRST, 0xc0);
//Set the start address 0C0H
if(atoi(val) >= 100 && atoi(val) <=1000)
{
shiftOut(dioPin, clkPin, LSBFIRST, digits[0]);
//thousand data
shiftOut(dioPin, clkPin, LSBFIRST, 0x00);
//filling high 8- bit data
shiftOut(dioPin, clkPin, LSBFIRST, digits[atoi(val)/100%10]); //hundred data
shiftOut(dioPin, clkPin, LSBFIRST, 0x00); //filling high 8- bit data
shiftOut(dioPin, clkPin, LSBFIRST, digits[atoi(val)/10%10]); //ten data
shiftOut(dioPin, clkPin, LSBFIRST, 0x00); //filling high 8- bit data
shiftOut(dioPin, clkPin, LSBFIRST, digits[atoi(val)%10]); //bit data
shiftOut(dioPin, clkPin, LSBFIRST, 0x00); //filling high 8- bit data
}
else if(atoi(val) >= 10 && atoi(val) <=100)
{
shiftOut(dioPin, clkPin, LSBFIRST, digits[0]);
//thousand data
shiftOut(dioPin, clkPin, LSBFIRST, 0x00);
//filling high 8- bit data
shiftOut(dioPin, clkPin, LSBFIRST, digits[0]); //hundred data
shiftOut(dioPin, clkPin, LSBFIRST, 0x00); //filling high 8- bit data
shiftOut(dioPin, clkPin, LSBFIRST, digits[atoi(val)/10%10]); //ten data
shiftOut(dioPin, clkPin, LSBFIRST, 0x00); //filling high 8- bit data
shiftOut(dioPin, clkPin, LSBFIRST, digits[atoi(val)%10]); //bit data
shiftOut(dioPin, clkPin, LSBFIRST, 0x00); //filling high 8- bit data
}
else if(atoi(val) > 0 && atoi(val) <=10)
{
shiftOut(dioPin, clkPin, LSBFIRST, digits[0]);//thousand data
shiftOut(dioPin, clkPin, LSBFIRST, 0x00); //filling high 8- bit data
shiftOut(dioPin, clkPin, LSBFIRST, digits[0]); //hundred data
shiftOut(dioPin, clkPin, LSBFIRST, 0x00); //filling high 8- bit data
shiftOut(dioPin, clkPin, LSBFIRST, digits[0]); //ten data
shiftOut(dioPin, clkPin, LSBFIRST, 0x00); //filling high 8- bit data
shiftOut(dioPin, clkPin, LSBFIRST, digits[atoi(val)%10]); //bit data
shiftOut(dioPin, clkPin, LSBFIRST, 0x00); //filling high 8- bit data
}
digitalWrite(stbPin, HIGH);
delay(500);
}
//pin high. Stop receiving data
packetSend(packetDataSaveTrans(NULL, datastr, val)); //将新数据值上传至数据流
break;
default:
;
break;
}
}
}
if (rcv_pkt.len > 0)
packetClear(&rcv_pkt);
delay(150); }
/** readEdpPkt * 从串口缓存中读数据到接收缓存 */