2025-07-13 16:10:06 +03:00

241 lines
7.9 KiB
C++

//=========================================================
// Header
//=========================================================
// WiFi Libraries
#include <FS.h> // This needs to be first, or it all crashes and burns...
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#ifdef ESP32
#include <SPIFFS.h>
#endif
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
// Variables
char joypad_identifier[40] = ""; // TCP Scoreboard IP Address
char scoreboard_server[40] = ""; // TCP Scoreboard IP Address
char scoreboard_port[6] = "8080"; // TCP Scoreboard Port
bool shouldSaveConfig = false; // Flag For Saving Data
unsigned long timing = 0; // For Keep Alive Signal
// JSON Documents
JsonDocument JSON;
// WiFi Manager Object
WiFiManager wifiManager;
// WiFi Manager Web UI Parameters
WiFiManagerParameter custom_identifier("identifier", "Device Identifier", "", 40); // New Addition
WiFiManagerParameter custom_scoreboard("server", "ScoreBoard Server", "", 40);
WiFiManagerParameter custom_port("port", "ScoreBoard Port", "", 6);
//=========================================================
// TCP Client
//=========================================================
// TCP Object
WiFiClient tcpClient;
// List Messages
#define MSG_SPACER "--------------------------------------------"
#define MSG_TCP_CONNECTED "TCP Server : Connected"
#define MSG_TCP_SEND "WHAT TO SEND ? "
//=========================================================
// LED ON BOARD
//=========================================================
// Defining Pin & Commands
#define LED_START pinMode(_led_pin, OUTPUT)
#define LED_ON digitalWrite(_led_pin, LOW)
#define LED_OFF digitalWrite(_led_pin, HIGH)
#define LED_TOGGLE digitalWrite(_led_pin, !digitalRead(_led_pin))
//=========================================================
// WIFI
//=========================================================
// Save Config Files Flag
//---------------------------------------------------------
void wifi_save_config_call_back () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
//---------------------------------------------------------
// Add Parameters
//---------------------------------------------------------
void wifi_add_parameter(WiFiManager& manager) {
// Set config save notify callback
manager.setSaveConfigCallback(wifi_save_config_call_back);
// Update The Values From Spiffs
custom_identifier.setValue(joypad_identifier, sizeof(joypad_identifier)); // New Addition
custom_scoreboard.setValue(scoreboard_server, sizeof(scoreboard_server));
custom_port.setValue(scoreboard_port, sizeof(scoreboard_port));
// Add All Parameters To WiFi Manager
manager.addParameter(&custom_identifier); // New Addition
manager.addParameter(&custom_scoreboard);
manager.addParameter(&custom_port);
}
//---------------------------------------------------------
// WiFi Connection
//---------------------------------------------------------
void wifi_start() {
// Read configuration from FS json
Serial.println("mounting FS...");
// Working With File Systems
if(SPIFFS.begin()) {
Serial.println("mounted file system");
if(SPIFFS.exists("/config.json")) {
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if(configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
#if defined(ARDUINOJSON_VERSION_MAJOR) && ARDUINOJSON_VERSION_MAJOR >= 6
DynamicJsonDocument json(1024);
auto deserializeError = deserializeJson(json, buf.get());
serializeJson(json, Serial);
if(!deserializeError) {
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if(json.success()) {
#endif
Serial.println("\nparsed json");
strcpy(joypad_identifier, json["joypad_identifier"]);
strcpy(scoreboard_server, json["scoreboard_server"]);
strcpy(scoreboard_port, json["scoreboard_port"]);
} else {
Serial.println("failed to load json config");
}
configFile.close();
}
}
} else {
Serial.println("failed to mount FS");
}
// Add Parameters Settings
wifi_add_parameter(wifiManager);
// Connecting To WiFi
oled_clear();
oled_text(1, 0, 2, "Connecting WiFi");
oled_text(1, 0, 14, "If It Takes Too Long");
oled_text(1, 0, 24, "Connect To");
oled_text(1, 0, 34, "TAKEONE JOYPADS");
oled_text(1, 0, 44, "192.168.4.1");
// Fetches ssid and pass and tries to connect
if(!wifiManager.autoConnect("TAKEONE JOYPADS")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.restart();
delay(5000);
}
// Connecting To WiFi
oled_clear();
oled_text(2, 0, 0, "WiFi Connected");
// Read updated parameters
strcpy(joypad_identifier, custom_identifier.getValue());
strcpy(scoreboard_server, custom_scoreboard.getValue());
strcpy(scoreboard_port, custom_port.getValue());
Serial.println("The Values In The File Are : ");
Serial.println("\tjoypad_identifier : " + String(joypad_identifier));
Serial.println("\tscoreboard_server : " + String(scoreboard_server));
Serial.println("\tscoreboard_port : " + String(scoreboard_port));
// Save the custom parameters to FS
if(shouldSaveConfig) {
Serial.println("saving config");
#if defined(ARDUINOJSON_VERSION_MAJOR) && ARDUINOJSON_VERSION_MAJOR >= 6
DynamicJsonDocument json(1024);
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
#endif
json["joypad_identifier"] = joypad_identifier;
json["scoreboard_server"] = scoreboard_server;
json["scoreboard_port"] = scoreboard_port;
File configFile = SPIFFS.open("/config.json", "w");
if(!configFile) {
Serial.println("failed to open config file for writing");
}
#if defined(ARDUINOJSON_VERSION_MAJOR) && ARDUINOJSON_VERSION_MAJOR >= 6
serializeJson(json, Serial);
serializeJson(json, configFile);
#else
json.printTo(Serial);
json.printTo(configFile);
#endif
Serial.println();
configFile.close();
}
}
//---------------------------------------------------------
// WiFi Reset
//---------------------------------------------------------
void wifi_reset() {
oled_clear();
oled_text(1, 0, 2, "Reset WiFi Settings");
wifiManager.resetSettings();
delay(500);
oled_text(1, 0, 12, "Rebooting");
delay(500);
oled_clear();
oled_clear();
ESP.restart();
}
//---------------------------------------------------------
// WiFi Wait For Reset Button
//---------------------------------------------------------
void wifi_wait_for_reset() {
// Clear Display
oled_clear();
// Draw Text For Reset Settings
oled_text(1, 0, 2, "Press Center Button");
oled_text(1, 0, 12, "To Reset Config");
// Button Functions
for(int i=0; i<12; i++) {
if(btnRedOne.pressed() && btnBlueOne.pressed()) {
wifi_reset();
break;
}
oled_text(1, i*5, 22, " . ");
delay(250);
}
}
//=========================================================
// Server
//=========================================================