mirror of
https://github.com/jomjol/AI-on-the-edge-device.git
synced 2025-12-08 12:36:52 +03:00
Boot phase: Add more error handling + provide verbose output in error cases (#2020)
* WLAN: add error handling * WLAN: parameter global struct * WLAN.ini -> more info text * RSSIThreshold * Rename logs * Boot process: error handling * Update texts * Comments * Init sequence * Prepare for check dir creation * add check makedir, update logs * Blink code for OTA+SoftAP * Blink code for missing time snyc * Update * reboot -> switch LED off * Update log texts * Update * Update log texts * create empty default folders at startup * Update * Adapt log level * Print log level switch * Update * Update text * Add SD free space to log * WIFI/MQTT disconnect message set to WARN (+ ERROR)
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "Helper.h"
|
||||
#include "statusled.h"
|
||||
#include "CImageBasis.h"
|
||||
|
||||
#include "server_ota.h"
|
||||
@@ -557,15 +558,17 @@ void CCamera::LightOnOff(bool status)
|
||||
|
||||
void CCamera::LEDOnOff(bool status)
|
||||
{
|
||||
// Init the GPIO
|
||||
gpio_pad_select_gpio(BLINK_GPIO);
|
||||
/* Set the GPIO as a push/pull output */
|
||||
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
|
||||
if (xHandle_task_StatusLED == NULL) {
|
||||
// Init the GPIO
|
||||
gpio_pad_select_gpio(BLINK_GPIO);
|
||||
/* Set the GPIO as a push/pull output */
|
||||
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
|
||||
|
||||
if (!status)
|
||||
gpio_set_level(BLINK_GPIO, 1);
|
||||
else
|
||||
gpio_set_level(BLINK_GPIO, 0);
|
||||
if (!status)
|
||||
gpio_set_level(BLINK_GPIO, 1);
|
||||
else
|
||||
gpio_set_level(BLINK_GPIO, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
#include "ClassLogFile.h"
|
||||
|
||||
#include "Helper.h"
|
||||
#include "statusled.h"
|
||||
#include "../../include/defines.h"
|
||||
|
||||
/*an ota data write buffer ready to write to the flash*/
|
||||
@@ -66,6 +67,8 @@ static void infinite_loop(void)
|
||||
|
||||
void task_do_Update_ZIP(void *pvParameter)
|
||||
{
|
||||
StatusLED(AP_OR_OTA, 1, true); // Signaling an OTA update
|
||||
|
||||
std::string filetype = toUpper(getFileType(_file_name_update));
|
||||
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "File: " + _file_name_update + " Filetype: " + filetype);
|
||||
@@ -87,13 +90,13 @@ void task_do_Update_ZIP(void *pvParameter)
|
||||
ota_update_task(retfirmware);
|
||||
}
|
||||
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Trigger reboot due to firmware update.");
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Trigger reboot due to firmware update");
|
||||
doRebootOTA();
|
||||
} else if (filetype == "BIN")
|
||||
{
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Do firmware update - file: " + _file_name_update);
|
||||
ota_update_task(_file_name_update);
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Trigger reboot due to firmware update.");
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Trigger reboot due to firmware update");
|
||||
doRebootOTA();
|
||||
}
|
||||
else
|
||||
@@ -108,7 +111,7 @@ void CheckUpdate()
|
||||
FILE *pfile;
|
||||
if ((pfile = fopen("/sdcard/update.txt", "r")) == NULL)
|
||||
{
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "No update triggered.");
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "No pending update");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -120,13 +123,14 @@ void CheckUpdate()
|
||||
std::string _szw = std::string(zw);
|
||||
if (_szw == "init")
|
||||
{
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Inital Setup triggered.");
|
||||
initial_setup = true; }
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Inital setup triggered");
|
||||
initial_setup = true;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(pfile);
|
||||
DeleteFile("/sdcard/update.txt"); // Prevent Boot Loop!!!
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Update during boot triggered - Update File: " + _file_name_update);
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Start update process (" + _file_name_update + ")");
|
||||
|
||||
|
||||
xTaskCreate(&task_do_Update_ZIP, "task_do_Update_ZIP", configMINIMAL_STACK_SIZE * 35, NULL, tskIDLE_PRIORITY+1, NULL);
|
||||
@@ -586,6 +590,9 @@ void task_reboot(void *KillAutoFlow)
|
||||
KillTFliteTasks(); // Kill autoflow task if executed in extra task, if not don't kill parent task
|
||||
}
|
||||
|
||||
Camera.LightOnOff(false);
|
||||
StatusLEDOff();
|
||||
|
||||
/* Stop service tasks */
|
||||
#ifdef ENABLE_MQTT
|
||||
MQTTdestroy_client(true);
|
||||
@@ -607,7 +614,7 @@ void task_reboot(void *KillAutoFlow)
|
||||
|
||||
void doReboot()
|
||||
{
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Reboot triggered by Software (5s).");
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Reboot triggered by Software (5s)");
|
||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Reboot in 5sec");
|
||||
|
||||
BaseType_t xReturned = xTaskCreate(&task_reboot, "task_reboot", configMINIMAL_STACK_SIZE * 3, (void*) true, 10, NULL);
|
||||
@@ -624,6 +631,8 @@ void doRebootOTA()
|
||||
{
|
||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Reboot in 5sec");
|
||||
|
||||
Camera.LightOnOff(false);
|
||||
StatusLEDOff();
|
||||
esp_camera_deinit();
|
||||
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
|
||||
@@ -25,6 +25,7 @@ extern "C" {
|
||||
#endif //ENABLE_MQTT
|
||||
|
||||
#include "server_help.h"
|
||||
#include "server_tflite.h"
|
||||
#include "../../include/defines.h"
|
||||
|
||||
static const char* TAG = "CTRL";
|
||||
@@ -599,6 +600,10 @@ bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph)
|
||||
{
|
||||
LogFile.setLogLevel(ESP_LOG_DEBUG);
|
||||
}
|
||||
|
||||
/* If system reboot was not triggered by user and reboot was caused by execption -> keep log level to DEBUG */
|
||||
if (!getIsPlannedReboot() && (esp_reset_reason() == ESP_RST_PANIC))
|
||||
LogFile.setLogLevel(ESP_LOG_DEBUG);
|
||||
}
|
||||
if ((toUpper(splitted[0]) == "LOGFILESRETENTION") && (splitted.size() > 1))
|
||||
{
|
||||
@@ -607,18 +612,21 @@ bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph)
|
||||
|
||||
/* TimeServer and TimeZone got already read from the config, see setupTime () */
|
||||
|
||||
#ifdef WLAN_USE_MESH_ROAMING
|
||||
if ((toUpper(splitted[0]) == "RSSITHRESHOLD") && (splitted.size() > 1))
|
||||
{
|
||||
if (ChangeRSSIThreshold(WLAN_CONFIG_FILE, atoi(splitted[1].c_str())))
|
||||
int RSSIThresholdTMP = atoi(splitted[1].c_str());
|
||||
RSSIThresholdTMP = min(0, max(-100, RSSIThresholdTMP)); // Verify input limits (-100 - 0)
|
||||
|
||||
if (ChangeRSSIThreshold(WLAN_CONFIG_FILE, RSSIThresholdTMP))
|
||||
{
|
||||
// reboot necessary so that the new wlan.ini is also used !!!
|
||||
fclose(pfile);
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Rebooting to activate new RSSITHRESHOLD ...");
|
||||
esp_restart();
|
||||
hard_restart();
|
||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Rebooting to activate new RSSITHRESHOLD ...");
|
||||
doReboot();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if ((toUpper(splitted[0]) == "HOSTNAME") && (splitted.size() > 1))
|
||||
{
|
||||
@@ -626,9 +634,7 @@ bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph)
|
||||
{
|
||||
// reboot necessary so that the new wlan.ini is also used !!!
|
||||
fclose(pfile);
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Rebooting to activate new HOSTNAME...");
|
||||
esp_restart();
|
||||
hard_restart();
|
||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Rebooting to activate new HOSTNAME...");
|
||||
doReboot();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "ClassFlowMQTT.h"
|
||||
#include "Helper.h"
|
||||
#include "connect_wlan.h"
|
||||
#include "read_wlanini.h"
|
||||
#include "ClassLogFile.h"
|
||||
|
||||
#include "time_sntp.h"
|
||||
@@ -31,7 +32,7 @@ void ClassFlowMQTT::SetInitialParameter(void)
|
||||
topicError = "";
|
||||
topicRate = "";
|
||||
topicTimeStamp = "";
|
||||
maintopic = hostname;
|
||||
maintopic = wlan_config.hostname;
|
||||
|
||||
topicUptime = "";
|
||||
topicFreeMem = "";
|
||||
|
||||
@@ -258,7 +258,7 @@ bool MakeDir(std::string path)
|
||||
break;
|
||||
|
||||
default:
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to create folder: " + path);
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to create folder: " + path + " (errno: " + std::to_string(errno) + ")");
|
||||
bSuccess = false;
|
||||
break;
|
||||
}
|
||||
|
||||
148
code/components/jomjol_helper/statusled.cpp
Normal file
148
code/components/jomjol_helper/statusled.cpp
Normal file
@@ -0,0 +1,148 @@
|
||||
#include "statusled.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include "driver/gpio.h"
|
||||
|
||||
#include "ClassLogFile.h"
|
||||
#include "../../include/defines.h"
|
||||
|
||||
|
||||
static const char* TAG = "STATUSLED";
|
||||
|
||||
TaskHandle_t xHandle_task_StatusLED = NULL;
|
||||
struct StatusLEDData StatusLEDData = {};
|
||||
|
||||
|
||||
void task_StatusLED(void *pvParameter)
|
||||
{
|
||||
//ESP_LOGD(TAG, "task_StatusLED - create");
|
||||
while (StatusLEDData.bProcessingRequest)
|
||||
{
|
||||
//ESP_LOGD(TAG, "task_StatusLED - start");
|
||||
struct StatusLEDData StatusLEDDataInt = StatusLEDData;
|
||||
|
||||
gpio_pad_select_gpio(BLINK_GPIO); // Init the GPIO
|
||||
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT); // Set the GPIO as a push/pull output
|
||||
gpio_set_level(BLINK_GPIO, 1);// LED off
|
||||
|
||||
for (int i=0; i<3; ) // Default: repeat 3 times
|
||||
{
|
||||
if (!StatusLEDDataInt.bInfinite)
|
||||
++i;
|
||||
|
||||
for (int j = 0; j < StatusLEDDataInt.iSourceBlinkCnt; ++j)
|
||||
{
|
||||
gpio_set_level(BLINK_GPIO, 0);
|
||||
vTaskDelay(StatusLEDDataInt.iBlinkTime / portTICK_PERIOD_MS);
|
||||
gpio_set_level(BLINK_GPIO, 1);
|
||||
vTaskDelay(StatusLEDDataInt.iBlinkTime / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
vTaskDelay(500 / portTICK_PERIOD_MS); // Delay between module code and error code
|
||||
|
||||
for (int j = 0; j < StatusLEDDataInt.iCodeBlinkCnt; ++j)
|
||||
{
|
||||
gpio_set_level(BLINK_GPIO, 0);
|
||||
vTaskDelay(StatusLEDDataInt.iBlinkTime / portTICK_PERIOD_MS);
|
||||
gpio_set_level(BLINK_GPIO, 1);
|
||||
vTaskDelay(StatusLEDDataInt.iBlinkTime / portTICK_PERIOD_MS);
|
||||
}
|
||||
vTaskDelay(1500 / portTICK_PERIOD_MS); // Delay to signal new round
|
||||
}
|
||||
|
||||
StatusLEDData.bProcessingRequest = false;
|
||||
//ESP_LOGD(TAG, "task_StatusLED - done/wait");
|
||||
vTaskDelay(10000 / portTICK_PERIOD_MS); // Wait for an upcoming request otherwise continue and delete task to save memory
|
||||
}
|
||||
//ESP_LOGD(TAG, "task_StatusLED - delete");
|
||||
xHandle_task_StatusLED = NULL;
|
||||
vTaskDelete(NULL); // Delete this task due to no request
|
||||
}
|
||||
|
||||
|
||||
void StatusLED(StatusLedSource _eSource, int _iCode, bool _bInfinite)
|
||||
{
|
||||
//ESP_LOGD(TAG, "StatusLED - start");
|
||||
|
||||
if (_eSource == WLAN_CONN) {
|
||||
StatusLEDData.iSourceBlinkCnt = WLAN_CONN;
|
||||
StatusLEDData.iCodeBlinkCnt = _iCode;
|
||||
StatusLEDData.iBlinkTime = 250;
|
||||
StatusLEDData.bInfinite = _bInfinite;
|
||||
}
|
||||
else if (_eSource == WLAN_INIT) {
|
||||
StatusLEDData.iSourceBlinkCnt = WLAN_INIT;
|
||||
StatusLEDData.iCodeBlinkCnt = _iCode;
|
||||
StatusLEDData.iBlinkTime = 250;
|
||||
StatusLEDData.bInfinite = _bInfinite;
|
||||
}
|
||||
else if (_eSource == SDCARD_INIT) {
|
||||
StatusLEDData.iSourceBlinkCnt = SDCARD_INIT;
|
||||
StatusLEDData.iCodeBlinkCnt = _iCode;
|
||||
StatusLEDData.iBlinkTime = 250;
|
||||
StatusLEDData.bInfinite = _bInfinite;
|
||||
}
|
||||
else if (_eSource == SDCARD_CHECK) {
|
||||
StatusLEDData.iSourceBlinkCnt = SDCARD_CHECK;
|
||||
StatusLEDData.iCodeBlinkCnt = _iCode;
|
||||
StatusLEDData.iBlinkTime = 250;
|
||||
StatusLEDData.bInfinite = _bInfinite;
|
||||
}
|
||||
else if (_eSource == CAM_INIT) {
|
||||
StatusLEDData.iSourceBlinkCnt = CAM_INIT;
|
||||
StatusLEDData.iCodeBlinkCnt = _iCode;
|
||||
StatusLEDData.iBlinkTime = 250;
|
||||
StatusLEDData.bInfinite = _bInfinite;
|
||||
}
|
||||
else if (_eSource == PSRAM_INIT) {
|
||||
StatusLEDData.iSourceBlinkCnt = PSRAM_INIT;
|
||||
StatusLEDData.iCodeBlinkCnt = _iCode;
|
||||
StatusLEDData.iBlinkTime = 250;
|
||||
StatusLEDData.bInfinite = _bInfinite;
|
||||
}
|
||||
else if (_eSource == TIME_CHECK) {
|
||||
StatusLEDData.iSourceBlinkCnt = TIME_CHECK;
|
||||
StatusLEDData.iCodeBlinkCnt = _iCode;
|
||||
StatusLEDData.iBlinkTime = 250;
|
||||
StatusLEDData.bInfinite = _bInfinite;
|
||||
}
|
||||
else if (_eSource == AP_OR_OTA) {
|
||||
StatusLEDData.iSourceBlinkCnt = AP_OR_OTA;
|
||||
StatusLEDData.iCodeBlinkCnt = _iCode;
|
||||
StatusLEDData.iBlinkTime = 350;
|
||||
StatusLEDData.bInfinite = _bInfinite;
|
||||
}
|
||||
|
||||
if (xHandle_task_StatusLED && !StatusLEDData.bProcessingRequest) {
|
||||
StatusLEDData.bProcessingRequest = true;
|
||||
BaseType_t xReturned = xTaskAbortDelay(xHandle_task_StatusLED); // Reuse still running status LED task
|
||||
/*if (xReturned == pdPASS)
|
||||
ESP_LOGD(TAG, "task_StatusLED - abort waiting delay");*/
|
||||
}
|
||||
else if (xHandle_task_StatusLED == NULL) {
|
||||
StatusLEDData.bProcessingRequest = true;
|
||||
BaseType_t xReturned = xTaskCreate(&task_StatusLED, "task_StatusLED", 1280, NULL, tskIDLE_PRIORITY+1, &xHandle_task_StatusLED);
|
||||
if(xReturned != pdPASS)
|
||||
{
|
||||
xHandle_task_StatusLED = NULL;
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "task_StatusLED failed to create");
|
||||
LogFile.WriteHeapInfo("task_StatusLED failed");
|
||||
}
|
||||
}
|
||||
else {
|
||||
ESP_LOGD(TAG, "task_StatusLED still processing, request skipped"); // Requests with high frequency could be skipped, but LED is only helpful for static states
|
||||
}
|
||||
//ESP_LOGD(TAG, "StatusLED - done");
|
||||
}
|
||||
|
||||
|
||||
void StatusLEDOff(void)
|
||||
{
|
||||
if (xHandle_task_StatusLED)
|
||||
vTaskDelete(xHandle_task_StatusLED); // Delete task for StatusLED to force stop of blinking
|
||||
|
||||
gpio_pad_select_gpio(BLINK_GPIO); // Init the GPIO
|
||||
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT); // Set the GPIO as a push/pull output
|
||||
gpio_set_level(BLINK_GPIO, 1);// LED off
|
||||
}
|
||||
34
code/components/jomjol_helper/statusled.h
Normal file
34
code/components/jomjol_helper/statusled.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef STATUSLED_H
|
||||
#define STATUSLED_H
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
|
||||
extern TaskHandle_t xHandle_task_StatusLED;
|
||||
|
||||
enum StatusLedSource {
|
||||
WLAN_CONN = 1,
|
||||
WLAN_INIT = 2,
|
||||
SDCARD_INIT = 3,
|
||||
SDCARD_CHECK = 4,
|
||||
CAM_INIT = 5,
|
||||
PSRAM_INIT = 6,
|
||||
TIME_CHECK = 7,
|
||||
AP_OR_OTA = 8
|
||||
};
|
||||
|
||||
struct StatusLEDData {
|
||||
int iSourceBlinkCnt = 1;
|
||||
int iCodeBlinkCnt = 1;
|
||||
int iBlinkTime = 250;
|
||||
bool bInfinite = false;
|
||||
bool bProcessingRequest = false;
|
||||
};
|
||||
|
||||
void StatusLED(StatusLedSource _eSource, int _iCode, bool _bInfinite);
|
||||
void StatusLEDOff(void);
|
||||
|
||||
#endif //STATUSLED_H
|
||||
@@ -78,11 +78,11 @@ void ClassLogFile::WriteToData(std::string _timestamp, std::string _name, std::s
|
||||
}
|
||||
|
||||
|
||||
void ClassLogFile::setLogLevel(esp_log_level_t _logLevel){
|
||||
loglevel = _logLevel;
|
||||
|
||||
void ClassLogFile::setLogLevel(esp_log_level_t _logLevel)
|
||||
{
|
||||
std::string levelText;
|
||||
|
||||
// Print log level to log file
|
||||
switch(_logLevel) {
|
||||
case ESP_LOG_WARN:
|
||||
levelText = "WARNING";
|
||||
@@ -95,13 +95,16 @@ void ClassLogFile::setLogLevel(esp_log_level_t _logLevel){
|
||||
case ESP_LOG_DEBUG:
|
||||
levelText = "DEBUG";
|
||||
break;
|
||||
|
||||
case ESP_LOG_ERROR:
|
||||
default:
|
||||
levelText = "ERROR";
|
||||
break;
|
||||
}
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Set log level to " + levelText);
|
||||
|
||||
ESP_LOGI(TAG, "Log Level set to %s", levelText.c_str());
|
||||
// set new log level
|
||||
loglevel = _logLevel;
|
||||
|
||||
/*
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Test");
|
||||
@@ -386,14 +389,17 @@ void ClassLogFile::RemoveOldDataLog()
|
||||
}
|
||||
|
||||
|
||||
void ClassLogFile::CreateLogDirectories()
|
||||
bool ClassLogFile::CreateLogDirectories()
|
||||
{
|
||||
MakeDir("/sdcard/log");
|
||||
MakeDir("/sdcard/log/data");
|
||||
MakeDir("/sdcard/log/analog");
|
||||
MakeDir("/sdcard/log/digit");
|
||||
MakeDir("/sdcard/log/message");
|
||||
MakeDir("/sdcard/log/source");
|
||||
bool bRetval = false;
|
||||
bRetval = MakeDir("/sdcard/log");
|
||||
bRetval = MakeDir("/sdcard/log/data");
|
||||
bRetval = MakeDir("/sdcard/log/analog");
|
||||
bRetval = MakeDir("/sdcard/log/digit");
|
||||
bRetval = MakeDir("/sdcard/log/message");
|
||||
bRetval = MakeDir("/sdcard/log/source");
|
||||
|
||||
return bRetval;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ public:
|
||||
|
||||
void CloseLogFileAppendHandle();
|
||||
|
||||
void CreateLogDirectories();
|
||||
bool CreateLogDirectories();
|
||||
void RemoveOldLogFile();
|
||||
void RemoveOldDataLog();
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ std::map<std::string, std::function<void()>>* connectFunktionMap = NULL;
|
||||
std::map<std::string, std::function<bool(std::string, char*, int)>>* subscribeFunktionMap = NULL;
|
||||
|
||||
int failedOnRound = -1;
|
||||
int MQTTReconnectCnt = 0;
|
||||
|
||||
esp_mqtt_event_id_t esp_mqtt_ID = MQTT_EVENT_ANY;
|
||||
// ESP_EVENT_ANY_ID
|
||||
@@ -89,29 +90,39 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) {
|
||||
std::string topic = "";
|
||||
switch (event->event_id) {
|
||||
case MQTT_EVENT_BEFORE_CONNECT:
|
||||
ESP_LOGD(TAG, "MQTT_EVENT_BEFORE_CONNECT");
|
||||
mqtt_initialized = true;
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_CONNECTED:
|
||||
ESP_LOGD(TAG, "MQTT_EVENT_CONNECTED");
|
||||
MQTTReconnectCnt = 0;
|
||||
mqtt_initialized = true;
|
||||
mqtt_connected = true;
|
||||
MQTTconnected();
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_DISCONNECTED:
|
||||
ESP_LOGD(TAG, "MQTT_EVENT_DISCONNECTED");
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Disconnected from broker");
|
||||
mqtt_connected = false;
|
||||
MQTTReconnectCnt++;
|
||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Disconnected, trying to reconnect");
|
||||
|
||||
if (MQTTReconnectCnt >= 5) {
|
||||
MQTTReconnectCnt = 0;
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Disconnected, multiple reconnect attempts failed, still retrying...");
|
||||
}
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_SUBSCRIBED:
|
||||
ESP_LOGD(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_UNSUBSCRIBED:
|
||||
ESP_LOGD(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_PUBLISHED:
|
||||
ESP_LOGD(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_DATA:
|
||||
ESP_LOGD(TAG, "MQTT_EVENT_DATA");
|
||||
ESP_LOGD(TAG, "TOPIC=%.*s", event->topic_len, event->topic);
|
||||
@@ -126,6 +137,7 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) {
|
||||
ESP_LOGW(TAG, "no handler available\r\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_ERROR:
|
||||
#ifdef DEBUG_DETAIL_ON
|
||||
ESP_LOGD(TAG, "MQTT_EVENT_ERROR - esp_mqtt_error_codes:");
|
||||
@@ -136,8 +148,9 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) {
|
||||
ESP_LOGD(TAG, "esp_tls_stack_err:%d", event->error_handle->esp_tls_stack_err);
|
||||
ESP_LOGD(TAG, "esp_tls_cert_verify_flags:%d", event->error_handle->esp_tls_cert_verify_flags);
|
||||
#endif
|
||||
mqtt_connected = false;
|
||||
//mqtt_connected = false;
|
||||
break;
|
||||
|
||||
default:
|
||||
ESP_LOGD(TAG, "Other event id:%d", event->event_id);
|
||||
break;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "esp_log.h"
|
||||
#include "ClassLogFile.h"
|
||||
#include "connect_wlan.h"
|
||||
#include "read_wlanini.h"
|
||||
#include "server_mqtt.h"
|
||||
#include "interface_mqtt.h"
|
||||
#include "time_sntp.h"
|
||||
@@ -201,7 +202,7 @@ void publishStaticData() {
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Publishing static MQTT topics...");
|
||||
MQTTPublish(maintopic + "/" + "MAC", getMac(), retainFlag);
|
||||
MQTTPublish(maintopic + "/" + "IP", *getIPAddress(), retainFlag);
|
||||
MQTTPublish(maintopic + "/" + "hostname", hostname, retainFlag);
|
||||
MQTTPublish(maintopic + "/" + "hostname", wlan_config.hostname, retainFlag);
|
||||
|
||||
std::stringstream stream;
|
||||
stream << std::fixed << std::setprecision(1) << roundInterval; // minutes
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "../../include/defines.h"
|
||||
#include "Helper.h"
|
||||
#include "statusled.h"
|
||||
|
||||
#include "esp_camera.h"
|
||||
#include "time_sntp.h"
|
||||
@@ -21,8 +22,11 @@
|
||||
#include "server_GPIO.h"
|
||||
|
||||
#include "server_file.h"
|
||||
|
||||
#include "read_wlanini.h"
|
||||
#include "connect_wlan.h"
|
||||
|
||||
|
||||
ClassFlowControll tfliteflow;
|
||||
|
||||
TaskHandle_t xHandletask_autodoFlow = NULL;
|
||||
@@ -44,20 +48,24 @@ static const char *TAG = "TFLITE SERVER";
|
||||
void CheckIsPlannedReboot()
|
||||
{
|
||||
FILE *pfile;
|
||||
if ((pfile = fopen("/sdcard/reboot.txt", "r")) == NULL)
|
||||
{
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Not a planned reboot.");
|
||||
if ((pfile = fopen("/sdcard/reboot.txt", "r")) == NULL) {
|
||||
//LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Initial boot or not a planned reboot");
|
||||
isPlannedReboot = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Planned reboot.");
|
||||
else {
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Planned reboot");
|
||||
DeleteFile("/sdcard/reboot.txt"); // Prevent Boot Loop!!!
|
||||
isPlannedReboot = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool getIsPlannedReboot()
|
||||
{
|
||||
return isPlannedReboot;
|
||||
}
|
||||
|
||||
|
||||
int getCountFlowRounds()
|
||||
{
|
||||
return countRounds;
|
||||
@@ -837,21 +845,15 @@ void task_autodoFlow(void *pvParameter)
|
||||
|
||||
bTaskAutoFlowCreated = true;
|
||||
|
||||
if (!isPlannedReboot)
|
||||
if (!isPlannedReboot && (esp_reset_reason() == ESP_RST_PANIC))
|
||||
{
|
||||
if (esp_reset_reason() == ESP_RST_PANIC) {
|
||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Restarted due to an Exception/panic! Postponing first round start by 5 minutes to allow for an OTA Update or to fetch the log!");
|
||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Setting logfile level to DEBUG until the next reboot!");
|
||||
LogFile.setLogLevel(ESP_LOG_DEBUG);
|
||||
tfliteflow.setActStatus("Initialization (delayed)");
|
||||
//#ifdef ENABLE_MQTT
|
||||
//MQTTPublish(mqttServer_getMainTopic() + "/" + "status", "Initialization (delayed)", false); // Right now, not possible -> MQTT Service is going to be started later
|
||||
//#endif //ENABLE_MQTT
|
||||
vTaskDelay(60*5000 / portTICK_RATE_MS); // Wait 5 minutes to give time to do an OTA Update or fetch the log
|
||||
}
|
||||
tfliteflow.setActStatus("Initialization (delayed)");
|
||||
//#ifdef ENABLE_MQTT
|
||||
//MQTTPublish(mqttServer_getMainTopic() + "/" + "status", "Initialization (delayed)", false); // Right now, not possible -> MQTT Service is going to be started later
|
||||
//#endif //ENABLE_MQTT
|
||||
vTaskDelay(60*5000 / portTICK_PERIOD_MS); // Wait 5 minutes to give time to do an OTA update or fetch the log
|
||||
}
|
||||
|
||||
|
||||
ESP_LOGD(TAG, "task_autodoFlow: start");
|
||||
doInit();
|
||||
|
||||
@@ -891,16 +893,22 @@ void task_autodoFlow(void *pvParameter)
|
||||
LogFile.RemoveOldLogFile();
|
||||
LogFile.RemoveOldDataLog();
|
||||
}
|
||||
|
||||
//Round finished -> Logfile
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Round #" + std::to_string(countRounds) +
|
||||
" completed (" + std::to_string(getUpTime() - roundStartTime) + " seconds)");
|
||||
|
||||
//CPU Temp -> Logfile
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CPU Temperature: " + std::to_string((int)temperatureRead()) + "°C");
|
||||
|
||||
// WIFI Signal Strength (RSSI) -> Logfile
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "WIFI Signal (RSSI): " + std::to_string(get_WIFI_RSSI()) + "dBm");
|
||||
|
||||
//Round finished -> Logfile
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Round #" + std::to_string(countRounds) +
|
||||
" completed (" + std::to_string(getUpTime() - roundStartTime) + " seconds)");
|
||||
|
||||
// Check if time is synchronized (if NTP is configured)
|
||||
if (getUseNtp() && !getTimeIsSet()) {
|
||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Time server is configured, but time is not yet set. Check configuration");
|
||||
StatusLED(TIME_CHECK, 1, false);
|
||||
}
|
||||
|
||||
fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
|
||||
if (auto_interval > fr_delta_ms)
|
||||
|
||||
@@ -21,6 +21,7 @@ bool isSetupModusActive();
|
||||
int getCountFlowRounds();
|
||||
|
||||
void CheckIsPlannedReboot();
|
||||
bool getIsPlannedReboot();
|
||||
|
||||
esp_err_t GetJPG(std::string _filename, httpd_req_t *req);
|
||||
esp_err_t GetRawJPG(httpd_req_t *req);
|
||||
|
||||
@@ -1,12 +1,24 @@
|
||||
#include "connect_wlan.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_wnm.h"
|
||||
#include "esp_rrm.h"
|
||||
#include "esp_mbo.h"
|
||||
#include "esp_mac.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
@@ -17,56 +29,20 @@
|
||||
#include "interface_mqtt.h"
|
||||
#endif //ENABLE_MQTT
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include "ClassLogFile.h"
|
||||
#include "read_wlanini.h"
|
||||
#include "Helper.h"
|
||||
#include "statusled.h"
|
||||
|
||||
//////////////////////
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_wnm.h"
|
||||
#include "esp_rrm.h"
|
||||
#include "esp_mbo.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_mac.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_netif.h"
|
||||
|
||||
|
||||
/////////////////////
|
||||
#include "../../include/defines.h"
|
||||
|
||||
|
||||
/* FreeRTOS event group to signal when we are connected*/
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
|
||||
static const char *TAG = "WIFI";
|
||||
|
||||
static int s_retry_num = 0;
|
||||
|
||||
bool WIFIConnected = false;
|
||||
int WIFIReconnectCnt = 0;
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
|
||||
int BlinkDauer;
|
||||
int BlinkAnzahl;
|
||||
bool BlinkOff;
|
||||
bool BlinkIsRunning = false;
|
||||
|
||||
std::string hostname = "";
|
||||
std::string std_hostname = "watermeter";
|
||||
std::string ipadress = "";
|
||||
std::string ssid = "";
|
||||
int RSSIThreshold;
|
||||
|
||||
/////////////////////////////////
|
||||
/////////////////////////////////
|
||||
|
||||
#ifdef WLAN_USE_MESH_ROAMING
|
||||
|
||||
@@ -301,97 +277,87 @@ static void esp_bss_rssi_low_handler(void* arg, esp_event_base_t event_base,
|
||||
|
||||
std::string* getIPAddress()
|
||||
{
|
||||
return &ipadress;
|
||||
return &wlan_config.ipaddress;
|
||||
}
|
||||
|
||||
|
||||
std::string* getSSID()
|
||||
{
|
||||
return &ssid;
|
||||
return &wlan_config.ssid;
|
||||
}
|
||||
|
||||
|
||||
void task_doBlink(void *pvParameter)
|
||||
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
|
||||
{
|
||||
ESP_LOGI("BLINK", "Flash - start");
|
||||
while (BlinkIsRunning)
|
||||
{
|
||||
// ESP_LOGI("BLINK", "Blinken - wait");
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
BlinkIsRunning = true;
|
||||
|
||||
// Init the GPIO
|
||||
gpio_pad_select_gpio(BLINK_GPIO);
|
||||
/* Set the GPIO as a push/pull output */
|
||||
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
|
||||
|
||||
for (int i = 0; i < BlinkAnzahl; ++i)
|
||||
{
|
||||
if (BlinkAnzahl > 1)
|
||||
{
|
||||
gpio_set_level(BLINK_GPIO, 1);
|
||||
vTaskDelay(BlinkDauer / portTICK_PERIOD_MS);
|
||||
}
|
||||
gpio_set_level(BLINK_GPIO, 0);
|
||||
vTaskDelay(BlinkDauer / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
if (BlinkOff)
|
||||
gpio_set_level(BLINK_GPIO, 1);
|
||||
|
||||
ESP_LOGI("BLINK", "Flash - done");
|
||||
BlinkIsRunning = false;
|
||||
|
||||
vTaskDelete(NULL); //Delete this task if it exits from the loop above
|
||||
}
|
||||
|
||||
|
||||
void LEDBlinkTask(int _dauer, int _anz, bool _off)
|
||||
{
|
||||
BlinkDauer = _dauer;
|
||||
BlinkAnzahl = _anz;
|
||||
BlinkOff = _off;
|
||||
|
||||
xTaskCreate(&task_doBlink, "task_doBlink", 4 * 1024, NULL, tskIDLE_PRIORITY+1, NULL);
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
{
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
|
||||
{
|
||||
WIFIConnected = false;
|
||||
LEDBlinkTask(200, 1, true);
|
||||
esp_wifi_connect();
|
||||
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
WIFIConnected = false;
|
||||
// if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
|
||||
esp_wifi_connect();
|
||||
s_retry_num++;
|
||||
ESP_LOGI(TAG, "retrying connection to the AP");
|
||||
// } else {
|
||||
// xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
// }
|
||||
ESP_LOGI(TAG,"connection to the AP failed");
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
|
||||
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
||||
ipadress = std::string(ip4addr_ntoa((const ip4_addr*) &event->ip_info.ip));
|
||||
s_retry_num = 0;
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
LEDBlinkTask(1000, 5, true);
|
||||
}
|
||||
|
||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
|
||||
{
|
||||
/* Disconnect reason: https://github.com/espressif/esp-idf/blob/d825753387c1a64463779bbd2369e177e5d59a79/components/esp_wifi/include/esp_wifi_types.h */
|
||||
wifi_event_sta_disconnected_t *disconn = (wifi_event_sta_disconnected_t *)event_data;
|
||||
if (disconn->reason == WIFI_REASON_ROAMING) {
|
||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Disconnected (" + std::to_string(disconn->reason) + ", Roaming)");
|
||||
// --> no reconnect neccessary, it should automatically reconnect to new AP
|
||||
}
|
||||
else {
|
||||
WIFIConnected = false;
|
||||
if (disconn->reason == WIFI_REASON_NO_AP_FOUND) {
|
||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Disconnected (" + std::to_string(disconn->reason) + ", No AP)");
|
||||
StatusLED(WLAN_CONN, 1, false);
|
||||
}
|
||||
else if (disconn->reason == WIFI_REASON_AUTH_EXPIRE ||
|
||||
disconn->reason == WIFI_REASON_AUTH_FAIL ||
|
||||
disconn->reason == WIFI_REASON_NOT_AUTHED ||
|
||||
disconn->reason == WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT ||
|
||||
disconn->reason == WIFI_REASON_HANDSHAKE_TIMEOUT) {
|
||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Disconnected (" + std::to_string(disconn->reason) + ", Auth fail)");
|
||||
StatusLED(WLAN_CONN, 2, false);
|
||||
}
|
||||
else if (disconn->reason == WIFI_REASON_BEACON_TIMEOUT) {
|
||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Disconnected (" + std::to_string(disconn->reason) + ", Timeout)");
|
||||
StatusLED(WLAN_CONN, 3, false);
|
||||
}
|
||||
else {
|
||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Disconnected (" + std::to_string(disconn->reason) + ")");
|
||||
StatusLED(WLAN_CONN, 4, false);
|
||||
}
|
||||
WIFIReconnectCnt++;
|
||||
esp_wifi_connect(); // Try to connect again
|
||||
}
|
||||
|
||||
if (WIFIReconnectCnt >= 10) {
|
||||
WIFIReconnectCnt = 0;
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Disconnected, multiple reconnect attempts failed (" +
|
||||
std::to_string(disconn->reason) + "), still retrying...");
|
||||
}
|
||||
}
|
||||
|
||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED)
|
||||
{
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Connected to: " + wlan_config.ssid + ", RSSI: " +
|
||||
std::to_string(get_WIFI_RSSI()));
|
||||
}
|
||||
|
||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
|
||||
{
|
||||
WIFIConnected = true;
|
||||
#ifdef ENABLE_MQTT
|
||||
WIFIReconnectCnt = 0;
|
||||
|
||||
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
|
||||
wlan_config.ipaddress = std::string(ip4addr_ntoa((const ip4_addr*) &event->ip_info.ip));
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Got IP: " + wlan_config.ipaddress);
|
||||
|
||||
#ifdef ENABLE_MQTT
|
||||
if (getMQTTisEnabled()) {
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
MQTT_Init(); // Init when WIFI is getting connected
|
||||
}
|
||||
#endif //ENABLE_MQTT
|
||||
}
|
||||
#endif //ENABLE_MQTT
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -403,148 +369,148 @@ void strinttoip4(const char *ip, int &a, int &b, int &c, int &d) {
|
||||
}
|
||||
|
||||
|
||||
void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostname, const char *_ipadr, const char *_gw, const char *_netmask, const char *_dns, int _rssithreshold)
|
||||
esp_err_t wifi_init_sta(void)
|
||||
{
|
||||
RSSI_Threshold = _rssithreshold;
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
esp_err_t retval = esp_netif_init();
|
||||
if (retval != ESP_OK) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "esp_netif_init: Error: " + std::to_string(retval));
|
||||
return retval;
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
retval = esp_event_loop_create_default();
|
||||
if (retval != ESP_OK) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "esp_event_loop_create_default: Error: " + std::to_string(retval));
|
||||
return retval;
|
||||
}
|
||||
|
||||
esp_netif_t *my_sta = esp_netif_create_default_wifi_sta();
|
||||
|
||||
if ((_ipadr != NULL) && (_gw != NULL) && (_netmask != NULL))
|
||||
if (!wlan_config.ipaddress.empty() && !wlan_config.gateway.empty() && !wlan_config.netmask.empty())
|
||||
{
|
||||
|
||||
ESP_LOGI(TAG, "set IP %s, GW %s, Netmask %s manual", _ipadr, _gw, _netmask);
|
||||
esp_netif_dhcpc_stop(my_sta);
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Manual interface config -> IP: " + wlan_config.ipaddress + ", Gateway: " +
|
||||
std::string(wlan_config.gateway) + ", Netmask: " + std::string(wlan_config.netmask));
|
||||
esp_netif_dhcpc_stop(my_sta); // Stop DHCP service
|
||||
|
||||
esp_netif_ip_info_t ip_info;
|
||||
int a, b, c, d;
|
||||
strinttoip4(_ipadr, a, b, c, d);
|
||||
IP4_ADDR(&ip_info.ip, a, b, c, d);
|
||||
strinttoip4(_gw, a, b, c, d);
|
||||
IP4_ADDR(&ip_info.gw, a, b, c, d);
|
||||
strinttoip4(_netmask, a, b, c, d);
|
||||
IP4_ADDR(&ip_info.netmask, a, b, c, d);
|
||||
strinttoip4(wlan_config.ipaddress.c_str(), a, b, c, d);
|
||||
IP4_ADDR(&ip_info.ip, a, b, c, d); // Set static IP address
|
||||
|
||||
esp_netif_set_ip_info(my_sta, &ip_info);
|
||||
strinttoip4(wlan_config.gateway.c_str(), a, b, c, d);
|
||||
IP4_ADDR(&ip_info.gw, a, b, c, d); // Set gateway
|
||||
|
||||
strinttoip4(wlan_config.netmask.c_str(), a, b, c, d);
|
||||
IP4_ADDR(&ip_info.netmask, a, b, c, d); // Set netmask
|
||||
|
||||
esp_netif_set_ip_info(my_sta, &ip_info); // Set static IP configuration
|
||||
}
|
||||
else {
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Automatic interface config --> Use DHCP service");
|
||||
}
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
retval = esp_wifi_init(&cfg);
|
||||
if (retval != ESP_OK) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "esp_wifi_init: Error: " + std::to_string(retval));
|
||||
return retval;
|
||||
}
|
||||
|
||||
if ((_ipadr != NULL) && (_gw != NULL) && (_netmask != NULL))
|
||||
if (!wlan_config.ipaddress.empty() && !wlan_config.gateway.empty() && !wlan_config.netmask.empty())
|
||||
{
|
||||
if (_dns == NULL)
|
||||
_dns = _gw;
|
||||
|
||||
ESP_LOGI(TAG, "set DNS manual");
|
||||
if (wlan_config.dns.empty()) {
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "No DNS server, use gateway");
|
||||
wlan_config.dns = wlan_config.gateway;
|
||||
}
|
||||
else {
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Manual interface config -> DNS: " + wlan_config.dns);
|
||||
}
|
||||
|
||||
esp_netif_dns_info_t dns_info;
|
||||
ip4_addr_t ip;
|
||||
ip.addr = esp_ip4addr_aton(_dns);
|
||||
ip.addr = esp_ip4addr_aton(wlan_config.dns.c_str());
|
||||
ip_addr_set_ip4_u32(&dns_info.ip, ip.addr);
|
||||
ESP_ERROR_CHECK(esp_netif_set_dns_info(my_sta, ESP_NETIF_DNS_MAIN, &dns_info));
|
||||
}
|
||||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
esp_event_handler_instance_t instance_bss_rssi_low;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&event_handler,
|
||||
NULL,
|
||||
&instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&event_handler,
|
||||
NULL,
|
||||
&instance_got_ip));
|
||||
retval = esp_netif_set_dns_info(my_sta, ESP_NETIF_DNS_MAIN, &dns_info);
|
||||
if (retval != ESP_OK) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "esp_netif_set_dns_info: Error: " + std::to_string(retval));
|
||||
return retval;
|
||||
}
|
||||
}
|
||||
|
||||
retval = esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID,
|
||||
&event_handler, NULL, NULL);
|
||||
if (retval != ESP_OK) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "esp_event_handler_instance_register - WIFI_ANY: Error: " + std::to_string(retval));
|
||||
return retval;
|
||||
}
|
||||
|
||||
retval = esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
|
||||
&event_handler, NULL, NULL);
|
||||
if (retval != ESP_OK) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "esp_event_handler_instance_register - GOT_IP: Error: " + std::to_string(retval));
|
||||
return retval;
|
||||
}
|
||||
|
||||
#ifdef WLAN_USE_MESH_ROAMING
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
WIFI_EVENT_STA_BSS_RSSI_LOW,
|
||||
&esp_bss_rssi_low_handler,
|
||||
NULL,
|
||||
&instance_bss_rssi_low));
|
||||
retval = esp_event_handler_instance_register(WIFI_EVENT, WIFI_EVENT_STA_BSS_RSSI_LOW,
|
||||
&esp_bss_rssi_low_handler, NULL, NULL);
|
||||
if (retval != ESP_OK) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "esp_event_handler_instance_register - BSS_RSSI_LOW: Error: " + std::to_string(retval));
|
||||
return retval;
|
||||
}
|
||||
#endif
|
||||
|
||||
wifi_config_t wifi_config = { };
|
||||
|
||||
strcpy((char*)wifi_config.sta.ssid, (const char*)_ssid);
|
||||
strcpy((char*)wifi_config.sta.password, (const char*)_password);
|
||||
strcpy((char*)wifi_config.sta.ssid, (const char*)wlan_config.ssid.c_str());
|
||||
strcpy((char*)wifi_config.sta.password, (const char*)wlan_config.password.c_str());
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
|
||||
ESP_ERROR_CHECK(esp_wifi_start() );
|
||||
retval = esp_wifi_set_mode(WIFI_MODE_STA);
|
||||
if (retval != ESP_OK) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "esp_wifi_set_mode: Error: " + std::to_string(retval));
|
||||
return retval;
|
||||
}
|
||||
|
||||
if (_hostname != NULL)
|
||||
retval = esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
|
||||
if (retval != ESP_OK) {
|
||||
if (retval == ESP_ERR_WIFI_PASSWORD) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "esp_wifi_set_config: SSID password invalid! Error: " + std::to_string(retval));
|
||||
}
|
||||
else {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "esp_wifi_set_config: Error: " + std::to_string(retval));
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
retval = esp_wifi_start();
|
||||
if (retval != ESP_OK) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "esp_wifi_start: Error: " + std::to_string(retval));
|
||||
return retval;
|
||||
}
|
||||
|
||||
if (!wlan_config.hostname.empty())
|
||||
{
|
||||
esp_err_t ret = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA , _hostname);
|
||||
hostname = std::string(_hostname);
|
||||
if(ret != ESP_OK ){
|
||||
ESP_LOGE(TAG,"Failed to set hostname: %d",ret);
|
||||
retval = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA , wlan_config.hostname.c_str());
|
||||
if(retval != ESP_OK ) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to set hostname! Error: " + std::to_string(retval));
|
||||
}
|
||||
else {
|
||||
ESP_LOGI(TAG,"Set hostname to: %s", _hostname);
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Set hostname to: " + wlan_config.hostname);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
||||
|
||||
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
|
||||
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
portMAX_DELAY);
|
||||
|
||||
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
|
||||
* happened. */
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
#ifdef __HIDE_PASSWORD
|
||||
ESP_LOGI(TAG, "Connected with AP: %s, password: XXXXXXX", _ssid);
|
||||
#else
|
||||
ESP_LOGI(TAG, "Connected with AP: %s, password: %s", _ssid, _password);
|
||||
#endif
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
#ifdef __HIDE_PASSWORD
|
||||
ESP_LOGI(TAG, "Failed to connect with AP: %s, password: XXXXXXXX", _ssid);
|
||||
#else
|
||||
ESP_LOGI(TAG, "Failed to connect with AP: %s, password: %s", _ssid, _password);
|
||||
#endif
|
||||
} else {
|
||||
ESP_LOGE(TAG, "UNEXPECTED EVENT");
|
||||
}
|
||||
ssid = std::string(_ssid);
|
||||
|
||||
|
||||
/* The event will not be processed after unregister */
|
||||
// ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
|
||||
// ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
|
||||
// vEventGroupDelete(s_wifi_event_group);
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Init successful");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
int get_WIFI_RSSI()
|
||||
{
|
||||
wifi_ap_record_t ap;
|
||||
esp_wifi_sta_get_ap_info(&ap);
|
||||
return ap.rssi;
|
||||
}
|
||||
|
||||
|
||||
void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostname)
|
||||
{
|
||||
wifi_init_sta(_ssid, _password, _hostname, NULL, NULL, NULL, NULL, 0);
|
||||
}
|
||||
|
||||
|
||||
void wifi_init_sta(const char *_ssid, const char *_password)
|
||||
{
|
||||
wifi_init_sta(_ssid, _password, NULL, NULL, NULL, NULL, NULL, 0);
|
||||
wifi_ap_record_t ap;
|
||||
if (esp_wifi_sta_get_ap_info(&ap) == ESP_OK)
|
||||
return ap.rssi;
|
||||
else
|
||||
return -127; // Return -127 if no info available e.g. not connected
|
||||
}
|
||||
|
||||
|
||||
@@ -556,14 +522,13 @@ bool getWIFIisConnected()
|
||||
|
||||
void WIFIDestroy()
|
||||
{
|
||||
esp_wifi_disconnect();
|
||||
|
||||
esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, event_handler);
|
||||
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, event_handler);
|
||||
#ifdef WLAN_USE_MESH_ROAMING
|
||||
esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_BSS_RSSI_LOW, esp_bss_rssi_low_handler);
|
||||
#endif
|
||||
|
||||
|
||||
esp_wifi_disconnect();
|
||||
esp_wifi_stop();
|
||||
esp_wifi_deinit();
|
||||
}
|
||||
|
||||
@@ -5,18 +5,11 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostname, const char *_ipadr, const char *_gw, const char *_netmask, const char *_dns, int _rssithreshold);
|
||||
void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostname);
|
||||
void wifi_init_sta(const char *_ssid, const char *_password);
|
||||
|
||||
int wifi_init_sta(void);
|
||||
std::string* getIPAddress();
|
||||
std::string* getSSID();
|
||||
int get_WIFI_RSSI();
|
||||
bool getWIFIisConnected();
|
||||
void WIFIDestroy();
|
||||
|
||||
extern std::string hostname;
|
||||
extern std::string std_hostname;
|
||||
extern int RSSIThreshold;
|
||||
|
||||
#endif //CONNECT_WLAN_H
|
||||
@@ -11,9 +11,14 @@
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
#include "esp_log.h"
|
||||
#include "ClassLogFile.h"
|
||||
#include "../../include/defines.h"
|
||||
|
||||
static const char *TAG = "WLAN.INI";
|
||||
static const char *TAG = "WLANINI";
|
||||
|
||||
|
||||
struct wlan_config wlan_config = {};
|
||||
|
||||
|
||||
std::vector<string> ZerlegeZeileWLAN(std::string input, std::string _delimiter = "")
|
||||
{
|
||||
@@ -40,207 +45,189 @@ std::vector<string> ZerlegeZeileWLAN(std::string input, std::string _delimiter =
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool LoadWlanFromFile(std::string fn, char *&_ssid, char *&_password, char *&_hostname, char *&_ipadr, char *&_gw, char *&_netmask, char *&_dns, int &_rssithreshold)
|
||||
int LoadWlanFromFile(std::string fn)
|
||||
{
|
||||
std::string ssid = "";
|
||||
std::string passphrase = "";
|
||||
std::string ipaddress = "";
|
||||
std::string gw = "";
|
||||
std::string netmask = "";
|
||||
std::string dns = "";
|
||||
int rssithreshold = 0;
|
||||
|
||||
std::string line = "";
|
||||
std::string tmp = "";
|
||||
std::vector<string> splitted;
|
||||
hostname = std_hostname;
|
||||
|
||||
FILE* pFile;
|
||||
fn = FormatFileName(fn);
|
||||
FILE* pFile = fopen(fn.c_str(), "r");
|
||||
if (pFile == NULL) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to open file (read). Device init aborted!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
pFile = fopen(fn.c_str(), "r");
|
||||
if (!pFile)
|
||||
return false;
|
||||
ESP_LOGD(TAG, "LoadWlanFromFile: wlan.ini opened");
|
||||
|
||||
ESP_LOGD(TAG, "file loaded");
|
||||
|
||||
if (pFile == NULL)
|
||||
return false;
|
||||
|
||||
char zw[1024];
|
||||
fgets(zw, 1024, pFile);
|
||||
line = std::string(zw);
|
||||
char zw[256];
|
||||
if (fgets(zw, sizeof(zw), pFile) == NULL) {
|
||||
line = "";
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "file opened, but empty or content not readable. Device init aborted!");
|
||||
fclose(pFile);
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
line = std::string(zw);
|
||||
}
|
||||
|
||||
while ((line.size() > 0) || !(feof(pFile)))
|
||||
{
|
||||
// ESP_LOGD(TAG, "%s", line.c_str());
|
||||
splitted = ZerlegeZeileWLAN(line, "=");
|
||||
splitted[0] = trim(splitted[0], " ");
|
||||
//ESP_LOGD(TAG, "line: %s", line.c_str());
|
||||
if (line[0] != ';') { // Skip lines which starts with ';'
|
||||
|
||||
if ((splitted.size() > 1) && (toUpper(splitted[0]) == "HOSTNAME")){
|
||||
hostname = trim(splitted[1]);
|
||||
if ((hostname[0] == '"') && (hostname[hostname.length()-1] == '"')){
|
||||
hostname = hostname.substr(1, hostname.length()-2);
|
||||
splitted = ZerlegeZeileWLAN(line, "=");
|
||||
splitted[0] = trim(splitted[0], " ");
|
||||
|
||||
if ((splitted.size() > 1) && (toUpper(splitted[0]) == "SSID")){
|
||||
tmp = trim(splitted[1]);
|
||||
if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
|
||||
tmp = tmp.substr(1, tmp.length()-2);
|
||||
}
|
||||
wlan_config.ssid = tmp;
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "SSID: " + wlan_config.ssid);
|
||||
}
|
||||
|
||||
else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "PASSWORD")){
|
||||
tmp = splitted[1];
|
||||
if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
|
||||
tmp = tmp.substr(1, tmp.length()-2);
|
||||
}
|
||||
wlan_config.password = tmp;
|
||||
#ifndef __HIDE_PASSWORD
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Password: " + wlan_config.password);
|
||||
#else
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Password: XXXXXXXX");
|
||||
#endif
|
||||
}
|
||||
|
||||
else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "HOSTNAME")){
|
||||
tmp = trim(splitted[1]);
|
||||
if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
|
||||
tmp = tmp.substr(1, tmp.length()-2);
|
||||
}
|
||||
wlan_config.hostname = tmp;
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Hostname: " + wlan_config.hostname);
|
||||
}
|
||||
|
||||
else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "IP")){
|
||||
tmp = splitted[1];
|
||||
if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
|
||||
tmp = tmp.substr(1, tmp.length()-2);
|
||||
}
|
||||
wlan_config.ipaddress = tmp;
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "IP-Address: " + wlan_config.ipaddress);
|
||||
}
|
||||
|
||||
else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "GATEWAY")){
|
||||
tmp = splitted[1];
|
||||
if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
|
||||
tmp = tmp.substr(1, tmp.length()-2);
|
||||
}
|
||||
wlan_config.gateway = tmp;
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Gateway: " + wlan_config.gateway);
|
||||
}
|
||||
|
||||
else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "NETMASK")){
|
||||
tmp = splitted[1];
|
||||
if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
|
||||
tmp = tmp.substr(1, tmp.length()-2);
|
||||
}
|
||||
wlan_config.netmask = tmp;
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Netmask: " + wlan_config.netmask);
|
||||
}
|
||||
|
||||
else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "DNS")){
|
||||
tmp = splitted[1];
|
||||
if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
|
||||
tmp = tmp.substr(1, tmp.length()-2);
|
||||
}
|
||||
wlan_config.dns = tmp;
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "DNS: " + wlan_config.dns);
|
||||
}
|
||||
#ifdef WLAN_USE_MESH_ROAMING
|
||||
else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSITHRESHOLD")) {
|
||||
tmp = trim(splitted[1]);
|
||||
if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
|
||||
tmp = tmp.substr(1, tmp.length()-2);
|
||||
}
|
||||
wlan_config.rssi_threshold = atoi(tmp.c_str());
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "RSSIThreshold: " + std::to_string(wlan_config.rssi_threshold));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if ((splitted.size() > 1) && (toUpper(splitted[0]) == "SSID")){
|
||||
ssid = trim(splitted[1]);
|
||||
if ((ssid[0] == '"') && (ssid[ssid.length()-1] == '"')){
|
||||
ssid = ssid.substr(1, ssid.length()-2);
|
||||
}
|
||||
}
|
||||
|
||||
if ((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSITHRESHOLD")){
|
||||
string _s = trim(splitted[1]);
|
||||
if ((_s[0] == '"') && (_s[_s.length()-1] == '"')){
|
||||
_s = _s.substr(1, ssid.length()-2);
|
||||
}
|
||||
rssithreshold = atoi(_s.c_str());
|
||||
}
|
||||
|
||||
|
||||
if ((splitted.size() > 1) && (toUpper(splitted[0]) == "PASSWORD")){
|
||||
passphrase = splitted[1];
|
||||
if ((passphrase[0] == '"') && (passphrase[passphrase.length()-1] == '"')){
|
||||
passphrase = passphrase.substr(1, passphrase.length()-2);
|
||||
}
|
||||
}
|
||||
|
||||
if ((splitted.size() > 1) && (toUpper(splitted[0]) == "IP")){
|
||||
ipaddress = splitted[1];
|
||||
if ((ipaddress[0] == '"') && (ipaddress[ipaddress.length()-1] == '"')){
|
||||
ipaddress = ipaddress.substr(1, ipaddress.length()-2);
|
||||
}
|
||||
}
|
||||
|
||||
if ((splitted.size() > 1) && (toUpper(splitted[0]) == "GATEWAY")){
|
||||
gw = splitted[1];
|
||||
if ((gw[0] == '"') && (gw[gw.length()-1] == '"')){
|
||||
gw = gw.substr(1, gw.length()-2);
|
||||
}
|
||||
}
|
||||
|
||||
if ((splitted.size() > 1) && (toUpper(splitted[0]) == "NETMASK")){
|
||||
netmask = splitted[1];
|
||||
if ((netmask[0] == '"') && (netmask[netmask.length()-1] == '"')){
|
||||
netmask = netmask.substr(1, netmask.length()-2);
|
||||
}
|
||||
}
|
||||
|
||||
if ((splitted.size() > 1) && (toUpper(splitted[0]) == "DNS")){
|
||||
dns = splitted[1];
|
||||
if ((dns[0] == '"') && (dns[dns.length()-1] == '"')){
|
||||
dns = dns.substr(1, dns.length()-2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (fgets(zw, 1024, pFile) == NULL)
|
||||
{
|
||||
/* read next line */
|
||||
if (fgets(zw, sizeof(zw), pFile) == NULL) {
|
||||
line = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
line = std::string(zw);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(pFile);
|
||||
|
||||
// Check if Hostname was empty in .ini if yes set to std_hostname
|
||||
if(hostname.length() == 0){
|
||||
hostname = std_hostname;
|
||||
/* Check if SSID is empty (mandatory parameter) */
|
||||
if (wlan_config.ssid.empty()) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "SSID empty. Device init aborted!");
|
||||
return -2;
|
||||
}
|
||||
|
||||
_hostname = new char[hostname.length() + 1];
|
||||
strcpy(_hostname, hostname.c_str());
|
||||
|
||||
_ssid = new char[ssid.length() + 1];
|
||||
strcpy(_ssid, ssid.c_str());
|
||||
|
||||
_password = new char[passphrase.length() + 1];
|
||||
strcpy(_password, passphrase.c_str());
|
||||
|
||||
if (ipaddress.length() > 0)
|
||||
{
|
||||
_ipadr = new char[ipaddress.length() + 1];
|
||||
strcpy(_ipadr, ipaddress.c_str());
|
||||
/* Check if password is empty (mandatory parameter) */
|
||||
if (wlan_config.password.empty()) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Password empty. Device init aborted!");
|
||||
return -2;
|
||||
}
|
||||
else
|
||||
_ipadr = NULL;
|
||||
|
||||
if (gw.length() > 0)
|
||||
{
|
||||
_gw = new char[gw.length() + 1];
|
||||
strcpy(_gw, gw.c_str());
|
||||
}
|
||||
else
|
||||
_gw = NULL;
|
||||
|
||||
if (netmask.length() > 0)
|
||||
{
|
||||
_netmask = new char[netmask.length() + 1];
|
||||
strcpy(_netmask, netmask.c_str());
|
||||
}
|
||||
else
|
||||
_netmask = NULL;
|
||||
|
||||
if (dns.length() > 0)
|
||||
{
|
||||
_dns = new char[dns.length() + 1];
|
||||
strcpy(_dns, dns.c_str());
|
||||
}
|
||||
else
|
||||
_dns = NULL;
|
||||
|
||||
_rssithreshold = rssithreshold;
|
||||
RSSIThreshold = rssithreshold;
|
||||
return true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool ChangeHostName(std::string fn, std::string _newhostname)
|
||||
{
|
||||
if (_newhostname == hostname)
|
||||
if (_newhostname == wlan_config.hostname)
|
||||
return false;
|
||||
|
||||
string line = "";
|
||||
std::string line = "";
|
||||
std::vector<string> splitted;
|
||||
|
||||
std::vector<string> neuesfile;
|
||||
bool found = false;
|
||||
|
||||
std::vector<string> neuesfile;
|
||||
FILE* pFile = NULL;
|
||||
|
||||
FILE* pFile;
|
||||
fn = FormatFileName(fn);
|
||||
pFile = fopen(fn.c_str(), "r");
|
||||
|
||||
ESP_LOGD(TAG, "file loaded\n");
|
||||
|
||||
if (pFile == NULL)
|
||||
if (pFile == NULL) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: Unable to open file wlan.ini (read)");
|
||||
return false;
|
||||
}
|
||||
|
||||
char zw[1024];
|
||||
fgets(zw, 1024, pFile);
|
||||
line = std::string(zw);
|
||||
ESP_LOGD(TAG, "ChangeHostName: wlan.ini opened");
|
||||
|
||||
char zw[256];
|
||||
if (fgets(zw, sizeof(zw), pFile) == NULL) {
|
||||
line = "";
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: File opened, but empty or content not readable");
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
line = std::string(zw);
|
||||
}
|
||||
|
||||
while ((line.size() > 0) || !(feof(pFile)))
|
||||
{
|
||||
ESP_LOGD(TAG, "%s", line.c_str());
|
||||
//ESP_LOGD(TAG, "ChangeHostName: line: %s", line.c_str());
|
||||
splitted = ZerlegeZeileWLAN(line, "=");
|
||||
splitted[0] = trim(splitted[0], " ");
|
||||
|
||||
if ((splitted.size() > 1) && (toUpper(splitted[0]) == "HOSTNAME")){
|
||||
if ((splitted.size() > 1) && ((toUpper(splitted[0]) == "HOSTNAME") || (toUpper(splitted[0]) == ";HOSTNAME"))){
|
||||
line = "hostname = \"" + _newhostname + "\"\n";
|
||||
found = true;
|
||||
}
|
||||
|
||||
neuesfile.push_back(line);
|
||||
|
||||
if (fgets(zw, 1024, pFile) == NULL)
|
||||
if (fgets(zw, sizeof(zw), pFile) == NULL)
|
||||
{
|
||||
line = "";
|
||||
}
|
||||
@@ -252,52 +239,64 @@ bool ChangeHostName(std::string fn, std::string _newhostname)
|
||||
|
||||
if (!found)
|
||||
{
|
||||
line = "\nhostname = \"" + _newhostname + "\"\n";
|
||||
line = "\n;++++++++++++++++++++++++++++++++++\n";
|
||||
line += "; Hostname: Name of device in network\n";
|
||||
line += "; This parameter can be configured via WebUI configuration\n";
|
||||
line += "; Default: \"watermeter\", if nothing is configured\n\n";
|
||||
line = "hostname = \"" + _newhostname + "\"\n";
|
||||
neuesfile.push_back(line);
|
||||
}
|
||||
|
||||
fclose(pFile);
|
||||
|
||||
pFile = fopen(fn.c_str(), "w+");
|
||||
if (pFile == NULL) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: Unable to open file wlan.ini (write)");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < neuesfile.size(); ++i)
|
||||
{
|
||||
ESP_LOGD(TAG, "%s", neuesfile[i].c_str());
|
||||
//ESP_LOGD(TAG, "%s", neuesfile[i].c_str());
|
||||
fputs(neuesfile[i].c_str(), pFile);
|
||||
}
|
||||
|
||||
fclose(pFile);
|
||||
|
||||
ESP_LOGD(TAG, "*** Hostname update done ***");
|
||||
ESP_LOGD(TAG, "ChangeHostName done");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
#ifdef WLAN_USE_MESH_ROAMING
|
||||
bool ChangeRSSIThreshold(std::string fn, int _newrssithreshold)
|
||||
{
|
||||
if (RSSIThreshold == _newrssithreshold)
|
||||
if (wlan_config.rssi_threshold == _newrssithreshold)
|
||||
return false;
|
||||
|
||||
string line = "";
|
||||
std::string line = "";
|
||||
std::vector<string> splitted;
|
||||
|
||||
std::vector<string> neuesfile;
|
||||
bool found = false;
|
||||
|
||||
std::vector<string> neuesfile;
|
||||
FILE* pFile = NULL;
|
||||
|
||||
FILE* pFile;
|
||||
fn = FormatFileName(fn);
|
||||
pFile = fopen(fn.c_str(), "r");
|
||||
|
||||
ESP_LOGD(TAG, "file loaded\n");
|
||||
|
||||
if (pFile == NULL)
|
||||
if (pFile == NULL) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: Unable to open file wlan.ini (read)");
|
||||
return false;
|
||||
}
|
||||
|
||||
char zw[1024];
|
||||
fgets(zw, 1024, pFile);
|
||||
line = std::string(zw);
|
||||
ESP_LOGD(TAG, "ChangeRSSIThreshold: wlan.ini opened");
|
||||
|
||||
char zw[256];
|
||||
if (fgets(zw, sizeof(zw), pFile) == NULL) {
|
||||
line = "";
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: File opened, but empty or content not readable");
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
line = std::string(zw);
|
||||
}
|
||||
|
||||
while ((line.size() > 0) || !(feof(pFile)))
|
||||
{
|
||||
@@ -305,43 +304,68 @@ bool ChangeRSSIThreshold(std::string fn, int _newrssithreshold)
|
||||
splitted = ZerlegeZeileWLAN(line, "=");
|
||||
splitted[0] = trim(splitted[0], " ");
|
||||
|
||||
if ((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSITHRESHOLD")){
|
||||
/* Workaround to eliminate line with typo "RSSIThreashold" or "rssi" if existing */
|
||||
if (((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSITHREASHOLD")) ||
|
||||
((splitted.size() > 1) && (toUpper(splitted[0]) == ";RSSITHREASHOLD")) ||
|
||||
((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSI")) ||
|
||||
((splitted.size() > 1) && (toUpper(splitted[0]) == ";RSSI"))) {
|
||||
if (fgets(zw, sizeof(zw), pFile) == NULL) {
|
||||
line = "";
|
||||
}
|
||||
else {
|
||||
line = std::string(zw);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((splitted.size() > 1) && ((toUpper(splitted[0]) == "RSSITHRESHOLD") || (toUpper(splitted[0]) == ";RSSITHRESHOLD"))) {
|
||||
line = "RSSIThreshold = " + to_string(_newrssithreshold) + "\n";
|
||||
found = true;
|
||||
}
|
||||
|
||||
|
||||
neuesfile.push_back(line);
|
||||
|
||||
if (fgets(zw, 1024, pFile) == NULL)
|
||||
{
|
||||
|
||||
if (fgets(zw, sizeof(zw), pFile) == NULL) {
|
||||
line = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
line = std::string(zw);
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
line = "RSSIThreshold = " + to_string(_newrssithreshold) + "\n";
|
||||
line = "\n;++++++++++++++++++++++++++++++++++\n";
|
||||
line += "; WIFI Roaming:\n";
|
||||
line += "; Network assisted roaming protocol is activated by default\n";
|
||||
line += "; AP / mesh system needs to support roaming protocol 802.11k/v\n";
|
||||
line += ";\n";
|
||||
line += "; Optional feature (usually not neccessary):\n";
|
||||
line += "; RSSI Threshold for client requested roaming query (RSSI < RSSIThreshold)\n";
|
||||
line += "; Note: This parameter can be configured via WebUI configuration\n";
|
||||
line += "; Default: 0 = Disable client requested roaming query\n\n";
|
||||
line += "RSSIThreshold = " + to_string(_newrssithreshold) + "\n";
|
||||
neuesfile.push_back(line);
|
||||
}
|
||||
|
||||
fclose(pFile);
|
||||
|
||||
pFile = fopen(fn.c_str(), "w+");
|
||||
if (pFile == NULL) {
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: Unable to open file wlan.ini (write)");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < neuesfile.size(); ++i)
|
||||
{
|
||||
ESP_LOGD(TAG, "%s", neuesfile[i].c_str());
|
||||
//ESP_LOGD(TAG, "%s", neuesfile[i].c_str());
|
||||
fputs(neuesfile[i].c_str(), pFile);
|
||||
}
|
||||
|
||||
fclose(pFile);
|
||||
|
||||
ESP_LOGD(TAG, "*** RSSIThreshold update done ***");
|
||||
ESP_LOGD(TAG, "ChangeRSSIThreshold done");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -5,8 +5,20 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
bool LoadWlanFromFile(std::string fn, char *&_ssid, char *&_password, char *&_hostname, char *&_ipadr, char *&_gw, char *&_netmask, char *&_dns, int &_rssithreshold);
|
||||
struct wlan_config {
|
||||
std::string ssid = "";
|
||||
std::string password = "";
|
||||
std::string hostname = "watermeter"; // Default: watermeter
|
||||
std::string ipaddress = "";
|
||||
std::string gateway = "";
|
||||
std::string netmask = "";
|
||||
std::string dns = "";
|
||||
int rssi_threshold = 0; // Default: 0 -> ROAMING disabled
|
||||
};
|
||||
extern struct wlan_config wlan_config;
|
||||
|
||||
|
||||
int LoadWlanFromFile(std::string fn);
|
||||
bool ChangeHostName(std::string fn, std::string _newhostname);
|
||||
bool ChangeRSSIThreshold(std::string fn, int _newrssithreshold);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user