-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp8266.c
349 lines (321 loc) · 7.87 KB
/
esp8266.c
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "colors.h"
#include "lpc_types.h"
#include "lpc17xx_clkpwr.h"
#include "lpc17xx_gpio.h"
#include "lpc17xx_libcfg_default.h"
#include "lpc17xx_nvic.h"
#include "lpc17xx_pinsel.h"
#include "lpc17xx_uart.h"
#include "SSD1289.h"
#include "LCD_delay.h"
#include "lpc17xx_timer.h"
#include "MQTTConnect.h"
#include "MQTTPacket.h"
#include "MQTTPublish.h"
#include "MQTTSubscribe.h"
#include "MQTTUnsubscribe.h"
#include "StackTrace.h"
#define RXBUFF_LEN 200
#define TXBUFF_LEN 200
uint8_t rxbuf[RXBUFF_LEN];
uint8_t txbuf[TXBUFF_LEN];
uint8_t response[100];
uint8_t IP[16];
uint8_t WIFIStat; /* on = 1, off = 0 */
uint8_t TCPSocket;
uint8_t TCPSend;
uint8_t WIFIInit;
uint8_t DataRcv;
char str[12];
int rcvlen;
void wait_ESP(int delay) {
int i = 10000, j = delay;
while (i > 0) {
while (j > 0)
j--;
j = delay;
i--;
}
}
uint32_t strlen(uint8_t *str) {
uint32_t len = 0;
while (str[len] != '\0')
len++;
return len;
}
int is_subString(uint8_t *str, uint8_t *sub) {
int i = 0, j = 0, flag = 0;
while (str[i] != '\0') {
if (sub[0] == str[i]) {
j = 0;
flag = 0;
while (sub[j] != '\0' && str[i + j] != '\0') {
if (sub[j] != str[i + j]) {
flag = 1;
break;
}
j++;
}
if (flag == 0)
if (strlen(sub) == j)
return i;
}
i++;
}
return -1;
}
char *num2str(int num) {
int i = 0, p = 0;
char temp[11];
for (i = 0; i < 10; i++) {
temp[i] = ' ';
str[i] = ' ';
}
temp[10] = '\0';
if (num == 0) {
str[0] = '0';
str[1] = '\0';
return str;
}
if (num < 0) {
temp[0] = '-';
num *= -1;
} else
temp[0] = ' ';
i = 0;
while (num > 0) {
temp[9 - i] = (char) ((num % 10) + 48);
num /= 10;
i++;
}
for (i = 0; temp[i] != '\0'; i++) {
if (temp[i] == ' ')
continue;
else {
str[p] = temp[i];
p++;
}
}
str[p] = '\0';
return str;
}
void str_cat(uint8_t *str, uint8_t *str1, uint8_t *str2, uint8_t *str3,
uint8_t *str4, uint8_t *str5) {
int i = 0, j = 0;
while (str1[i] != '\0') {
str[i] = str1[i];
i++;
}
while (str2[j] != '\0') {
str[i] = str2[j];
i++;
j++;
}
j = 0;
while (str3[j] != '\0') {
str[i] = str3[j];
i++;
j++;
}
j = 0;
while (str4[j] != '\0') {
str[i] = str4[j];
i++;
j++;
}
j = 0;
while (str5[j] != '\0') {
str[i] = str5[j];
i++;
j++;
}
str[i] = '\0';
}
void getIP(uint8_t *str) {
int pos = is_subString(str, (uint8_t *) "STAIP"), i = 0;
while (str[pos + 7] != '\"') {
IP[i] = str[pos + 7];
i++;
pos++;
}
IP[i] = '\0';
}
void UART0_IRQHandler() {
rcvlen = UART_Receive(LPC_UART0, rxbuf, sizeof(rxbuf), BLOCKING);
//LCD_PutString((char *)rxbuf,12,RED);
if (is_subString(rxbuf, (uint8_t *) "WIFI GOT IP") >= 0)
WIFIStat = 1;
if (is_subString(rxbuf, (uint8_t *) "WIFI DISCONNECT") >= 0)
WIFIStat = 0;
if (is_subString(rxbuf, (uint8_t *) "SEND OK") >= 0)
TCPSend = 1;
if (is_subString(rxbuf, (uint8_t *) "+IPD,") >= 0)
DataRcv = 1;
/*if(is_subString(rxbuf,(uint8_t *)"WIFI DISCONNECT") >= 0)
LCD_PutString("\nWiFi DISCONNECTED",12,RED);
if(is_subString(rxbuf,(uint8_t *)"WIFI CONNECTED") >= 0)
LCD_PutString("\nWiFi CONNECTED",12,BLUE);*/
}
void Send_ESP(uint8_t* cmd, int size) {
while (UART_CheckBusy(LPC_UART0) == SET)
;
UART_Send(LPC_UART0, cmd, size, BLOCKING);
}
void AT_cmd(uint8_t *cmd) {
//uint8_t *res;
//int i=0;
//LCD_PutString("\nSending ",12,WHITE);
//LCD_PutString((char *)cmd,12,RED);
while (UART_CheckBusy(LPC_UART0) == SET)
;
/*while(i<sizeof(rxbuf)) {
rxbuf[i] = '\0';
i++;
}*/
Send_ESP(cmd, strlen(cmd));
wait_ESP(200);
}
void create_TCPSocket(uint8_t *IP, uint8_t *port) {
uint8_t cmd[50];
int timeout=20;
int i;
while (i < sizeof(cmd)) {
cmd[i] = '\0';
i++;
}
str_cat(cmd, (uint8_t *) "AT+CIPSTART=", (uint8_t *) "\"TCP\",", IP,
port, (uint8_t *) "\r\n");
AT_cmd((uint8_t *) "AT+CIPSTATUS?\r\n");
//LCD_PutString((char *)rxbuf,12,RED);
if (is_subString(rxbuf, (uint8_t *)"ERROR") >= 0) {
while(TCPSocket!=1&&timeout!=0) {
AT_cmd((uint8_t *) "AT+CIPMUX=0\r\n");
AT_cmd(cmd);
AT_cmd((uint8_t *)"AT+CIPSTATUS?\r\n");
//LCD_PutString((char *)rxbuf,12,RED);
if (is_subString(rxbuf, (uint8_t *) "CONNECT") >= 0)
TCPSocket = 1;
else {
TCPSocket = 0;
timeout--;
}
}
}
}
void close_TCPSocket() {
AT_cmd((uint8_t *) "AT+CIPCLOSE\r\n");
TCPSocket = 0;
}
void PIN_Init(void) {
/* Pin configuration for UART0 */
PINSEL_CFG_Type PinCfg;
/*
* Initialize UART0 pin connect
*/
//LCD_PutString("\nInitialising UART PINS",12,WHITE);
PinCfg.Funcnum = PINSEL_FUNC_1;
PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL;
PinCfg.Pinmode = PINSEL_PINMODE_PULLUP;
PinCfg.Portnum = PINSEL_PORT_0;
PinCfg.Pinnum = PINSEL_PIN_2;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = PINSEL_PIN_3;
PINSEL_ConfigPin(&PinCfg);
}
void Wifi_Status() {
AT_cmd((uint8_t *) "AT+CIFSR\r\n");
}
void Wifi_init(uint32_t baud) {
/* UART Configuration structure variable */
UART_CFG_Type UARTConfigStruct;
/* UART FIFO configuration Struct variable */
UART_FIFO_CFG_Type UARTFIFOConfigStruct;
/* Initialize UART Configuration parameter structure to default state:
* Baudrate = 9600bps
* 8 data bit
* 1 Stop bit
* None parity
*/
UART_ConfigStructInit(&UARTConfigStruct);
/* Set Baudrate to 115200 */
(&UARTConfigStruct)->Baud_rate = baud;
(&UARTConfigStruct)->Databits = UART_DATABIT_8;
(&UARTConfigStruct)->Parity = UART_PARITY_NONE;
(&UARTConfigStruct)->Stopbits = UART_STOPBIT_1;
PIN_Init();
/* Initialize UART0 peripheral with given to corresponding parameter */
UART_Init(LPC_UART0, &UARTConfigStruct);
SystemCoreClockUpdate();
/* Initialize FIFOConfigStruct to default state:
* - FIFO_DMAMode = DISABLE
* - FIFO_Level = UART_FIFO_TRGLEV0
* - FIFO_ResetRxBuf = ENABLE
* - FIFO_ResetTxBuf = ENABLE
* - FIFO_State = ENABLE
*/
UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);
(&UARTFIFOConfigStruct)->FIFO_Level = UART_FCR_TRG_LEV0; /* 8 Bytes */
//(&UARTFIFOConfigStruct)->FIFO_ResetRxBuf = DISABLE;
/* Initialize FIFO for UART0 peripheral */
UART_FIFOConfig(LPC_UART0, &UARTFIFOConfigStruct);
/** config and enable interrupter */
UART_IntConfig(LPC_UART0, UART_INTCFG_RBR, ENABLE);
/** NVIC enable interrupter , it's request锛�*/
NVIC_EnableIRQ(UART0_IRQn);
/* Enable UART Transmit */
UART_TxCmd(LPC_UART0, ENABLE);
while(is_subString(rxbuf, (uint8_t *) "OK") < 0) {
AT_cmd((uint8_t *) "AT\r\n");
}
WIFIInit = 1;
//Wifi_Status();
}
void Wifi_connect(uint8_t *usr, uint8_t *pass) {
uint8_t cmd[100];
str_cat(cmd, (uint8_t *) "AT+CWJAP_DEF=\"", usr, (uint8_t *) "\",\"", pass,
(uint8_t *) "\"\r\n");
AT_cmd(cmd);
Wifi_Status();
}
void Wifi_disconnect(void) {
AT_cmd((uint8_t *) "AT+CWQAP\r\n");
//AT_cmd((uint8_t *)"AT+RST");
WIFIStat = 0;
}
void send_TCP(uint8_t *data, int len) {
uint8_t *buf_len = (uint8_t *) num2str(len);
uint8_t cmd[50];
str_cat(cmd, (uint8_t *) "AT+CIPSEND=", buf_len, (uint8_t *) "\r\n",
(uint8_t *) "\0", (uint8_t *) "\0");
create_TCPSocket((uint8_t *) "\"test.mosquitto.org\",", (uint8_t *) "1883");
//LCD_PutString(rxbuf,12,RED);
/*if (TCPSocket == 1)
LCD_PutString("TCP Socket Created\n", 12, BLUE);
else
LCD_PutString("TCP Socket Failed\n", 12, RED);*/
AT_cmd(cmd);
/* while (ESPRecReady != 1);
ESPRecReady = 0;
*/
UART_Send(LPC_UART0, data, len, BLOCKING);
TCPSocket =0;
//LCD_PutString((char *)rxbuf,12,RED);
}
int getData(unsigned char *buf, int len) {
int rlen=0;
while(DataRcv!=1);
while(rxbuf[i]!=',') {
i++;
}
i++;
while(rxbuf[i]!=':') {
rlen = rlen*10+(rxbuf[i]-48);
i++;
}
i++;
len = MIN(len,rlen);
for(j=0;j<len;j++) {
buf[j]= rxbuf[i+j];
}
return len;
}