Skip to content

Commit

Permalink
adaptMQTT to new Config namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
cpainchaud committed Mar 3, 2021
1 parent 56e8c47 commit eaea5ff
Show file tree
Hide file tree
Showing 15 changed files with 457 additions and 253 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ jobs:
- name: ESP32_sw
run: |
pio run --project-conf .travis.platformio.ini --environment ESP32_sw
- name: ESP32_sw_mqtt
run: |
pio run --project-conf .travis.platformio.ini --environment ESP32_sw_mqtt
- name: D1_none_enabled
Expand All @@ -39,8 +36,5 @@ jobs:
- name: D1_sw
run: |
pio run --project-conf .travis.platformio.ini --environment D1_sw
- name: D1_sw_mqtt
run: |
pio run --project-conf .travis.platformio.ini --environment D1_sw_mqtt
14 changes: 0 additions & 14 deletions .travis.platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ src_build_flags =
-D RFLINK_OTA_ENABLED
'-D RFLINK_OTA_PASSWORD="test"'
-D RFLINK_ASYNC_RECEIVER_ENABLED
-D MQTT_ENABLED
-D RFLINK_SERIAL2NET_ENABLED
-D RFLINK_SERIAL2NET_DEBUG

Expand All @@ -74,13 +73,6 @@ src_build_flags =
-D RFLINK_WIFI_ENABLED


[env:ESP32_sw_mqtt]
extends = ESP32_base
src_build_flags =
-D RFLINK_WIFI_ENABLED
-D MQTT_ENABLED


[env:D1_none_enabled]
extends = D1_base
src_build_flags =
Expand All @@ -93,12 +85,6 @@ src_build_flags =
-D RFLINK_WIFI_ENABLED


[env:D1_sw_mqtt]
extends = D1_base
src_build_flags =
-D RFLINK_WIFI_ENABLED
-D MQTT_ENABLED


;[env:mega_async]
;platform = atmelavr
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ So far, in addition of core libraries, we use:

## 7. COMPILE OPTIONS AND FLAGS
Many features are not enabled by default or can be disabled for various reasons : firmware size, compability etc etc. Here is a listing with some instructions:
### MQTT Server (disabled by default)
- Define compilation flag MQTT_ENABLED or define it in 6_WIFI_MQTT.h
- If you have not enabled WifiManager, make sure you update Wifi settings in 6_Credentials.h
### OTA (disabled by default)
There are 3 types of OTA tp update your firwware
#### Arduino/ESP's classic push over UDP (disabled by default)
Expand Down
22 changes: 10 additions & 12 deletions RFLink/10_Wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
#ifdef RFLINK_ASYNC_RECEIVER_ENABLED
#include "2_Signal.h"
#endif
#ifdef MQTT_ENABLED
#include "6_WiFi_MQTT.h"
#endif

#include "6_MQTT.h"

#ifdef RFLINK_AUTOOTA_ENABLED
#include <HTTPClient.h>
Expand Down Expand Up @@ -72,9 +71,6 @@ const char json_name_ap_mask[] = "ap_mask";


Config::ConfigItem configItems[] = {
Config::ConfigItem("host", Config::SectionId::MQTT_id, "enter a hostname here", clientParamsUpdatedCallback),
Config::ConfigItem("port", Config::SectionId::MQTT_id, 1900, clientParamsUpdatedCallback),

Config::ConfigItem(json_name_client_enabled, Config::SectionId::Wifi_id, false, clientParamsUpdatedCallback),
Config::ConfigItem(json_name_client_dhcp_enabled, Config::SectionId::Wifi_id, true, clientParamsUpdatedCallback),
Config::ConfigItem(json_name_client_ssid, Config::SectionId::Wifi_id, "My Home Wifi", clientParamsUpdatedCallback),
Expand Down Expand Up @@ -247,11 +243,6 @@ void resetClientWifi() {
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);

delay(1000);

if( WiFi.status() != WL_CONNECTED ) {
Serial.println(" failed connect to connect before timeout, it will automatically retry later.");
}
}
else {
Serial.println("WiFi Client mode will be disconnected");
Expand Down Expand Up @@ -306,6 +297,11 @@ void setup_WIFI_OFF()
#endif
}

void reconnectServices() {
if(RFLink::Mqtt::params::enabled)
RFLink::Mqtt::reconnect(1, true);
}

#ifdef ESP32
void eventHandler_WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.println("Connected to AP!");
Expand All @@ -328,12 +324,13 @@ void eventHandler_WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info)
Serial.println(info.connected.channel);

Serial.print("Auth mode: ");
Serial.println(info.connected.authmode);
Serial.println(info.connected.authmode);

}

void eventHandler_WiFiStationGotIp(WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.printf("WiFi Client has received a new IP: %s\n", WiFi.localIP().toString().c_str());
reconnectServices();
}

void eventHandler_WiFiStationLostIp(WiFiEvent_t event, WiFiEventInfo_t info) {
Expand All @@ -356,6 +353,7 @@ void eventHandler_WiFiStationConnected(const WiFiEventSoftAPModeStationConnected
}
void eventHandler_WiFiStationGotIp(const WiFiEventStationModeGotIP& evt) {
Serial.printf("WiFi Client has received a new IP: %s\n", WiFi.localIP().toString().c_str());
reconnectServices();
}
void eventHandler_WiFiStationDisconnected(const WiFiEventStationModeDisconnected& evt) {
Serial.println("WiFi Client has been disconnected");
Expand Down
1 change: 1 addition & 0 deletions RFLink/10_Wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace RFLink {

void clientParamsUpdatedCallback();
void accessPointParamsUpdatedCallback();
void reconnectServices();

void getStatusJsonString(JsonObject &output);
}
Expand Down
22 changes: 14 additions & 8 deletions RFLink/11_Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif

#include "2_Signal.h"
#include "6_MQTT.h"
#include "10_Wifi.h"
#include "12_Portal.h"

Expand Down Expand Up @@ -41,6 +42,7 @@ ConfigItem* configItemLists[] = {
#endif
&RFLink::Portal::configItems[0],
&RFLink::Signal::configItems[0],
&RFLink::Mqtt::configItems[0],
};
#define configItemListsSize (sizeof(configItemLists)/sizeof(ConfigItem*))

Expand Down Expand Up @@ -246,7 +248,7 @@ bool pushNewConfiguration(JsonObject &data, String &message) {
if(!section_variant.is<JsonObject>()) {
message += "root entry '";
message += kv.key().c_str();
message += "' is not an object, it will be ignored!\n";
message += "' is not an object, it will be ignored!\\n";
continue;
}
JsonObject && sectionObject = section_variant.as<JsonObject>();
Expand All @@ -255,7 +257,7 @@ bool pushNewConfiguration(JsonObject &data, String &message) {
if( lookupSectionID == SectionId::EOF_id ) {
message += "root entry '";
message += kv.key().c_str();
message += "' is not a valid section name, it will be ignored!\n";
message += "' is not a valid section name, it will be ignored!\\n";
continue;
}

Expand All @@ -270,7 +272,7 @@ bool pushNewConfiguration(JsonObject &data, String &message) {
message += kv.key().c_str();
message += "' has extra configuration item named '";
message += section_kv.key().c_str();
message += "' it will be ignored\n";
message += "' it will be ignored\\n";
continue;
}

Expand All @@ -282,7 +284,7 @@ bool pushNewConfiguration(JsonObject &data, String &message) {
message += kv.key().c_str();
message += "' has '";
message += section_kv.key().c_str();
message += "' with mismatched type (not string) so it will be ignored\n";
message += "' with mismatched type (not string) so it will be ignored\\n";
continue;
}
const char *str = remoteVariant.as<const char *>();
Expand All @@ -294,28 +296,32 @@ bool pushNewConfiguration(JsonObject &data, String &message) {
item->setCharValue(str);

} else if(item->typeIsLongInt()) {
if( !remoteVariant.is<signed long>() ) {

long int remote_value;
if(remoteVariant.is<unsigned long>())
remote_value = remoteVariant.as<unsigned long>();
else {
message += F("section '");
message += kv.key().c_str();
message += F("' has item '");
message += section_kv.key().c_str();
message += "' with mismatched type (not long int) so it will be ignored\n";
message += "' with mismatched type (not long int) so it will be ignored\\n";
continue;
}
auto remote_value = remoteVariant.as<signed long>();
if(remote_value == item->getLongIntValue() ) // no change!
continue;

configHasChanged = true;
callbackMgr.add(item->update_callback);
item->setLongIntValue(remote_value);

} else if(item->typeIsBool()) {
if( !remoteVariant.is<bool>() ) {
message += F("section '");
message += kv.key().c_str();
message += F("' has item '");
message += section_kv.key().c_str();
message += "' with mismatched type (not bool) so it will be ignored\n";
message += "' with mismatched type (not bool) so it will be ignored\\n";
continue;
}
auto remote_value = remoteVariant.as<bool>();
Expand Down
7 changes: 4 additions & 3 deletions RFLink/12_Portal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ void serverApiConfigPush(AsyncWebServerRequest *request, JsonVariant &json) {
response.reserve(256);

if( !Config::pushNewConfiguration(data, message) ) {
response = "{ \"success\": False, \"message\": ";
response = "{ \"success\": false, \"message\": ";
}
else {
response = "{ \"success\": True, \"message\": ";
response = "{ \"success\": true, \"message\": ";
}

if( message.length() > 0 ) {
response += message + " \"}";
response += '"';
response += message + "\"}";
} else {
response += " null }";
}
Expand Down
6 changes: 5 additions & 1 deletion RFLink/2_Signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,10 @@ void refreshParametersFromConfig(bool triggerChanges) {

// Applying changes will happen in mainLoop()
if(triggerChanges && changesDetected) {
Serial.println("Signal parameters have changed");
Serial.println("Signal parameters have changed.");
if(params::async_mode_enabled && AsyncSignalScanner::isStopped()) {
AsyncSignalScanner::startScanning();
}
}

}
Expand Down Expand Up @@ -558,6 +561,7 @@ namespace AsyncSignalScanner {

void enableAsyncReceiver() {
params::async_mode_enabled = true;
startScanning();
}

void disableAsyncReceiver() {
Expand Down
13 changes: 12 additions & 1 deletion RFLink/2_Signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ namespace RFLink {
extern unsigned long int min_pulse_len; // US
extern unsigned long int signal_end_timeout; // US
extern unsigned long int signal_repeat_time; // MS
extern unsigned long int scan_high_time; // MS
extern unsigned long int scan_high_time; // MS
}

namespace counters {

}

Expand All @@ -101,6 +104,14 @@ namespace RFLink {
void clearAllTimers();
void IRAM_ATTR RX_pin_changed_state();
void onPulseTimerTimeout();

inline bool isStopped() {
return scanningStopped;
};

inline bool isEnabled() {
return params::async_mode_enabled;
};
};

} // end of ns Signal
Expand Down
2 changes: 1 addition & 1 deletion RFLink/6_Credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

// MQTT Server
#define MQTT_SERVER "192.168.1.xxx"
#define MQTT_PORT "1883"
#define MQTT_PORT 1883
#define MQTT_ID "ESP8266-RFLink_xxx"
#define MQTT_USER "xxx"
#define MQTT_PSWD "xxx"
Expand Down
Loading

0 comments on commit eaea5ff

Please sign in to comment.