Skip to content

Commit d3db1a5

Browse files
committed
wireless, support getting device name, device_id, show in cli & disp, and make it faster by '*'
1 parent 74ccd3e commit d3db1a5

File tree

9 files changed

+151
-34
lines changed

9 files changed

+151
-34
lines changed

esp/mlrs-wireless-bridge/mlrs-wireless-bridge-at-mode.h

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ typedef enum {
3737
AT_PROTOCOL_2_UDPSTA,
3838
AT_PROTOCOL_3_BT,
3939
AT_PROTOCOL_4_UDPCl,
40+
AT_WIFIDEVICEID_QUERY,
41+
AT_WIFIDEVICENAME_QUERY,
4042
AT_BINDPHRASE_QUERY,
4143
AT_BINDPHRASE_XXXXXX,
4244
AT_CMDS_NUM,
@@ -67,6 +69,8 @@ const char* at_cmds[AT_CMDS_NUM] = {
6769
"AT+PROTOCOL=2",
6870
"AT+PROTOCOL=3",
6971
"AT+PROTOCOL=4",
72+
"AT+WIFIDEVICEID=?", // only query, no option to set
73+
"AT+WIFIDEVICENAME=?", // only query, no option to set
7074
"AT+BINDPHRASE=?",
7175
"AT+BINDPHRASE=xxxxxx",
7276
};
@@ -162,8 +166,9 @@ bool AtMode::Do(void)
162166

163167
// check if it is a full match already, and if so take action
164168
if (strcmp(at_buf, at_cmd) == 0) {
165-
if (i == AT_NAME_QUERY || i == AT_BAUD_QUERY || i == AT_WIFICHANNEL_QUERY ||
166-
i == AT_WIFIPOWER_QUERY || i == AT_PROTOCOL_QUERY || i == AT_BINDPHRASE_QUERY) {
169+
if (i == AT_NAME_QUERY || i == AT_BAUD_QUERY || i == AT_WIFICHANNEL_QUERY || i == AT_WIFIPOWER_QUERY ||
170+
i == AT_PROTOCOL_QUERY || i == AT_WIFIDEVICEID_QUERY || i == AT_WIFIDEVICENAME_QUERY ||
171+
i == AT_BINDPHRASE_QUERY) {
167172
at_buf[0] = 'O';
168173
at_buf[1] = 'K';
169174
SERIAL.write(at_buf, at_pos - 1); // don't send the '?'
@@ -176,6 +181,8 @@ bool AtMode::Do(void)
176181
case AT_WIFICHANNEL_QUERY: SERIAL.print(g_wifichannel); break;
177182
case AT_WIFIPOWER_QUERY: SERIAL.print(g_wifipower); break;
178183
case AT_PROTOCOL_QUERY: SERIAL.print(g_protocol); break;
184+
case AT_WIFIDEVICEID_QUERY: SERIAL.print(device_id); break;
185+
case AT_WIFIDEVICENAME_QUERY: SERIAL.print(device_name); break;
179186
case AT_BINDPHRASE_QUERY: SERIAL.print(g_bindphrase); break;
180187
}
181188
SERIAL.write("\r\n");
@@ -197,58 +204,63 @@ bool AtMode::Do(void)
197204
if (i >= AT_BAUD_9600 && i <= AT_BAUD_230400) {
198205
at_buf[0] = 'O';
199206
at_buf[1] = 'K';
200-
SERIAL.write(at_buf, at_pos);
201-
SERIAL.write("\r\n");
202207
int new_baudrate = atoi(at_buf + 8); // AT+BAUD=9600
203208
if (new_baudrate != g_baudrate) {
204209
preferences.putInt(G_BAUDRATE_STR, new_baudrate);
205210
restart_needed = true;
211+
at_buf[2] = '*'; // replace '+' by '*' to mark change
206212
}
213+
SERIAL.write(at_buf, at_pos);
214+
SERIAL.write("\r\n");
207215
} else
208216
if (i >= AT_WIFICHANNEL_1 && i <= AT_WIFICHANNEL_13) {
209217
at_buf[0] = 'O';
210218
at_buf[1] = 'K';
211-
SERIAL.write(at_buf, at_pos);
212-
SERIAL.write("\r\n");
213219
int new_wifichannel = atoi(at_buf + 15); // AT+WIFICHANNEL=1
214220
if (new_wifichannel != g_wifichannel) {
215221
preferences.putInt(G_WIFICHANNEL_STR, new_wifichannel);
216222
restart_needed = true;
223+
at_buf[2] = '*'; // replace '+' by '*' to mark change
217224
}
225+
SERIAL.write(at_buf, at_pos);
226+
SERIAL.write("\r\n");
218227
} else
219228
if (i >= AT_WIFIPOWER_0_LOW && i <= AT_WIFIPOWER_2_MAX) {
220229
at_buf[0] = 'O';
221230
at_buf[1] = 'K';
222-
SERIAL.write(at_buf, at_pos);
223-
SERIAL.write("\r\n");
224231
int new_wifipower = atoi(at_buf + 13); // AT+WIFIPOWER=1
225232
if (new_wifipower != g_wifipower) {
226233
preferences.putInt(G_WIFIPOWER_STR, new_wifipower);
227234
restart_needed = true;
235+
at_buf[2] = '*'; // replace '+' by '*' to mark change
228236
}
237+
SERIAL.write(at_buf, at_pos);
238+
SERIAL.write("\r\n");
229239
} else
230240
if (i >= AT_PROTOCOL_0_TCP && i <= AT_PROTOCOL_4_UDPCl) {
231241
at_buf[0] = 'O';
232242
at_buf[1] = 'K';
233-
SERIAL.write(at_buf, at_pos);
234-
SERIAL.write("\r\n");
235243
int new_protocol = atoi(at_buf + 12); // AT+PROTOCOL=1
236244
if (new_protocol != g_protocol) {
237245
preferences.putInt(G_PROTOCOL_STR, new_protocol);
238246
restart_needed = true;
247+
at_buf[2] = '*'; // replace '+' by '*' to mark change
239248
}
240-
} else
249+
SERIAL.write(at_buf, at_pos);
250+
SERIAL.write("\r\n");
251+
} else
241252
if (i == AT_BINDPHRASE_XXXXXX) {
242253
at_buf[0] = 'O';
243254
at_buf[1] = 'K';
244-
SERIAL.write(at_buf, at_pos);
245-
SERIAL.write("\r\n");
246255
String new_bindphrase = "";
247256
for (int pp = 14; pp < 14+6; pp++) new_bindphrase += at_buf[pp]; // AT+BINDPHRASE=mlrs.0
248257
if (new_bindphrase != g_bindphrase) {
249258
preferences.putString(G_BINDPHRASE_STR, new_bindphrase);
250259
restart_needed = true;
260+
at_buf[2] = '*'; // replace '+' by '*' to mark change
251261
}
262+
SERIAL.write(at_buf, at_pos);
263+
SERIAL.write("\r\n");
252264
}
253265
at_pos = 0;
254266
break;

esp/mlrs-wireless-bridge/mlrs-wireless-bridge.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,17 @@ int g_wifipower = WIFIPOWER_DEFAULT;
267267
#define G_BINDPHRASE_STR "bindphrase"
268268
String g_bindphrase = "mlrs.0";
269269

270+
uint16_t device_id = 0; // is going to be set by setup_device_name_and_password(), and can be queried in at mode
271+
String device_name = "";
272+
String device_password = "";
273+
270274
#ifdef USE_AT_MODE
271275
#include <Preferences.h>
272276
Preferences preferences;
273277
#include "mlrs-wireless-bridge-at-mode.h"
274278
AtMode at_mode;
275279
#endif
276280

277-
String device_name = "";
278-
String device_password = "";
279281
bool wifi_initialized;
280282
bool led_state;
281283
unsigned long led_tlast_ms;
@@ -304,7 +306,6 @@ void setup_device_name_and_password(void)
304306
#else
305307
wifi_get_macaddr(STATION_IF, MAC_buf);
306308
#endif
307-
uint16_t device_id = 0;
308309
for (uint8_t i = 0; i < 5; i++) device_id += MAC_buf[i] + ((uint16_t)MAC_buf[i + 1] << 8) / 39;
309310
device_id += MAC_buf[5];
310311
device_name = "mLRS-";
@@ -354,8 +355,6 @@ void setup_wifipower()
354355

355356
void setup_wifi()
356357
{
357-
setup_device_name_and_password();
358-
359358
if (g_protocol == WIRELESS_PROTOCOL_TCP || g_protocol == WIRELESS_PROTOCOL_UDP) {
360359
//-- WiFi TCP, UDP
361360

@@ -482,6 +481,7 @@ void setup()
482481
g_bindphrase = preferences.getString(G_BINDPHRASE_STR, "mlrs.0"); // "mlrs.0" is the mLRS default bind phrase
483482
// TODO: we should check for sanity
484483
#endif
484+
setup_device_name_and_password();
485485

486486
// Serial
487487
size_t rxbufsize = SERIAL.setRxBufferSize(2*1024); // must come before uart started, retuns 0 if it fails

mLRS/Common/setup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void setup_configure_metadata(void)
153153
SetupMetaData.Tx_Buzzer_allowed_mask = 0; // not available, do not display
154154
#endif
155155

156-
// Tx Bridge WiFi Protocol : "TCP,UDP,BT,UDPCl"
156+
// Tx Bridge WiFi Protocol : "TCP,UDP,BT,UDPSTA"
157157
#ifdef DEVICE_HAS_ESP_WIFI_BRIDGE_ESP8266
158158
SetupMetaData.Tx_WiFiProt_allowed_mask = 0b1011; // no BT
159159
#else

mLRS/CommonTx/cli.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern tStats stats;
2525
extern tSetupMetaData SetupMetaData;
2626
extern tSetup Setup;
2727
extern tGlobalConfig Config;
28+
extern tTxInfo info;
2829
extern tTasks tasks;
2930

3031

@@ -623,7 +624,13 @@ void tTxCli::print_device_version(void)
623624
{
624625
print_layout_version_warning();
625626

626-
putsn(" Tx: " DEVICE_NAME ", " VERSIONONLYSTR);
627+
puts(" Tx: " DEVICE_NAME ", " VERSIONONLYSTR);
628+
char s[48];
629+
if (info.WirelessDeviceName_cli(s)) {
630+
puts(", ");
631+
puts(s);
632+
}
633+
putsn("");
627634

628635
puts(" Rx: ");
629636
if (connected()) {

mLRS/CommonTx/disp.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extern tGDisplay gdisp;
4444
extern tSetupMetaData SetupMetaData;
4545
extern tSetup Setup;
4646
extern tGlobalConfig Config;
47+
extern tTxInfo info;
4748
extern tTasks tasks;
4849
void i2c_spin(uint16_t chunksize);
4950

@@ -909,6 +910,11 @@ char s[32];
909910
gdisp_setcurXY(0, 1 * 10 + 20);
910911
gdisp_puts(VERSIONONLYSTR);
911912

913+
if (info.WirelessDeviceName_disp(s)) {
914+
gdisp_setcurXY(60, 1 * 10 + 20);
915+
gdisp_puts(s);
916+
}
917+
912918
if (connected_and_rx_setup_available()) {
913919
gdisp_setcurXY(0, 3 * 10 + 20);
914920
gdisp_puts(SetupMetaData.rx_device_name);

mLRS/CommonTx/esp.h

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class tTxEspWifiBridge
114114
private:
115115
#ifdef ESP_STARTUP_CONFIGURE
116116
bool esp_read(const char* const cmd, char* const res, uint8_t* const len);
117+
void esp_wait_after_read(const char* const res);
117118
void esp_configure_baudrate(void);
118119
void esp_configure_wifiprotocol(void);
119120
void esp_configure_wifichannel(void);
@@ -136,6 +137,8 @@ class tTxEspWifiBridge
136137

137138
uint8_t dtr_rts_last;
138139
uint8_t boot0_last;
140+
141+
uint32_t version;
139142
};
140143

141144

@@ -169,6 +172,8 @@ void tTxEspWifiBridge::Init(
169172
dtr_rts_last = 0;
170173
boot0_last = 0;
171174

175+
version = 0; // unknown
176+
172177
#ifdef ESP_STARTUP_CONFIGURE
173178
run_configure();
174179
#endif
@@ -377,6 +382,20 @@ ESP_DBG(dbg.puts("!ENDE!");)
377382
}
378383

379384

385+
void tTxEspWifiBridge::esp_wait_after_read(const char* const res)
386+
{
387+
if (version >= 10307){ // sends a '*' instead of a '+' if setting had been changed
388+
if (res[2] == '+') { // no change, so no need to wait for long
389+
delay_ms(5);
390+
return;
391+
}
392+
}
393+
394+
// wait for save on esp to finish
395+
delay_ms(100);
396+
}
397+
398+
380399
void tTxEspWifiBridge::esp_configure_baudrate(void)
381400
{
382401
char s[ESP_CMDRES_LEN+2];
@@ -388,12 +407,12 @@ char cmd_str[32];
388407
remove_leading_zeros(baud_str);
389408
strcpy(cmd_str, "AT+BAUD=");
390409
strcat(cmd_str, baud_str);
410+
391411
if (!esp_read(cmd_str, s, &len)) { // AT+BAUD sends response with "old" baud rate, when stores it, but does NOT change it
392412
return;
393413
}
394414

395-
// wait for save on esp to finish
396-
delay_ms(100);
415+
esp_wait_after_read(s);
397416
}
398417

399418

@@ -417,8 +436,7 @@ char cmd_str[32];
417436
return;
418437
}
419438

420-
// wait for save on esp to finish
421-
delay_ms(100);
439+
esp_wait_after_read(s);
422440
}
423441

424442

@@ -442,8 +460,7 @@ char cmd_str[32];
442460
return;
443461
}
444462

445-
// wait for save on esp to finish
446-
delay_ms(100);
463+
esp_wait_after_read(s);
447464
}
448465

449466

@@ -466,8 +483,7 @@ char cmd_str[32];
466483
return;
467484
}
468485

469-
// wait for save on esp to finish
470-
delay_ms(100);
486+
esp_wait_after_read(s);
471487
}
472488

473489

@@ -484,16 +500,14 @@ char cmd_str[32];
484500
return;
485501
}
486502

487-
// wait for save on esp to finish
488-
delay_ms(100);
503+
esp_wait_after_read(s);
489504
}
490505

491506

492507
void tTxEspWifiBridge::run_configure(void)
493508
{
494509
char s[ESP_CMDRES_LEN+2];
495510
uint8_t len;
496-
uint32_t ver;
497511

498512
if (ser == nullptr) return; // we need a serial
499513

@@ -502,7 +516,6 @@ uint32_t ver;
502516
delay_ms(500); // not so nice, but it starts up really slowly ...
503517

504518
bool found = false;
505-
ver = 0; // unknown
506519

507520
uint32_t bauds[7] = { ser_baud, 9600, 19200, 38400, 57600, 115200, 230400 };
508521
uint8_t baud_idx = 0;
@@ -515,7 +528,7 @@ uint32_t ver;
515528
s[len-2] = '\0';
516529
if (!strncmp(s, "OK+NAME=mLRS-Wireless-Bridge", 28)) { // correct name, it's her we are looking for
517530
found = true;
518-
if (strlen(s) > 32) ver = version_from_str(s + 28);
531+
if (strlen(s) > 32) version = version_from_str(s + 28);
519532
}
520533
cc = 128; // break also higher for loop, don't do 255 LOL
521534
break;
@@ -538,11 +551,24 @@ esp_read("dAT+BINDPHRASE=?", s, &len);)
538551
esp_configure_wifiprotocol();
539552
esp_configure_wifichannel();
540553
esp_configure_wifipower();
541-
if (ver >= 10307) { // not available before v1.3.07
554+
if (version >= 10307) { // not available before v1.3.07
542555
esp_configure_bindphrase();
543556
} else {
544557
// Houston, we have a problem. UDPCl is not available but we allow the user to select
545558
}
559+
if (version >= 10307) { // not available before v1.3.07
560+
// esp_read("AT+WIFIDEVICEID=?", s, &len);
561+
// if (len > 18) device_id = atoi(s + 16);
562+
esp_read("AT+WIFIDEVICENAME=?", s, &len);
563+
if (len > 22) {
564+
strcpy(info.wireless.device_name, s + 18);
565+
info.wireless.device_name[strlen(info.wireless.device_name)-1] = '\0'; // strip off '\n'
566+
info.wireless.device_name[strlen(info.wireless.device_name)-1] = '\0'; // strip off '\r'
567+
}
568+
if (strlen(info.wireless.device_name) > 9 && !strncmp(info.wireless.device_name, "mLRS-", 5)) {
569+
info.wireless.device_id = atoi(info.wireless.device_name + 5);
570+
}
571+
}
546572

547573
if (esp_read("AT+RESTART", s, &len)) { // will respond with 'KO' if a restart isn't needed
548574
delay_ms(1500); // 500 ms is too short, 1000 ms is sometimes too short, 1200 ms works fine, play it safe

mLRS/CommonTx/hc04.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ uint8_t len;
223223
strcat(ok_device_name, u16toBCD_s(device_id));
224224
strcat(ok_device_name, "-BT");
225225

226+
info.wireless.device_id = device_id;
227+
strcpy(info.wireless.device_name, ok_device_name + 8); // strip off "OK+NAME="
228+
226229
for (uint8_t baud_idx = 0; baud_idx < 7; baud_idx++) {
227230
ser->SetBaudRate(bauds[baud_idx]);
228231
ser->flush();

0 commit comments

Comments
 (0)