-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cpp
More file actions
197 lines (161 loc) · 6.34 KB
/
Client.cpp
File metadata and controls
197 lines (161 loc) · 6.34 KB
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
#include "mbed.h"
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
UnbufferedSerial pc(CONSOLE_TX, CONSOLE_RX, 230400);
UnbufferedSerial wifi(ARDUINO_UNO_D8, ARDUINO_UNO_D2, 115200);
int VER = 0;
Thread sock_thread;
char buffer[80];
char receive[20];
char barricade[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x00, 0x04, 0x00, 0x01};
char illuminance[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x04, 0x00, 0x00, 0x00, 0x02};
void Char2Hex(unsigned char ch, char* szHex)
{
static unsigned char saucHex[] = "0123456789ABCDEF";
szHex[0] = saucHex[ch >> 4];
szHex[1] = saucHex[ch&0xF];
szHex[2] = 0;
}
void check_status (){
sprintf(buffer, "AT+CWMODE=1\r\n");
wifi.write(buffer, strlen(buffer));
ThisThread::sleep_for(3000ms);
sprintf(buffer, "AT+CWJAP=\"tp_4ahnssu\",\"01051611257a\"\r\n");
wifi.write(buffer, strlen(buffer));
ThisThread::sleep_for(10000ms);
sprintf(buffer, "AT+CIPMUX=1\r\n");
wifi.write(buffer, strlen(buffer));
ThisThread::sleep_for(3000ms);
sprintf(buffer, "AT+CIPSTART=0,\"TCP\",\"192.168.0.141\",50000\r\n"); //RTU server
wifi.write(buffer, strlen(buffer));
ThisThread::sleep_for(10000ms);
sprintf(buffer, "AT+CIPSTART=1,\"TCP\",\"192.168.0.200\",502\r\n"); //TCP server
wifi.write(buffer, strlen(buffer));
ThisThread::sleep_for(10000ms);
sprintf(buffer, "AT+CIPSTATUS\r\n");
wifi.write(buffer, strlen(buffer));
ThisThread::sleep_for(3000ms);
}
int main(){
sock_thread.start(&check_status);
char ch_pc;
char ch_wifi;
char hex[3];
int flag = 0;
int read = 0;
int cnt = 0;
sprintf(buffer, "\r\n---- TRAFFIC CONTROL ----\r\n");
pc.write(buffer, strlen(buffer));
while(true){
if(VER == 0){
if(pc.readable()){
pc.read(&ch_pc, 1);
pc.write(&ch_pc,1);
if(ch_pc==0x5B){ // Input = '['
//open led
sprintf(buffer, "AT+CIPSEND=0,%d\r\n",2);
wifi.write(buffer,strlen(buffer));
ThisThread::sleep_for(500ms);
sprintf(buffer,"[\n");
wifi.write(buffer, strlen(buffer));
ThisThread::sleep_for(200ms);
//open barricade
barricade[11]=0x01;
sprintf(buffer, "AT+CIPSEND=1,%d\r\n",12);
wifi.write(buffer,strlen(buffer));
ThisThread::sleep_for(500ms);
for(int i=0; i<12; ++i)
wifi.write(barricade+i,1);
}
else if(ch_pc==0x5D){ // Output = ']'
//close led
sprintf(buffer, "AT+CIPSEND=0,%d\r\n",2);
wifi.write(buffer,strlen(buffer));
ThisThread::sleep_for(500ms);
sprintf(buffer,"]\n");
wifi.write(buffer, strlen(buffer));
ThisThread::sleep_for(2000ms);
//close barricade
barricade[11]=0x02;
sprintf(buffer, "AT+CIPSEND=1,%d\r\n",12);
wifi.write(buffer,strlen(buffer));
ThisThread::sleep_for(500ms);
for(int i=0; i<12; ++i)
wifi.write(barricade+i,1);
}
else if(ch_pc==0x2A){ // change ver = '*'
VER = 1;
sprintf(buffer, "version changed: %d\n\n",VER);
pc.write(buffer, strlen(buffer));
}
}
if(wifi.readable()){
wifi.read(&ch_wifi,1);
pc.write(&ch_wifi,1);
}
}
else if(VER == 1){
if(pc.readable()){
pc.read(&ch_pc, 1);
pc.write(&ch_pc,1);
if(ch_pc==0x49){ // I: illuminance sensor
sprintf(buffer, "AT+CIPSEND=1,%d\r\n",12);
wifi.write(buffer,strlen(buffer));
ThisThread::sleep_for(500ms);
for(int i=0; i<12; ++i){
wifi.write(illuminance+i,1);
}
}
else if(ch_pc==0x2A){ // change ver = '*'
VER = 0;
sprintf(buffer, "version changed: %d\n\n",VER);
pc.write(buffer, strlen(buffer));
}
}
if(wifi.readable()){
wifi.read(&ch_wifi,1);
if(ch_wifi==0x32)
flag=1;
if(flag){
if(ch_wifi == 0x3A||cnt>0){
receive[cnt] = ch_wifi;
char hex[3];
cnt++;
if(cnt==14) {
flag = 0;
cnt=0;
for(int i=0; i<14; ++i){
Char2Hex(receive[i], hex);
sprintf(buffer, "*%s\n", hex);
pc.write(buffer, 5);
}
if(receive[10] < 10){
// dark -> open street light
sprintf(buffer, "AT+CIPSEND=0,%d\r\n",2);
wifi.write(buffer,strlen(buffer));
ThisThread::sleep_for(500ms);
sprintf(buffer,"@\n");
wifi.write(buffer, strlen(buffer));
ThisThread::sleep_for(2000ms);
}
else{
// bright -> close street light
sprintf(buffer, "AT+CIPSEND=0,%d\r\n",2);
wifi.write(buffer,strlen(buffer));
ThisThread::sleep_for(500ms);
sprintf(buffer,"#\n");
wifi.write(buffer, strlen(buffer));
ThisThread::sleep_for(2000ms);
}
}
}
else
pc.write(&ch_wifi,1);
}
else
pc.write(&ch_wifi,1);
}
}
}
}