Skip to content

Commit

Permalink
Merge branch 'tardis' into tucar/feature/MQTT-command-throttling
Browse files Browse the repository at this point in the history
  • Loading branch information
atroncoso-tucar authored Aug 27, 2024
2 parents 4e47ec2 + b4a7551 commit 1e00c93
Show file tree
Hide file tree
Showing 42 changed files with 1,758 additions and 61 deletions.
42 changes: 35 additions & 7 deletions vehicle/OVMS.V3/components/esp32wifi/src/esp32wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ esp32wifi::esp32wifi(const char* name)
m_sta_reconnect = 0;
m_sta_connected = false;
m_sta_rssi = -1270;
m_good_signal = false;
memset(&m_wifi_ap_cfg,0,sizeof(m_wifi_ap_cfg));
memset(&m_wifi_sta_cfg,0,sizeof(m_wifi_sta_cfg));
memset(&m_mac_ap,0,sizeof(m_mac_ap));
Expand All @@ -399,6 +400,9 @@ esp32wifi::esp32wifi(const char* name)
MyEvents.RegisterEvent(TAG,"system.wifi.ap.sta.connected",std::bind(&esp32wifi::EventWifiApUpdate, this, _1, _2));
MyEvents.RegisterEvent(TAG,"system.wifi.ap.sta.disconnected",std::bind(&esp32wifi::EventWifiApUpdate, this, _1, _2));
MyEvents.RegisterEvent(TAG,"system.shuttingdown",std::bind(&esp32wifi::EventSystemShuttingDown, this, _1, _2));
MyEvents.RegisterEvent(TAG,"config.mounted", std::bind(&esp32wifi::ConfigChanged, this, _1, _2));
MyEvents.RegisterEvent(TAG,"config.changed", std::bind(&esp32wifi::ConfigChanged, this, _1, _2));
ConfigChanged("config.mounted", NULL);
}

esp32wifi::~esp32wifi()
Expand Down Expand Up @@ -996,13 +1000,19 @@ void esp32wifi::UpdateNetMetrics()
m_sta_rssi = -1270;
}

StdMetrics.ms_m_net_wifi_network->SetValue(GetSSID());
StdMetrics.ms_m_net_wifi_sq->SetValue((float)m_sta_rssi/10, dbm);
if (StdMetrics.ms_m_net_type->AsString() == "wifi")
{
StdMetrics.ms_m_net_provider->SetValue(GetSSID());
StdMetrics.ms_m_net_sq->SetValue((int)(m_sta_rssi-5)/10, dbm);
}
float current_dbm = (float)m_sta_rssi / 10;
StdMetrics.ms_m_net_wifi_network->SetValue(GetSSID());
StdMetrics.ms_m_net_wifi_sq->SetValue(current_dbm, dbm);
if (StdMetrics.ms_m_net_type->AsString() == "wifi")
{
StdMetrics.ms_m_net_provider->SetValue(GetSSID());
StdMetrics.ms_m_net_sq->SetValue((int)(m_sta_rssi-5)/10, dbm);
if (m_good_signal && current_dbm < m_bad_dbm)
m_good_signal = false;
if (!m_good_signal && current_dbm > m_good_dbm)
m_good_signal = true;
StdMetrics.ms_m_net_good_sq->SetValue(m_good_signal);
}
}

void esp32wifi::EventWifiGotIp(std::string event, void* data)
Expand All @@ -1021,6 +1031,13 @@ void esp32wifi::EventWifiGotIp(std::string event, void* data)
IP2STR(&m_ip_info_sta.ip), IP2STR(&m_ip_info_sta.netmask), IP2STR(&m_ip_info_sta.gw));
}

bool esp32wifi::WifiHasIp()
{
char numstr[150];
sprintf(numstr, IPSTR, IP2STR(&m_ip_info_sta.ip));
return strcmp(numstr, "0.0.0.0") == 1;
}

void esp32wifi::EventWifiLostIp(std::string event, void* data)
{
memset(&m_ip_info_sta,0,sizeof(m_ip_info_sta));
Expand Down Expand Up @@ -1393,6 +1410,17 @@ void esp32wifi::OutputStatus(int verbosity, OvmsWriter* writer)
}
}

void esp32wifi::ConfigChanged(std::string event, void* data)
{
OvmsConfigParam* param = (OvmsConfigParam*)data;
if (event == "config.mounted" || !param || param->GetName() == "network")
{
// Network config has been changed, apply:
m_good_dbm = MyConfig.GetParamValueFloat("network", "wifi.sq.good", -87);
m_bad_dbm = MyConfig.GetParamValueFloat("network", "wifi.sq.bad", -89);
}
}

void esp32wifi::StartDhcpClient()
{
if (m_mode == ESP32WIFI_MODE_CLIENT || m_mode == ESP32WIFI_MODE_APCLIENT)
Expand Down
5 changes: 5 additions & 0 deletions vehicle/OVMS.V3/components/esp32wifi/src/esp32wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class esp32wifi : public pcp, public InternalRamAllocated
void StartDhcpClient();
void SetSTAWifiIP(std::string ip="", std::string sn="", std::string gw="");
void SetAPWifiBW();
bool WifiHasIp();

public:
void EventWifiStaState(std::string event, void* data);
Expand All @@ -100,6 +101,7 @@ class esp32wifi : public pcp, public InternalRamAllocated
void EventWifiScanDone(std::string event, void* data);
void EventSystemShuttingDown(std::string event, void* data);
void OutputStatus(int verbosity, OvmsWriter* writer);
void ConfigChanged(std::string event, void *data);

protected:
bool m_poweredup;
Expand Down Expand Up @@ -132,6 +134,9 @@ class esp32wifi : public pcp, public InternalRamAllocated
uint32_t m_sta_reconnect;
wifi_ap_record_t m_sta_ap_info;
int m_sta_rssi; // smoothed RSSI [dBm/10]
float m_good_dbm;
float m_bad_dbm;
bool m_good_signal;
};

#endif //#ifndef __ESP32WIFI_H__
30 changes: 29 additions & 1 deletion vehicle/OVMS.V3/components/ovms_cellular/src/ovms_cellular.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ static const char *TAG = "cellular";
#include "ovms_events.h"
#include "ovms_notify.h"
#include "ovms_boot.h"
#include "ovms_config.h"

////////////////////////////////////////////////////////////////////////////////
// Global convenience variables
Expand Down Expand Up @@ -292,6 +293,7 @@ modem::modem(const char* name, uart_port_t uartnum, int baud, int rxpin, int txp
for (size_t k=0; k<CELLULAR_NETREG_COUNT; k++) { m_netreg_d[k] = Unknown; }
m_provider = "";
m_sq = 99; // Unknown
m_good_signal = false;
m_powermode = Off;
m_pincode_required = false;
m_err_uart_fifo_ovf = 0;
Expand All @@ -311,8 +313,11 @@ modem::modem(const char* name, uart_port_t uartnum, int baud, int rxpin, int txp

using std::placeholders::_1;
using std::placeholders::_2;
MyEvents.RegisterEvent(TAG,"ticker.1", std::bind(&modem::Ticker, this, _1, _2));
MyEvents.RegisterEvent(TAG, "ticker.1", std::bind(&modem::Ticker, this, _1, _2));
MyEvents.RegisterEvent(TAG, "system.shuttingdown", std::bind(&modem::EventListener, this, _1, _2));
MyEvents.RegisterEvent(TAG, "config.mounted", std::bind(&modem::ConfigChanged, this, _1, _2));
MyEvents.RegisterEvent(TAG, "config.changed", std::bind(&modem::ConfigChanged, this, _1, _2));
ConfigChanged("config.mounted", NULL);
}

modem::~modem()
Expand Down Expand Up @@ -358,6 +363,11 @@ void modem::SetPowerMode(PowerMode powermode)
}
}

bool modem::ModemIsNetMode()
{
return m_state1 == NetMode;
}

void modem::AutoInit()
{
if (MyConfig.GetParamValueBool("auto", "modem", false))
Expand Down Expand Up @@ -1508,6 +1518,17 @@ void modem::EventListener(std::string event, void* data)
}
}

void modem::ConfigChanged(std::string event, void* data)
{
OvmsConfigParam* param = (OvmsConfigParam*)data;
if (event == "config.mounted" || !param || param->GetName() == "network")
{
// Network config has been changed, apply:
m_good_dbm = MyConfig.GetParamValueFloat("network", "modem.sq.good", -95);
m_bad_dbm = MyConfig.GetParamValueFloat("network", "modem.sq.bad", -93);
}
}

void modem::IncomingMuxData(GsmMuxChannel* channel)
{
// The MUX has indicated there is data on the specified channel
Expand Down Expand Up @@ -1616,10 +1637,16 @@ void modem::SetSignalQuality(int newsq)
{
m_sq = newsq;
ESP_LOGD(TAG, "Signal Quality is: %d (%d dBm)", m_sq, UnitConvert(sq, dbm, m_sq));
float current_dbm = UnitConvert(sq, dbm, m_sq);
StdMetrics.ms_m_net_mdm_sq->SetValue(m_sq, sq);
if (StdMetrics.ms_m_net_type->AsString() == "modem")
{
StdMetrics.ms_m_net_sq->SetValue(m_sq, sq);
if (m_good_signal && current_dbm < m_bad_dbm)
m_good_signal = false;
if (!m_good_signal && current_dbm > m_good_dbm)
m_good_signal = true;
StdMetrics.ms_m_net_good_sq->SetValue(m_good_signal);
}
}
}
Expand All @@ -1634,6 +1661,7 @@ void modem::ClearNetMetrics()
StdMetrics.ms_m_net_mdm_network->Clear();

m_sq = 99;
m_good_signal = false;
StdMetrics.ms_m_net_mdm_sq->Clear();

if (StdMetrics.ms_m_net_type->AsString() == "modem")
Expand Down
5 changes: 5 additions & 0 deletions vehicle/OVMS.V3/components/ovms_cellular/src/ovms_cellular.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class modem : public pcp, public InternalRamAllocated
int m_txpin;
int m_pwregpio;
int m_dtregpio;
float m_good_dbm;
float m_bad_dbm;
bool m_good_signal;

public:
typedef enum
Expand Down Expand Up @@ -199,6 +202,7 @@ class modem : public pcp, public InternalRamAllocated

public:
// High level API functions
bool ModemIsNetMode();
void StartTask();
void StopTask();
bool StartNMEA(bool force=false);
Expand All @@ -211,6 +215,7 @@ class modem : public pcp, public InternalRamAllocated
void Task();
void Ticker(std::string event, void* data);
void EventListener(std::string event, void* data);
void ConfigChanged(std::string event, void *data);
void IncomingMuxData(GsmMuxChannel* channel);
void SendSetState1(modem_state1_t newstate);
bool IsStarted();
Expand Down
49 changes: 41 additions & 8 deletions vehicle/OVMS.V3/components/ovms_server_v3/src/ovms_server_v3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ static void OvmsServerV3MongooseCallback(struct mg_connection *nc, int ev, void
StandardMetrics.ms_s_v3_peers->SetValue(0);
MyOvmsServerV3->SetStatus("Error: Connection failed", true, OvmsServerV3::WaitReconnect);
MyOvmsServerV3->m_connretry = 60;
MyOvmsServerV3->m_connection_counter = 0;
}
}
}
Expand All @@ -108,6 +109,7 @@ static void OvmsServerV3MongooseCallback(struct mg_connection *nc, int ev, void
{
MyOvmsServerV3->Disconnect();
MyOvmsServerV3->m_connretry = 60;
MyOvmsServerV3->m_connection_counter = 0;
}
}
else
Expand Down Expand Up @@ -178,6 +180,7 @@ static void OvmsServerV3MongooseCallback(struct mg_connection *nc, int ev, void
{
MyOvmsServerV3->Disconnect();
MyOvmsServerV3->m_connretry = 60;
MyOvmsServerV3->m_connection_counter = 0;
}
break;
default:
Expand All @@ -196,6 +199,7 @@ OvmsServerV3::OvmsServerV3(const char* name)

SetStatus("Server has been started", false, WaitNetwork);
m_connretry = 0;
m_connection_counter = 0;
m_mgconn = NULL;
m_sendall = false;
m_lasttx = 0;
Expand All @@ -215,6 +219,7 @@ OvmsServerV3::OvmsServerV3(const char* name)
m_notify_data_waitcomp = 0;
m_notify_data_waittype = NULL;
m_notify_data_waitentry = NULL;
m_connection_available = false;
m_accept_command = 0;

ESP_LOGI(TAG, "OVMS Server v3 running");
Expand Down Expand Up @@ -631,6 +636,13 @@ void OvmsServerV3::CountClients()

void OvmsServerV3::Connect()
{
if (!m_connection_available)
{
ESP_LOGE(TAG, "No connection available, waiting for network");
m_connretry = 10;
m_connection_counter = 0;
return;
}
m_msgid = 1;
m_vehicleid = MyConfig.GetParamValue("vehicle", "id");
m_server = MyConfig.GetParamValue("server.v3", "server");
Expand Down Expand Up @@ -680,6 +692,7 @@ void OvmsServerV3::Connect()
{
SetStatus("Error: Parameter server.v3/server must be defined", true, WaitReconnect);
m_connretry = 20; // Try again in 20 seconds...
m_connection_counter = 0;
return;
}

Expand All @@ -705,6 +718,7 @@ void OvmsServerV3::Connect()
{
ESP_LOGE(TAG, "mg_connect(%s) failed: %s", address.c_str(), err);
m_connretry = 20; // Try again in 20 seconds...
m_connection_counter = 0;
return;
}
return;
Expand Down Expand Up @@ -861,6 +875,7 @@ void OvmsServerV3::NetReconfigured(std::string event, void* data)
ESP_LOGI(TAG, "Network was reconfigured: disconnect, and reconnect in 10 seconds");
Disconnect();
m_connretry = 10;
m_connection_counter = 0;
}

void OvmsServerV3::NetmanInit(std::string event, void* data)
Expand All @@ -883,21 +898,39 @@ void OvmsServerV3::NetmanStop(std::string event, void* data)

void OvmsServerV3::Ticker1(std::string event, void* data)
{
m_connection_available = StdMetrics.ms_m_net_connected->AsBool() &&
StdMetrics.ms_m_net_ip->AsBool() &&
StdMetrics.ms_m_net_good_sq->AsBool();
if (!m_connection_available && m_mgconn)
{
Disconnect();
m_connretry = 10;
}

if (!m_connection_available) m_connection_counter = 0;
else if (m_connection_counter < 10)
{
m_connection_counter++;
if (m_connretry == 0 && m_connection_counter == 10)
{
if (m_mgconn) Disconnect(); // Disconnect first (timeout)
Connect(); // Kick off the connection
return;
}
}

if (m_accept_command > 0)
{
m_accept_command--;
}
if (m_connretry > 0)
{
if (MyNetManager.m_connected_any)
m_connretry--;
if (m_connretry == 0 && m_connection_counter == 10)
{
m_connretry--;
if (m_connretry == 0)
{
if (m_mgconn) Disconnect(); // Disconnect first (timeout)
Connect(); // Kick off the connection
return;
}
if (m_mgconn) Disconnect(); // Disconnect first (timeout)
Connect(); // Kick off the connection
return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class OvmsServerV3 : public OvmsServer
struct mg_connection *m_mgconn;
OvmsMutex m_mgconn_mutex;
int m_connretry;
int m_connection_counter;
bool m_sendall;
int m_accept_command;
int m_msgid;
Expand All @@ -109,6 +110,7 @@ class OvmsServerV3 : public OvmsServer
int m_updatetime_charging;
int m_updatetime_sendall;

bool m_connection_available;
bool m_notify_info_pending;
bool m_notify_error_pending;
bool m_notify_alert_pending;
Expand Down
Binary file modified vehicle/OVMS.V3/components/ovms_webserver/assets/charts.js.gz
Binary file not shown.
Loading

0 comments on commit 1e00c93

Please sign in to comment.