commit | author | age
|
d6b4a7
|
1 |
/********************************************************************************* |
G |
2 |
* Copyright: (C) 2023 LingYun IoT System Studio. |
|
3 |
* All rights reserved. |
|
4 |
* |
|
5 |
* Filename: esp32.c |
|
6 |
* Description: This file is ESP32 high level logic API functions |
|
7 |
* |
|
8 |
* Version: 1.0.0(11/08/23) |
|
9 |
* Author: Guo Wenxue <guowenxue@gmail.com> |
|
10 |
* ChangeLog: 1, Release initial version on "11/08/23 16:18:43" |
|
11 |
* |
|
12 |
********************************************************************************/ |
|
13 |
|
|
14 |
#include "logger.h" |
|
15 |
#include "esp32.h" |
|
16 |
|
|
17 |
int esp32_init_module(comport_t *comport) |
|
18 |
{ |
|
19 |
int rv; |
|
20 |
char version[256] = {0}; |
|
21 |
|
|
22 |
if( !comport ) |
|
23 |
return -1; |
|
24 |
|
|
25 |
rv = esp32_reset(comport); |
|
26 |
if( rv < 0) |
|
27 |
{ |
|
28 |
log_error("Reset ESP32 WiFi module failed: %d\n", rv); |
|
29 |
return rv; |
|
30 |
} |
|
31 |
|
|
32 |
esp32_set_echo(comport, DISABLE); |
|
33 |
|
|
34 |
esp32_set_sysstore(comport, ENABLE); |
|
35 |
|
|
36 |
rv = esp32_get_firmware(comport, version, sizeof(version)); |
|
37 |
if( rv < 0) |
|
38 |
{ |
|
39 |
log_error("Query ESP32 firmware version failed: %d\n", rv); |
|
40 |
return rv; |
|
41 |
} |
|
42 |
|
|
43 |
log_info("ESP32 firmware version:\n%s\n", version); |
|
44 |
|
|
45 |
return 0; |
|
46 |
} |
|
47 |
|
|
48 |
int esp32_setup_softap(comport_t *comport, char *ssid, char *pwd) |
|
49 |
{ |
|
50 |
esp32_set_wmode(comport, MODE_SOFTAP, ENABLE); |
|
51 |
|
|
52 |
esp32_set_ipaddr(comport, MODE_SOFTAP, DEF_SOFTAP_IPADDR, DEF_SOFTAP_IPADDR); |
|
53 |
|
|
54 |
esp32_set_dhcp(comport, MODE_SOFTAP, ENABLE); |
|
55 |
|
|
56 |
esp32_set_softap(comport, ssid, pwd, 11, MODE_WPA2PSK); |
|
57 |
|
|
58 |
return 0; |
|
59 |
} |
|
60 |
|
|
61 |
int esp32_join_network(comport_t *comport, char *ssid, char *pwd) |
|
62 |
{ |
|
63 |
int rv, status = 0; |
|
64 |
int times=10; |
|
65 |
char buf[128] = {0}; |
|
66 |
|
|
67 |
if( !comport ) |
|
68 |
return -1; |
|
69 |
|
|
70 |
esp32_join_status(comport, &status, buf); |
|
71 |
if( 2==status && !strcmp(buf, ssid) ) |
|
72 |
{ |
|
73 |
log_info("ESP32 connected to \"%s\" already\n", ssid); |
|
74 |
return 0; |
|
75 |
} |
|
76 |
|
|
77 |
esp32_set_wmode(comport, MODE_STATION, ENABLE); |
|
78 |
|
|
79 |
esp32_set_dhcp(comport, MODE_STATION, ENABLE); |
|
80 |
|
|
81 |
rv = esp32_connect_ap(comport, ssid, pwd); |
|
82 |
if( rv < 0 ) |
|
83 |
{ |
|
84 |
log_error("connect to AP \"%s\" failed, rv=%d", ssid, rv); |
|
85 |
return rv; |
|
86 |
} |
|
87 |
|
|
88 |
while(times--) |
|
89 |
{ |
|
90 |
rv = esp32_join_status(comport, &status, buf); |
|
91 |
if( 2 == status ) |
|
92 |
{ |
|
93 |
log_info("ESP32 connected to \"%s\" already\n", ssid); |
|
94 |
return 0; |
|
95 |
} |
|
96 |
rv = -3; |
|
97 |
} |
|
98 |
|
|
99 |
return rv; |
|
100 |
} |
|
101 |
|
|
102 |
|
|
103 |
int esp32_check_network(comport_t *comport) |
|
104 |
{ |
|
105 |
int rv, times=5; |
|
106 |
char ip[IP_LEN] = {0}; |
|
107 |
char gateway[IP_LEN] = {0}; |
|
108 |
|
|
109 |
memset(ip, 0, sizeof(ip)); |
|
110 |
memset(gateway, 0, sizeof(gateway)); |
|
111 |
rv = esp32_get_ipaddr(comport, MODE_STATION, ip, gateway); |
|
112 |
if( rv<0 ) |
|
113 |
return rv; |
|
114 |
|
|
115 |
if( !strlen(ip) || !strlen(gateway) ) |
|
116 |
return -3; |
|
117 |
|
|
118 |
log_info("IP address: %s, netmask: %s\n", ip, gateway); |
|
119 |
|
|
120 |
while( times-- ) |
|
121 |
{ |
|
122 |
if( !(rv=esp32_ping(comport, gateway, 5000)) ) |
|
123 |
{ |
|
124 |
rv = 0; |
|
125 |
break; |
|
126 |
} |
|
127 |
} |
|
128 |
|
|
129 |
return 0; |
|
130 |
} |
|
131 |
|
|
132 |
int esp32_setup_tcp_server(comport_t *comport, int port) |
|
133 |
{ |
|
134 |
int rv; |
|
135 |
|
|
136 |
rv = esp32_set_socket_mux(comport, ENABLE); |
|
137 |
if(rv<0) |
|
138 |
return rv; |
|
139 |
|
|
140 |
rv = esp32_set_socket_clients(comport, 2); |
|
141 |
if(rv<0) |
|
142 |
return rv; |
|
143 |
|
|
144 |
rv = esp32_set_tcp_server(comport, port); |
|
145 |
if(rv<0) |
|
146 |
return rv; |
|
147 |
|
|
148 |
rv = esp32_set_socket_timeout(comport, 600); |
|
149 |
if(rv<0) |
|
150 |
return rv; |
|
151 |
|
|
152 |
return 0; |
|
153 |
} |
|
154 |
|
|
155 |
int esp32_setup_tcp_client(comport_t *comport, char *host, int port) |
|
156 |
{ |
|
157 |
int rv; |
|
158 |
|
|
159 |
rv = esp32_set_tcp_client(comport, DISABLE, host, port); |
|
160 |
if(rv<0) |
|
161 |
return rv; |
|
162 |
|
|
163 |
|
|
164 |
return 0; |
|
165 |
} |
|
166 |
|