2023-03-20 01:10:44 +01:00
# define ESP_DRD_USE_SPIFFS true
// Include Libraries
//#include ".h"
# include <WiFi.h>
# include <SPIFFS.h>
# include <FS.h>
# include <WiFiManager.h>
# include <ArduinoJson.h>
2023-04-17 02:07:18 +02:00
# include "wManager.h"
2023-08-27 11:21:26 +02:00
# include "monitor.h"
2023-09-02 01:01:42 +02:00
# include "drivers/display.h"
2023-03-20 01:10:44 +01:00
// JSON configuration file
# define JSON_CONFIG_FILE " / config.json"
// Flag for saving data
bool shouldSaveConfig = false ;
// Variables to hold data from custom textboxes
2023-08-27 11:21:26 +02:00
char poolString [ 80 ] = " public-pool.io " ;
2023-07-30 11:01:06 +02:00
int portNumber = 21496 ; //3333;
2023-03-20 01:10:44 +01:00
char btcString [ 80 ] = " yourBtcAddress " ;
2023-06-08 09:29:18 +02:00
int GMTzone = 2 ; //Currently selected in spain
2023-09-07 22:01:50 +02:00
bool saveStatsToNVS = false ; //Track mining stats in non volatile memory
2023-03-20 01:10:44 +01:00
// Define WiFiManager Object
WiFiManager wm ;
2023-08-27 11:21:26 +02:00
extern monitor_data mMonitor ;
2023-03-20 01:10:44 +01:00
void saveConfigFile ( )
// Save Config in JSON format
{
Serial . println ( F ( " Saving configuration... " ) ) ;
// Create a JSON document
StaticJsonDocument < 512 > json ;
json [ " poolString " ] = poolString ;
json [ " portNumber " ] = portNumber ;
json [ " btcString " ] = btcString ;
2023-06-08 09:29:18 +02:00
json [ " gmtZone " ] = GMTzone ;
2023-09-07 22:01:50 +02:00
json [ " saveStatsToNVS " ] = String ( saveStatsToNVS ) ;
2023-03-20 01:10:44 +01:00
// Open config file
File configFile = SPIFFS . open ( JSON_CONFIG_FILE , " w " ) ;
if ( ! configFile )
{
// Error, file did not open
Serial . println ( " failed to open config file for writing " ) ;
}
// Serialize JSON data to write to file
serializeJsonPretty ( json , Serial ) ;
if ( serializeJson ( json , configFile ) = = 0 )
{
// Error writing file
Serial . println ( F ( " Failed to write to file " ) ) ;
}
// Close file
configFile . close ( ) ;
}
bool loadConfigFile ( )
// Load existing configuration file
{
// Uncomment if we need to format filesystem
// SPIFFS.format();
// Read configuration from FS json
Serial . println ( " Mounting File System... " ) ;
// May need to make it begin(true) first time you are using SPIFFS
if ( SPIFFS . begin ( false ) | | SPIFFS . begin ( true ) )
{
Serial . println ( " mounted file system " ) ;
if ( SPIFFS . exists ( JSON_CONFIG_FILE ) )
{
// The file exists, reading and loading
Serial . println ( " reading config file " ) ;
File configFile = SPIFFS . open ( JSON_CONFIG_FILE , " r " ) ;
if ( configFile )
{
Serial . println ( " Opened configuration file " ) ;
StaticJsonDocument < 512 > json ;
DeserializationError error = deserializeJson ( json , configFile ) ;
2023-04-17 02:07:18 +02:00
configFile . close ( ) ;
2023-03-20 01:10:44 +01:00
serializeJsonPretty ( json , Serial ) ;
if ( ! error )
{
Serial . println ( " Parsing JSON " ) ;
strcpy ( poolString , json [ " poolString " ] ) ;
strcpy ( btcString , json [ " btcString " ] ) ;
portNumber = json [ " portNumber " ] . as < int > ( ) ;
2023-06-08 09:29:18 +02:00
GMTzone = json [ " gmtZone " ] . as < int > ( ) ;
2023-09-07 22:01:50 +02:00
if ( json . containsKey ( " saveStatsToNVS " ) )
saveStatsToNVS = json [ " saveStatsToNVS " ] . as < int > ( ) ;
2023-03-20 01:10:44 +01:00
return true ;
}
else
{
// Error loading JSON data
Serial . println ( " Failed to load json config " ) ;
}
}
}
}
else
{
// Error mounting file system
Serial . println ( " Failed to mount FS " ) ;
}
return false ;
}
void saveConfigCallback ( )
// Callback notifying us of the need to save configuration
{
Serial . println ( " Should save config " ) ;
shouldSaveConfig = true ;
2023-06-08 09:29:18 +02:00
//wm.setConfigPortalBlocking(false);
2023-03-20 01:10:44 +01:00
}
void configModeCallback ( WiFiManager * myWiFiManager )
// Called when config mode launched
{
Serial . println ( " Entered Configuration Mode " ) ;
Serial . print ( " Config SSID: " ) ;
Serial . println ( myWiFiManager - > getConfigPortalSSID ( ) ) ;
Serial . print ( " Config IP Address: " ) ;
Serial . println ( WiFi . softAPIP ( ) ) ;
}
void init_WifiManager ( )
{
Serial . begin ( 115200 ) ;
//Serial.setTxTimeoutMs(10);
2023-05-01 22:26:01 +02:00
//Init pin 15 to eneble 5V external power (LilyGo bug)
2023-09-02 01:01:42 +02:00
# ifdef PIN_ENABLE5V
pinMode ( PIN_ENABLE5V , OUTPUT ) ;
digitalWrite ( PIN_ENABLE5V , HIGH ) ;
# endif
2023-05-01 22:26:01 +02:00
2023-03-20 01:10:44 +01:00
// Change to true when testing to force configuration every time we run
bool forceConfig = false ;
2023-08-11 13:37:25 +02:00
2023-09-02 01:01:42 +02:00
# if defined(PIN_BUTTON_2)
// Check if button2 is pressed to enter configMode with actual configuration
if ( ! digitalRead ( PIN_BUTTON_2 ) ) {
Serial . println ( F ( " Button pressed to force start config mode " ) ) ;
forceConfig = true ;
wm . setBreakAfterConfig ( true ) ; //Set to detect config edition and save
}
2023-08-11 13:37:25 +02:00
# endif
2023-03-20 01:10:44 +01:00
bool spiffsSetup = loadConfigFile ( ) ;
if ( ! spiffsSetup )
{
Serial . println ( F ( " Forcing config mode as there is no saved config " ) ) ;
forceConfig = true ;
2023-06-08 09:29:18 +02:00
2023-03-20 01:10:44 +01:00
}
// Explicitly set WiFi mode
WiFi . mode ( WIFI_STA ) ;
// Reset settings (only for development)
//wm.resetSettings();
//Set dark theme
//wm.setClass("invert"); // dark theme
// Set config save notify callback
wm . setSaveConfigCallback ( saveConfigCallback ) ;
// Set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
wm . setAPCallback ( configModeCallback ) ;
//Advanced settings
wm . setConfigPortalBlocking ( false ) ; //Hacemos que el portal no bloquee el firmware
2023-05-01 22:26:01 +02:00
wm . setConnectTimeout ( 50 ) ; // how long to try to connect for before continuing
2023-03-20 01:10:44 +01:00
//wm.setConfigPortalTimeout(30); // auto close configportal after n seconds
// wm.setCaptivePortalEnable(false); // disable captive portal redirection
// wm.setAPClientCheck(true); // avoid timeout if client connected to softap
//wm.setTimeout(120);
//wm.setConfigPortalTimeout(120); //seconds
// Custom elements
// Text box (String) - 80 characters maximum
WiFiManagerParameter pool_text_box ( " Poolurl " , " Pool url " , poolString , 80 ) ;
// Need to convert numerical input to string to display the default value.
char convertedValue [ 6 ] ;
sprintf ( convertedValue , " %d " , portNumber ) ;
// Text box (Number) - 7 characters maximum
WiFiManagerParameter port_text_box_num ( " Poolport " , " Pool port " , convertedValue , 7 ) ;
// Text box (String) - 80 characters maximum
2023-06-08 09:29:18 +02:00
WiFiManagerParameter addr_text_box ( " btcAddress " , " Your BTC address " , btcString , 80 ) ;
// Text box (Number) - 2 characters maximum
char charZone [ 6 ] ;
sprintf ( charZone , " %d " , GMTzone ) ;
2023-09-07 22:01:50 +02:00
WiFiManagerParameter time_text_box_num ( " TimeZone " , " TimeZone fromUTC (-12/+12) " , charZone, 3) ;
WiFiManagerParameter features_html ( " <hr><br><label style= \" font-weight: bold;margin-bottom: 25px;display: inline-block; \" >Features</label> " ) ;
char checkboxParams [ 24 ] = " type= \" checkbox \" " ;
if ( saveStatsToNVS )
{
strcat ( checkboxParams , " checked " ) ;
}
WiFiManagerParameter save_stats_to_nvs ( " SaveStatsToNVS " , " Track Uptime, Best Diff, Total Hashes in device Flash memory. (Experimental) " , " T " , 2, checkboxParams, WFM_LABEL_AFTER) ;
2023-03-20 01:10:44 +01:00
// Add all defined parameters
wm . addParameter ( & pool_text_box ) ;
wm . addParameter ( & port_text_box_num ) ;
wm . addParameter ( & addr_text_box ) ;
2023-06-08 09:29:18 +02:00
wm . addParameter ( & time_text_box_num ) ;
2023-09-07 22:01:50 +02:00
wm . addParameter ( & features_html ) ;
wm . addParameter ( & save_stats_to_nvs ) ;
2023-03-20 01:10:44 +01:00
Serial . println ( " AllDone: " ) ;
if ( forceConfig )
// Run if we need a configuration
{
//No configuramos timeout al modulo
wm . setConfigPortalBlocking ( true ) ; //Hacemos que el portal SI bloquee el firmware
2023-08-28 22:54:13 +02:00
drawSetupScreen ( ) ;
2023-03-20 01:10:44 +01:00
if ( ! wm . startConfigPortal ( " NerdMinerAP " , " MineYourCoins " ) )
{
Serial . println ( " failed to connect and hit timeout " ) ;
2023-06-08 09:29:18 +02:00
//Could be break forced after edditing, so save new config
strncpy ( poolString , pool_text_box . getValue ( ) , sizeof ( poolString ) ) ;
portNumber = atoi ( port_text_box_num . getValue ( ) ) ;
strncpy ( btcString , addr_text_box . getValue ( ) , sizeof ( btcString ) ) ;
GMTzone = atoi ( time_text_box_num . getValue ( ) ) ;
2023-09-07 22:01:50 +02:00
saveStatsToNVS = ( strncmp ( save_stats_to_nvs . getValue ( ) , " T " , 1 ) = = 0 ) ;
2023-06-08 09:29:18 +02:00
saveConfigFile ( ) ;
2023-03-20 01:10:44 +01:00
delay ( 3000 ) ;
//reset and try again, or maybe put it to deep sleep
ESP . restart ( ) ;
delay ( 5000 ) ;
}
}
else
{
//Tratamos de conectar con la configuración inicial ya almacenada
2023-08-27 11:21:26 +02:00
mMonitor . NerdStatus = NM_Connecting ;
2023-03-20 01:10:44 +01:00
wm . setCaptivePortalEnable ( false ) ; // disable captive portal redirection
if ( ! wm . autoConnect ( " NerdMinerAP " , " MineYourCoins " ) )
{
Serial . println ( " Failed to connect and hit timeout " ) ;
//delay(3000);
// if we still have not connected restart and try all over again
//ESP.restart();
//delay(5000);
}
}
2023-08-27 11:21:26 +02:00
mMonitor . NerdStatus = NM_Connecting ;
2023-03-20 01:10:44 +01:00
//Conectado a la red Wifi
if ( WiFi . status ( ) = = WL_CONNECTED ) {
//tft.pushImage(0, 0, MinerWidth, MinerHeight, MinerScreen);
Serial . println ( " " ) ;
Serial . println ( " WiFi connected " ) ;
Serial . print ( " IP address: " ) ;
Serial . println ( WiFi . localIP ( ) ) ;
// Lets deal with the user config values
// Copy the string value
strncpy ( poolString , pool_text_box . getValue ( ) , sizeof ( poolString ) ) ;
Serial . print ( " PoolString: " ) ;
Serial . println ( poolString ) ;
//Convert the number value
portNumber = atoi ( port_text_box_num . getValue ( ) ) ;
Serial . print ( " portNumber: " ) ;
Serial . println ( portNumber ) ;
// Copy the string value
strncpy ( btcString , addr_text_box . getValue ( ) , sizeof ( btcString ) ) ;
Serial . print ( " btcString: " ) ;
Serial . println ( btcString ) ;
2023-06-08 09:29:18 +02:00
//Convert the number value
GMTzone = atoi ( time_text_box_num . getValue ( ) ) ;
Serial . print ( " TimeZone fromUTC: " ) ;
Serial . println ( GMTzone ) ;
2023-03-20 01:10:44 +01:00
}
// Save the custom parameters to FS
if ( shouldSaveConfig )
{
saveConfigFile ( ) ;
}
}
2023-05-12 10:27:36 +02:00
void reset_configurations ( ) {
Serial . println ( " Erasing Config, restarting " ) ;
wm . resetSettings ( ) ;
SPIFFS . remove ( JSON_CONFIG_FILE ) ; //Borramos fichero
ESP . restart ( ) ;
2023-03-20 01:10:44 +01:00
}
2023-04-17 02:07:18 +02:00
2023-05-26 13:02:14 +02:00
//----------------- MAIN PROCESS WIFI MANAGER --------------
int oldStatus = 0 ;
2023-03-20 01:10:44 +01:00
void wifiManagerProcess ( ) {
2023-05-26 13:02:14 +02:00
2023-04-17 02:07:18 +02:00
wm . process ( ) ; // avoid delays() in loop when non-blocking and other long running code
2023-05-26 13:02:14 +02:00
int newStatus = WiFi . status ( ) ;
if ( newStatus ! = oldStatus ) {
if ( newStatus = = WL_CONNECTED ) {
Serial . println ( " CONNECTED - Current ip: " + WiFi . localIP ( ) . toString ( ) ) ;
} else {
Serial . print ( " [Error] - current status: " ) ;
Serial . println ( newStatus ) ;
}
oldStatus = newStatus ;
}
2023-03-20 01:10:44 +01:00
}