Implement wi-fi configuration

Add console commands to setup wi-fi
Add parameter for choosing between STA and AP mode.
Add parameter for udp ports.
Remove WIFI_ENABLED macro.
This commit is contained in:
Oleg Kalachev
2026-01-19 00:53:36 +03:00
parent 6548ae5708
commit cef1834ea3
8 changed files with 89 additions and 38 deletions

View File

@@ -7,6 +7,7 @@
"MD024": false, "MD024": false,
"MD033": false, "MD033": false,
"MD034": false, "MD034": false,
"MD040": false,
"MD059": false, "MD059": false,
"MD044": { "MD044": {
"html_elements": false, "html_elements": false,

View File

@@ -243,9 +243,37 @@ In this mode, the pilot inputs are ignored (except the mode switch, if configure
If the pilot moves the control sticks, the drone will switch back to *STAB* mode. If the pilot moves the control sticks, the drone will switch back to *STAB* mode.
## Wi-Fi configuration
You can configure the Wi-Fi using parameters and console commands.
The Wi-Fi mode is chosen using `WIFI_MODE` parameter in QGroundControl or in the console:
* `0` — Wi-Fi is disabled.
* `1` — Access Point mode (*AP*) — the drone creates a Wi-Fi network.
* `2` — Client mode (*STA*) — the drone connects to an existing Wi-Fi network.
* `3` — *ESP-NOW (not implemented yet)*.
> [!WARNING]
> Tests showed that Client mode may cause **additional delays** in remote control (due to retranslations), so it's generally not recommended.
The SSID and password are configured using the `ap` and `sta` console commands:
```
ap <ssid> <password>
sta <ssid> <password>
```
Example of configuring the Access Point mode:
```
ap my-flix-ssid mypassword123
p WIFI_MODE 1
```
## Flight log ## Flight log
After the flight, you can download the flight log for analysis wirelessly. Use the following for that: After the flight, you can download the flight log for analysis wirelessly. Use the following command on your computer for that:
```bash ```bash
make log make log

View File

@@ -38,6 +38,8 @@ const char* motd =
"raw/stab/acro/auto - set mode\n" "raw/stab/acro/auto - set mode\n"
"rc - show RC data\n" "rc - show RC data\n"
"wifi - show Wi-Fi info\n" "wifi - show Wi-Fi info\n"
"ap <ssid> <password> - setup Wi-Fi access point\n"
"sta <ssid> <password> - setup Wi-Fi client mode\n"
"mot - show motor output\n" "mot - show motor output\n"
"log [dump] - print log header [and data]\n" "log [dump] - print log header [and data]\n"
"cr - calibrate RC\n" "cr - calibrate RC\n"
@@ -54,9 +56,7 @@ void print(const char* format, ...) {
vsnprintf(buf, sizeof(buf), format, args); vsnprintf(buf, sizeof(buf), format, args);
va_end(args); va_end(args);
Serial.print(buf); Serial.print(buf);
#if WIFI_ENABLED
mavlinkPrint(buf); mavlinkPrint(buf);
#endif
} }
void pause(float duration) { void pause(float duration) {
@@ -64,9 +64,7 @@ void pause(float duration) {
while (t - start < duration) { while (t - start < duration) {
step(); step();
handleInput(); handleInput();
#if WIFI_ENABLED
processMavlink(); processMavlink();
#endif
delay(50); delay(50);
} }
} }
@@ -136,9 +134,11 @@ void doCommand(String str, bool echo = false) {
print("mode: %s\n", getModeName()); print("mode: %s\n", getModeName());
print("armed: %d\n", armed); print("armed: %d\n", armed);
} else if (command == "wifi") { } else if (command == "wifi") {
#if WIFI_ENABLED
printWiFiInfo(); printWiFiInfo();
#endif } else if (command == "ap") {
configWiFi(true, arg0.c_str(), arg1.c_str());
} else if (command == "sta") {
configWiFi(false, arg0.c_str(), arg1.c_str());
} else if (command == "mot") { } else if (command == "mot") {
print("front-right %g front-left %g rear-right %g rear-left %g\n", print("front-right %g front-left %g rear-right %g rear-left %g\n",
motors[MOTOR_FRONT_RIGHT], motors[MOTOR_FRONT_LEFT], motors[MOTOR_REAR_RIGHT], motors[MOTOR_REAR_LEFT]); motors[MOTOR_FRONT_RIGHT], motors[MOTOR_FRONT_LEFT], motors[MOTOR_REAR_RIGHT], motors[MOTOR_REAR_LEFT]);

View File

@@ -7,7 +7,6 @@
#include "quaternion.h" #include "quaternion.h"
#include "util.h" #include "util.h"
#define WIFI_ENABLED 1
extern float t, dt; extern float t, dt;
extern float controlRoll, controlPitch, controlYaw, controlThrottle, controlMode; extern float controlRoll, controlPitch, controlYaw, controlThrottle, controlMode;
@@ -25,9 +24,7 @@ void setup() {
setupLED(); setupLED();
setupMotors(); setupMotors();
setLED(true); setLED(true);
#if WIFI_ENABLED
setupWiFi(); setupWiFi();
#endif
setupIMU(); setupIMU();
setupRC(); setupRC();
setLED(false); setLED(false);
@@ -42,9 +39,7 @@ void loop() {
control(); control();
sendMotors(); sendMotors();
handleInput(); handleInput();
#if WIFI_ENABLED
processMavlink(); processMavlink();
#endif
logData(); logData();
syncParameters(); syncParameters();
} }

View File

@@ -3,8 +3,6 @@
// MAVLink communication // MAVLink communication
#if WIFI_ENABLED
#include <MAVLink.h> #include <MAVLink.h>
#include "util.h" #include "util.h"
@@ -274,5 +272,3 @@ void sendMavlinkPrint() {
} }
mavlinkPrintBuffer.clear(); mavlinkPrintBuffer.clear();
} }
#endif

View File

@@ -9,6 +9,7 @@
extern float channelZero[16]; extern float channelZero[16];
extern float channelMax[16]; extern float channelMax[16];
extern float rollChannel, pitchChannel, throttleChannel, yawChannel, armedChannel, modeChannel; extern float rollChannel, pitchChannel, throttleChannel, yawChannel, armedChannel, modeChannel;
extern int wifiMode, udpLocalPort, udpRemotePort;
Preferences storage; Preferences storage;
@@ -82,6 +83,10 @@ Parameter parameters[] = {
{"RC_THROTTLE", &throttleChannel}, {"RC_THROTTLE", &throttleChannel},
{"RC_YAW", &yawChannel}, {"RC_YAW", &yawChannel},
{"RC_MODE", &modeChannel}, {"RC_MODE", &modeChannel},
// wifi
{"WIFI_MODE", &wifiMode},
{"WIFI_LOC_PORT", &udpLocalPort},
{"WIFI_REM_PORT", &udpRemotePort},
}; };
void setupParameters() { void setupParameters() {

View File

@@ -1,49 +1,76 @@
// Copyright (c) 2023 Oleg Kalachev <okalachev@gmail.com> // Copyright (c) 2023 Oleg Kalachev <okalachev@gmail.com>
// Repository: https://github.com/okalachev/flix // Repository: https://github.com/okalachev/flix
// Wi-Fi support // Wi-Fi connectivity
#if WIFI_ENABLED
#include <WiFi.h> #include <WiFi.h>
#include <WiFiAP.h> #include <WiFiAP.h>
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include "Preferences.h"
#define WIFI_SSID "flix" extern Preferences storage; // use the main preferences storage
#define WIFI_PASSWORD "flixwifi"
#define WIFI_UDP_PORT 14550 const int W_DISABLED = 0, W_AP = 1, W_STA = 2;
#define WIFI_UDP_REMOTE_PORT 14550 int wifiMode = W_AP;
#define WIFI_UDP_REMOTE_ADDR "255.255.255.255" int udpLocalPort = 14550;
int udpRemotePort = 14550;
IPAddress udpRemoteIP = "255.255.255.255";
WiFiUDP udp; WiFiUDP udp;
void setupWiFi() { void setupWiFi() {
print("Setup Wi-Fi\n"); print("Setup Wi-Fi\n");
WiFi.softAP(WIFI_SSID, WIFI_PASSWORD); if (wifiMode == W_AP) {
udp.begin(WIFI_UDP_PORT); WiFi.softAP(storage.getString("WIFI_AP_SSID", "flix").c_str(), storage.getString("WIFI_AP_PASS", "flixwifi").c_str());
} else if (wifiMode == W_STA) {
WiFi.begin(storage.getString("WIFI_STA_SSID", "").c_str(), storage.getString("WIFI_STA_PASS", "").c_str());
}
udp.begin(udpLocalPort);
} }
void sendWiFi(const uint8_t *buf, int len) { void sendWiFi(const uint8_t *buf, int len) {
if (WiFi.softAPIP() == IPAddress(0, 0, 0, 0) && WiFi.status() != WL_CONNECTED) return; if (WiFi.softAPgetStationNum() == 0 && !WiFi.isConnected()) return;
udp.beginPacket(udp.remoteIP() ? udp.remoteIP() : WIFI_UDP_REMOTE_ADDR, WIFI_UDP_REMOTE_PORT); udp.beginPacket(udpRemoteIP, udpRemotePort);
udp.write(buf, len); udp.write(buf, len);
udp.endPacket(); udp.endPacket();
} }
int receiveWiFi(uint8_t *buf, int len) { int receiveWiFi(uint8_t *buf, int len) {
udp.parsePacket(); udp.parsePacket();
if (udp.remoteIP()) udpRemoteIP = udp.remoteIP();
return udp.read(buf, len); return udp.read(buf, len);
} }
void printWiFiInfo() { void printWiFiInfo() {
print("MAC: %s\n", WiFi.softAPmacAddress().c_str()); if (WiFi.getMode() == WIFI_MODE_AP) {
print("SSID: %s\n", WiFi.softAPSSID().c_str()); print("Mode: Access Point (AP)\n");
print("Password: %s\n", WIFI_PASSWORD); print("MAC: %s\n", WiFi.softAPmacAddress().c_str());
print("Clients: %d\n", WiFi.softAPgetStationNum()); print("SSID: %s\n", WiFi.softAPSSID().c_str());
print("Status: %d\n", WiFi.status()); print("Password: ***\n");
print("IP: %s\n", WiFi.softAPIP().toString().c_str()); print("Clients: %d\n", WiFi.softAPgetStationNum());
print("Remote IP: %s\n", udp.remoteIP().toString().c_str()); print("IP: %s\n", WiFi.softAPIP().toString().c_str());
} else if (WiFi.getMode() == WIFI_MODE_STA) {
print("Mode: Client (STA)\n");
print("Connected: %d\n", WiFi.isConnected());
print("MAC: %s\n", WiFi.macAddress().c_str());
print("SSID: %s\n", WiFi.SSID().c_str());
print("Password: ***\n");
print("IP: %s\n", WiFi.localIP().toString().c_str());
} else {
print("Mode: Disabled\n");
return;
}
print("Remote IP: %s\n", udpRemoteIP.toString().c_str());
print("MAVLink connected: %d\n", mavlinkConnected); print("MAVLink connected: %d\n", mavlinkConnected);
} }
#endif void configWiFi(bool ap, const char *ssid, const char *password) {
if (ap) {
storage.putString("WIFI_AP_SSID", ssid);
storage.putString("WIFI_AP_PASS", password);
} else {
storage.putString("WIFI_STA_SSID", ssid);
storage.putString("WIFI_STA_PASS", password);
}
print("✓ Reboot to apply new settings\n");
}

View File

@@ -10,8 +10,6 @@
#include "Arduino.h" #include "Arduino.h"
#include "wifi.h" #include "wifi.h"
#define WIFI_ENABLED 1
extern float t, dt; extern float t, dt;
extern float controlRoll, controlPitch, controlYaw, controlThrottle, controlMode; extern float controlRoll, controlPitch, controlYaw, controlThrottle, controlMode;
extern Vector rates; extern Vector rates;
@@ -73,3 +71,4 @@ void calibrateAccel() { print("Skip accel calibrating\n"); };
void printIMUCalibration() { print("cal: N/A\n"); }; void printIMUCalibration() { print("cal: N/A\n"); };
void printIMUInfo() {}; void printIMUInfo() {};
void printWiFiInfo() {}; void printWiFiInfo() {};
void configWiFi(bool, const char*, const char*) { print("Skip WiFi config\n"); };