Skip to content

Commit

Permalink
[SERIAL] Enable SERIAL to X communication
Browse files Browse the repository at this point in the history
Add Serial as a communication layer so as to transmit data to/from serial
Enable discovery of BT sensors through serial
Add serial heartbeat

Change discovery principle for interval and intervalacts, keep them always discovered and deactivate adaptivescan when changed. Instead of removing their discovery when adaptive scan is activated
  • Loading branch information
1technophile committed Sep 25, 2024
1 parent 0d338cc commit 87fde65
Show file tree
Hide file tree
Showing 52 changed files with 544 additions and 415 deletions.
9 changes: 6 additions & 3 deletions docs/use/ble.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ So as to keep your white/black list persistent you can publish it with the retai

## Setting the time between BLE scans and force a scan (available with HA discovery)

If you want to change the time between readings you can change the interval by MQTT. `adaptivescan` parameter needs to be `false` for the `interval` change to be taken into account.
Example if you want the BLE to scan every 66 seconds:

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"interval":66000}'`
Expand All @@ -152,6 +151,8 @@ you can also force a scan to be done by the following command:

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"interval":0}'`

Changing the interval between scans will deactivate adaptive scanning.

::: tip
With Home Assistant, this command is directly available through MQTT auto discovery as a switch into the HASS OpenMQTTGateway device entities list.
:::
Expand Down Expand Up @@ -210,7 +211,7 @@ An overview with background information to better understand the different setti

**Active scanning:** With this scanning mode the gateway sends out requests for sensor broadcasts first, before then picking up the broadcast advertisement data. Some devices require this request before they send out all data in their broadcasts. The interval for this active scanning with request first is set by [{"intervalacts":300000}](#setting-the-time-between-active-scanning)

If adaptive scanning is set to false and you want to manually set these intervals, setting [Publishing advertisement and advanced data](#advanced-publishing-advertisement-and-advanced-data-default-false) to true will show you additional data about which of your devices require active scanning and/or continuous scanning, so that you can tune these setting to your devices and your individual requirements of their data.
Setting [Publishing advertisement and advanced data](#advanced-publishing-advertisement-and-advanced-data-default-false) to true will show you additional data about which of your devices require active scanning and/or continuous scanning, so that you can tune these setting to your devices and your individual requirements of their data.

**"cont":true** - the device requires continuous scanning. If passive ({"interval":100}) or active ({"intervalacts":100}) depends on the additional device specification.

Expand All @@ -220,10 +221,12 @@ If adaptive scanning is set to false and you want to manually set these interval

If you have passive scanning activated, but also have some devices which require active scanning, this defines the time interval between two intermittent active scans.

If you want to change the time between active scans you can change it by MQTT, `adaptivescan` parameter needs to be `false` for the `intervalacts` change to be taken into account. Example for setting the active scan interval time to every 5 minutes:
Example for setting the active scan interval time to every 5 minutes:

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"intervalacts":300000}'`

Changing the active scan interval will deactivate adaptive scanning.

::: warning Note
The active scan interval `intervalacts` can only bet set equal to or higher than the passive scan interval `interval`, as any lower value would not make any sense.
:::
Expand Down
30 changes: 17 additions & 13 deletions main/User_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@

#ifndef JSON_MSG_BUFFER
# if defined(ESP32)
# define JSON_MSG_BUFFER 816 // adjusted to minimum size covering largest Theengs device JSON properties (RuuviTag_RAWv2)
# define JSON_MSG_BUFFER 976 // adjusted to minimum size covering largest home assistant discovery messages
# elif defined(ESP8266)
# define JSON_MSG_BUFFER 512 // Json message max buffer size, don't put 768 or higher it is causing unexpected behaviour on ESP8266, certificates handling with ESP8266 is not tested
# endif
Expand Down Expand Up @@ -480,7 +480,6 @@ ss_cnt_parameters cnt_parameters_array[cnt_parameters_array_size] = {
# endif
#endif

// TODO adapt to other boards
#ifndef DEFAULT_ADJ_BRIGHTNESS
# define DEFAULT_ADJ_BRIGHTNESS 255 // Set Default RGB adjustable brightness
#endif
Expand Down Expand Up @@ -582,12 +581,22 @@ ss_cnt_parameters cnt_parameters_array[cnt_parameters_array_size] = {
#define TimeBetweenCheckingSYS 3600 // time between (s) system checkings (like updates)
#define TimeLedON 1 // time LED are ON
#define InitialMQTTConnectionTimeout 10 // time estimated (s) before the board is connected to MQTT
#define subjectSYStoMQTT "/SYStoMQTT" // system parameters
#define subjectLOGtoMQTT "/LOGtoMQTT" // log informations
#define subjectRLStoMQTT "/RLStoMQTT" // latest release information
#define subjectMQTTtoSYSset "/commands/MQTTtoSYS/config"
#define subjectMQTTtoSYSupdate "/commands/MQTTtoSYS/firmware_update"
#define TimeToResetAtStart 5000 // Time we allow the user at start for the reset command by button press
#ifndef subjectSYStoMQTT
# define subjectSYStoMQTT "/SYStoMQTT" // system parameters
#endif
#ifndef subjectLOGtoMQTT
# define subjectLOGtoMQTT "/LOGtoMQTT" // log informations
#endif
#ifndef subjectRLStoMQTT
# define subjectRLStoMQTT "/RLStoMQTT" // latest release information
#endif
#ifndef subjectMQTTtoSYSset
# define subjectMQTTtoSYSset "/commands/MQTTtoSYS/config"
#endif
#ifndef subjectMQTTtoSYSupdate
# define subjectMQTTtoSYSupdate "/commands/MQTTtoSYS/firmware_update"
#endif
#define TimeToResetAtStart 5000 // Time we allow the user at start for the reset command by button press
/*-------------------DEFINE LOG LEVEL----------------------*/
#ifndef LOG_LEVEL
# define LOG_LEVEL LOG_LEVEL_NOTICE
Expand Down Expand Up @@ -624,9 +633,7 @@ void connectMQTT();
unsigned long uptime();
bool cmpToMainTopic(const char*, const char*);
bool pub(const char*, const char*, bool);
// void pub(const char*, JsonObject&);
bool pub(const char*, const char*);
// void pub_custom_topic(const char*, JsonObject&, boolean);

#if defined(ESP32)
# include <Preferences.h>
Expand Down Expand Up @@ -690,9 +697,6 @@ bool isAduplicateSignal(uint64_t);
void storeSignalValue(uint64_t);
#endif

// Origin topics
#define subjectBTtoMQTT "/BTtoMQTT"

#define convertTemp_CtoF(c) ((c * 1.8) + 32)
#define convertTemp_FtoC(f) ((f - 32) * 5 / 9)

Expand Down
6 changes: 3 additions & 3 deletions main/ZactuatorFASTLED.ino
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ void FASTLEDLoop() {
FastLED.show();
}

boolean FASTLEDtoMQTT() {
boolean FASTLEDtoX() {
return false;
}
# if jsonReceiving
void MQTTtoFASTLED(char* topicOri, JsonObject& jsonData) {
void XtoFASTLED(const char* topicOri, JsonObject& jsonData) {
currentLEDState = GENERAL;
//trc(topicOri);
//number = (long)strtol(&datacallback[1], NULL, 16);
Expand All @@ -122,7 +122,7 @@ void MQTTtoFASTLED(char* topicOri, JsonObject& jsonData) {
# endif

# if simpleReceiving
void MQTTtoFASTLED(char* topicOri, char* datacallback) {
void XtoFASTLED(const char* topicOri, const char* datacallback) {
Log.trace(F("MQTTtoFASTLED: " CR));
currentLEDState = GENERAL;
long number = 0;
Expand Down
4 changes: 2 additions & 2 deletions main/ZactuatorONOFF.ino
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void setupONOFF() {
}

# if jsonReceiving
void MQTTtoONOFF(char* topicOri, JsonObject& ONOFFdata) {
void XtoONOFF(const char* topicOri, JsonObject& ONOFFdata) {
if (cmpToMainTopic(topicOri, subjectMQTTtoONOFF)) {
Log.trace(F("MQTTtoONOFF json data analysis" CR));
int boolSWITCHTYPE = ONOFFdata["cmd"] | 99;
Expand Down Expand Up @@ -207,7 +207,7 @@ void MQTTtoONOFF(char* topicOri, JsonObject& ONOFFdata) {
# endif

# if simpleReceiving
void MQTTtoONOFF(char* topicOri, char* datacallback) {
void XtoONOFF(const char* topicOri, const char* datacallback) {
if ((cmpToMainTopic(topicOri, subjectMQTTtoONOFF))) {
Log.trace(F("MQTTtoONOFF" CR));
char* endptr = NULL;
Expand Down
4 changes: 2 additions & 2 deletions main/ZactuatorPWM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ void PWMLoop() {
}
}

boolean PWMtoMQTT() {
boolean PWMtoX() {
return false;
}

# if jsonReceiving
void MQTTtoPWM(char* topicOri, JsonObject& jsonData) {
void XtoPWM(const char* topicOri, JsonObject& jsonData) {
if (cmpToMainTopic(topicOri, subjectMQTTtoPWMset)) {
Log.trace(F("MQTTtoPWM JSON analysis" CR));
// Parse the target value for each channel
Expand Down
2 changes: 1 addition & 1 deletion main/ZactuatorSomfy.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void setupSomfy() {
}

# if jsonReceiving
void MQTTtoSomfy(char* topicOri, JsonObject& jsonData) {
void XtoSomfy(const char* topicOri, JsonObject& jsonData) {
if (cmpToMainTopic(topicOri, subjectMQTTtoSomfy)) {
Log.trace(F("MQTTtoSomfy json data analysis" CR));
float txFrequency = jsonData["frequency"] | RFConfig.frequency;
Expand Down
4 changes: 2 additions & 2 deletions main/Zblufi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void receivingCommandTask(void* pvParameters) {
jsonBlufi.remove("target");
char jsonStr[JSON_MSG_BUFFER_MAX];
serializeJson(jsonBlufi, jsonStr);
receivingMQTT(topic, jsonStr);
receivingDATA(topic, jsonStr);
} else {
Log.notice(F("No target found in the received command using SYS target, default index and save command" CR));
if (!json.containsKey("cnt_index")) {
Expand All @@ -105,7 +105,7 @@ void receivingCommandTask(void* pvParameters) {
snprintf(topic, sizeof(topic), "%s%s%s", mqtt_topic, gateway_name, subjectMQTTtoSYSset);
char jsonStr[JSON_MSG_BUFFER_MAX];
serializeJson(jsonBlufi, jsonStr);
receivingMQTT(topic, jsonStr);
receivingDATA(topic, jsonStr);
}
}

Expand Down
2 changes: 1 addition & 1 deletion main/ZboardM5.ino
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void loopM5() {
previousLogLevel = currentLogLevel;
}

void MQTTtoM5(char* topicOri, JsonObject& M5data) { // json object decoding
void XtoM5(const char* topicOri, JsonObject& M5data) { // json object decoding
if (cmpToMainTopic(topicOri, subjectMQTTtoM5set)) {
Log.trace(F("MQTTtoM5 json set" CR));
// Log display set between M5 lcd (true) and serial monitor (false)
Expand Down
5 changes: 3 additions & 2 deletions main/ZcommonRF.ino
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ String stateRFMeasures() {
# endif
}
# endif
pub(subjectcommonRFtoMQTT, RFdata);
RFdata["origin"] = subjectcommonRFtoMQTT;
enqueueJsonObject(RFdata);

String output;
serializeJson(RFdata, output);
Expand Down Expand Up @@ -300,7 +301,7 @@ void RFConfig_load() {
# endif
}

void MQTTtoRFset(char* topicOri, JsonObject& RFdata) {
void XtoRFset(const char* topicOri, JsonObject& RFdata) {
if (cmpToMainTopic(topicOri, subjectMQTTtoRFset)) {
Log.trace(F("MQTTtoRF json set" CR));

Expand Down
4 changes: 2 additions & 2 deletions main/ZdisplaySSD1306.ino
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Handler for mqtt commands sent to the module
- log-oled: boolean
Enable / Disable display of log messages on display
*/
void MQTTtoSSD1306(char* topicOri, JsonObject& SSD1306data) { // json object decoding
void XtoSSD1306(const char* topicOri, JsonObject& SSD1306data) { // json object decoding
bool success = false;
if (cmpToMainTopic(topicOri, subjectMQTTtoSSD1306set)) {
Log.trace(F("MQTTtoSSD1306 json set" CR));
Expand Down Expand Up @@ -222,7 +222,7 @@ void MQTTtoSSD1306(char* topicOri, JsonObject& SSD1306data) { // json object dec
if (success) {
stateSSD1306Display();
} else {
Log.error(F("[ SSD1306 ] MQTTtoSSD1306 Fail json" CR), SSD1306data);
Log.error(F("[ SSD1306 ] XtoSSD1306 Fail json" CR), SSD1306data);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions main/Zgateway2G.ino
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void signalStrengthAnalysis() {
}
}

bool _2GtoMQTT() {
bool _2GtoX() {
// Get the memory locations of unread SMS messages.
unreadSMSNum = A6l.getUnreadSMSLocs(unreadSMSLocs, 512);
Log.trace(F("Creating SMS buffer" CR));
Expand All @@ -96,7 +96,7 @@ bool _2GtoMQTT() {
return false;
}
# if simpleReceiving
void MQTTto2G(char* topicOri, char* datacallback) {
void Xto2G(const char* topicOri, const char* datacallback) {
String data = datacallback;
String topic = topicOri;

Expand Down Expand Up @@ -127,7 +127,7 @@ void MQTTto2G(char* topicOri, char* datacallback) {
# endif

# if jsonReceiving
void MQTTto2G(char* topicOri, JsonObject& SMSdata) {
void Xto2G(const char* topicOri, JsonObject& SMSdata) {
if (cmpToMainTopic(topicOri, subjectMQTTto2G)) {
const char* sms = SMSdata["message"];
const char* phone = SMSdata["phone"];
Expand Down
Loading

0 comments on commit 87fde65

Please sign in to comment.