-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathEspMain.c
247 lines (208 loc) · 5.34 KB
/
EspMain.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
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include <string.h>
#include <stdarg.h>
#include "FreeRTOS.h"
#include "task.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "lwip/api.h"
#include "ssid_config.h"
#include <selib.h>
#include <ledctrl.h>
extern void initLED(void);
extern int _setLed(int ledId, int on);
static int errorCounter;
static void restart()
{
int i;
printf("Restarting....\n");
for(i = 0 ; i < 10; i++)
{
_setLed(1, TRUE);
vTaskDelay(150 / portTICK_PERIOD_MS);
_setLed(1, FALSE);
vTaskDelay(150 / portTICK_PERIOD_MS);
}
sdk_system_restart();
}
int pollkb(void)
{
int c=uart_getc_nowait(0);
if(c < 0) return 0;
//printf("---->>> %d\n", c);
return c=='\r' ? '\n' : c;
}
U32 getMilliSec(void)
{
return xTaskGetTickCount() * portTICK_PERIOD_MS;
}
static U32 baseUnixTime = 1517846288;
U32 baGetUnixTime(void)
{
return baseUnixTime +
(U32)(((U64)xTaskGetTickCount() * (U64)portTICK_PERIOD_MS) / 1000);
}
static void setTime(void)
{
int chk=0;
ip_addr_t addr;
struct netconn* ncon = netconn_new(NETCONN_TCP);
if(ncon)
{
chk++;
if(!netconn_gethostbyname("time.nist.gov", &addr))
{
chk++;
if(!netconn_connect(ncon, &addr, 37))
{
struct netbuf *buf;
chk++;
if(!netconn_recv(ncon, &buf))
{
U32 now;
chk++;
if(4 == netbuf_copy(buf, (void*)&now, 4))
{
chk++;
now = lwip_htonl(now);
/* Convert from 1900 to 1970 format */
now = now - 2208988800U;
printf("The current Unix epoch time is %u\n",now);
baseUnixTime =
now - (xTaskGetTickCount() * portTICK_PERIOD_MS / 1000);
}
netbuf_delete(buf);
}
}
}
netconn_delete(ncon);
}
if( chk != 5 )
printf("Connecting to time.nist.gov failed! %d\n",chk);
}
void _xprintf(const char* fmt, ...)
{
va_list varg;
va_start(varg, fmt);
vprintf(fmt, varg);
va_end(varg);
}
/*
Returns the name of this device. Used by some examples
*/
const char* getDevName(void)
{
return "ESP8266 FreeRTOS/lwIP";
}
/* Return MAC addr. Used by some examples
*/
int getUniqueId(const char** id)
{
struct netif *netif = sdk_system_get_netif(STATION_IF);
if(!netif)
{
printf("Cannot get MAC addr!\n");
restart();
}
*id = (char*)netif->hwaddr;
return (int)netif->hwaddr_len;
}
/* Used by some examples */
void setProgramStatus(ProgramStatus s)
{
switch(s)
{
case ProgramStatus_DnsError:
case ProgramStatus_ConnectionError:
if(++errorCounter < 10)
{
vTaskDelay(500 / portTICK_PERIOD_MS);
break;
}
/* fall through */
case ProgramStatus_SocketError:
case ProgramStatus_PongResponseError:
case ProgramStatus_InvalidCommandError:
case ProgramStatus_MemoryError:
case ProgramStatus_Restarting:
restart();
break;
case ProgramStatus_CloseCommandReceived:
for(;;)
{
vTaskDelay(1000 / portTICK_PERIOD_MS);
printf("CloseCommandReceived\n");
}
case ProgramStatus_Starting:
case ProgramStatus_CertificateNotTrustedError:
case ProgramStatus_SslHandshakeError:
case ProgramStatus_WebServiceNotAvailError:
break;
case ProgramStatus_Connecting:
_setLed(2, TRUE);
break;
case ProgramStatus_SslHandshake:
_setLed(3, TRUE);
break;
case ProgramStatus_DeviceReady:
_setLed(1, FALSE);
_setLed(2, FALSE);
_setLed(3, FALSE);
break;
}
}
static void setupWiFi(void)
{
errorCounter=0;
vTaskDelay(1500 / portTICK_PERIOD_MS);
printf("%sSDK version:%s\n",
"\n\nSharkSSL Example Task starting.\n",
sdk_system_get_sdk_version());
for(;;)
{
uint8_t status = sdk_wifi_station_get_connect_status();
printf("WiFi connection status %u\n",(unsigned int)status);
if(status == STATION_GOT_IP)
break;
if (status == STATION_WRONG_PASSWORD)
printf("WiFi: wrong password\n\r");
else if (status == STATION_NO_AP_FOUND)
printf("WiFi: AP not found\n\r");
else if (status == STATION_CONNECT_FAIL)
printf("WiFi: connection failed\r\n");
else
{
if(++errorCounter < 25)
{
vTaskDelay(500 / portTICK_PERIOD_MS);
continue;
}
printf("Giving up\n");
}
restart();
}
}
static void mainTaskWrapper(void *pvParameters)
{
initLED();
setupWiFi();
setTime();
mainTask(0); /* shark example */
restart();
}
void user_init(void)
{
uart_set_baud(0, 115200);
struct sdk_station_config config = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
};
/* Must call_set_opmode before station_set_config */
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_set_config(&config);
xTaskCreate(&mainTaskWrapper, "SharkSSL Example", 600, NULL, 2, NULL);
}