Skip to content

Commit

Permalink
Tinkered around protocols.json
Browse files Browse the repository at this point in the history
May help on #24 as a side effect
  • Loading branch information
couin3 committed Apr 29, 2020
1 parent a7b48e3 commit b9aeee6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 59 deletions.
69 changes: 32 additions & 37 deletions RFLink/5_Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
#include "RFLink.h"
#include "2_Signal.h"
#include "5_Plugin.h"
#include <EEPROM.h> //used to store current plugins states
#include <FS.h> // To save MQTT parameters
#ifdef AUTOCONNECT_ENABLED
#include "9_AutoConnect.h"
#ifdef ESP8266
#include <FS.h> // To save plugins parameters
#include <ArduinoJson.h>
#elif ESP32
#include <SPIFFS.h>
#endif
#endif

boolean (*Plugin_ptr[PLUGIN_MAX])(byte, char *); // Receive plugins
byte Plugin_id[PLUGIN_MAX];
Expand Down Expand Up @@ -546,7 +552,6 @@ void PluginInit(void)
Plugin_ptr[x] = 0;
Plugin_id[x] = 0;
Plugin_State[x] = P_Enabled;
//EEPROM.get(x, Plugin_State[x]);
}

x = 0;
Expand Down Expand Up @@ -1310,47 +1315,37 @@ void PluginInit(void)

// read config file to desactivated protocols
#ifdef AUTOCONNECT_ENABLED
Serial.println("mounting FS...");

if (SPIFFS.begin())
SPIFFS.begin();
Serial.print(PROTOCOL_FILE);
File configFile = SPIFFS.open(PROTOCOL_FILE, "r");
if (configFile)
{
Serial.println("mounted file system");
if (SPIFFS.exists("/protocols.json"))
StaticJsonDocument<6400> doc;
if (deserializeJson(doc, configFile))
{
Serial.println(F(" failed to load"));
}
else
{
//file exists, reading and loading
Serial.println("reading protocols file");
File configFile = SPIFFS.open("/protocols.json", "r");
if (configFile)
for (x = 0; x < PLUGIN_MAX; x++)
{
Serial.println("opened protocols file");
size_t size = configFile.size();
if (size == 0)
{
Serial.println("History file empty");
}
else
{
StaticJsonDocument<6400> doc;
DeserializationError error = deserializeJson(doc, configFile);
if (error)
{
Serial.println(F("Failed to read file, using default configuration"));
}


for (x = 0; x < PLUGIN_MAX; x++)
{
if (doc[x][String(Plugin_id[x])] == 0)
{
Plugin_State[x] = P_Disabled;
}
}
configFile.close();

}
if (doc[x][String(Plugin_id[x])] == 0)
Plugin_State[x] = P_Disabled;
}
Serial.println(F(" loaded"));
}
configFile.close();
}
else
{
Serial.println(F(" open+r failed"));
#ifdef ESP32
Serial.println(F("If you get error as 'SPIFFS: mount failed, -10025', Please modify with 'SPIFFS.begin(true)'."));
#endif // ESP32
}
SPIFFS.end();

#endif // AUTOCONNECT_ENABLED

// Initialiseer alle plugins door aanroep met verwerkingsparameter PLUGIN_INIT
Expand Down
45 changes: 23 additions & 22 deletions RFLink/9_AutoConnect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,25 @@
// ************************************* //

#include <Arduino.h>
#include <ArduinoJson.h>
#include "RFLink.h"
#ifdef AUTOCONNECT_ENABLED

#include "4_Display.h" // To allow displaying the last message
#include "5_Plugin.h"
#include "6_WiFi_MQTT.h"
#include "9_AutoConnect.h"
#ifdef AUTOCONNECT_ENABLED

#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
//#include <ESP8266WebServer.h> // Replace with WebServer.h for ESP32
// #include <ESP8266HTTPClient.h>
typedef ESP8266WebServer WebServer;
#include <FS.h> // To save plugins parameters
#include <ArduinoJson.h>
#elif ESP32
#include <WiFi.h>
//#include <WebServer.h>
#include <ESPmDNS.h>
#include <SPIFFS.h>
typedef WebServer WebServer;
#endif

//#include <SPI.h> // To acces Flash Memory
#include <FS.h> // To save MQTT parameters
#include <AutoConnect.h>

AutoConnect portal;
Expand All @@ -54,22 +49,21 @@ String saveParams(AutoConnectAux &aux, PageArgument &args);

void rootPage()
{

WebServer &webServer = portal.host();
if (webServer.hasArg("BtnSave"))
{ // On n'enregistre les values que si ce n'est pas le bouton "test" qui a été appuyé

// === Debug Part ===
String message = "Number of args received: ";
message += webServer.args(); //Get number of parameters
message += "\n"; //Add a new line
for (int i = 0; i < webServer.args(); i++)
{
message += "Arg nº" + (String)i + " – > "; //Include the current iteration value
message += webServer.argName(i) + ": "; //Get the name of the parameter
message += webServer.arg(i) + "\n"; //Get the value of the parameter
}
Serial.println(message);
// String message = "Number of args received: ";
// message += webServer.args(); //Get number of parameters
// message += "\n"; //Add a new line
// for (int i = 0; i < webServer.args(); i++)
// {
// message += "Arg nº" + (String)i + " – > "; //Include the current iteration value
// message += webServer.argName(i) + ": "; //Get the name of the parameter
// message += webServer.arg(i) + "\n"; //Get the value of the parameter
// }
// Serial.println(message);
// ==================

//const int capacity = JSON_ARRAY_SIZE(254) + 2 * JSON_OBJECT_SIZE(2);
Expand Down Expand Up @@ -98,15 +92,20 @@ void rootPage()
}
}

File configFile = SPIFFS.open("/protocols.json", "w");
SPIFFS.begin();
File configFile = SPIFFS.open(PROTOCOL_FILE, "w");
Serial.print(PROTOCOL_FILE);

String configString;
serializeJson(doc, configString);
configFile.print(configString);
// === Debug Part ===
Serial.println(configString);
// Serial.println(configString);
// ==================

Serial.println(F(" saved"));
configFile.close();
SPIFFS.end();
}

// This is the Home Page - Choose theme here : https://www.bootstrapcdn.com/bootswatch/?theme
Expand Down Expand Up @@ -451,7 +450,9 @@ String saveParams(AutoConnectAux &aux, PageArgument &args)
echo.value += Adv_Power;
echo.value += F("<br>");

#ifdef MQTT_ENABLED
setup_MQTT(); // Reload settings
#endif

return String("");
}
Expand Down
1 change: 1 addition & 0 deletions RFLink/9_AutoConnect.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

// Adds MQTT tab to Autoconnect
#define PARAM_FILE "/settings.json"
#define PROTOCOL_FILE "/protocols.json"
#define AUX_SETTING_URI "/settings"
#define AUX_SAVE_URI "/settings_save"
//#define AUX_CLEAR_URI "/settings_clear"
Expand Down

0 comments on commit b9aeee6

Please sign in to comment.