Skip to content

Commit

Permalink
save wifiscan to flash and remove some unused Strings and code
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorYbema committed Sep 1, 2024
1 parent 8f450d7 commit e0fd87c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
4 changes: 1 addition & 3 deletions HeishaMon/HeishaMon.ino
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,10 @@ Adafruit_NeoPixel pixels(1, LEDPIN);
#endif

// store actual data
String openTherm[2];
char actData[DATASIZE] = { '\0' };
char actDataExtra[DATASIZE] = { '\0' };
#define OPTDATASIZE 20
char actOptData[OPTDATASIZE] = { '\0' };
String RESTmsg = "";

// log message to sprintf to
char log_msg[256];
Expand Down Expand Up @@ -271,7 +269,7 @@ void check_wifi() {
WiFi.psk().toCharArray(heishamonSettings.wifi_password, 40);
JsonDocument jsonDoc;
settingsToJson(jsonDoc, &heishamonSettings); //stores current settings in a json document
saveJsonToConfig(jsonDoc); //save to config file
saveJsonToFile(jsonDoc, "config.json"); //save to config file
}

ntpReload(&heishamonSettings);
Expand Down
3 changes: 2 additions & 1 deletion HeishaMon/src/opentherm/opentherm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ void OpenTherm::end() {
detachInterrupt(digitalPinToInterrupt(inPin));
}
}

/*
const char *OpenTherm::statusToString(OpenThermResponseStatus status) {
switch (status) {
case NONE: return "NONE";
Expand All @@ -328,6 +328,7 @@ const char *OpenTherm::messageTypeToString(OpenThermMessageType message_type) {
default: return "UNKNOWN";
}
}
*/

//building requests

Expand Down
4 changes: 2 additions & 2 deletions HeishaMon/src/opentherm/opentherm.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ class OpenTherm
unsigned long buildRequest(OpenThermMessageType type, OpenThermMessageID id, unsigned int data);
unsigned long buildResponse(OpenThermMessageType type, OpenThermMessageID id, unsigned int data);
OpenThermResponseStatus getLastResponseStatus();
const char *statusToString(OpenThermResponseStatus status);
//const char *statusToString(OpenThermResponseStatus status);
void handleInterrupt();
void process();
void end();

bool parity(unsigned long frame);
OpenThermMessageType getMessageType(unsigned long message);
OpenThermMessageID getDataID(unsigned long frame);
const char *messageTypeToString(OpenThermMessageType message_type);
//const char *messageTypeToString(OpenThermMessageType message_type);
bool isValidRequest(unsigned long request);
bool isValidResponse(unsigned long response);

Expand Down
38 changes: 23 additions & 15 deletions HeishaMon/webfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

#define UPTIME_OVERFLOW 4294967295 // Uptime overflow value

static String wifiJsonList = "";

static uint8_t ntpservers = 0;

void log_message(char* string);
Expand All @@ -35,7 +33,6 @@ int dBmToQuality(int dBm) {

void getWifiScanResults(int numSsid) {
if (numSsid > 0) { //found wifi networks
wifiJsonList = "[";
int indexes[numSsid];
for (int i = 0; i < numSsid; i++) { //fill the sorted list with normal indexes first
indexes[i] = i;
Expand All @@ -59,18 +56,19 @@ void getWifiScanResults(int numSsid) {
}
}
}
bool firstSSID = true;
JsonDocument wifiJsonDoc;
JsonArray wifiJsonArray = wifiJsonDoc.to<JsonArray>();
for (int i = 0; i < numSsid; i++) { //then output json
if (indexes[i] == -1) {
continue;
}
if (!firstSSID) {
wifiJsonList = wifiJsonList + ",";
}
wifiJsonList = wifiJsonList + "{\"ssid\":\"" + WiFi.SSID(indexes[i]) + "\", \"rssi\": \"" + dBmToQuality(WiFi.RSSI(indexes[i])) + "%\"}";
firstSSID = false;
JsonObject wifiJsonObject = wifiJsonArray.add<JsonObject>();
wifiJsonObject["ssid"] = WiFi.SSID(indexes[i]);
String quality = String(dBmToQuality(WiFi.RSSI(indexes[i]))) + "%";
wifiJsonObject["rssi"] = quality;
}
wifiJsonList = wifiJsonList + "]";
saveJsonToFile(wifiJsonDoc,"wifiscan.json");
WiFi.scanDelete();
}
}

Expand Down Expand Up @@ -461,9 +459,9 @@ void settingsToJson(JsonDocument &jsonDoc, settingsStruct *heishamonSettings) {
jsonDoc["updataAllDallasTime"] = heishamonSettings->updataAllDallasTime;
}

void saveJsonToConfig(JsonDocument &jsonDoc) {
void saveJsonToFile(JsonDocument &jsonDoc, const char* filename) {
if (LittleFS.begin()) {
File configFile = LittleFS.open("/config.json", "w");
File configFile = LittleFS.open(filename, "w");
if (configFile) {
serializeJson(jsonDoc, configFile);
configFile.close();
Expand Down Expand Up @@ -615,7 +613,7 @@ int saveSettings(struct webserver_t *client, settingsStruct *heishamonSettings)
jsonDoc["wifi_password"] = String(wifi_password);
}

saveJsonToConfig(jsonDoc); //save to config file
saveJsonToFile(jsonDoc, "config.json"); //save to config file
loadSettings(heishamonSettings); //load config file to current settings

while (client->userdata) {
Expand Down Expand Up @@ -972,8 +970,18 @@ int handleWifiScan(struct webserver_t *client) {
#endif
if (client->content == 0) {
webserver_send(client, 200, (char *)"application/json", 0);
char *str = (char *)wifiJsonList.c_str();
webserver_send_content(client, str, strlen(str));
if (LittleFS.begin()) {
File scanfile = LittleFS.open("wifiscan.json", "r");
if (scanfile) {
size_t size = scanfile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
scanfile.readBytes(buf.get(), size);
webserver_send_content(client, buf.get(), size);
scanfile.close();
}
}

}
//initatie a new async scan for next try
#if defined(ESP8266)
Expand Down
2 changes: 1 addition & 1 deletion HeishaMon/webfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ int handleFactoryReset(struct webserver_t *client);
int handleReboot(struct webserver_t *client);
int handleDebug(struct webserver_t *client, char *hex, byte hex_len);
void settingsToJson(JsonDocument &jsonDoc, settingsStruct *heishamonSettings);
void saveJsonToConfig(JsonDocument &jsonDoc);
void saveJsonToFile(JsonDocument &jsonDoc, const char *filename);
void loadSettings(settingsStruct *heishamonSettings);
int getSettings(struct webserver_t *client, settingsStruct *heishamonSettings);
int handleSettings(struct webserver_t *client);
Expand Down

0 comments on commit e0fd87c

Please sign in to comment.