mqtt client id is now a mac address, fixing issue of not being able to have more than one tag connected at the time. Logo and html is updated. Tag will now correctly go into and stay in provision for 2 minutes before reset if it wasn't able to connect to wifi

This commit is contained in:
Miro Zmrzli 2024-07-22 13:52:16 -07:00
parent aa93a71fa1
commit ee77bb4e78
8 changed files with 52 additions and 9 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 62 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View File

@ -389,15 +389,15 @@ input {
<input type="button" onClick="onButton1()" name="button3" id="button1" value="Button1">
<input type="button" onClick="onButton2()" name="button4" id="button2" value="Button2">
</div>
<h2>Timmah's special WiFi configuration and stuff</h2>
<h2>Welcome to WellHub Wifi intialization</h2>
<p><img src="logo.png" alt=""/><br>
</p>
<div id="msg_ok">
<h3>Timmah has ben successfully configured and it is ready for normal use.<br>You can now close this window.</h3>
<h3>WellHub has ben successfully configured and it is ready for normal use.<br>You can now close this window.</h3>
</div>
<div id="msg_error">
<h3>Timmah was not able to connect to selected WiFi. Please check the spelling and password.</h3>
<h3>WellHub was not able to connect to selected WiFi. Please check the spelling and password.</h3>
</div>
<div id="wait_scan">
<h3>Please wait, scanning local WiFi...</h3>

View File

@ -77,6 +77,8 @@ void App::init()
needs_provision = false;
}
else
m_wifi->pause(true);
}
if(needs_provision)

View File

@ -46,18 +46,21 @@ void MqttService::task()
uint8_t len = 0;
if(m_app_if.getBuffer()->getBlock(buffer, len))
{
while (!m_mqtt_client->connected())
{
ESP_LOGI(TAG, "connecting to mqtt broker, dev id '%s'...", SETTINGS.mqtt.device_id);
if (m_mqtt_client->connect("Esp32 client", SETTINGS.mqtt.device_id, NULL))
{
ESP_LOGI(TAG, "connected");
char top[64];
uint8_t mac[6];
WiFi.macAddress(mac);
char top[64];
while (!m_mqtt_client->connected())
{
sprintf(top, "wh_%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
ESP_LOGI(TAG, "connecting to mqtt broker, dev id '%s'...", SETTINGS.mqtt.device_id);
if (m_mqtt_client->connect(top, SETTINGS.mqtt.device_id, NULL))
{
ESP_LOGI(TAG, "connected");
sprintf(top, "/%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
ESP_LOGI(TAG, "Subscribing to %s", top);
if(m_mqtt_client->subscribe(top))
ESP_LOGI(TAG, "subscribed");

View File

@ -42,10 +42,12 @@ void ProvisionSoftAP::wifiEvent(arduino_event_id_t event, arduino_event_info_t i
switch (event)
{
case ARDUINO_EVENT_WIFI_AP_STACONNECTED:
esp_timer_stop(m_timer);
ESP_LOGI(TAG, "station " MACSTR " joined, AID=%d", MAC2STR(info.wifi_ap_staconnected.mac), info.wifi_ap_staconnected.aid);
break;
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:
ESP_ERROR_CHECK(esp_timer_start_periodic(m_timer, INACTIVE_TIMER));
ESP_LOGI(TAG, "station " MACSTR " left", MAC2STR(info.wifi_ap_stadisconnected.mac));
m_webSocket->closeAll();
break;
@ -175,12 +177,27 @@ void ProvisionSoftAP::start(const char * ssid, const char * password)
}
}
void ProvisionSoftAP::timerCallback(void* arg)
{
esp_restart();
}
void ProvisionSoftAP::start()
{
uint8_t mac[6];
WiFi.macAddress(mac);
char ssid[33];
esp_timer_create_args_t timer;
timer.arg = this;
timer.callback = &timerCallback;
timer.dispatch_method = ESP_TIMER_TASK;
timer.skip_unhandled_events = true;
timer.name = "provision reset timer";
ESP_ERROR_CHECK(esp_timer_create(&timer, &m_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(m_timer, INACTIVE_TIMER)); // 5 min
sprintf(ssid, "Wellhub-%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
start(ssid, "12345678");
}

View File

@ -11,9 +11,12 @@
#include <ESPAsyncWebServer.h>
#include <DNSServer.h>
#include <list>
#include <esp_timer.h>
#include "ReaderWriter.h"
#include "Settings.h"
#define INACTIVE_TIMER (2*60*1000*1000)
class ProvisionSoftAP
{
private:
@ -26,6 +29,9 @@ private:
ReaderWriter * m_rwriter = nullptr;
esp_timer_handle_t m_timer;
static void timerCallback(void* arg);
private:
char m_ssid[32] = {0};
char m_pwd[32] = {0};

View File

@ -64,11 +64,23 @@ void Wifi::task()
{
m_wifi_status = WIFI_STATUS::PENDING;
m_wifi_status = startConnecting();
delay(500);
while(m_pause)
{
delay(100);
}
}
delay(15000);
}
}
void Wifi::pause(bool pause)
{
m_pause = pause;
}
/// @brief Connect to provisioned wifi
/// @param index index of wifi in settings
/// @return WIFI_STATUS::CONNECTED/NOT_CONENCTED connected

View File

@ -36,6 +36,7 @@ protected:
WIFI_STATUS startConnecting();
WIFI_STATUS connectTo(int index);
int scan();
bool m_pause = false;
public:
Wifi();
@ -45,6 +46,8 @@ protected:
public:
int start();
void pause(bool pause);
WIFI_STATUS status() { return m_wifi_status; }
WIFI_STATUS waitForConnection();
};