-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP8266.c
341 lines (317 loc) · 9.8 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
/*
* ESP8266.c
*
* Created: 02-01-2018
* Author : Norlinux
* Based in Source Code Piotr Styczy?ski
* http://www.microelectronic.pt
* http://maquina.96.lt
* https://www.facebook.com/MundoDosMicrocontroladores/
* Released under GPLv3.
* Please refer to LICENSE file for licensing information.
* which can be found at http://www.gnu.org/licenses/gpl.txt
*/
#define F_CPU 8000000UL /* Define CPU Frequency e.g. here its Ext. 8MHz */
#include <inttypes.h> /* Include uintxx_t library */
#include <string.h> /* Include string library */
#include <stdio.h> /* Include standard IO library */
#include <util/delay.h> /* Include Delay header file */
//#include <stdlib.h> /* Include free memory file */
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "ESP8266.h"
#include "lcd_lib.h"
#include "USART_V1.h"
#include "Timer2.h"
#define delay() _delay_ms(5);
#define Wait() _delay_ms(800);
//static uint8_t IPD_Data[ESP_CONNBUFFER_SIZE + 1]; /* Data buffer for incoming connection */
// Variavel para confirmar que foi enviado com sucesso.
//uint8_t WIFI_EVENT_IPD_OK=0;
volatile static Timer_Status_t Timer2;
char msg[18]={};
/**
* Internal command buffer.
* The responses from ESP8266 normally should never fill this up!
*/
char Wifi_Buffer[Buffer_Len];
/******************************************************************************/
/*** Public API **/
/******************************************************************************/
ESP_Result WIFI_Disconnect(char* RespData);
int WIFI_Events ( ESP_Result esp_Events)
{
switch(esp_Events){
case espOK:
LCDclr();
LCDprintXY("ESP OK...",0,0);
//return 1;
break;
case espBUSY:
LCDclr();
LCDprintXY("ESP Busy.RESTART",0,0);
return 0;
break;
case espSENDERROR:
LCDclr();
LCDprintXY("ESP SEND ERROR",0,0);
break;
case espWIFINOTCONNECTED:
LCDclr();
LCDprintXY("WIFI NOT CONNECT",0,0);
break;
case espERROR:
LCDclr();
LCDprintXY("ERRO WIFI CONNEC",0,0);
break;
case espTIMEOUT:
LCDclr();
LCDprintXY("ESP TIMEOUT :<",0,0);
return 0;
break;
case espWRONGPASSWORD:
LCDclr();
LCDprintXY("WRONG PASSWORD",0,0);
break;
case espNOTFOUNDTARGETAP:
LCDclr();
LCDprintXY("NOT FOUND TARGET",0,0); // maybe USSID wrong
break;
case espDEVICENOTCONNECTED:
LCDclr();
LCDprintXY("NOT CONNECTED",0,0); // maybe USSID wrong
return 0;
break;
case espINVALIDPARAMETERS:
LCDclr();
LCDprintXY("INVALID PARAMETR",0,0);
break;
case espRESTART:
LCDclr();
LCDprintXY("ESP8266 RESTART",0,0); // tried reconnect maybe wrong password WIFI
Wait();
break;
case espTIMEandPASS:
LCDclr();
LCDprintXY("PASSWRD TIMEOUT",0,0);
return 0;
break;
}
return 0;
}
ESP_Result WIFI_Listening(char* RespData, char* RespAlternData1, char* RespAlternData2) {
int i = 0;
uart_flush();
int count = 0;
//Wifi_Buffer[count] = '\0';
memset(Wifi_Buffer, 0, Buffer_Len); /* Set buffer values to all zeros */
uint16_t input_count = 0;
uint16_t Rx_Usart = 0;
//int input_hb = 0;
int input_lb = 0;
while(1) {
++i;
if(i > F_CPU) {
WIFI_Events(espTIMEOUT);
return 0;
}
//Waiting for buffer
delay();
if((input_count = uart_available()) > 0)
{
//for(int i=0;i<input_count;++i) {
while (input_count--) {
Rx_Usart = uart_getc();
input_lb = Rx_Usart & 0xFF; /* Convert value to string */
if(input_lb != '\n') {
Wifi_Buffer[count] = input_lb;
++count;
Wifi_Buffer[count] = '\0';
} else {
//WIFI_Events(espBUSY);
if(count <= 1) {LCDclr(); LCDprintXY("NO BUFFER",0,0); // Will imcrease this code in future
return espRESTART;
}
else {
LCDclr();
LCDprintXY(Wifi_Buffer,0,0);
if(strcmp(Wifi_Buffer, RespData) == 0){
return espOK;
}else if(strcmp(Wifi_Buffer, RespAlternData1) == 0){
return espOK;
}else if(strcmp(Wifi_Buffer, RespAlternData2) == 0){
return espOK;
}else if(strncmp(Wifi_Buffer, "+IPD,", 5)==0){
return espOK;
}else if (strncmp(Wifi_Buffer, FROMMEM("+IPD"), 4) == 0){
return espOK;
}else if (strcmp(Wifi_Buffer, "busy p...\r") == 0){
return espBUSY;
}else if (strcmp(Wifi_Buffer, "ALREADY CONNECTED\r") == 0){
return espOK;
}else if (strcmp(Wifi_Buffer,RESP_DISCONNECT)==0){
memset(Wifi_Buffer, 0, Buffer_Len);
WIFI_Events(espDEVICENOTCONNECTED);
//return espDEVICENOTCONNECTED;
}
else if (strstr(Wifi_Buffer,"ets"))
{
memset(Wifi_Buffer, 0, Buffer_Len);
WIFI_Events(espRESTART);
return espRESTART;
}
else if(strstr(Wifi_Buffer, "+CWJAP:1"))
WIFI_Events(espTIMEandPASS);
else if(strstr(Wifi_Buffer, "+CWJAP:2"))
WIFI_Events(espWRONGPASSWORD);
else if(strstr(Wifi_Buffer, "+CWJAP:3"))
WIFI_Events(espNOTFOUNDTARGETAP);
else if(strstr(Wifi_Buffer, "+CWJAP:4"))
WIFI_Events(espDEVICENOTCONNECTED);
}
//count = 0;
//Wifi_Buffer[count] = '\0';
}
}
}
return 0;
}
}
uint8_t ESP_Callback(ESP_Control_t ctrl, void* param, void* result) {
switch (ctrl) {
case ESP_Control_Init: { /* Initialize low-level part of communication */
ESP_Baud_t* USART = (ESP_Baud_t *)param; /* Get low-level value from callback */
/************************************/
/* Device specific initialization */
/************************************/
//USART_Init_baud(USART->Baudrate);
USART_Init(USART->Baudrate);
LCDprintXY("USART INIT...",0,0);
Wait();
if (result) {
*(uint8_t *)result = 0; /* Successfully initialized */
}
return 1; /* Return 1 = command was processed */
}
case ESP_Control_Send: {
ESP_Send_t* send = (ESP_Send_t *)param; /* Get send parameters */
/* Send actual data to UART */
ESP8266_Send((uint8_t *)send->Data, send->Count); /* Send actual data */
if (result) {
*(uint8_t *)result = 0; /* Successfully send */
}
return 1; /* Command processed */
}
default:
return 0;
}
}
/* Initialize necessary parts */
ESP_Result ESP_Init(void) {
UART_SEND_STR(FROMMEM("ATE0")); /* Send data ATE0 Echo*/
UART_SEND_STR(_CRLF);
delay()
while(!ESP8266_listen(RESP_OK,RESP_ATE_OK,RESP_CONNECT));
LCDprintXY("ATE0 OK...",0,0);
Wait();
return espOK;
}
ESP_Result ESP_Begin(uint32_t baudrate) {
uint8_t result=1; /* Set to default value first */
if(!ESP_Callback(ESP_Control_Init, (void *)&baudrate, &result)|| result){
return espERROR; /* Return error */
}
return (ESP_Init());
}
ESP_Result ESP_Restart(void)
{
UART_SEND_STR(FROMMEM("AT+RST")); /* Send data RESET*/
UART_SEND_STR(_CRLF);
delay();
while (timeout_event(60) && !ESP8266_listen(RESP_GOT_IP,RESP_CONNECT,RESP_OK)); //
if(Timer2.Timeout==1)
return espERROR;
else
return espOK;
}
ESP_Result ESP_WIFIMode(uint8_t Mode, uint8_t Connection)
{
char atBuffer[16],str[32];
memset(atBuffer, 0, strlen(atBuffer));
memset(str,0,strlen(str));
/* 1 = Station mode (client)
2 = AP mode (host)
3 = AP + Station mode (Yes, ESP8266 has a dual mode!)*/
sprintf(atBuffer, "AT+CWMODE=%d", Mode);
strcpy(str, atBuffer);
strcat(str,"\r\rOK\r");
UART_SEND_STR(atBuffer); /* Send data MODE*/
UART_SEND_STR(_CRLF);
while (!ESP8266_listen(RESP_OK,RESP_OK_CR,str));
LCDprintXY(str,0,0);
memset(atBuffer, 0, strlen(atBuffer));
memset(str,0,strlen(str));
/* 0 = Single Connection
1 = Multiple Connection*/
sprintf(atBuffer, "AT+CIPMUX=%d",Connection);
strcpy(str, atBuffer);
strcat(str,"\r\rOK\r");
UART_SEND_STR(atBuffer);
UART_SEND_STR(_CRLF);
while (!ESP8266_listen(RESP_OK,RESP_OK_CR,str));
LCDprintXY(str,0,0);
return espOK;
}
ESP_Result ESP_WIFIConnect(const char* ssid, const char* passwd){
char atBuffer[90];
/*String, APs SSID
pwd?String, not longer than 64 characters*/
memset(atBuffer, 0, strlen(atBuffer));
sprintf(atBuffer, "AT+CWJAP=\"%s\",\"%s\"", ssid, passwd);
//atBuffer[strlen(atBuffer-1)] = '\0';
UART_SEND_STR(atBuffer); /* Send data */
UART_SEND_STR(_CRLF);
delay();
//while (timeout_event(10) && 1);
while (timeout_event(120) && !ESP8266_listen(RESP_GOT_IP,RESP_OK,RESP_OK_CR));
//return espERROR == Timer2.Timeout ? espERROR : espOK;
if(Timer2.Timeout==1)
return espERROR;
else
return espOK;
}
ESP_Result WIFI_Client_Connection (const char* Protocol, const char* Host, const int Port)
{
char atBuffer[128];
//resent:
memset(atBuffer, 0, strlen(atBuffer));
sprintf(atBuffer, "AT+CIPSTART=\"%s\",\"%s\",%d", Protocol, Host, Port);
//UART_SEND_STR(atBuffer); /* Send data */
//UART_SEND_STR(_CRLF);
do
{
Wait();
UART_SEND_STR(atBuffer); /* Send data */
UART_SEND_STR(_CRLF);
} while(timeout_event(30) && !ESP8266_listen(RESP_CONNECT_OK,RESP_CONNECT_CR,RESP_CONNECTED));
if(Timer2.Timeout==1)
return espERROR;
else
return espOK;
}
ESP_Result WIFI_Send (const char* Data)
{
const int Length = strlen(Data);
char atBuffer[Buffer_Len];
memset(atBuffer, 0, Buffer_Len);
sprintf(atBuffer, "AT+CIPSEND=%d", Length);
UART_SEND_STR(atBuffer); /* Send data */
UART_SEND_STR(_CRLF);
//delay();
while (!ESP8266_listen(RESP_OK,RESP_OK_CR,RESP_SEND_OK));
UART_SEND_STR(Data); /* Send data */
UART_SEND_STR(_CRLF);
delay();
while (!ESP8266_listen(RESP_ATMEGA,RESP_CLOSED,RESP_OK));
return espOK;
}