Skip to content

Commit

Permalink
manually merge wifi-station-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
marchingband committed Dec 25, 2023
1 parent f238475 commit 033c35c
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/bundle.h

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion src/file_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
// char waver_tag[METADATA_TAG_LENGTH] = "wvr_magic_13"; // v1.x.x
// char waver_tag[METADATA_TAG_LENGTH] = "wvr_magic_14"; // v2.x.x
char waver_tag[METADATA_TAG_LENGTH] = "wvr_magic_15"; // v3.x.x

static const char* TAG = "file_system";

// declare prototypes from emmc.c
Expand Down Expand Up @@ -217,7 +218,10 @@ void init_metadata(void){
.wifi_power = 8,
.midi_channel = 0,
.pitch_bend_semitones_up = 2,
.pitch_bend_semitones_down = 2
.pitch_bend_semitones_down = 2,
.do_station_mode = 0,
.station_ssid = "",
.station_passphrase = ""
};
memcpy(new_metadata.tag, waver_tag, METADATA_TAG_LENGTH);
write_metadata(new_metadata);
Expand Down Expand Up @@ -931,6 +935,9 @@ void add_metadata_json(cJSON * RESPONSE_ROOT){
cJSON_AddNumberToObject(RESPONSE_ROOT,"pitchBendSemitonesDown",metadata.pitch_bend_semitones_down);
cJSON_AddStringToObject(RESPONSE_ROOT,"wifiNetworkName",metadata.ssid);
cJSON_AddStringToObject(RESPONSE_ROOT,"wifiNetworkPassword",metadata.passphrase);
cJSON_AddNumberToObject(RESPONSE_ROOT,"doStationMode",metadata.do_station_mode);
cJSON_AddStringToObject(RESPONSE_ROOT,"stationWifiNetworkName",metadata.station_ssid);
cJSON_AddStringToObject(RESPONSE_ROOT,"stationWifiNetworkPassword",metadata.station_passphrase);
}

void add_pin_config_json(cJSON *RESPONSE_ROOT){
Expand Down Expand Up @@ -1327,6 +1334,9 @@ void updateMetadata(cJSON *config){
metadata.midi_channel = cJSON_GetObjectItemCaseSensitive(json, "midiChannel")->valueint;
metadata.pitch_bend_semitones_up = cJSON_GetObjectItemCaseSensitive(json, "pitchBendSemitonesUp")->valueint;
metadata.pitch_bend_semitones_down = cJSON_GetObjectItemCaseSensitive(json, "pitchBendSemitonesDown")->valueint;
metadata.do_station_mode = cJSON_GetObjectItemCaseSensitive(json, "doStationMode")->valueint;
memcpy(&metadata.station_ssid,cJSON_GetObjectItemCaseSensitive(json, "stationWifiNetworkName")->valuestring,20);
memcpy(&metadata.station_passphrase,cJSON_GetObjectItemCaseSensitive(json, "stationWifiNetworkPassword")->valuestring,20);
memcpy(&metadata.ssid,cJSON_GetObjectItemCaseSensitive(json, "wifiNetworkName")->valuestring,20);
memcpy(&metadata.passphrase,cJSON_GetObjectItemCaseSensitive(json, "wifiNetworkPassword")->valuestring,20);
write_metadata(metadata);
Expand Down
3 changes: 3 additions & 0 deletions src/file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ struct metadata_t {
uint8_t midi_channel; // 0 is omni
uint8_t pitch_bend_semitones_up;
uint8_t pitch_bend_semitones_down;
uint8_t do_station_mode;
char station_ssid[20];
char station_passphrase[20];
};

struct vol_t {
Expand Down
4 changes: 3 additions & 1 deletion src/html.h
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
const char HTML[] = R"=====(<!doctype html><html lang="en"><head><title>WVR</title><link href="http://192.168.4.1/favicon.ico" rel="icon" type="image/x-icon"/></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script src="http://192.168.4.1/bundle" type="text/javascript"></script></body></html>)=====";
// const char HTML[] = R"=====(<!doctype html><html lang="en"><head><title>WVR</title><link href="http://192.168.4.1/favicon.ico" rel="icon" type="image/x-icon"/></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script src="http://192.168.4.1/bundle" type="text/javascript"></script></body></html>)=====";

const char HTML[] = R"=====(<!doctype html><html lang="en"><head><title>WVR</title><link href="/favicon.ico" rel="icon" type="image/x-icon"/></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script src="/bundle" type="text/javascript"></script></body></html>)=====";
114 changes: 107 additions & 7 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,51 @@ void handleConfigJSON(AsyncWebServerRequest *request){
request->send(response);
}

void handleFetchLocalIP(AsyncWebServerRequest *request){
log_i("handleFetchLocalIP");
if(WiFi.status() == WL_CONNECTED)
{
request->send(200, "text/plain", WiFi.localIP().toString().c_str());
}
else if(WiFi.status() == WL_DISCONNECTED)
{
request->send(200, "text/plain", "trying to connect");
}
else if(WiFi.status() == WL_CONNECTION_LOST)
{
request->send(200, "text/plain", "connection lost");
}
else if(WiFi.status() == WL_CONNECT_FAILED)
{
request->send(200, "text/plain", "wrong password");
}
else if(WiFi.status() == WL_NO_SSID_AVAIL)
{
request->send(200, "text/plain", "wrong network name");
}
else
{
request->send(200, "text/plain", "unkown error");
}
}

void handleTryLogonLocalNetwork(AsyncWebServerRequest *request){
log_e("handleTryLogonLocalNetwork");
const char* ssid = request->getHeader("ssid")->value().c_str();
const char* password = request->getHeader("password")->value().c_str();
memcpy(&metadata->station_ssid, ssid, 20);
memcpy(&metadata->station_passphrase, password, 20);
metadata->do_station_mode = 1;
log_e("headers: %s %s meta: %s %s",
ssid, password,
metadata->station_ssid,
metadata->station_passphrase
);
write_metadata(*metadata);
request->send(200, "text/plain", "saved, please reset");
try_log_on_network();
}

void handleBackupEMMC(AsyncWebServerRequest *request){
uint8_t *buf = (uint8_t*)ps_malloc(SECTOR_SIZE);
size_t numSectors = getNumSectorsInEmmc();
Expand Down Expand Up @@ -632,6 +677,21 @@ void handlePlayWav(AsyncWebServerRequest *request){
request->send(204);
}

void try_log_on_network()
{
log_e("begin try_log_on_network");
WiFi.begin(metadata->station_ssid, metadata->station_passphrase);
log_e("done begin try_log_on_network");
// WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info){
// log_e("WiFi lost connection. Reason: %s", info.wifi_sta_disconnected.reason);
// }, ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
// log_e("done disconnect listener");
WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info){
log_e("WiFi connected. IP: %s", IPAddress(info.got_ip.ip_info.ip.addr).toString().c_str());
}, ARDUINO_EVENT_WIFI_STA_GOT_IP);
log_e("done connect listener");
}

void _server_pause(){
ws.closeAll();
server.end();
Expand All @@ -641,7 +701,8 @@ void _server_pause(){
void server_begin() {
Serial.println("Configuring access point...");

WiFi.mode(WIFI_AP);
// WiFi.mode(WIFI_AP);
WiFi.mode(WIFI_MODE_APSTA);

// IPAddress IP = IPAddress (192, 168, 5, 18);
// IPAddress gateway = IPAddress (192, 168, 5, 17);
Expand All @@ -666,6 +727,22 @@ void server_begin() {
esp_wifi_get_max_tx_power(&power);
log_i("wifi power is %d", power);

if(metadata->do_station_mode == 1){
log_e("starting station");
WiFi.begin(metadata->station_ssid, metadata->station_passphrase);
int retries = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
log_e("Connecting to WiFi..");
if(retries++ > 10){
log_e("Failed to connect to WiFi..");
break;
}
}
if(WiFi.status() == WL_CONNECTED){
log_e("connect on IP: %s", WiFi.localIP().toString().c_str());
}
}

server.on(
"/",
Expand All @@ -691,6 +768,18 @@ void server_begin() {
handleMain
);

server.on(
"/fetchLocalIP",
HTTP_GET,
handleFetchLocalIP
);

server.on(
"/tryLogonLocalNetwork",
HTTP_GET,
handleTryLogonLocalNetwork
);

server.on(
"/emmc",
HTTP_GET,
Expand Down Expand Up @@ -831,7 +920,6 @@ void server_begin() {
handleDeleteFirmware
);


ws.onEvent(onWsEvent);
server.addHandler(&ws);

Expand All @@ -844,18 +932,18 @@ void recovery_server_begin() {

WiFi.mode(WIFI_AP);

IPAddress IP = IPAddress (192, 168, 5, 18);
IPAddress gateway = IPAddress (192, 168, 5, 20);
IPAddress NMask = IPAddress (255, 255, 255, 0);
// IPAddress IP = IPAddress (192, 168, 5, 18);
// IPAddress gateway = IPAddress (192, 168, 5, 20);
// IPAddress NMask = IPAddress (255, 255, 255, 0);

WiFi.softAPConfig(IP, gateway, NMask);
// WiFi.softAPConfig(IP, gateway, NMask);

WiFi.softAP("WVR", "12345678");
log_i("recovery mode ssid :WVR, passphrase: 12345678");
log_i("normal mode wifi ssid is :%s, passphrase is: %s",metadata->ssid, metadata->passphrase);

// again??
WiFi.softAPConfig(IP, gateway, NMask);
// WiFi.softAPConfig(IP, gateway, NMask);

IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Expand Down Expand Up @@ -885,6 +973,18 @@ void recovery_server_begin() {
handleFavicon
);

server.on(
"/fetchLocalIP",
HTTP_GET,
handleFetchLocalIP
);

server.on(
"/tryLogonLocalNetwork",
HTTP_GET,
handleTryLogonLocalNetwork
);

server.on(
"/update",
HTTP_POST,
Expand Down
1 change: 1 addition & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
void server_pause();
void server_resume();
bool get_wifi_is_on();
void try_log_on_network();

#endif
8 changes: 8 additions & 0 deletions src/wvr_0.3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "file_system.h"
#include "WVR.h"
#include "gpio.h"
#include "server.h"

struct wav_lu_t **wav_lut;

Expand Down Expand Up @@ -128,6 +129,13 @@ void wvr_init(bool useFTDI, bool useUsbMidi, bool checkRecoveryModePin) {
server_begin();
logSize("server");

log_i("do_station_mode:%d network:%s pass:%s",get_metadata()->do_station_mode,get_metadata()->station_ssid,get_metadata()->station_passphrase);

if(get_metadata()->do_station_mode == 1)
{
try_log_on_network();
}

button_init();
logSize("button");

Expand Down

0 comments on commit 033c35c

Please sign in to comment.