Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
f9f8f17803 | |||
58ddb4fb74 | |||
c940a2afec | |||
0c42b53902 | |||
566ee35935 | |||
40f4d615d6 | |||
676e5fcadb | |||
97205ac43f | |||
d67dea275e | |||
06da06c519 | |||
763254b603 |
36
API_DOC.txt
36
API_DOC.txt
@@ -1,36 +0,0 @@
|
||||
API Location Method Returns Redirects to Description
|
||||
---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
/api/ API root
|
||||
----------------
|
||||
|
||||
|
||||
/api/v1/playback/ Playback functions
|
||||
----------------
|
||||
/api/v1/playback/toggle GET JSON object Toggle Playback
|
||||
/api/v1/playback/play GET JSON object Start playback
|
||||
/api/v1/playback/pause GET JSON object Pause playback
|
||||
/api/v1/playback/next GET JSON object Play next audio
|
||||
/api/v1/playback/previous GET JSON object Play previous audio
|
||||
/api/v1/playback/info GET JSON object Get the title, artist, album, path, type, ... of the current played resource
|
||||
|
||||
/api/v1/playlist/
|
||||
----------------
|
||||
/api/v1/playlist/get GET JSON object Returns the whole current playlist
|
||||
/api/v1/playlist/create POST JSON object Creates a playlist of the given directory
|
||||
/api/v1/playlist/play POST JSON object Plays a playlist from the SD card
|
||||
|
||||
/api/v1/volume/
|
||||
----------------
|
||||
/api/v1/volume/up GET JSON object Higher the volume (max. 20)
|
||||
/api/v1/volume/down GET JSON object Lower the volume (min. 0)
|
||||
/api/v1/volume/mute GET JSON object Mute (does not affect volume)
|
||||
/api/v1/volume/unmute GET JSON object Unmute (set volume to current volume)
|
||||
/api/v1/volume/get GET JSON object Do nothing, just get the volume
|
||||
/api/v1/volume/<0-20> GET JSON object Set volume to a specific value between 0 and 20
|
||||
|
||||
/api/v1/settings/
|
||||
----------------
|
||||
/api/v1/settings/restart GET JSON object Performs a reboot of the microcontroller (after waiting 5000ms)
|
||||
|
20
README.md
20
README.md
@@ -35,23 +35,29 @@ Features, already implemented, or still in progress for the v1.0.0 release!
|
||||
- [X] /api/v1/playback/pause
|
||||
- [X] /api/v1/playback/next
|
||||
- [X] /api/v1/playback/previous
|
||||
- [ ] /api/v1/playback/<index>
|
||||
- [X] /api/v1/playback/<index>
|
||||
- [X] /api/v1/playback/info
|
||||
- [ ] /api/v1/playback/id3_image
|
||||
- [X] /api/v1/playlist/get
|
||||
- [ ] /api/v1/playlist/append
|
||||
- [ ] /api/v1/playlist/remove
|
||||
- [X] /api/v1/volume/get
|
||||
- [X] /api/v1/volume/up
|
||||
- [X] /api/v1/volume/down
|
||||
- [X] /api/v1/volume/mute
|
||||
- [X] /api/v1/volume/<0-20>
|
||||
- [X] /api/v1/settings/restart/
|
||||
- [X] /api/v1/balance/<0-32>
|
||||
- [X] /api/v1/balance/get
|
||||
- [X] /api/v1/eq/get
|
||||
- [X] /api/v1/eq/low/get
|
||||
- [X] /api/v1/eq/low/<0-46>
|
||||
- [X] /api/v1/eq/mid/get
|
||||
- [X] /api/v1/eq/mid/<0-46>
|
||||
- [X] /api/v1/eq/high/get
|
||||
- [X] /api/v1/eq/high/<0-46>
|
||||
- [X] /api/v1/system/restart/
|
||||
- [ ] /api/v1/system/name
|
||||
- [ ] /api/v1/system/info
|
||||
- [ ] /api/v1/system/name
|
||||
- [ ] /api/v1/system/wifi/change
|
||||
- [ ] /api/v1/system/wifi/get_ssid
|
||||
- [X] /api/v1/system/wifi/change
|
||||
- [X] /api/v1/system/wifi/get_ssid
|
||||
- [ ] /api/v1/files/get
|
||||
- [ ] /api/v1/files/upload
|
||||
- [X] Automatic WiFi connection
|
||||
|
@@ -21,7 +21,7 @@ For more information, please refer to <http://unlicense.org/>
|
||||
|
||||
|
||||
// define all constants
|
||||
const String version = "NetSpeaker v0.2.0-dev"; // version string used e.g. for access point ssid when wifi couldn't connect
|
||||
const String version = "NetSpeaker v0.2.5-dev"; // version string used e.g. for access point ssid when wifi couldn't connect
|
||||
const int operation_mode = 0; // 0: interconnected (no buttons, but api and wifi); 1: standalone (no wifi; no api)
|
||||
const int SD_CS = 5; // BOARD SPECIFIC
|
||||
const int SPI_MISO = 19; // BOARD SPECIFIC
|
||||
@@ -48,7 +48,12 @@ const String apSSID = version; // ssid of access point open
|
||||
const String apPSK = "aA16161Aa"; // pre-shared-key of access point opened when not able to connect to wifi
|
||||
|
||||
// create all needed variables
|
||||
int currentVolume = 20; // variable where current volume (0...20) is stored
|
||||
int currentVolume = 50; // variable where current volume (0...20) is stored
|
||||
int maxVolume = 100; // defines the volume steps (max. 255)
|
||||
int balanceLevel = 0; // left-right balance between -16 to 16 inclusive (both)
|
||||
int eqLow = 0; // equalizer low value between -40 and 6dB
|
||||
int eqMid = 0; // equalizer mid value between -40 and 6dB
|
||||
int eqHigh = 0; // equalizer high value between -40 and 6dB
|
||||
bool muted = false; // currently muted (does not affect currentVolume)
|
||||
String currentSongPath; // path to currently playing song
|
||||
bool audioPlaying = false; // play song or not?
|
||||
|
@@ -13,7 +13,7 @@ For more information, please refer to <http://unlicense.org/>
|
||||
|
||||
void setupAudio() {
|
||||
audio.setPinout(I2S_BLCK, I2S_LRC, I2S_DOUT); // tell the audio library what output pins to use
|
||||
audio.setVolumeSteps(20);
|
||||
audio.setVolumeSteps(maxVolume);
|
||||
Serial.printf("[SETUP] Set up audio card successfully (Pins: BLCK %d | LRC %d | DOUT %d)\n", I2S_BLCK, I2S_LRC, I2S_DOUT);
|
||||
}
|
||||
|
||||
@@ -115,11 +115,11 @@ void backwardButtonHandler() {
|
||||
}
|
||||
|
||||
void setAudioVolume() {
|
||||
int newCurrentVolume = analogRead(audioVolumePin) / 204.75; // read voltage from audioVolumePin and divide by 204.75 (min. volume is 0, max. 20; 20 fits 204.75 times into 4095 (maximum input))
|
||||
int newCurrentVolume = analogRead(audioVolumePin) / (4095/maxVolume); // read voltage from audioVolumePin and divide by the calculated steps (4095 is the maximum input)
|
||||
if (currentVolume != newCurrentVolume) { // just do it if the volume changed
|
||||
currentVolume = newCurrentVolume;
|
||||
audio.setVolume(currentVolume); // set volume
|
||||
Serial.printf("[INFO] Set volume to %d/20!\n", currentVolume);
|
||||
Serial.printf("[INFO] Set volume to %d/%d!\n", currentVolume, maxVolume);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,10 +188,9 @@ void audio_lasthost(const char *info) { // stream URL played
|
||||
}
|
||||
|
||||
// *********************************************************************************** //
|
||||
// *************** Code heavily inspired by schreibfaul1's wiki entry ************** //
|
||||
// ******** Following function heavily inspired by schreibfaul1's wiki entry ******** //
|
||||
// * https://github.com/schreibfaul1/ESP32-audioI2S/wiki#what-audio-events-are-there * //
|
||||
// *********************************************************************************** //
|
||||
|
||||
void audio_id3image(File &file, const size_t pos, const size_t size) { // cover image
|
||||
Serial.printf("[Audio.h] ID3Image Found at position: %u | Length: %u\n", pos, size);
|
||||
uint8_t buf[1024];
|
||||
|
@@ -20,6 +20,21 @@ int setupSD(int SD_CS, int SPI_MISO, int SPI_MOSI, int SPI_SCK) {
|
||||
return ret; // returns 1 if everything is OK, 0 if an error occured (eg. wiring not correct)
|
||||
}
|
||||
|
||||
bool writeWiFiSecrets(String confPath, String ssid, String psk) {
|
||||
File wifiSecrets = SD.open(confPath, FILE_WRITE);
|
||||
|
||||
wifiSecrets.printf("%s\n", ssid.c_str());
|
||||
wifiSecrets.printf("%s", psk.c_str());
|
||||
|
||||
wifiSecrets.flush();
|
||||
wifiSecrets.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
bool writeWiFiSecrets(String ssid, String psk) {
|
||||
return writeWiFiSecrets(wifiConfigPath, ssid, psk);
|
||||
}
|
||||
|
||||
String getWiFiSSID(String confPath) {
|
||||
String ssid;
|
||||
char new_char;
|
||||
@@ -28,7 +43,7 @@ String getWiFiSSID(String confPath) {
|
||||
File confFile = SD.open(confPath);
|
||||
|
||||
ssid = (char)confFile.read();
|
||||
while (ssid[ssid.length() - 1] != '\n' && confFile.available()) {
|
||||
while (ssid[ssid.length() - 1] != '\n' || ssid[ssid.length() - 1] != '\r' && confFile.available()) {
|
||||
new_char = (char)confFile.read();
|
||||
if (new_char == '\n') break;
|
||||
ssid += new_char;
|
||||
@@ -55,7 +70,7 @@ String getWiFiPSK(String confPath) {
|
||||
psk = (char)confFile.read();
|
||||
while (confFile.available()) {
|
||||
new_char = (char)confFile.read();
|
||||
if (new_char == '\n') break;
|
||||
if (new_char == '\n' || new_char == '\r') break;
|
||||
psk += new_char;
|
||||
}
|
||||
|
||||
|
@@ -32,7 +32,12 @@ String generate_api_json(bool success) { // if you just want the basic json fra
|
||||
String generate_api_json(bool success, String content) { // when info is to be embedded
|
||||
return generate_api_json(success, content, false);
|
||||
}
|
||||
|
||||
bool isStringConvertable2Int(String toIntStr) {
|
||||
for (int i = 0; i < toIntStr.length(); i++) { // check if a non-digit character is in the given string
|
||||
if (!isDigit((char)toIntStr[i])) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void configRoot() {
|
||||
Serial.println("[HTTP] [Config] 200 - GET '/'");
|
||||
@@ -88,7 +93,6 @@ void api_v1_playback_next() {
|
||||
String content = "\"resource_playlist_index\": ";
|
||||
content += String(currentPlaylistPosition);
|
||||
|
||||
|
||||
api_server.send(200, "application/json", generate_api_json(true, content));
|
||||
}
|
||||
|
||||
@@ -102,7 +106,34 @@ void api_v1_playback_previous() {
|
||||
api_server.send(200, "application/json", generate_api_json(true, content));
|
||||
}
|
||||
|
||||
void api_v1_playback_volume() {
|
||||
void api_v1_playback_byindex() {
|
||||
String option = api_server.pathArg(0);
|
||||
String toIntStr;
|
||||
Serial.printf("[HTTP] [API] 200 - GET '/api/v1/playback/%s'\n", option);
|
||||
|
||||
// convert string to int
|
||||
for (int i = 0; i < option.length(); i++) {
|
||||
if (isDigit((char)option[i])) toIntStr += option[i];
|
||||
else {
|
||||
api_server.send(200, "application/json", generate_api_json(false));
|
||||
return;
|
||||
}
|
||||
}
|
||||
int index = toIntStr.toInt();
|
||||
|
||||
if (getURLFromPlaylist(currentPlaylist, index) != "") { // if this index exists
|
||||
currentPlaylistPosition = index - 1;
|
||||
nextAudio(); // the clean way of playing the next resource
|
||||
} else {
|
||||
api_server.send(200, "application/json", generate_api_json(false));
|
||||
}
|
||||
|
||||
String content = "\"resource_playlist_index\": ";
|
||||
content += String(currentPlaylistPosition);
|
||||
api_server.send(200, "application/json", generate_api_json(true, content));
|
||||
}
|
||||
|
||||
void api_v1_volume() {
|
||||
String option = api_server.pathArg(0);
|
||||
bool success = true;
|
||||
Serial.printf("[HTTP] [API] 200 - GET '/api/v1/volume/%s'\n", option);
|
||||
@@ -119,53 +150,19 @@ void api_v1_playback_volume() {
|
||||
muted = false;
|
||||
} else if (option == "get") {
|
||||
// just here that no 'success: false' is sent
|
||||
} else if (option == "0") {
|
||||
currentVolume = 0;
|
||||
} else if (option == "1") {
|
||||
currentVolume = 1;
|
||||
} else if (option == "2") {
|
||||
currentVolume = 2;
|
||||
} else if (option == "3") {
|
||||
currentVolume = 3;
|
||||
} else if (option == "4") {
|
||||
currentVolume = 4;
|
||||
} else if (option == "5") {
|
||||
currentVolume = 5;
|
||||
} else if (option == "6") {
|
||||
currentVolume = 6;
|
||||
} else if (option == "7") {
|
||||
currentVolume = 7;
|
||||
} else if (option == "8") {
|
||||
currentVolume = 8;
|
||||
} else if (option == "9") {
|
||||
currentVolume = 9;
|
||||
} else if (option == "10") {
|
||||
currentVolume = 10;
|
||||
} else if (option == "11") {
|
||||
currentVolume = 11;
|
||||
} else if (option == "12") {
|
||||
currentVolume = 12;
|
||||
} else if (option == "13") {
|
||||
currentVolume = 13;
|
||||
} else if (option == "14") {
|
||||
currentVolume = 14;
|
||||
} else if (option == "15") {
|
||||
currentVolume = 15;
|
||||
} else if (option == "16") {
|
||||
currentVolume = 16;
|
||||
} else if (option == "17") {
|
||||
currentVolume = 17;
|
||||
} else if (option == "18") {
|
||||
currentVolume = 18;
|
||||
} else if (option == "19") {
|
||||
currentVolume = 19;
|
||||
} else if (option == "20") {
|
||||
currentVolume = 20;
|
||||
} else {
|
||||
success = false;
|
||||
String toIntStr;
|
||||
int volumeLevel;
|
||||
// convert string to int
|
||||
for (int i = 0; i < option.length(); i++) {
|
||||
if (isDigit((char)option[i])) toIntStr += option[i];
|
||||
else success = false;
|
||||
}
|
||||
if (success && toIntStr.toInt() <= maxVolume) currentVolume = toIntStr.toInt();
|
||||
else success = false;
|
||||
}
|
||||
|
||||
if (!muted) audio.setVolume(currentVolume); // set volume if not muted
|
||||
if (!muted) audio.setVolume(currentVolume); // set volume if not muted and in range
|
||||
else audio.setVolume(0); // if muted, set volume to 0
|
||||
|
||||
String content = "\"volume\": "; // prepare the http response
|
||||
@@ -175,6 +172,146 @@ void api_v1_playback_volume() {
|
||||
api_server.send(200, "application/json", generate_api_json(success, content)); // generate json and send it
|
||||
}
|
||||
|
||||
void api_v1_balance() {
|
||||
String option = api_server.pathArg(0);
|
||||
bool success = true;
|
||||
Serial.printf("[HTTP] [API] 200 - GET '/api/v1/balance/%s'\n", option);
|
||||
|
||||
if (option == "right") {
|
||||
balanceLevel--;
|
||||
if (balanceLevel < -16) balanceLevel = -16;
|
||||
} else if (option == "left") {
|
||||
balanceLevel++;
|
||||
if (balanceLevel > 16) balanceLevel = 16;
|
||||
} else if (option == "get") {
|
||||
// just here that no 'success: false' is sent
|
||||
} else {
|
||||
String toIntStr; // between 0 and 32
|
||||
// convert string to int
|
||||
for (int i = 0; i < option.length(); i++) {
|
||||
if (isDigit((char)option[i])) toIntStr += option[i];
|
||||
else success = false;
|
||||
}
|
||||
if (success && toIntStr.toInt() <= 32) balanceLevel = toIntStr.toInt() - 16; // as the input string is between 0 and 32
|
||||
else success = false;
|
||||
}
|
||||
|
||||
audio.setBalance(balanceLevel); // set volume if not muted and in range
|
||||
|
||||
String content = "\"balance\": "; // prepare the http response
|
||||
content += String(balanceLevel);
|
||||
api_server.send(200, "application/json", generate_api_json(success, content)); // generate json and send it
|
||||
}
|
||||
|
||||
void api_v1_eq_get() {
|
||||
Serial.println("[HTTP] [API] 200 - GET '/api/v1/eq/get");
|
||||
|
||||
String content = "\"equalizer_low\": "; // prepare the http response
|
||||
content += String(eqLow);
|
||||
content += ", \"equalizer_mid\": "; // prepare the http response
|
||||
content += String(eqMid);
|
||||
content += ", \"equalizer_high\": "; // prepare the http response
|
||||
content += String(eqHigh);
|
||||
|
||||
api_server.send(200, "application/json", generate_api_json(true, content)); // generate json and send it
|
||||
}
|
||||
|
||||
void api_v1_eq_reset() {
|
||||
Serial.println("[HTTP] [API] 200 - GET '/api/v1/eq/reset");
|
||||
|
||||
eqLow = 0;
|
||||
eqMid = 0;
|
||||
eqHigh = 0;
|
||||
audio.setTone(0, 0, 0);
|
||||
|
||||
Serial.printf("[INFO] Set balance to %ddB | %ddB | %ddB!\n", eqLow, eqMid, eqHigh);
|
||||
|
||||
String content = "\"equalizer_low\": "; // prepare the http response
|
||||
content += String(eqLow);
|
||||
content += ", \"equalizer_mid\": "; // prepare the http response
|
||||
content += String(eqMid);
|
||||
content += ", \"equalizer_high\": "; // prepare the http response
|
||||
content += String(eqHigh);
|
||||
|
||||
api_server.send(200, "application/json", generate_api_json(true, content)); // generate json and send it
|
||||
}
|
||||
|
||||
void api_v1_eq_low() {
|
||||
String option = api_server.pathArg(0);
|
||||
bool success = true;
|
||||
Serial.printf("[HTTP] [API] 200 - GET '/api/v1/eq/low/%s'\n", option);
|
||||
|
||||
if (option == "get") {
|
||||
// just here to make /get available
|
||||
} else if (option == "reset") {
|
||||
eqLow = 0;
|
||||
} else if (isStringConvertable2Int(option)) {
|
||||
int res = option.toInt();
|
||||
if (res > 46) { // the maximum is 6dB and there will be 40 subtracted one line beneath
|
||||
success = false;
|
||||
} else {
|
||||
eqLow = res - 40;
|
||||
Serial.printf("[INFO] Set balance to %ddB | %ddB | %ddB!\n", eqLow, eqMid, eqHigh);
|
||||
}
|
||||
} else success = false;
|
||||
|
||||
audio.setTone(eqLow, eqMid, eqHigh); // set volume if not muted and in range
|
||||
|
||||
String content = "\"equalizer_low\": "; // prepare the http response
|
||||
content += String(eqLow);
|
||||
api_server.send(200, "application/json", generate_api_json(success, content)); // generate json and send it
|
||||
}
|
||||
void api_v1_eq_mid() {
|
||||
String option = api_server.pathArg(0);
|
||||
bool success = true;
|
||||
Serial.printf("[HTTP] [API] 200 - GET '/api/v1/eq/mid/%s'\n", option);
|
||||
|
||||
if (option == "get") {
|
||||
// just here to make /get available
|
||||
} else if (option == "reset") {
|
||||
eqMid = 0;
|
||||
} else if (isStringConvertable2Int(option)) {
|
||||
int res = option.toInt();
|
||||
if (res > 46) { // the maximum is 6dB and there will be 40 subtracted one line beneath
|
||||
success = false;
|
||||
} else {
|
||||
eqMid = res - 40;
|
||||
Serial.printf("[INFO] Set balance to %ddB | %ddB | %ddB!\n", eqLow, eqMid, eqHigh);
|
||||
}
|
||||
} else success = false;
|
||||
|
||||
audio.setTone(eqLow, eqMid, eqHigh); // set volume if not muted and in range
|
||||
|
||||
String content = "\"equalizer_mid\": "; // prepare the http response
|
||||
content += String(eqMid);
|
||||
api_server.send(200, "application/json", generate_api_json(success, content)); // generate json and send it
|
||||
}
|
||||
void api_v1_eq_high() {
|
||||
String option = api_server.pathArg(0);
|
||||
bool success = true;
|
||||
Serial.printf("[HTTP] [API] 200 - GET '/api/v1/eq/high/%s'\n", option);
|
||||
|
||||
if (option == "get") {
|
||||
// just here to make /get available
|
||||
} else if (option == "reset") {
|
||||
eqHigh = 0;
|
||||
} else if (isStringConvertable2Int(option)) {
|
||||
int res = option.toInt();
|
||||
if (res > 46) { // the maximum is 6dB and there will be 40 subtracted one line beneath
|
||||
success = false;
|
||||
} else {
|
||||
eqHigh = res - 40;
|
||||
Serial.printf("[INFO] Set balance to %dB | %ddB | %ddB!\n", eqLow, eqMid, eqHigh);
|
||||
}
|
||||
} else success = false;
|
||||
|
||||
audio.setTone(eqLow, eqMid, eqHigh); // set volume if not muted and in range
|
||||
|
||||
String content = "\"equalizer_high\": "; // prepare the http response
|
||||
content += String(eqHigh);
|
||||
api_server.send(200, "application/json", generate_api_json(success, content)); // generate json and send it
|
||||
}
|
||||
|
||||
void api_v1_playback_info() {
|
||||
Serial.println("[HTTP] [API] 200 - GET '/api/v1/playback/info'");
|
||||
String content = "\"resource_path\": \"" + pbInfo.resourcePath + "\", "; // resource's path
|
||||
@@ -254,7 +391,7 @@ void api_v1_playlist_play() {
|
||||
}
|
||||
}
|
||||
|
||||
if (getURLFromPlaylist(playlistPath, 0) == "") { // if playlist is empty or nonexistent
|
||||
if (getURLFromPlaylist(playlistPath, 0) == "") { // if playlist is empty or nonexistent
|
||||
api_server.send(200, "application/json", generate_api_json(false)); // send no success info
|
||||
} else {
|
||||
currentPlaylist = playlistPath;
|
||||
@@ -264,15 +401,58 @@ void api_v1_playlist_play() {
|
||||
}
|
||||
}
|
||||
|
||||
void api_v1_settings_restart() {
|
||||
void api_v1_system_restart() {
|
||||
Serial.println("[HTTP] [API] 200 - GET '/api/v1/system/restart'");
|
||||
|
||||
SD.end(); // delete SD object and sync its cache to flash
|
||||
WiFi.disconnect(); // disconnect wifi
|
||||
Serial.printf("[INFO] Restarting after 5000ms.\n", waitOnSDCardEject, retrySDMountTreshold);
|
||||
delay(5000);
|
||||
api_server.send(200, "application/json", "{\"restart\": true, \"wait_time\": 5000}");
|
||||
delay(5000);
|
||||
ESP.restart(); // reset everything and restart the program
|
||||
}
|
||||
|
||||
void api_v1_system_wifi_getssid() {
|
||||
Serial.println("[HTTP] [API] 200 - GET '/api/v1/system/wifi/get_ssid'");
|
||||
|
||||
api_server.send(200, "application/json", generate_api_json(true, "\"wifi_ssid\": \"" + getWiFiSSID() + "\""));
|
||||
}
|
||||
|
||||
void api_v1_system_wifi_change() {
|
||||
Serial.println("[HTTP] [API] 200 - POST '/api/v1/system/wifi/change'");
|
||||
|
||||
// get the right POST params
|
||||
String ssid = "";
|
||||
String psk = "";
|
||||
for (int i = 0; i < api_server.args(); i++) {
|
||||
if (api_server.argName(i) == "ssid") {
|
||||
ssid = api_server.arg(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < api_server.args(); i++) {
|
||||
if (api_server.argName(i) == "psk") {
|
||||
psk = api_server.arg(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ssid != "" && psk != "") { // both ssid and psk are given
|
||||
writeWiFiSecrets(ssid, psk);
|
||||
} else if (ssid != "" && psk == "") { // just the ssid is given
|
||||
writeWiFiSecrets(ssid, getWiFiPSK());
|
||||
} else if (ssid == "" && psk != "") { // just the psk is given
|
||||
writeWiFiSecrets(getWiFiSSID(), psk);
|
||||
} else { // none of ssid or psk is given
|
||||
api_server.send(200, "application/json", generate_api_json(false));
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.printf("[INFO] Changed wifi credentials (SSID: '%s' | PSK: '%s')\n", ssid.c_str(), psk.c_str());
|
||||
api_server.send(200, "application/json", generate_api_json(true, "\"ssid\": \"" + ssid + "\", \"psk\": \"" + psk + "\""));
|
||||
}
|
||||
|
||||
|
||||
void setupConfigWeb() {
|
||||
if (runConfServer) {
|
||||
conf_server.onNotFound([]() {
|
||||
@@ -299,11 +479,20 @@ void setupApiWeb() {
|
||||
api_server.on("/api/v1/playback/next", HTTP_GET, api_v1_playback_next);
|
||||
api_server.on("/api/v1/playback/previous", HTTP_GET, api_v1_playback_previous);
|
||||
api_server.on("/api/v1/playback/info", HTTP_GET, api_v1_playback_info);
|
||||
api_server.on(UriBraces("/api/v1/playback/{}"), HTTP_GET, api_v1_playback_byindex);
|
||||
api_server.on("/api/v1/playlist/get", HTTP_GET, api_v1_playlist_get);
|
||||
api_server.on("/api/v1/playlist/create", HTTP_POST, api_v1_playlist_create);
|
||||
api_server.on("/api/v1/playlist/play", HTTP_POST, api_v1_playlist_play);
|
||||
api_server.on(UriBraces("/api/v1/volume/{}"), HTTP_GET, api_v1_playback_volume);
|
||||
api_server.on("/api/v1/settings/restart", HTTP_GET, api_v1_settings_restart);
|
||||
api_server.on(UriBraces("/api/v1/volume/{}"), HTTP_GET, api_v1_volume);
|
||||
api_server.on(UriBraces("/api/v1/balance/{}"), HTTP_GET, api_v1_balance);
|
||||
api_server.on("/api/v1/eq/get", HTTP_GET, api_v1_eq_get);
|
||||
api_server.on(UriBraces("/api/v1/eq/reset"), HTTP_GET, api_v1_eq_reset);
|
||||
api_server.on(UriBraces("/api/v1/eq/low/{}"), HTTP_GET, api_v1_eq_low);
|
||||
api_server.on(UriBraces("/api/v1/eq/mid/{}"), HTTP_GET, api_v1_eq_mid);
|
||||
api_server.on(UriBraces("/api/v1/eq/high/{}"), HTTP_GET, api_v1_eq_high);
|
||||
api_server.on("/api/v1/system/restart", HTTP_GET, api_v1_system_restart);
|
||||
api_server.on("/api/v1/system/wifi/change", HTTP_POST, api_v1_system_wifi_change);
|
||||
api_server.on("/api/v1/system/wifi/get_ssid", HTTP_GET, api_v1_system_wifi_getssid);
|
||||
|
||||
Serial.println("[HTTP] [API] Starting API server (http) on port " + String(webport_api));
|
||||
api_server.begin();
|
||||
|
@@ -32,11 +32,13 @@ void setupWiFi() {
|
||||
for(int i = 0; i<33; i++) { if(WiFiSSIDstr.length() >= i) {WiFiSSID[i] = WiFiSSIDstr[i];} else break; }
|
||||
for(int i = 0; i<64; i++) { if(WiFiPSKstr.length() >= i) {WiFiPSK[i] = WiFiPSKstr[i];} else break; }
|
||||
|
||||
//Serial.printf("[WiFi] Credentials: SSID '%s' | PSK '%s'\n", WiFiSSID, WiFiPSK); // kept here fordebugging wifi problems
|
||||
|
||||
WiFi.disconnect();
|
||||
WiFi.mode(WIFI_AP_STA);
|
||||
WiFi.begin(WiFiSSID, WiFiPSK);
|
||||
// wait for 10 seconds for wifi to connect
|
||||
int start_timer = millis(); while(WiFi.status() != WL_CONNECTED) { if((millis()-start_timer) > 20000) break; }
|
||||
int start_timer = millis(); while(WiFi.status() != WL_CONNECTED) { if((millis()-start_timer) > 20000) break; delay(10); }
|
||||
if(WiFi.status() != WL_CONNECTED) {
|
||||
currentWiFiMode = 1;
|
||||
Serial.printf("[WiFi] Unable to connect to %s\n", WiFiSSID);
|
||||
|
Reference in New Issue
Block a user