Repository navigation
Expand file tree
/
Copy pathesp8266.cpp
More file actions
209 lines (152 loc) · 4.03 KB
/
Copy pathesp8266.cpp
File metadata and controls
209 lines (152 loc) · 4.03 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
198
199
200
201
202
203
204
205
206
207
208
209
#include "util.h"
#include "esp8266.h"
#if defined(ESP8266)
WiFiUDP Udp;
int init_wifi() {
WiFi.persistent(false);
WiFi.forceSleepWake();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
DEBUGP("Connecting to wifi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
DEBUGP(".");
}
DEBUGP("Connected, IP address: %s\r\n", WiFi.localIP().toString().c_str());
return 1;
}
int wifi_send_data(uint8_t *data, int len) {
int result = 0;
IPAddress broadcast_addr;
if (!broadcast_addr.fromString(BROADCAST_ADDR)) {
DEBUGP("Couldn't parse IP address %s\r\n", BROADCAST_ADDR);
}
Udp.beginPacket(broadcast_addr, BROADCAST_PORT);
Udp.write(data, len);
result = Udp.endPacket();
return result;
}
int init_server() {
return Udp.begin(BROADCAST_PORT);
}
int recv_datagram(uint8_t *data, int len) {
int result = 0;
int packet_len = Udp.parsePacket();
//DEBUGP("parsePacket() = %u\r\n", packet_len);
if (packet_len > 0 && packet_len <= len) {
result = Udp.read(data, len);
DEBUGP("read() = %u\r\n", result);
//Udp.flush();
}
return result;
}
#else
#define BUF_SIZ 256
char wifi_buf[BUF_SIZ];
void flush_input() {
if (wifiSerial->available() > 0) {
DEBUGP("characters in the receive buffer, discarding...\r\n");
while (wifiSerial->available() > 0)
wifiSerial->read();
}
}
int esp_cmd(char *cmd, char *resp) {
int bytes = 0;
wifiSerial->write(cmd);
wifiSerial->flush();
delay(100);
bytes = wifiSerial->available();
if (bytes > 0) {
bytes = wifiSerial->readBytes(resp, min(bytes, BUF_SIZ-1));
resp[min(bytes, BUF_SIZ-1)] = '\0';
DEBUGP("%s\r\n", resp);
}
flush_input();
delay(100);
return bytes;
}
char* disable_echo() {
DEBUGP("disable echo\r\n");
esp_cmd("ATE0\r\n", wifi_buf);
return wifi_buf;
}
char* reset_module() {
DEBUGP("reset module\r\n");
esp_cmd("AT+RST\r\n", wifi_buf);
return rtrim(ltrim(wifi_buf));
}
char* set_station_mode() {
DEBUGP("set station mode\r\n");
esp_cmd("AT+CWMODE=1\r\n", wifi_buf);
return rtrim(ltrim(wifi_buf));
}
char* set_network(char *ssid, char *password) {
char network_command[64];
DEBUGP("select network\r\n");
sprintf(network_command, "AT+CWJAP_CUR=\"%s\",\"%s\"\r\n", ssid, password);
esp_cmd(network_command, wifi_buf);
return rtrim(ltrim(wifi_buf));
}
char* set_target_address(char *address, char *port) {
char address_command[64];
DEBUGP("setup UDP target\r\n");
sprintf(address_command, "AT+CIPSTART=\"UDP\",\"%s\",%s\r\n", address,
port);
esp_cmd("AT+CIPSTART=\"UDP\",\"255.255.255.255\",1080\r\n", wifi_buf);
return rtrim(ltrim(wifi_buf));
}
char* get_module_version() {
DEBUGP("check module version\r\n");
esp_cmd("AT+GMR\r\n", wifi_buf);
return rtrim(ltrim(wifi_buf));
}
char* get_module_mac() {
DEBUGP("check module MAC\r\n");
esp_cmd("AT+CIPSTAMAC_CUR?\r\n", wifi_buf);
return rtrim(ltrim(wifi_buf));
}
char *get_conn_status() {
DEBUGP("check connection status\r\n");
esp_cmd("AT+CIPSTATUS\r\n", wifi_buf);
return rtrim(ltrim(wifi_buf));
}
int wifi_send_data(uint8_t *data, int len) {
char cmd[20];
DEBUGP("send datagram\r\n");
sprintf(cmd, "AT+CIPSEND=%u\r\n", len);
esp_cmd(cmd, wifi_buf);
wifiSerial->write(data, len);
wifiSerial->flush();
delay(100);
flush_input();
delay(200);
}
int init_wifi() {
DEBUGP("init_wifi\r\n");
wifiSerial->begin(DEFAULT_WIFI_BAUDRATE);
reset_module();
delay(1000);
flush_input();
disable_echo();
// Put ESP into station mode. This can also be replaced with SoftAP mode for some use cases.
set_station_mode();
DEBUGP("Set station mode: %s\r\n", wifi_buf);
set_network(WIFI_SSID, WIFI_PASSWORD);
DEBUGP("Set network: %s\r\n", wifi_buf);
wifi_buf[0] = '\0';
// It may take a bit to acquire registration. Obviously we can get stuck here forever too...
while(strstr(wifi_buf, "STATUS:2") != wifi_buf) {
get_conn_status();
DEBUGP("Connection status: %s\r\n", wifi_buf);
delay(1000);
}
set_target_address(BROADCAST_ADDR, BROADCAST_PORT);
DEBUGP("Set target address: %s\r\n", wifi_buf);
return 1;
}
int init_server() {
return 1;
}
int recv_datagram(uint8_t *data, int len) {
return 0;
}
#endif