Implemented persistent settings as binary format, got rid of json architecture
This commit is contained in:
parent
d27a42c7e8
commit
0b216e2e83
10
Notes.md
10
Notes.md
@ -25,3 +25,13 @@
|
||||
### 19.02.2022
|
||||
+ Stable and fast csv database implemented.
|
||||
+ Basic REST API: read, create and delete users / read and drop csv db
|
||||
|
||||
### 19.03.2022
|
||||
+ Settings pos
|
||||
+ WifiSSID - terminator 0x00 - Pos.: 0-31 (00 - 1F)
|
||||
+ WifiPassword - terminator 0x00 - Pos.: 32-63 (20 - 3F)
|
||||
+ Device IP - 40-43
|
||||
+ Subnet 44 - 47
|
||||
+ Gateway 48 - 4B
|
||||
+ Flags:
|
||||
+ Mode 0 - station; 1 - client 4C
|
BIN
data/admin
BIN
data/admin
Binary file not shown.
@ -1,5 +0,0 @@
|
||||
{
|
||||
"THEMEID": 1,
|
||||
"SSID":"DoorLock",
|
||||
"PASS":"geheim123"
|
||||
}
|
BIN
data/settings
Normal file
BIN
data/settings
Normal file
Binary file not shown.
@ -1,5 +0,0 @@
|
||||
{
|
||||
"GREET":"Hallo $name",
|
||||
"FAIL":"PIN Incorrect",
|
||||
"HOME":"Willkommen!|Pin oder Karte"
|
||||
}
|
103
src/Config.cpp
103
src/Config.cpp
@ -1,5 +1,5 @@
|
||||
#include "Config.h"
|
||||
|
||||
#define CONFIG_SIZE 0x4C
|
||||
Config::Config()
|
||||
{
|
||||
if (!LittleFS.begin())
|
||||
@ -8,14 +8,99 @@ Config::Config()
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Config::loadConfig()
|
||||
Config::~Config()
|
||||
{
|
||||
StaticJsonDocument<1024> doc;
|
||||
File config_file = LittleFS.open("/config.json","r");
|
||||
deserializeJson(doc, config_file);
|
||||
this->SSID=doc["SSID"].as<const char*>();
|
||||
this->PASS=doc["PASS"].as<const char*>();
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
bool Config::loadBin()
|
||||
{
|
||||
File config_file = LittleFS.open("/settings", "r");
|
||||
if(buffer==nullptr)
|
||||
buffer = (uint8_t*) malloc(CONFIG_SIZE);
|
||||
if (config_file.available())
|
||||
{
|
||||
config_file.read(buffer, CONFIG_SIZE);
|
||||
SSID = (char*) buffer;
|
||||
buffer[0x1F] = 0x00;
|
||||
buffer[0x3F] = 0x00;
|
||||
PASS = (char*)(buffer + 0x20);
|
||||
ip = (uint32_t *)(buffer + 0x40);
|
||||
subnet = (uint32_t *)(buffer + 0x44);
|
||||
gw = (uint32_t *)(buffer + 0x48);
|
||||
mode = (uint8_t *)(buffer + 0x4C);
|
||||
}
|
||||
config_file.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Config::saveBin()
|
||||
{
|
||||
File config_file = LittleFS.open("/settings", "w");
|
||||
config_file.write(buffer,CONFIG_SIZE);
|
||||
delay(100);
|
||||
config_file.close();
|
||||
return true;
|
||||
}
|
||||
bool Config::setSSID(const char *ssid){
|
||||
size_t offset = 0;
|
||||
bool copy = true;
|
||||
for(int i=0; i<31;i++){
|
||||
if(ssid[i]==0x00)
|
||||
copy = false;
|
||||
buffer[offset+i] = copy?ssid[i]:0x00;
|
||||
}
|
||||
buffer[offset+31] = '\n';
|
||||
return true;
|
||||
}
|
||||
bool Config::setPASS(const char *pass){
|
||||
size_t offset = 0x20;
|
||||
bool copy = true;
|
||||
for(int i=0; i<31;i++){
|
||||
if(pass[i]==0x00)
|
||||
copy = false;
|
||||
buffer[offset+i] = copy?pass[i]:0x00;
|
||||
}
|
||||
buffer[offset+31] = '\n';
|
||||
return true;
|
||||
}
|
||||
void Config::print(){
|
||||
Serial.print("BufferHEX ");
|
||||
for(int i=0; i<CONFIG_SIZE;i++){
|
||||
Serial.print(i,HEX);
|
||||
Serial.print(">");
|
||||
Serial.print(buffer[i],HEX);
|
||||
Serial.print("|");
|
||||
}
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(SSID);
|
||||
Serial.print("PASS: ");
|
||||
Serial.println(PASS);
|
||||
Serial.print("IP");
|
||||
Serial.print((uint8_t) *ip, DEC);
|
||||
Serial.print(".");
|
||||
Serial.print((uint8_t)*((uint8_t*)ip+1), DEC);
|
||||
Serial.print(".");
|
||||
Serial.print((uint8_t) *((uint8_t*)ip+2), DEC);
|
||||
Serial.print(".");
|
||||
Serial.println((uint8_t) *((uint8_t*)ip+3), DEC);
|
||||
Serial.print("GW");
|
||||
Serial.print((uint8_t) *((uint8_t*)gw), DEC);
|
||||
Serial.print(".");
|
||||
Serial.print((uint8_t) *((uint8_t*)gw+1), DEC);
|
||||
Serial.print(".");
|
||||
Serial.print((uint8_t) *((uint8_t*)gw+2), DEC);
|
||||
Serial.print(".");
|
||||
Serial.println((uint8_t) *((uint8_t*)gw+3), DEC);
|
||||
Serial.print("SUBNET");
|
||||
Serial.print((uint8_t)*((uint8_t*)subnet), DEC);
|
||||
Serial.print(".");
|
||||
Serial.print((uint8_t)*((uint8_t*)subnet+1), DEC);
|
||||
Serial.print(".");
|
||||
Serial.print((uint8_t)*((uint8_t*)subnet+2), DEC);
|
||||
Serial.print(".");
|
||||
Serial.println((uint8_t)*((uint8_t*)subnet+3), DEC);
|
||||
|
||||
|
||||
|
||||
}
|
14
src/Config.h
14
src/Config.h
@ -3,10 +3,22 @@
|
||||
#include "ArduinoJson.h"
|
||||
class Config
|
||||
{
|
||||
private:
|
||||
uint8_t *buffer = nullptr;
|
||||
|
||||
public:
|
||||
Config();
|
||||
~Config();
|
||||
const char *SSID;
|
||||
const char *PASS;
|
||||
int THEME;
|
||||
uint8_t *mode;
|
||||
uint32_t *ip;
|
||||
uint32_t *subnet;
|
||||
uint32_t *gw;
|
||||
bool loadConfig();
|
||||
bool loadBin();
|
||||
bool saveBin();
|
||||
void print();
|
||||
bool setSSID(const char *ssid);
|
||||
bool setPASS(const char *pass);
|
||||
};
|
@ -31,12 +31,13 @@ IPAddress dns(192, 168, 178, 1);
|
||||
DNSServer dnsServer;
|
||||
void setup()
|
||||
{
|
||||
config.loadConfig();
|
||||
//config.loadConfig();
|
||||
config.loadBin();
|
||||
Serial.begin(115200);
|
||||
Serial.println("Starting System");
|
||||
Serial.print("\t1. Network config ->");
|
||||
WiFi.mode(WIFI_AP);
|
||||
Serial.println(WiFi.softAPConfig(local_IP, local_IP, subnet) ? "Ready" : "Failed!");
|
||||
Serial.println(WiFi.softAPConfig(*config.ip, *config.gw, *config.subnet) ? "Ready" : "Failed!");
|
||||
Serial.print("\t2 AP setup " + String(config.SSID) + " -> ");
|
||||
if (strlen(config.PASS) > 0)
|
||||
Serial.println(WiFi.softAP(config.SSID, config.PASS) ? "Ready" : "Failed!");
|
||||
|
Loading…
x
Reference in New Issue
Block a user