Lean on Arduino log library instead of custom logging methods (#546)
* Lean on Arduino log library instead of custom logging methods
This commit is contained in:
1 parent
824346b19c
commit
f3c9d6dd90
28 files changed
+557
-745
No files matched your search
+3
-5
@@ -109,7 +109,7 @@ char gateway_name[parameters_size * 2] = Gateway_Name;
|
||||
//#define valueAsASubject true
|
||||
|
||||
/*-------------DEFINE THE MODULES YOU WANT BELOW----------------*/
|
||||
//Addons and module management, comment the Z line
|
||||
//Addons and module management, uncomment the Z line corresponding to the module you want to use
|
||||
|
||||
//#define ZgatewayRF "RF" //ESP8266, Arduino, ESP32
|
||||
//#define ZgatewayIR "IR" //ESP8266, Arduino, Sonoff RF Bridge
|
||||
@@ -240,9 +240,7 @@ char gateway_name[parameters_size * 2] = Gateway_Name;
|
||||
#define subjectSYStoMQTT "/SYStoMQTT"
|
||||
#define subjectMQTTtoSYSset "/commands/MQTTtoSYS/config"
|
||||
|
||||
//#define subjectTRACEtoMQTT "OpenMQTTGateway/log" //uncomment if you want to see traces on OpenMQTTGateway/log topic
|
||||
|
||||
/*-------------------ACTIVATE TRACES----------------------*/
|
||||
#define TRACE 1 // commented = trace off, uncommented = trace on
|
||||
/*-------------------DEFINE LOG LEVEL----------------------*/
|
||||
#define LOG_LEVEL LOG_LEVEL_VERBOSE
|
||||
|
||||
#endif
|
||||
+18
-21
@@ -44,11 +44,9 @@ CRGBPalette16 gPal;
|
||||
|
||||
void setupFASTLED()
|
||||
{
|
||||
trc(F("FASTLED_DATA_PIN "));
|
||||
trc(String(FASTLED_DATA_PIN));
|
||||
trc(F("FASTLED_NUM_LEDS "));
|
||||
trc(String(FASTLED_NUM_LEDS));
|
||||
trc(F("ZactuatorFASTLED setup done "));
|
||||
Log.notice(F("FASTLED_DATA_PIN: %d" CR), FASTLED_DATA_PIN);
|
||||
Log.notice(F("FASTLED_NUM_LEDS: %d" CR), FASTLED_NUM_LEDS);
|
||||
Log.trace(F("ZactuatorFASTLED setup done " CR));
|
||||
FastLED.addLeds<FASTLED_TYPE, FASTLED_DATA_PIN>(leds, FASTLED_NUM_LEDS);
|
||||
}
|
||||
|
||||
@@ -119,44 +117,43 @@ boolean FASTLEDtoMQTT()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
void MQTTtoFASTLEDJSON(char *topicOri, JsonObject &jsonData)
|
||||
#ifdef jsonReceiving
|
||||
void MQTTtoFASTLED(char *topicOri, JsonObject &jsonData)
|
||||
{
|
||||
trc(F("MQTTtoFASTLEDJSON: "));
|
||||
currentLEDState = GENERAL;
|
||||
trc(topicOri);
|
||||
//trc(topicOri);
|
||||
//number = (long)strtol(&datacallback[1], NULL, 16);
|
||||
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTtoFASTLEDsetled))
|
||||
{
|
||||
trc(F("JSON parsed"));
|
||||
Log.trace(F("MQTTtoFASTLED JSON analysis" CR));
|
||||
int ledNr = jsonData["led"];
|
||||
trc(F("led"));
|
||||
trc(ledNr);
|
||||
Log.notice(F("Led numero: %d" CR), ledNr);
|
||||
const char *color = jsonData["hex"];
|
||||
trc(F("hex"));
|
||||
Log.notice(F("Color hex: %s" CR), color);
|
||||
|
||||
long number = (long)strtol(color, NULL, 16);
|
||||
trc(number);
|
||||
bool blink = jsonData["blink"];
|
||||
if (ledNr <= FASTLED_NUM_LEDS)
|
||||
{
|
||||
trc(F("blink"));
|
||||
trc(blink);
|
||||
Log.notice(F("Blink: %d" CR), blink);
|
||||
blinkLED[ledNr] = blink;
|
||||
leds[ledNr] = number;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef simpleReceiving
|
||||
void MQTTtoFASTLED(char *topicOri, char *datacallback)
|
||||
{
|
||||
trc(F("MQTTtoFASTLED: "));
|
||||
Log.trace(F("MQTTtoFASTLED: " CR));
|
||||
currentLEDState = GENERAL;
|
||||
long number = 0;
|
||||
trc(topicOri);
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTtoFASTLED))
|
||||
{
|
||||
number = (long)strtol(&datacallback[1], NULL, 16);
|
||||
trc(number);
|
||||
Log.notice(F("Number: %l" CR), number);
|
||||
for (int i = 0; i < FASTLED_NUM_LEDS; i++)
|
||||
{
|
||||
leds[i] = number;
|
||||
@@ -166,14 +163,14 @@ void MQTTtoFASTLED(char *topicOri, char *datacallback)
|
||||
else if (cmpToMainTopic(topicOri, subjectMQTTtoFASTLEDsetbrightness))
|
||||
{
|
||||
number = (long)strtol(&datacallback[1], NULL, 16);
|
||||
trc(number);
|
||||
Log.notice(F("Number: %l" CR), number);
|
||||
FastLED.setBrightness(number);
|
||||
FastLED.show();
|
||||
}
|
||||
else if (cmpToMainTopic(topicOri, subjectMQTTtoFASTLEDsetanimation))
|
||||
{
|
||||
String payload = datacallback;
|
||||
trc(payload);
|
||||
Log.notice(F("Datacallback: %s" CR), datacallback);
|
||||
if (strstr(datacallback, "fire") != NULL)
|
||||
{
|
||||
currentLEDState = FIRE;
|
||||
@@ -196,7 +193,7 @@ void MQTTtoFASTLED(char *topicOri, char *datacallback)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
// Fire2012 by Mark Kriegsman, July 2012
|
||||
// as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
|
||||
////
|
||||
|
||||
@@ -35,15 +35,13 @@ void MQTTtoONOFF(char *topicOri, JsonObject &ONOFFdata)
|
||||
{
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTtoONOFF))
|
||||
{
|
||||
trc(F("MQTTtoONOFF json data analysis"));
|
||||
Log.trace(F("MQTTtoONOFF json data analysis" CR));
|
||||
int boolSWITCHTYPE = ONOFFdata["cmd"] | 99;
|
||||
int pin = ONOFFdata["pin"] | ACTUATOR_ONOFF_PIN;
|
||||
if (boolSWITCHTYPE != 99)
|
||||
{
|
||||
trc(F("MQTTtoONOFF boolSWITCHTYPE ok"));
|
||||
trc(boolSWITCHTYPE);
|
||||
trc(F("pin number"));
|
||||
trc(pin);
|
||||
Log.notice(F("MQTTtoONOFF boolSWITCHTYPE ok: %d" CR), boolSWITCHTYPE);
|
||||
Log.notice(F("Pin number: %d" CR), pin);
|
||||
pinMode(pin, OUTPUT);
|
||||
digitalWrite(pin, boolSWITCHTYPE);
|
||||
// we acknowledge the sending by publishing the value to an acknowledgement topic
|
||||
@@ -51,7 +49,7 @@ void MQTTtoONOFF(char *topicOri, JsonObject &ONOFFdata)
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("MQTTtoONOFF failed json read"));
|
||||
Log.error(F("MQTTtoONOFF failed json read" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,10 +61,9 @@ void MQTTtoONOFF(char *topicOri, char *datacallback)
|
||||
if ((cmpToMainTopic(topicOri, subjectMQTTtoONOFF)))
|
||||
{
|
||||
|
||||
trc(F("MQTTtoONOFF"));
|
||||
Log.trace(F("MQTTtoONOFF" CR));
|
||||
int pin = strtol(datacallback, NULL, 10); // we will not be able to pass values > 4294967295
|
||||
trc(F("pin number"));
|
||||
trc(pin);
|
||||
Log.notice(F("Pin number: %d" CR), pin);
|
||||
pinMode(pin, OUTPUT);
|
||||
|
||||
bool ON = false;
|
||||
|
||||
+25
-33
@@ -41,25 +41,21 @@ SMSmessage sms;
|
||||
|
||||
void setup2G()
|
||||
{
|
||||
|
||||
trc(F("_2G_TX_PIN "));
|
||||
trc(_2G_TX_PIN);
|
||||
trc(F("_2G_RX_PIN "));
|
||||
trc(_2G_RX_PIN);
|
||||
Log.notice(F("_2G_TX_PIN: %d " CR), _2G_TX_PIN);
|
||||
Log.notice(F("_2G_RX_PIN: %d " CR), _2G_RX_PIN);
|
||||
setupGSM(false);
|
||||
trc(F("Zgateway2G setup done "));
|
||||
Log.trace(F("Zgateway2G setup done " CR));
|
||||
}
|
||||
|
||||
void setupGSM(bool deleteSMS)
|
||||
{
|
||||
trc(F("Init 2G module: "));
|
||||
trc(_2G_PWR_PIN);
|
||||
Log.trace(F("Init 2G module: %d" CR), _2G_PWR_PIN);
|
||||
delay(1000);
|
||||
// Power-cycle the module to reset it.
|
||||
A6l.powerCycle(_2G_PWR_PIN);
|
||||
trc(F("waiting for network connection"));
|
||||
Log.notice(F("waiting for network connection at bd: %d" CR), _2G_MODULE_BAUDRATE);
|
||||
A6l.blockUntilReady(_2G_MODULE_BAUDRATE);
|
||||
trc(F("A6/A7 gsm ready"));
|
||||
Log.notice(F("A6/A7 gsm ready" CR));
|
||||
signalStrengthAnalysis();
|
||||
delay(1000);
|
||||
// deleting all sms
|
||||
@@ -67,11 +63,11 @@ void setupGSM(bool deleteSMS)
|
||||
{
|
||||
if (A6l.deleteSMS(1, 4) == A6_OK)
|
||||
{
|
||||
trc(F("delete SMS OK"));
|
||||
Log.notice(F("delete SMS OK" CR));
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("delete SMS KO"));
|
||||
Log.error(F("delete SMS KO" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,11 +76,10 @@ void signalStrengthAnalysis()
|
||||
{
|
||||
int signalStrength = 0;
|
||||
signalStrength = A6l.getSignalStrength();
|
||||
trc(F("Signal strength: "));
|
||||
trc(signalStrength);
|
||||
Log.trace(F("Signal strength: %d" CR), signalStrength);
|
||||
if (signalStrength < _2G_MIN_SIGNAL || signalStrength > _2G_MAX_SIGNAL)
|
||||
{
|
||||
trc(F("Signal too low restart the module"));
|
||||
Log.trace(F("Signal too low restart the module" CR));
|
||||
setupGSM(false); // if we are below or above a threshold signal we relaunch the setup of GSM module
|
||||
}
|
||||
}
|
||||
@@ -93,19 +88,18 @@ bool _2GtoMQTT()
|
||||
{
|
||||
// Get the memory locations of unread SMS messages.
|
||||
unreadSMSNum = A6l.getUnreadSMSLocs(unreadSMSLocs, 512);
|
||||
trc(F("Creating SMS buffer"));
|
||||
Log.trace(F("Creating SMS buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &SMSdata = jsonBuffer.createObject();
|
||||
for (int i = 0; i < unreadSMSNum; i++)
|
||||
{
|
||||
trc(F("New message at index: "));
|
||||
trc(unreadSMSNum);
|
||||
Log.notice(F("New message at index: %d" CR), unreadSMSNum);
|
||||
sms = A6l.readSMS(unreadSMSLocs[i]);
|
||||
SMSdata.set("message", (char *)sms.message.c_str());
|
||||
SMSdata.set("date", (char *)sms.date.c_str());
|
||||
SMSdata.set("phone", (char *)sms.number.c_str());
|
||||
A6l.deleteSMS(unreadSMSLocs[i]); // we delete the SMS received
|
||||
trc(F("Adv data 2GtoMQTT"));
|
||||
Log.trace(F("Adv data 2GtoMQTT" CR));
|
||||
pub(subject2GtoMQTT, SMSdata);
|
||||
return true;
|
||||
}
|
||||
@@ -120,7 +114,7 @@ void MQTTto2G(char *topicOri, char *datacallback)
|
||||
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTto2G))
|
||||
{
|
||||
trc(F("MQTTto2G data analysis"));
|
||||
Log.trace(F("MQTTto2G data analysis" CR));
|
||||
// 2G DATA ANALYSIS
|
||||
String phone_number = "";
|
||||
int pos0 = topic.lastIndexOf(_2GPhoneKey);
|
||||
@@ -128,26 +122,24 @@ void MQTTto2G(char *topicOri, char *datacallback)
|
||||
{
|
||||
pos0 = pos0 + strlen(_2GPhoneKey);
|
||||
phone_number = topic.substring(pos0);
|
||||
trc(F("MQTTto2G phone ok"));
|
||||
trc(phone_number);
|
||||
trc(F("MQTTto2G sms"));
|
||||
trc(data);
|
||||
Log.notice(F("MQTTto2G phone: %s" CR),(char *)phone_number.c_str());
|
||||
Log.notice(F("MQTTto2G sms: %s" CR),(char *)data.c_str());
|
||||
if (A6l.sendSMS(phone_number, data) == A6_OK)
|
||||
{
|
||||
trc(F("SMS OK"));
|
||||
Log.notice(F("SMS OK" CR));
|
||||
// Acknowledgement to the GTW2G topic
|
||||
pub(subjectGTW2GtoMQTT, "SMS OK"); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("SMS KO"));
|
||||
Log.error(F("SMS KO" CR));
|
||||
// Acknowledgement to the GTW2G topic
|
||||
pub(subjectGTW2GtoMQTT, "SMS KO"); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("MQTTto2G Fail reading phone number"));
|
||||
Log.error(F("MQTTto2G Fail reading phone number" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -161,26 +153,26 @@ void MQTTto2G(char *topicOri, JsonObject &SMSdata)
|
||||
{
|
||||
const char *sms = SMSdata["message"];
|
||||
const char *phone = SMSdata["phone"];
|
||||
trc(F("MQTTto2G json data analysis"));
|
||||
Log.trace(F("MQTTto2G json data analysis" CR));
|
||||
if (sms && phone)
|
||||
{
|
||||
trc(F("MQTTto2G sms & phone ok"));
|
||||
trc(sms);
|
||||
Log.notice(F("MQTTto2G phone: %s" CR),phone);
|
||||
Log.notice(F("MQTTto2G sms: %s" CR),sms);
|
||||
if (A6l.sendSMS(String(phone), String(sms)) == A6_OK)
|
||||
{
|
||||
trc(F("SMS OK"));
|
||||
Log.notice(F("SMS OK" CR));
|
||||
// Acknowledgement to the GTW2G topic
|
||||
pub(subjectGTW2GtoMQTT, "SMS OK"); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("SMS KO"));
|
||||
Log.error(F("SMS KO" CR));
|
||||
pub(subjectGTW2GtoMQTT, "SMS KO"); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("MQTTto2G failed json read"));
|
||||
Log.error(F("MQTTto2G failed json read" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+93
-139
@@ -109,10 +109,10 @@ void dumpDevices()
|
||||
{
|
||||
for (vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p)
|
||||
{
|
||||
trc(p->macAdr);
|
||||
trc(p->isDisc);
|
||||
trc(p->isWhtL);
|
||||
trc(p->isBlkL);
|
||||
Log.trace(F("macAdr %s" CR),p->macAdr);
|
||||
Log.trace(F("isDisc %s" CR),p->isDisc);
|
||||
Log.trace(F("isWhtL %s" CR),p->isWhtL);
|
||||
Log.trace(F("isBlkL %s" CR),p->isBlkL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ void strupp(char *beg)
|
||||
void MiFloraDiscovery(char *mac)
|
||||
{
|
||||
#define MiFloraparametersCount 4
|
||||
trc(F("MiFloraDiscovery"));
|
||||
Log.trace(F("MiFloraDiscovery" CR));
|
||||
char *MiFlorasensor[MiFloraparametersCount][8] = {
|
||||
{"sensor", "MiFlora-lux", mac, "illuminance", "{{ value_json.lux | is_defined }}", "", "", "lu"},
|
||||
{"sensor", "MiFlora-tem", mac, "temperature", "{{ value_json.tem | is_defined }}", "", "", "°C"},
|
||||
@@ -137,8 +137,7 @@ void MiFloraDiscovery(char *mac)
|
||||
|
||||
for (int i = 0; i < MiFloraparametersCount; i++)
|
||||
{
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(MiFlorasensor[i][1]);
|
||||
Log.trace(F("CreateDiscoverySensor %s" CR),MiFlorasensor[i][1]);
|
||||
String discovery_topic = String(subjectBTtoMQTT) + "/" + String(mac);
|
||||
String unique_id = String(mac) + "-" + MiFlorasensor[i][1];
|
||||
createDiscovery(MiFlorasensor[i][0],
|
||||
@@ -158,7 +157,7 @@ void MiFloraDiscovery(char *mac)
|
||||
void VegTrugDiscovery(char *mac)
|
||||
{
|
||||
#define VegTrugparametersCount 4
|
||||
trc(F("VegTrugDiscovery"));
|
||||
Log.trace(F("VegTrugDiscovery" CR));
|
||||
char *VegTrugsensor[VegTrugparametersCount][8] = {
|
||||
{"sensor", "VegTrug-lux", mac, "illuminance", "{{ value_json.lux | is_defined }}", "", "", "lu"},
|
||||
{"sensor", "VegTrug-tem", mac, "temperature", "{{ value_json.tem | is_defined }}", "", "", "°C"},
|
||||
@@ -169,8 +168,7 @@ void VegTrugDiscovery(char *mac)
|
||||
|
||||
for (int i = 0; i < VegTrugparametersCount; i++)
|
||||
{
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(VegTrugsensor[i][1]);
|
||||
Log.trace(F("CreateDiscoverySensor %s" CR),VegTrugsensor[i][1]);
|
||||
String discovery_topic = String(subjectBTtoMQTT) + "/" + String(mac);
|
||||
String unique_id = String(mac) + "-" + VegTrugsensor[i][1];
|
||||
createDiscovery(VegTrugsensor[i][0],
|
||||
@@ -190,7 +188,7 @@ void VegTrugDiscovery(char *mac)
|
||||
void MiJiaDiscovery(char *mac)
|
||||
{
|
||||
#define MiJiaparametersCount 3
|
||||
trc(F("MiJiaDiscovery"));
|
||||
Log.trace(F("MiJiaDiscovery" CR));
|
||||
char *MiJiasensor[MiJiaparametersCount][8] = {
|
||||
{"sensor", "MiJia-batt", mac, "battery", "{{ value_json.batt | is_defined }}", "", "", "%"},
|
||||
{"sensor", "MiJia-tem", mac, "temperature", "{{ value_json.tem | is_defined }}", "", "", "°C"},
|
||||
@@ -200,8 +198,7 @@ void MiJiaDiscovery(char *mac)
|
||||
|
||||
for (int i = 0; i < MiJiaparametersCount; i++)
|
||||
{
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(MiJiasensor[i][1]);
|
||||
Log.trace(F("CreateDiscoverySensor %s" CR),MiJiasensor[i][1]);
|
||||
String discovery_topic = String(subjectBTtoMQTT) + "/" + String(mac);
|
||||
String unique_id = String(mac) + "-" + MiJiasensor[i][1];
|
||||
createDiscovery(MiJiasensor[i][0],
|
||||
@@ -221,7 +218,7 @@ void MiJiaDiscovery(char *mac)
|
||||
void LYWSD02Discovery(char *mac)
|
||||
{
|
||||
#define LYWSD02parametersCount 3
|
||||
trc(F("LYWSD02Discovery"));
|
||||
Log.trace(F("LYWSD02Discovery" CR));
|
||||
char *LYWSD02sensor[LYWSD02parametersCount][8] = {
|
||||
{"sensor", "LYWSD02-batt", mac, "battery", "{{ value_json.batt | is_defined }}", "", "", "V"},
|
||||
{"sensor", "LYWSD02-tem", mac, "temperature", "{{ value_json.tem | is_defined }}", "", "", "°C"},
|
||||
@@ -231,8 +228,7 @@ void LYWSD02Discovery(char *mac)
|
||||
|
||||
for (int i = 0; i < LYWSD02parametersCount; i++)
|
||||
{
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(LYWSD02sensor[i][1]);
|
||||
Log.trace(F("CreateDiscoverySensor %s" CR),LYWSD02sensor[i][1]);
|
||||
String discovery_topic = String(subjectBTtoMQTT) + "/" + String(mac);
|
||||
String unique_id = String(mac) + "-" + LYWSD02sensor[i][1];
|
||||
createDiscovery(LYWSD02sensor[i][0],
|
||||
@@ -252,7 +248,7 @@ void LYWSD02Discovery(char *mac)
|
||||
void CLEARGRASSTRHDiscovery(char *mac)
|
||||
{
|
||||
#define CLEARGRASSTRHparametersCount 3
|
||||
trc(F("CLEARGRASSTRHDiscovery"));
|
||||
Log.trace(F("CLEARGRASSTRHDiscovery" CR));
|
||||
char *CLEARGRASSTRHsensor[CLEARGRASSTRHparametersCount][8] = {
|
||||
{"sensor", "CLEARGRASSTRH-batt", mac, "battery", "{{ value_json.batt | is_defined }}", "", "", "V"},
|
||||
{"sensor", "CLEARGRASSTRH-tem", mac, "temperature", "{{ value_json.tem | is_defined }}", "", "", "°C"},
|
||||
@@ -262,8 +258,7 @@ void CLEARGRASSTRHDiscovery(char *mac)
|
||||
|
||||
for (int i = 0; i < CLEARGRASSTRHparametersCount; i++)
|
||||
{
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(CLEARGRASSTRHsensor[i][1]);
|
||||
Log.trace(F("CreateDiscoverySensor %s" CR),CLEARGRASSTRHsensor[i][1]);
|
||||
String discovery_topic = String(subjectBTtoMQTT) + "/" + String(mac);
|
||||
String unique_id = String(mac) + "-" + CLEARGRASSTRHsensor[i][1];
|
||||
createDiscovery(CLEARGRASSTRHsensor[i][0],
|
||||
@@ -283,7 +278,7 @@ void CLEARGRASSTRHDiscovery(char *mac)
|
||||
void CLEARGRASSTRHKPADiscovery(char *mac)
|
||||
{
|
||||
#define CLEARGRASSTRHKPAparametersCount 3
|
||||
trc(F("CLEARGRASSTRHKPADiscovery"));
|
||||
Log.trace(F("CLEARGRASSTRHKPADiscovery" CR));
|
||||
char *CLEARGRASSTRHKPAsensor[CLEARGRASSTRHKPAparametersCount][8] = {
|
||||
{"sensor", "CLEARGRASSTRHKPA-pres", mac, "pressure", "{{ value_json.pres | is_defined }}", "", "", "kPa"},
|
||||
{"sensor", "CLEARGRASSTRHKPA-tem", mac, "temperature", "{{ value_json.tem | is_defined }}", "", "", "°C"},
|
||||
@@ -293,8 +288,7 @@ void CLEARGRASSTRHKPADiscovery(char *mac)
|
||||
|
||||
for (int i = 0; i < CLEARGRASSTRHKPAparametersCount; i++)
|
||||
{
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(CLEARGRASSTRHKPAsensor[i][1]);
|
||||
Log.trace(F("CreateDiscoverySensor %s" CR),CLEARGRASSTRHKPAsensor[i][1]);
|
||||
String discovery_topic = String(subjectBTtoMQTT) + "/" + String(mac);
|
||||
String unique_id = String(mac) + "-" + CLEARGRASSTRHKPAsensor[i][1];
|
||||
createDiscovery(CLEARGRASSTRHKPAsensor[i][0],
|
||||
@@ -314,7 +308,7 @@ void CLEARGRASSTRHKPADiscovery(char *mac)
|
||||
void MiScaleDiscovery(char *mac)
|
||||
{
|
||||
#define MiScaleparametersCount 1
|
||||
trc(F("MiScaleDiscovery"));
|
||||
Log.trace(F("MiScaleDiscovery" CR));
|
||||
char *MiScalesensor[MiScaleparametersCount][8] = {
|
||||
{"sensor", "MiScale-weight", mac, "weight", "{{ value_json.weight | is_defined }}", "", "", "kg"},
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
@@ -322,8 +316,7 @@ void MiScaleDiscovery(char *mac)
|
||||
|
||||
for (int i = 0; i < MiScaleparametersCount; i++)
|
||||
{
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(MiScalesensor[i][1]);
|
||||
Log.trace(F("CreateDiscoverySensor %s" CR),MiScalesensor[i][1]);
|
||||
String discovery_topic = String(subjectBTtoMQTT) + "/" + String(mac);
|
||||
String unique_id = String(mac) + "-" + MiScalesensor[i][1];
|
||||
createDiscovery(MiScalesensor[i][0],
|
||||
@@ -343,7 +336,7 @@ void MiScaleDiscovery(char *mac)
|
||||
void MiLampDiscovery(char *mac)
|
||||
{
|
||||
#define MiLampparametersCount 1
|
||||
trc(F("MiLampDiscovery"));
|
||||
Log.trace(F("MiLampDiscovery" CR));
|
||||
char *MiLampsensor[MiLampparametersCount][8] = {
|
||||
{"sensor", "MiLamp-presence", mac, "presence", "{{ value_json.presence}}", "", "", "d"},
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
@@ -351,8 +344,7 @@ void MiLampDiscovery(char *mac)
|
||||
|
||||
for (int i = 0; i < MiLampparametersCount; i++)
|
||||
{
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(MiLampsensor[i][1]);
|
||||
Log.trace(F("CreateDiscoverySensor %s" CR),MiLampsensor[i][1]);
|
||||
String discovery_topic = String(subjectBTtoMQTT) + "/" + String(mac);
|
||||
String unique_id = String(mac) + "-" + MiLampsensor[i][1];
|
||||
createDiscovery(MiLampsensor[i][0],
|
||||
@@ -372,7 +364,7 @@ void MiLampDiscovery(char *mac)
|
||||
void MiBandDiscovery(char *mac)
|
||||
{
|
||||
#define MiBandparametersCount 1
|
||||
trc(F("MiBandDiscovery"));
|
||||
Log.trace(F("MiBandDiscovery" CR));
|
||||
char *MiBandsensor[MiBandparametersCount][8] = {
|
||||
{"sensor", "MiBand-steps", mac, "steps", "{{ value_json.steps | is_defined }}", "", "", "nb"},
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
@@ -380,8 +372,7 @@ void MiBandDiscovery(char *mac)
|
||||
|
||||
for (int i = 0; i < MiBandparametersCount; i++)
|
||||
{
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(MiBandsensor[i][1]);
|
||||
Log.trace(F("CreateDiscoverySensor %s" CR),MiBandsensor[i][1]);
|
||||
String discovery_topic = String(subjectBTtoMQTT) + "/" + String(mac);
|
||||
String unique_id = String(mac) + "-" + MiBandsensor[i][1];
|
||||
createDiscovery(MiBandsensor[i][0],
|
||||
@@ -421,7 +412,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
{
|
||||
void onResult(BLEAdvertisedDevice advertisedDevice)
|
||||
{
|
||||
trc(F("Creating BLE buffer"));
|
||||
Log.trace(F("Creating BLE buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &BLEdata = jsonBuffer.createObject();
|
||||
String mac_adress = advertisedDevice.getAddress().toString().c_str();
|
||||
@@ -431,8 +422,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
String mactopic = subjectBTtoMQTT + String("/") + mac_adress;
|
||||
char mac[mac_adress.length() + 1];
|
||||
mac_adress.toCharArray(mac, mac_adress.length() + 1);
|
||||
trc("device detected");
|
||||
trc(mac);
|
||||
Log.notice(F("Device detected: %s" CR),mac);
|
||||
if ((!oneWhite() || isWhite(mac)) && !isBlack(mac))
|
||||
{ //if not black listed mac we go AND if we have no white mac or this mac is white we go out
|
||||
if (advertisedDevice.haveName())
|
||||
@@ -449,9 +439,8 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
#endif
|
||||
if (advertisedDevice.haveServiceData())
|
||||
{
|
||||
trc(F("Get services data :"));
|
||||
int serviceDataCount = advertisedDevice.getServiceDataCount();
|
||||
trc(serviceDataCount);
|
||||
Log.trace(F("Get services data number: %d" CR),serviceDataCount);
|
||||
for (int j = 0; j < serviceDataCount; j++)
|
||||
{
|
||||
std::string serviceData = advertisedDevice.getServiceData(j);
|
||||
@@ -469,6 +458,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
char service_data[returnedString.length() + 1];
|
||||
returnedString.toCharArray(service_data, returnedString.length() + 1);
|
||||
service_data[returnedString.length()] = '\0';
|
||||
Log.notice(F("Service data: %s" CR),service_data);
|
||||
#ifdef pubBLEServiceData
|
||||
BLEdata.set("servicedata", service_data);
|
||||
#endif
|
||||
@@ -477,13 +467,13 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
{ // publish only the devices close enough
|
||||
pub((char *)mactopic.c_str(), BLEdata);
|
||||
if (strstr(BLEdata["servicedatauuid"].as<char *>(), "fe95") != NULL)
|
||||
{ //Mi FLora, Mi jia, Cleargrass Method 1, LYWDS02, VegTrug
|
||||
trc("Processing BLE device data");
|
||||
{ //Mi FLora, Mi jia, Cleargrass Method 1, LYWSD02, VegTrug
|
||||
Log.trace(F("Processing BLE device data" CR));
|
||||
int pos = -1;
|
||||
pos = strpos(service_data, "209800");
|
||||
if (pos != -1)
|
||||
{
|
||||
trc(F("mi flora data reading"));
|
||||
Log.trace(F("mi flora data reading" CR));
|
||||
//example "servicedata":"71209800bc63b6658d7cc40d0910023200"
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(mac))
|
||||
@@ -495,7 +485,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
pos = strpos(service_data, "20bc03");
|
||||
if (pos != -1)
|
||||
{
|
||||
trc(F("vegtrug data reading"));
|
||||
Log.trace(F("vegtrug data reading" CR));
|
||||
//example "servicedata":"7120bc0399c309688d7cc40d0910020000"
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(mac))
|
||||
@@ -507,7 +497,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
pos = strpos(service_data, "20aa01");
|
||||
if (pos != -1)
|
||||
{
|
||||
trc(F("mi jia data reading"));
|
||||
Log.trace(F("mi jia data reading" CR));
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(mac))
|
||||
MiJiaDiscovery(mac);
|
||||
@@ -518,7 +508,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
pos = strpos(service_data, "205b04");
|
||||
if (pos != -1)
|
||||
{
|
||||
trc(F("LYWSD02 data reading"));
|
||||
Log.trace(F("LYWSD02 data reading" CR));
|
||||
//example "servicedata":"70205b04b96ab883c8593f09041002e000"
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(mac))
|
||||
@@ -530,7 +520,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
pos = strpos(service_data, "304703");
|
||||
if (pos != -1)
|
||||
{
|
||||
trc(F("ClearGrass T RH data reading method 1"));
|
||||
Log.trace(F("ClearGrass T RH data reading method 1" CR));
|
||||
//example "servicedata":"5030470340743e10342d58041002d6000a100164"
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(mac))
|
||||
@@ -542,7 +532,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
pos = strpos(service_data, "4030dd");
|
||||
if (pos != -1)
|
||||
{
|
||||
trc(F("Mi Lamp data reading"));
|
||||
Log.trace(F("Mi Lamp data reading" CR));
|
||||
//example "servicedata":4030DD031D0300010100
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(mac))
|
||||
@@ -553,7 +543,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
}
|
||||
if (strstr(BLEdata["servicedatauuid"].as<char *>(), "181d") != NULL)
|
||||
{ // Mi Scale V1
|
||||
trc(F("Mi Scale V1 data reading"));
|
||||
Log.trace(F("Mi Scale V1 data reading" CR));
|
||||
//example "servicedata":"a2ac2be307060207122b" /"a28039e3070602070e28"
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(mac))
|
||||
@@ -563,7 +553,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
}
|
||||
if (strstr(BLEdata["servicedatauuid"].as<char *>(), "181b") != NULL)
|
||||
{ // Mi Scale V2
|
||||
trc(F("Mi Scale V2 data reading"));
|
||||
Log.trace(F("Mi Scale V2 data reading" CR));
|
||||
//example "servicedata":02c4e1070b1e13050c00002607 / 02a6e20705150a251df401443e /02a6e20705180c0d04d701943e
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(mac))
|
||||
@@ -573,7 +563,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
}
|
||||
if (strstr(BLEdata["servicedatauuid"].as<char *>(), "fee0") != NULL)
|
||||
{ // Mi Band //0000fee0-0000-1000-8000-00805f9b34fb // ESP32 only
|
||||
trc(F("Mi Band data reading"));
|
||||
Log.trace(F("Mi Band data reading" CR));
|
||||
//example "servicedata":a21e0000
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(mac))
|
||||
@@ -583,7 +573,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
}
|
||||
if (strstr(BLEdata["servicedata"].as<char *>(), "08094c") != NULL)
|
||||
{ // Clear grass with air pressure//08094c0140342d580104d8000c020702612702015a
|
||||
trc(F("Clear grass data with air pressure reading"));
|
||||
Log.trace(F("Clear grass data with air pressure reading" CR));
|
||||
//example "servicedata":08094c0140342d580104 c400 2402 0702 5d27 02015a
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(mac))
|
||||
@@ -593,7 +583,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
}
|
||||
if (strstr(BLEdata["servicedata"].as<char *>(), "080774") != NULL)
|
||||
{ // Clear grass standard method 2/0807743e10342d580104c3002c0202012a
|
||||
trc(F("Clear grass data reading method 2"));
|
||||
Log.trace(F("Clear grass data reading method 2" CR));
|
||||
//example "servicedata":0807743e10342d580104 c300 2c02 02012a
|
||||
// no discovery as it is already available with method 1
|
||||
process_cleargrass(service_data, mac);
|
||||
@@ -601,7 +591,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
}
|
||||
else
|
||||
{
|
||||
trc("Low rssi, device filtered");
|
||||
Log.trace(F("Low rssi, device filtered" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -613,13 +603,13 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
}
|
||||
else
|
||||
{
|
||||
trc("Low rssi, device filtered");
|
||||
Log.trace(F("Low rssi, device filtered" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Filtered mac device"));
|
||||
Log.trace(F("Filtered mac device" CR));
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -630,26 +620,23 @@ void BLEscan()
|
||||
TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
|
||||
TIMERG0.wdt_feed = 1;
|
||||
TIMERG0.wdt_wprotect = 0;
|
||||
trc(F("Scan begin"));
|
||||
Log.notice(F("Scan begin" CR));
|
||||
BLEDevice::init("");
|
||||
BLEScan *pBLEScan = BLEDevice::getScan(); //create new scan
|
||||
MyAdvertisedDeviceCallbacks myCallbacks;
|
||||
pBLEScan->setAdvertisedDeviceCallbacks(&myCallbacks);
|
||||
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
|
||||
BLEScanResults foundDevices = pBLEScan->start(Scan_duration);
|
||||
trc(F("Scan end, deinit controller"));
|
||||
Log.notice(F("Scan end, deinit controller" CR));
|
||||
esp_bt_controller_deinit();
|
||||
}
|
||||
|
||||
void coreTask(void *pvParameters)
|
||||
{
|
||||
|
||||
String taskMessage = "BT Task running on core ";
|
||||
taskMessage = taskMessage + xPortGetCoreID();
|
||||
|
||||
while (true)
|
||||
{
|
||||
trc(taskMessage);
|
||||
Log.trace(F('BT Task running on core: %d'),xPortGetCoreID());
|
||||
delay(BLEinterval);
|
||||
if (client.state() == 0)
|
||||
{
|
||||
@@ -657,7 +644,7 @@ void coreTask(void *pvParameters)
|
||||
}
|
||||
else
|
||||
{
|
||||
trc("MQTT client disconnected no BLE scan");
|
||||
Log.warning(F("MQTT client disconnected no BLE scan" CR));
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
@@ -667,10 +654,8 @@ void setupBT()
|
||||
{
|
||||
BLEinterval = TimeBtw_Read;
|
||||
Minrssi = MinimumRSSI;
|
||||
trc(F("BLEinterval btw scans"));
|
||||
trc(BLEinterval);
|
||||
trc(F("Minrssi"));
|
||||
trc(Minrssi);
|
||||
Log.notice(F("BLEinterval: %d" CR),BLEinterval);
|
||||
Log.notice(F("Minrssi: %d" CR),Minrssi);
|
||||
// we setup a task with priority one to avoid conflict with other gateways
|
||||
xTaskCreatePinnedToCore(
|
||||
coreTask, /* Function to implement the task */
|
||||
@@ -680,7 +665,7 @@ void setupBT()
|
||||
1, /* Priority of the task */
|
||||
NULL, /* Task handle. */
|
||||
taskCore); /* Core where the task should run */
|
||||
trc(F("ZgatewayBT multicore ESP32 setup done "));
|
||||
Log.trace(F("ZgatewayBT multicore ESP32 setup done " CR));
|
||||
}
|
||||
|
||||
bool BTtoMQTT()
|
||||
@@ -709,22 +694,20 @@ void setupBT()
|
||||
{
|
||||
BLEinterval = TimeBtw_Read;
|
||||
Minrssi = MinimumRSSI;
|
||||
trc(F("BLEinterval btw scans"));
|
||||
trc(BLEinterval);
|
||||
trc(F("Minrssi"));
|
||||
trc(Minrssi);
|
||||
Log.notice(F("BLEinterval: %d" CR),BLEinterval);
|
||||
Log.notice(F("Minrssi: %d" CR),Minrssi);
|
||||
softserial.begin(9600);
|
||||
softserial.print(F("AT+ROLE1"));
|
||||
softserial.print(F("AT+ROLE1" CR));
|
||||
delay(100);
|
||||
softserial.print(F("AT+IMME1"));
|
||||
softserial.print(F("AT+IMME1" CR));
|
||||
delay(100);
|
||||
softserial.print(F("AT+RESET"));
|
||||
softserial.print(F("AT+RESET" CR));
|
||||
delay(100);
|
||||
#ifdef HM_BLUE_LED_STOP
|
||||
softserial.print(F("AT+PIO11")); // When not connected (as in BLE mode) the LED is off. When connected the LED is solid on.
|
||||
softserial.print(F("AT+PIO11" CR)); // When not connected (as in BLE mode) the LED is off. When connected the LED is solid on.
|
||||
#endif
|
||||
delay(100);
|
||||
trc(F("ZgatewayBT HM1X setup done "));
|
||||
Log.trace(F("ZgatewayBT HM1X setup done " CR));
|
||||
}
|
||||
|
||||
bool BTtoMQTT()
|
||||
@@ -759,7 +742,7 @@ bool BTtoMQTT()
|
||||
returnedString.remove(pos, returnedString.length());
|
||||
char token_char[token.length() + 1];
|
||||
token.toCharArray(token_char, token.length() + 1);
|
||||
trc(token);
|
||||
Log.trace(F("Token: %s" CR),token_char);
|
||||
if (token.length() > 60)
|
||||
{ // we extract data only if we have detailled infos
|
||||
for (int i = 0; i < 6; i++)
|
||||
@@ -771,7 +754,7 @@ bool BTtoMQTT()
|
||||
|
||||
if ((strlen(d[0].extract)) == 12) // if a mac adress is detected we publish it
|
||||
{
|
||||
trc(F("Creating BLE buffer"));
|
||||
Log.trace(F("Creating BLE buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &BLEdata = jsonBuffer.createObject();
|
||||
strupp(d[0].extract);
|
||||
@@ -787,8 +770,7 @@ bool BTtoMQTT()
|
||||
if (((i - 1) % 2 == 0) && (i != 11))
|
||||
HomePresenceId += ":";
|
||||
}
|
||||
trc(F("HomePresenceId"));
|
||||
trc(HomePresenceId);
|
||||
Log.trace(F("HomePresenceId %s" CR),(char *)HomePresenceId.c_str());
|
||||
BLEdata.set("id", (char *)HomePresenceId.c_str());
|
||||
#endif
|
||||
String topic = subjectBTtoMQTT + String("/") + String(d[0].extract);
|
||||
@@ -798,6 +780,7 @@ bool BTtoMQTT()
|
||||
haRoomPresence(BLEdata); // this device has an rssi in consequence we can use it for home assistant room presence component
|
||||
#endif
|
||||
String service_data(d[5].extract);
|
||||
Log.notice(F("Service data: %s" CR),d[5].extract);
|
||||
service_data = service_data.substring(14);
|
||||
#ifdef pubBLEServiceData
|
||||
BLEdata.set("servicedata", (char *)service_data.c_str());
|
||||
@@ -809,7 +792,7 @@ bool BTtoMQTT()
|
||||
pos = strpos(d[5].extract, "209800");
|
||||
if (pos != -1)
|
||||
{
|
||||
trc("mi flora data reading");
|
||||
Log.trace(F("mi flora data reading" CR));
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(d[0].extract))
|
||||
MiFloraDiscovery(d[0].extract);
|
||||
@@ -821,7 +804,7 @@ bool BTtoMQTT()
|
||||
//example "servicedata":"5020aa0194dfaa33342d580d1004e3002c02"
|
||||
if (pos != -1)
|
||||
{
|
||||
trc("mi jia data reading");
|
||||
Log.trace(F("mi jia data reading" CR));
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(d[0].extract))
|
||||
MiJiaDiscovery(d[0].extract);
|
||||
@@ -833,7 +816,7 @@ bool BTtoMQTT()
|
||||
//example "servicedata":"141695fe70205b0461298882c8593f09061002d002"
|
||||
if (pos != -1)
|
||||
{
|
||||
trc("LYWSD02 data reading");
|
||||
Log.trace(F("LYWSD02 data reading" CR));
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(d[0].extract))
|
||||
LYWSD02Discovery(d[0].extract);
|
||||
@@ -844,7 +827,7 @@ bool BTtoMQTT()
|
||||
pos = strpos(d[5].extract, "304703");
|
||||
if (pos != -1)
|
||||
{
|
||||
trc("CLEARGRASSTRH data reading method 1");
|
||||
Log.trace(F("CLEARGRASSTRH data reading method 1" CR));
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(d[0].extract))
|
||||
CLEARGRASSTRHDiscovery(d[0].extract);
|
||||
@@ -855,7 +838,7 @@ bool BTtoMQTT()
|
||||
pos = strpos(d[5].extract, "e30706");
|
||||
if (pos != -1)
|
||||
{
|
||||
trc("Mi Scale data reading");
|
||||
Log.trace(F("Mi Scale data reading" CR));
|
||||
//example "servicedata":"a2ac2be307060207122b" /"a28039e3070602070e28"
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(d[0].extract))
|
||||
@@ -867,7 +850,7 @@ bool BTtoMQTT()
|
||||
pos = strpos(d[5].extract, "4030dd");
|
||||
if (pos != -1)
|
||||
{
|
||||
trc(F("Mi Lamp data reading"));
|
||||
Log.trace(F("Mi Lamp data reading" CR));
|
||||
//example "servicedata":4030dd31d0300010100
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(d[0].extract))
|
||||
@@ -879,7 +862,7 @@ bool BTtoMQTT()
|
||||
pos = strpos(d[5].extract, "08094c"); // Clear grass with air pressure//08094c0140342d580104d8000c020702612702015a
|
||||
if (pos != -1)
|
||||
{
|
||||
trc(F("Clear grass data with air pressure reading"));
|
||||
Log.trace(F("Clear grass data with air pressure reading" CR));
|
||||
//example "servicedata":08094c0140342d580104 c400 2402 0702 5d27 02015a
|
||||
#ifdef ZmqttDiscovery
|
||||
if (!isDiscovered(d[0].extract))
|
||||
@@ -891,7 +874,7 @@ bool BTtoMQTT()
|
||||
pos = strpos(d[5].extract, "080774"); // Clear grass standard method 2/0807743e10342d580104c3002c0202012a
|
||||
if (pos != -1)
|
||||
{
|
||||
trc(F("Clear grass data reading method 2"));
|
||||
Log.trace(F("Clear grass data reading method 2" CR));
|
||||
//example "servicedata":0807743e10342d580104 c300 2c02 02012a
|
||||
// no discovery as it is already available with method 1
|
||||
process_cleargrass((char *)service_data.c_str(), d[0].extract);
|
||||
@@ -900,7 +883,7 @@ bool BTtoMQTT()
|
||||
}
|
||||
else
|
||||
{
|
||||
trc("Low rssi, device filtered");
|
||||
Log.trace(F("Low rssi, device filtered" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -930,21 +913,18 @@ double value_from_service_data(char *service_data, int offset, int data_length)
|
||||
double value = strtol(data, NULL, 16);
|
||||
if (value > 65000 && data_length <= 4)
|
||||
value = value - 65535;
|
||||
trc(value);
|
||||
Log.trace(F("value %D" CR),value);
|
||||
return value;
|
||||
}
|
||||
|
||||
bool process_sensors(int offset, char *rest_data, char *mac_adress)
|
||||
{
|
||||
|
||||
trc(F("Creating BLE buffer"));
|
||||
Log.trace(F("Creating BLE buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &BLEdata = jsonBuffer.createObject();
|
||||
trc("rest_data");
|
||||
trc(rest_data);
|
||||
int data_length = 0;
|
||||
trc("data_length");
|
||||
trc(rest_data[51 + offset]);
|
||||
|
||||
switch (rest_data[51 + offset])
|
||||
{
|
||||
case '1':
|
||||
@@ -952,10 +932,10 @@ bool process_sensors(int offset, char *rest_data, char *mac_adress)
|
||||
case '3':
|
||||
case '4':
|
||||
data_length = ((rest_data[51 + offset] - '0') * 2);
|
||||
trc("valid data_length");
|
||||
Log.trace(F("Valid data_length: %d" CR), data_length);
|
||||
break;
|
||||
default:
|
||||
trc("invalid data_length");
|
||||
Log.trace(F("Invalid data_length" CR));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -996,7 +976,7 @@ bool process_sensors(int offset, char *rest_data, char *mac_adress)
|
||||
BLEdata.set("hum", (double)value / 10);
|
||||
break;
|
||||
default:
|
||||
trc("can't read values");
|
||||
Log.trace(F("can't read values" CR));
|
||||
return false;
|
||||
}
|
||||
String mactopic(mac_adress);
|
||||
@@ -1008,10 +988,7 @@ bool process_sensors(int offset, char *rest_data, char *mac_adress)
|
||||
bool process_scale_v1(char *rest_data, char *mac_adress)
|
||||
{
|
||||
//example "servicedata":"a2ac2be307060207122b" /"a28039e3070602070e28"
|
||||
trc("rest_data");
|
||||
trc(rest_data);
|
||||
|
||||
trc(F("Creating BLE buffer"));
|
||||
Log.trace(F("Creating BLE buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &BLEdata = jsonBuffer.createObject();
|
||||
|
||||
@@ -1030,10 +1007,7 @@ bool process_scale_v1(char *rest_data, char *mac_adress)
|
||||
bool process_scale_v2(char *rest_data, char *mac_adress)
|
||||
{
|
||||
//example "servicedata":"a2ac2be307060207122b" /"a28039e3070602070e28"
|
||||
trc("rest_data");
|
||||
trc(rest_data);
|
||||
|
||||
trc(F("Creating BLE buffer"));
|
||||
Log.trace(F("Creating BLE buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &BLEdata = jsonBuffer.createObject();
|
||||
|
||||
@@ -1054,10 +1028,7 @@ bool process_scale_v2(char *rest_data, char *mac_adress)
|
||||
bool process_miband(char *rest_data, char *mac_adress)
|
||||
{
|
||||
//example "servicedata":"a21e0000"
|
||||
trc("rest_data");
|
||||
trc(rest_data);
|
||||
|
||||
trc(F("Creating BLE buffer"));
|
||||
Log.trace(F("Creating BLE buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &BLEdata = jsonBuffer.createObject();
|
||||
|
||||
@@ -1076,10 +1047,7 @@ bool process_miband(char *rest_data, char *mac_adress)
|
||||
bool process_milamp(char *rest_data, char *mac_adress)
|
||||
{
|
||||
//example "servicedata":"4030dd31d0300010100"
|
||||
trc("rest_data");
|
||||
trc(rest_data);
|
||||
|
||||
trc(F("Creating BLE buffer"));
|
||||
Log.trace(F("Creating BLE buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &BLEdata = jsonBuffer.createObject();
|
||||
|
||||
@@ -1100,10 +1068,7 @@ bool process_cleargrass_air(char *rest_data, char *mac_adress)
|
||||
{
|
||||
//example "servicedata":"08094c0140342d580104c400240207025d2702015a"
|
||||
//decoding "08094c0140342d580104 temp:c400 hum:2402 0702 pressure:5d27 02015a"
|
||||
trc("rest_data");
|
||||
trc(rest_data);
|
||||
|
||||
trc(F("Creating BLE buffer"));
|
||||
Log.trace(F("Creating BLE buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &BLEdata = jsonBuffer.createObject();
|
||||
|
||||
@@ -1127,10 +1092,7 @@ bool process_cleargrass_air(char *rest_data, char *mac_adress)
|
||||
bool process_cleargrass(char *rest_data, char *mac_adress)
|
||||
{
|
||||
//example "servicedata":0807743e10342d580104 c300 2c02 02012a
|
||||
trc("rest_data");
|
||||
trc(rest_data);
|
||||
|
||||
trc(F("Creating BLE buffer"));
|
||||
Log.trace(F("Creating BLE buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &BLEdata = jsonBuffer.createObject();
|
||||
|
||||
@@ -1152,13 +1114,11 @@ bool process_cleargrass(char *rest_data, char *mac_adress)
|
||||
void haRoomPresence(JsonObject &HomePresence)
|
||||
{
|
||||
int BLErssi = HomePresence["rssi"];
|
||||
trc(F("BLErssi"));
|
||||
trc(BLErssi);
|
||||
Log.trace(F("BLErssi %d" CR), BLErssi);
|
||||
int txPower = HomePresence["txpower"] | 0;
|
||||
if (txPower >= 0)
|
||||
txPower = -59; //if tx power is not found we set a default calibration value
|
||||
trc(F("txPower"));
|
||||
trc(txPower);
|
||||
Log.trace(F("TxPower: %d" CR),txPower);
|
||||
double ratio = BLErssi * 1.0 / txPower;
|
||||
double distance;
|
||||
if (ratio < 1.0)
|
||||
@@ -1170,8 +1130,7 @@ void haRoomPresence(JsonObject &HomePresence)
|
||||
distance = (0.89976) * pow(ratio, 7.7095) + 0.111;
|
||||
}
|
||||
HomePresence["distance"] = distance;
|
||||
trc(F("BLE DISTANCE :"));
|
||||
trc(distance);
|
||||
Log.trace(F("Ble distance %D" CR),distance);
|
||||
String topic = String(Base_Topic) + "home_presence/" + String(gateway_name);
|
||||
pub_custom_topic((char *)topic.c_str(), HomePresence, false);
|
||||
}
|
||||
@@ -1181,26 +1140,26 @@ void MQTTtoBT(char *topicOri, JsonObject &BTdata)
|
||||
{ // json object decoding
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTtoBTset))
|
||||
{
|
||||
trc(F("MQTTtoBT json set"));
|
||||
Log.trace(F("MQTTtoBT json set" CR));
|
||||
|
||||
// Black list & white list set
|
||||
int WLsize = BTdata["white-list"].size();
|
||||
if (WLsize > 0)
|
||||
{
|
||||
trc(F("WL set"));
|
||||
for (int i = 0; i < WLsize; i++)
|
||||
{
|
||||
const char *whiteMac = BTdata["white-list"][i];
|
||||
Log.trace(F("WL set: %s" CR), whiteMac);
|
||||
setWorBMac((char *)whiteMac, true);
|
||||
}
|
||||
}
|
||||
int BLsize = BTdata["black-list"].size();
|
||||
if (BLsize > 0)
|
||||
{
|
||||
trc(F("BL set"));
|
||||
for (int i = 0; i < BLsize; i++)
|
||||
{
|
||||
const char *blackMac = BTdata["black-list"][i];
|
||||
Log.trace(F("BL set: %s" CR), blackMac);
|
||||
setWorBMac((char *)blackMac, false);
|
||||
}
|
||||
}
|
||||
@@ -1210,33 +1169,28 @@ void MQTTtoBT(char *topicOri, JsonObject &BTdata)
|
||||
// Scan interval set
|
||||
if (BTdata.containsKey("interval"))
|
||||
{
|
||||
trc(F("BLE interval"));
|
||||
Log.trace(F("BLE interval setup" CR));
|
||||
// storing BLE interval for further use if needed
|
||||
unsigned int prevBLEinterval = BLEinterval;
|
||||
trc("previous interval");
|
||||
trc(BLEinterval);
|
||||
Log.trace(F("Previous interval: %d ms" CR), BLEinterval);
|
||||
// set BLE interval if present if not setting default value
|
||||
BLEinterval = (unsigned int)BTdata["interval"];
|
||||
trc("new interval");
|
||||
trc(BLEinterval);
|
||||
Log.notice(F("New interval: %d ms" CR), BLEinterval);
|
||||
if (BLEinterval == 0)
|
||||
{
|
||||
if (BTtoMQTT()) // as BLEinterval is = to 0 we can launch the loop and the scan will execute immediately
|
||||
trc(F("Scan done"));
|
||||
Log.trace(F("Scan done" CR));
|
||||
BLEinterval = prevBLEinterval; // as 0 was just used as a command we recover previous scan duration
|
||||
}
|
||||
}
|
||||
// MinRSSI set
|
||||
if (BTdata.containsKey("minrssi"))
|
||||
{
|
||||
trc(F("Min RSSI"));
|
||||
// storing Min RSSI for further use if needed
|
||||
trc("previous Minrssi");
|
||||
trc(Minrssi);
|
||||
Log.trace(F("Previous Minrssi: %d" CR), Minrssi);
|
||||
// set Min RSSI if present if not setting default value
|
||||
Minrssi = (unsigned int)BTdata["minrssi"];
|
||||
trc("new Minrssi");
|
||||
trc(Minrssi);
|
||||
Log.notice(F("New Minrssi: %d" CR), Minrssi);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+24
-27
@@ -103,11 +103,9 @@ void setupIR()
|
||||
|
||||
irrecv.enableIRIn(); // Start the receiver
|
||||
|
||||
trc(F("IR_EMITTER_PIN "));
|
||||
trc(IR_EMITTER_PIN);
|
||||
trc(F("IR_RECEIVER_PIN "));
|
||||
trc(IR_RECEIVER_PIN);
|
||||
trc(F("ZgatewayIR setup done "));
|
||||
Log.notice(F("IR_EMITTER_PIN: %d " CR), IR_EMITTER_PIN);
|
||||
Log.notice(F("IR_RECEIVER_PIN: %d " CR), IR_RECEIVER_PIN);
|
||||
Log.trace(F("ZgatewayIR setup done " CR));
|
||||
}
|
||||
|
||||
void IRtoMQTT()
|
||||
@@ -116,15 +114,15 @@ void IRtoMQTT()
|
||||
|
||||
if (irrecv.decode(&results))
|
||||
{
|
||||
trc(F("Creating IR buffer"));
|
||||
Log.trace(F("Creating IR buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &IRdata = jsonBuffer.createObject();
|
||||
|
||||
trc(F("Rcv. IR"));
|
||||
Log.trace(F("Rcv. IR" CR));
|
||||
#ifdef ESP32
|
||||
String taskMessage = "Task running on core ";
|
||||
taskMessage = taskMessage + xPortGetCoreID();
|
||||
trc(taskMessage);
|
||||
//trc(taskMessage);
|
||||
#endif
|
||||
IRdata.set("value", (unsigned long long)(results.value));
|
||||
IRdata.set("protocol", (int)(results.decode_type));
|
||||
@@ -148,7 +146,7 @@ void IRtoMQTT()
|
||||
if (i < results.rawlen - 1)
|
||||
rawCode = rawCode + ","; // ',' not needed on last one
|
||||
}
|
||||
trc(rawCode);
|
||||
//trc(rawCode);
|
||||
IRdata.set("raw", rawCode);
|
||||
// if needed we directly resend the raw code
|
||||
#ifdef RawDirectForward
|
||||
@@ -166,24 +164,24 @@ void IRtoMQTT()
|
||||
rawsend[i] = results.rawbuf[i];
|
||||
}
|
||||
irsend.sendRaw(rawsend, results.rawlen, RawFrequency);
|
||||
trc(F("raw redirected"));
|
||||
Log.trace(F("raw redirected" CR));
|
||||
#endif
|
||||
irrecv.resume(); // Receive the next value
|
||||
unsigned long long MQTTvalue = IRdata.get<unsigned long long>("value");
|
||||
//trc(MQTTvalue);
|
||||
if ((pubIRunknownPrtcl == false && IRdata.get<int>("protocol") == -1) /*|| MQTTvalue == ULONG_MAX*/)
|
||||
{ // don't publish unknown IR protocol or data to high
|
||||
trc(F("--no pub unknwn prt or data to high--"));
|
||||
if ((pubIRunknownPrtcl == false && IRdata.get<int>("protocol") == -1))
|
||||
{ // don't publish unknown IR protocol
|
||||
Log.notice(F("--no pub unknwn prt--" CR));
|
||||
}
|
||||
else if (!isAduplicate(MQTTvalue) && MQTTvalue != 0)
|
||||
{ // conditions to avoid duplications of IR -->MQTT
|
||||
trc(F("Adv data IRtoMQTT"));
|
||||
Log.trace(F("Adv data IRtoMQTT" CR));
|
||||
pub(subjectIRtoMQTT, IRdata);
|
||||
trc(F("Store val"));
|
||||
Log.trace(F("Store val: %D" CR), MQTTvalue);
|
||||
storeValue(MQTTvalue);
|
||||
if (repeatIRwMQTT)
|
||||
{
|
||||
trc(F("Pub. IR for rpt"));
|
||||
Log.trace(F("Pub. IR for rpt" CR));
|
||||
pubMQTT(subjectMQTTtoIR, MQTTvalue);
|
||||
}
|
||||
}
|
||||
@@ -196,7 +194,7 @@ void MQTTtoIR(char *topicOri, JsonObject &IRdata)
|
||||
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTtoIR))
|
||||
{
|
||||
trc(F("MQTTtoIR json"));
|
||||
Log.trace(F("MQTTtoIR json" CR));
|
||||
uint64_t data = IRdata["value"];
|
||||
const char *raw = IRdata["raw"];
|
||||
const char *datastring = IRdata["datastring"];
|
||||
@@ -206,12 +204,11 @@ void MQTTtoIR(char *topicOri, JsonObject &IRdata)
|
||||
}
|
||||
if (data != 0 || raw || datastring)
|
||||
{
|
||||
trc(F("MQTTtoIR value || raw || datasring ok"));
|
||||
Log.trace(F("MQTTtoIR value || raw || datasring ok" CR));
|
||||
bool signalSent = false;
|
||||
if (datastring)
|
||||
{
|
||||
trc(F("datastring"));
|
||||
trc(datastring);
|
||||
Log.notice(F("Datastring: %s" CR), datastring);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -223,8 +220,7 @@ void MQTTtoIR(char *topicOri, JsonObject &IRdata)
|
||||
|
||||
if (raw)
|
||||
{
|
||||
trc(F("raw"));
|
||||
trc(raw);
|
||||
Log.notice(F("Raw: %s" CR), raw);
|
||||
unsigned int s = strlen(raw);
|
||||
//number of "," value count
|
||||
int count = 0;
|
||||
@@ -238,7 +234,7 @@ void MQTTtoIR(char *topicOri, JsonObject &IRdata)
|
||||
#ifdef IR_GC
|
||||
if (strstr(protocol_name, "GC") != NULL)
|
||||
{ // sending GC data from https://irdb.globalcache.com
|
||||
trc(F("GC"));
|
||||
Log.trace(F("GC" CR));
|
||||
//buffer allocation from char datacallback
|
||||
uint16_t GC[count + 1];
|
||||
String value = "";
|
||||
@@ -263,7 +259,7 @@ void MQTTtoIR(char *topicOri, JsonObject &IRdata)
|
||||
#ifdef IR_RAW
|
||||
if (strstr(protocol_name, "Raw") != NULL)
|
||||
{ // sending Raw data
|
||||
trc(F("Raw"));
|
||||
Log.trace(F("Raw" CR));
|
||||
//buffer allocation from char datacallback
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
uint16_t Raw[count + 1];
|
||||
@@ -292,11 +288,12 @@ void MQTTtoIR(char *topicOri, JsonObject &IRdata)
|
||||
}
|
||||
else if (protocol_name && (strstr(protocol_name, "NEC") == NULL))
|
||||
{
|
||||
Log.trace(F("Using Identified Protocol: %s bits: %d repeat: %d" CR),protocol_name, valueBITS, valueRPT);
|
||||
signalSent = sendIdentifiedProtocol(protocol_name, data, datastring, valueBITS, valueRPT);
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Using NEC protocol"));
|
||||
Log.trace(F("Using NEC protocol" CR));
|
||||
if (valueBITS == 0)
|
||||
valueBITS = NEC_BITS;
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
@@ -309,14 +306,14 @@ void MQTTtoIR(char *topicOri, JsonObject &IRdata)
|
||||
}
|
||||
if (signalSent)
|
||||
{ // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
trc(F("MQTTtoIR OK"));
|
||||
Log.notice(F("MQTTtoIR OK" CR));
|
||||
pub(subjectGTWIRtoMQTT, IRdata);
|
||||
}
|
||||
irrecv.enableIRIn(); // ReStart the IR receiver (if not restarted it is not able to receive data)
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("MQTTtoIR failed json read"));
|
||||
Log.error(F("MQTTtoIR failed json read" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+15
-21
@@ -40,24 +40,18 @@ void setupLORA()
|
||||
|
||||
if (!LoRa.begin(LORA_BAND))
|
||||
{
|
||||
trc(F("ZgatewayLORA setup failed!"));
|
||||
Log.error(F("ZgatewayLORA setup failed!" CR));
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
LoRa.receive();
|
||||
trc(F("LORA_SCK"));
|
||||
trc(LORA_SCK);
|
||||
trc(F("LORA_MISO"));
|
||||
trc(LORA_MISO);
|
||||
trc(F("LORA_MOSI"));
|
||||
trc(LORA_MOSI);
|
||||
trc(F("LORA_SS"));
|
||||
trc(LORA_SS);
|
||||
trc(F("LORA_RST"));
|
||||
trc(LORA_RST);
|
||||
trc(F("LORA_DI0"));
|
||||
trc(LORA_DI0);
|
||||
trc(F("ZgatewayLORA setup done"));
|
||||
Log.notice(F("LORA_SCK: %s" CR), LORA_SCK);
|
||||
Log.notice(F("LORA_MISO: %s" CR), LORA_MISO);
|
||||
Log.notice(F("LORA_MOSI: %s" CR), LORA_MOSI);
|
||||
Log.notice(F("LORA_SS: %s" CR), LORA_SS);
|
||||
Log.notice(F("LORA_RST: %s" CR), LORA_RST);
|
||||
Log.notice(F("LORA_DI0: %s" CR), LORA_DI0);
|
||||
Log.trace(F("ZgatewayLORA setup done" CR));
|
||||
}
|
||||
|
||||
void LORAtoMQTT()
|
||||
@@ -67,11 +61,11 @@ void LORAtoMQTT()
|
||||
{
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &LORAdata = jsonBuffer.createObject();
|
||||
trc(F("Rcv. LORA"));
|
||||
Log.trace(F("Rcv. LORA" CR));
|
||||
#ifdef ESP32
|
||||
String taskMessage = "LORA Task running on core ";
|
||||
taskMessage = taskMessage + xPortGetCoreID();
|
||||
trc(taskMessage);
|
||||
//trc(taskMessage);
|
||||
#endif
|
||||
String packet;
|
||||
packet = "";
|
||||
@@ -87,7 +81,7 @@ void LORAtoMQTT()
|
||||
pub(subjectLORAtoMQTT, LORAdata);
|
||||
if (repeatLORAwMQTT)
|
||||
{
|
||||
trc(F("Pub LORA for rpt"));
|
||||
Log.trace(F("Pub LORA for rpt" CR));
|
||||
pub(subjectMQTTtoLORA, LORAdata);
|
||||
}
|
||||
}
|
||||
@@ -98,7 +92,7 @@ void MQTTtoLORA(char *topicOri, JsonObject &LORAdata)
|
||||
{ // json object decoding
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTtoLORA))
|
||||
{
|
||||
trc(F("MQTTtoLORA json"));
|
||||
Log.trace(F("MQTTtoLORA json" CR));
|
||||
const char *message = LORAdata["message"];
|
||||
int txPower = LORAdata["txpower"] | LORA_TX_POWER;
|
||||
int spreadingFactor = LORAdata["spreadingfactor"] | LORA_SPREADING_FACTOR;
|
||||
@@ -122,12 +116,12 @@ void MQTTtoLORA(char *topicOri, JsonObject &LORAdata)
|
||||
LoRa.beginPacket();
|
||||
LoRa.print(message);
|
||||
LoRa.endPacket();
|
||||
trc(F("MQTTtoLORA OK"));
|
||||
Log.trace(F("MQTTtoLORA OK" CR));
|
||||
pub(subjectGTWLORAtoMQTT, LORAdata); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("MQTTtoLORA Fail json"));
|
||||
Log.error(F("MQTTtoLORA Fail json" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -140,7 +134,7 @@ void MQTTtoLORA(char *topicOri, char *LORAdata)
|
||||
LoRa.beginPacket();
|
||||
LoRa.print(LORAdata);
|
||||
LoRa.endPacket();
|
||||
trc(F("MQTTtoLORA OK"));
|
||||
Log.notice(F("MQTTtoLORA OK" CR));
|
||||
pub(subjectGTWLORAtoMQTT, LORAdata); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
}
|
||||
|
||||
+22
-24
@@ -36,7 +36,7 @@ void pilightCallback(const String &protocol, const String &message, int status,
|
||||
{
|
||||
if (status == VALID)
|
||||
{
|
||||
trc(F("Creating RF PiLight buffer"));
|
||||
Log.trace(F("Creating RF PiLight buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &RFPiLightdata = jsonBuffer.createObject();
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer2;
|
||||
@@ -49,7 +49,7 @@ void pilightCallback(const String &protocol, const String &message, int status,
|
||||
pub(subjectPilighttoMQTT, RFPiLightdata);
|
||||
if (repeatPilightwMQTT)
|
||||
{
|
||||
trc(F("Pub Pilight for rpt"));
|
||||
Log.trace(F("Pub Pilight for rpt" CR));
|
||||
pub(subjectMQTTtoPilight, RFPiLightdata);
|
||||
}
|
||||
}
|
||||
@@ -57,16 +57,14 @@ void pilightCallback(const String &protocol, const String &message, int status,
|
||||
|
||||
void setupPilight()
|
||||
{
|
||||
#ifndef ZgatewayRF &&ZgatewayRF2 &&ZgatewayRF315 //receiving with Pilight is not compatible with ZgatewayRF or RF2 or RF315 as far as I can tell
|
||||
#ifndef ZgatewayRF &&ZgatewayRF2 //receiving with Pilight is not compatible with ZgatewayRF or RF2 or RF315 as far as I can tell
|
||||
rf.setCallback(pilightCallback);
|
||||
rf.initReceiver(RF_RECEIVER_PIN);
|
||||
trc(F("RF_EMITTER_PIN "));
|
||||
trc(String(RF_EMITTER_PIN));
|
||||
trc(F("RF_RECEIVER_PIN "));
|
||||
trc(String(RF_RECEIVER_PIN));
|
||||
trc(F("ZgatewayPilight setup done "));
|
||||
Log.notice(F("RF_EMITTER_PIN: %d " CR), RF_EMITTER_PIN);
|
||||
Log.notice(F("RF_RECEIVER_PIN: %d " CR), RF_RECEIVER_PIN);
|
||||
Log.trace(F("ZgatewayPilight setup done " CR));
|
||||
#else
|
||||
trc(F("ZgatewayPilight setup cannot be done, comment first ZgatewayRF && ZgatewayRF2 && ZgatewayRF315"));
|
||||
Log.trace(F("ZgatewayPilight setup cannot be done, comment first ZgatewayRF && ZgatewayRF2" CR));
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -82,7 +80,7 @@ void MQTTtoPilight(char *topicOri, JsonObject &Pilightdata)
|
||||
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTtoPilight))
|
||||
{
|
||||
trc(F("MQTTtoPilight json data analysis"));
|
||||
Log.trace(F("MQTTtoPilight json data analysis" CR));
|
||||
const char *message = Pilightdata["message"];
|
||||
const char *protocol = Pilightdata["protocol"];
|
||||
const char *raw = Pilightdata["raw"];
|
||||
@@ -95,43 +93,43 @@ void MQTTtoPilight(char *topicOri, JsonObject &Pilightdata)
|
||||
codes, MAXPULSESTREAMLENGTH);
|
||||
if (msgLength > 0)
|
||||
{
|
||||
trc(F("MQTTtoPilight raw ok"));
|
||||
rf.sendPulseTrain(codes, msgLength);
|
||||
Log.notice(F("MQTTtoPilight raw ok" CR));
|
||||
result = msgLength;
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("MQTTtoPilight raw KO"));
|
||||
Log.trace(F("MQTTtoPilight raw KO" CR));
|
||||
switch (result)
|
||||
{
|
||||
case ESPiLight::ERROR_INVALID_PULSETRAIN_MSG_C:
|
||||
trc(F("'c' not found in string, or has no data"));
|
||||
Log.trace(F("'c' not found in string, or has no data" CR));
|
||||
break;
|
||||
case ESPiLight::ERROR_INVALID_PULSETRAIN_MSG_P:
|
||||
trc(F("'p' not found in string, or has no data"));
|
||||
Log.trace(F("'p' not found in string, or has no data" CR));
|
||||
break;
|
||||
case ESPiLight::ERROR_INVALID_PULSETRAIN_MSG_END:
|
||||
trc(F("';' or '@' not found in data string"));
|
||||
Log.trace(F("';' or '@' not found in data string" CR));
|
||||
break;
|
||||
case ESPiLight::ERROR_INVALID_PULSETRAIN_MSG_TYPE:
|
||||
trc(F("pulse type not defined"));
|
||||
Log.trace(F("pulse type not defined" CR));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (message && protocol)
|
||||
{
|
||||
trc(F("MQTTtoPilight msg & protocol ok"));
|
||||
Log.trace(F("MQTTtoPilight msg & protocol ok" CR));
|
||||
result = rf.send(protocol, message);
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("MQTTtoPilight failed json read"));
|
||||
Log.error(F("MQTTtoPilight failed json read" CR));
|
||||
}
|
||||
|
||||
if (result > 0)
|
||||
{
|
||||
trc(F("Adv data MQTTtoPilight push state via PilighttoMQTT"));
|
||||
Log.trace(F("Adv data MQTTtoPilight push state via PilighttoMQTT" CR));
|
||||
pub(subjectGTWPilighttoMQTT, Pilightdata);
|
||||
}
|
||||
else
|
||||
@@ -139,19 +137,19 @@ void MQTTtoPilight(char *topicOri, JsonObject &Pilightdata)
|
||||
switch (result)
|
||||
{
|
||||
case ESPiLight::ERROR_UNAVAILABLE_PROTOCOL:
|
||||
trc(F("protocol is not available"));
|
||||
Log.error(F("protocol is not available" CR));
|
||||
break;
|
||||
case ESPiLight::ERROR_INVALID_PILIGHT_MSG:
|
||||
trc(F("message is invalid"));
|
||||
Log.error(F("message is invalid" CR));
|
||||
break;
|
||||
case ESPiLight::ERROR_INVALID_JSON:
|
||||
trc(F("message is not a proper json object"));
|
||||
Log.error(F("message is not a proper json object" CR));
|
||||
break;
|
||||
case ESPiLight::ERROR_NO_OUTPUT_PIN:
|
||||
trc(F("no transmitter pin"));
|
||||
Log.error(F("no transmitter pin" CR));
|
||||
break;
|
||||
default:
|
||||
trc(F("invalid json data, can't read raw or message/protocol"));
|
||||
Log.error(F("invalid json data, can't read raw or message/protocol" CR));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
+20
-28
@@ -38,12 +38,11 @@ void RFtoMQTTdiscovery(unsigned long MQTTvalue)
|
||||
{ //on the fly switch creation from received RF values
|
||||
char val[11];
|
||||
sprintf(val, "%lu", MQTTvalue);
|
||||
trc(F("switchRFDiscovery"));
|
||||
Log.trace(F("switchRFDiscovery" CR));
|
||||
char *switchRF[8] = {"switch", val, "", "", "", val, "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySwitch"));
|
||||
trc(switchRF[1]);
|
||||
Log.trace(F("CreateDiscoverySwitch: %s" CR),switchRF[1]);
|
||||
createDiscovery(switchRF[0],
|
||||
subjectRFtoMQTT, switchRF[1], (char *)getUniqueId(switchRF[1], switchRF[2]).c_str(),
|
||||
will_Topic, switchRF[3], switchRF[4],
|
||||
@@ -56,14 +55,12 @@ void setupRF()
|
||||
{
|
||||
|
||||
//RF init parameters
|
||||
Log.notice(F("RF_EMITTER_PIN: %d " CR), RF_EMITTER_PIN);
|
||||
Log.notice(F("RF_RECEIVER_PIN: %d " CR), RF_RECEIVER_PIN);
|
||||
mySwitch.enableTransmit(RF_EMITTER_PIN);
|
||||
trc(F("RF_EMITTER_PIN "));
|
||||
trc(RF_EMITTER_PIN);
|
||||
mySwitch.setRepeatTransmit(RF_EMITTER_REPEAT);
|
||||
mySwitch.enableReceive(RF_RECEIVER_PIN);
|
||||
trc(F("RF_RECEIVER_PIN "));
|
||||
trc(RF_RECEIVER_PIN);
|
||||
trc(F("RF setup ok"));
|
||||
Log.trace(F("ZgatewayRF setup done" CR));
|
||||
}
|
||||
|
||||
void RFtoMQTT()
|
||||
@@ -74,11 +71,9 @@ void RFtoMQTT()
|
||||
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(4);
|
||||
StaticJsonBuffer<JSON_MSG_CALC_BUFFER> jsonBuffer;
|
||||
JsonObject &RFdata = jsonBuffer.createObject();
|
||||
trc(F("Rcv. RF"));
|
||||
Log.trace(F("Rcv. RF" CR));
|
||||
#ifdef ESP32
|
||||
String taskMessage = "RF Task running on core ";
|
||||
taskMessage = taskMessage + xPortGetCoreID();
|
||||
trc(taskMessage);
|
||||
Log.trace(F("RF Task running on core :%d" CR),xPortGetCoreID());
|
||||
#endif
|
||||
RFdata.set("value", (unsigned long)mySwitch.getReceivedValue());
|
||||
RFdata.set("protocol", (int)mySwitch.getReceivedProtocol());
|
||||
@@ -93,11 +88,11 @@ void RFtoMQTT()
|
||||
RFtoMQTTdiscovery(MQTTvalue);
|
||||
#endif
|
||||
pub(subjectRFtoMQTT, RFdata);
|
||||
trc(F("Store val"));
|
||||
Log.trace(F("Store val: %lu" CR), MQTTvalue);
|
||||
storeValue(MQTTvalue);
|
||||
if (repeatRFwMQTT)
|
||||
{
|
||||
trc(F("Pub RF for rpt"));
|
||||
Log.trace(F("Pub RF for rpt" CR));
|
||||
pub(subjectMQTTtoRF, RFdata);
|
||||
}
|
||||
}
|
||||
@@ -121,8 +116,6 @@ void MQTTtoRF(char *topicOri, char *datacallback)
|
||||
{
|
||||
pos = pos + +strlen(RFprotocolKey);
|
||||
valuePRT = (topic.substring(pos, pos + 1)).toInt();
|
||||
trc(F("RF Protocol:"));
|
||||
trc(valuePRT);
|
||||
}
|
||||
//We look into the subject to see if a special RF pulselength is defined
|
||||
int pos2 = topic.lastIndexOf(RFpulselengthKey);
|
||||
@@ -130,21 +123,17 @@ void MQTTtoRF(char *topicOri, char *datacallback)
|
||||
{
|
||||
pos2 = pos2 + strlen(RFpulselengthKey);
|
||||
valuePLSL = (topic.substring(pos2, pos2 + 3)).toInt();
|
||||
trc(F("RF Pulse Lgth:"));
|
||||
trc(valuePLSL);
|
||||
}
|
||||
int pos3 = topic.lastIndexOf(RFbitsKey);
|
||||
if (pos3 != -1)
|
||||
{
|
||||
pos3 = pos3 + strlen(RFbitsKey);
|
||||
valueBITS = (topic.substring(pos3, pos3 + 2)).toInt();
|
||||
trc(F("Bits nb:"));
|
||||
trc(valueBITS);
|
||||
}
|
||||
|
||||
if ((cmpToMainTopic(topicOri, subjectMQTTtoRF)) && (valuePRT == 0) && (valuePLSL == 0) && (valueBITS == 0))
|
||||
{
|
||||
trc(F("MQTTtoRF dflt"));
|
||||
Log.trace(F("MQTTtoRF dflt" CR));
|
||||
mySwitch.setProtocol(1, 350);
|
||||
mySwitch.send(data, 24);
|
||||
// Acknowledgement to the GTWRF topic
|
||||
@@ -152,16 +141,16 @@ void MQTTtoRF(char *topicOri, char *datacallback)
|
||||
}
|
||||
else if ((valuePRT != 0) || (valuePLSL != 0) || (valueBITS != 0))
|
||||
{
|
||||
trc(F("MQTTtoRF usr par."));
|
||||
Log.trace(F("MQTTtoRF usr par." CR));
|
||||
if (valuePRT == 0)
|
||||
valuePRT = 1;
|
||||
if (valuePLSL == 0)
|
||||
valuePLSL = 350;
|
||||
if (valueBITS == 0)
|
||||
valueBITS = 24;
|
||||
trc(valuePRT);
|
||||
trc(valuePLSL);
|
||||
trc(valueBITS);
|
||||
Log.notice(F("RF Protocol:%d" CR), valuePRT);
|
||||
Log.notice(F("RF Pulse Lgth: %d" CR), valuePLSL);
|
||||
Log.notice(F("Bits nb: %d" CR), valueBITS);
|
||||
mySwitch.setProtocol(valuePRT, valuePLSL);
|
||||
mySwitch.send(data, valueBITS);
|
||||
// Acknowledgement to the GTWRF topic
|
||||
@@ -175,7 +164,7 @@ void MQTTtoRF(char *topicOri, JsonObject &RFdata)
|
||||
{ // json object decoding
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTtoRF))
|
||||
{
|
||||
trc(F("MQTTtoRF json"));
|
||||
Log.trace(F("MQTTtoRF json" CR));
|
||||
unsigned long data = RFdata["value"];
|
||||
if (data != 0)
|
||||
{
|
||||
@@ -183,16 +172,19 @@ void MQTTtoRF(char *topicOri, JsonObject &RFdata)
|
||||
int valuePLSL = RFdata["delay"] | 350;
|
||||
int valueBITS = RFdata["length"] | 24;
|
||||
int valueRPT = RFdata["repeat"] | RF_EMITTER_REPEAT;
|
||||
Log.notice(F("RF Protocol:%d" CR), valuePRT);
|
||||
Log.notice(F("RF Pulse Lgth: %d" CR), valuePLSL);
|
||||
Log.notice(F("Bits nb: %d" CR), valueBITS);
|
||||
mySwitch.setRepeatTransmit(valueRPT);
|
||||
mySwitch.setProtocol(valuePRT, valuePLSL);
|
||||
mySwitch.send(data, valueBITS);
|
||||
trc(F("MQTTtoRF OK"));
|
||||
Log.notice(F("MQTTtoRF OK" CR));
|
||||
pub(subjectGTWRFtoMQTT, RFdata); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
mySwitch.setRepeatTransmit(RF_EMITTER_REPEAT); // Restore the default value
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("MQTTtoRF Fail json"));
|
||||
Log.error(F("MQTTtoRF Fail json" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+21
-41
@@ -56,11 +56,9 @@ void setupRF2()
|
||||
{
|
||||
#ifndef ZgatewayRF //receiving with RF2 is not compatible with ZgatewayRF
|
||||
NewRemoteReceiver::init(RF_RECEIVER_PIN, 2, rf2Callback);
|
||||
trc(F("RF_EMITTER_PIN "));
|
||||
trc(RF_EMITTER_PIN);
|
||||
trc(F("RF_RECEIVER_PIN "));
|
||||
trc(RF_RECEIVER_PIN);
|
||||
trc(F("ZgatewayRF2 setup done "));
|
||||
Log.notice(F("RF_EMITTER_PIN: %d " CR), RF_EMITTER_PIN);
|
||||
Log.notice(F("RF_RECEIVER_PIN: %d " CR), RF_RECEIVER_PIN);
|
||||
Log.trace(F("ZgatewayRF2 setup done " CR));
|
||||
#endif
|
||||
pinMode(RF_EMITTER_PIN, OUTPUT);
|
||||
digitalWrite(RF_EMITTER_PIN, LOW);
|
||||
@@ -71,21 +69,20 @@ void RF2toMQTT()
|
||||
|
||||
if (rf2rd.hasNewData)
|
||||
{
|
||||
trc(F("Creating RF2 buffer"));
|
||||
Log.trace(F("Creating RF2 buffer" CR));
|
||||
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(5);
|
||||
StaticJsonBuffer<JSON_MSG_CALC_BUFFER> jsonBuffer;
|
||||
JsonObject &RF2data = jsonBuffer.createObject();
|
||||
|
||||
rf2rd.hasNewData = false;
|
||||
|
||||
trc(F("Rcv. RF2"));
|
||||
Log.trace(F("Rcv. RF2" CR));
|
||||
RF2data.set("unit", (int)rf2rd.unit);
|
||||
RF2data.set("groupBit", (int)rf2rd.groupBit);
|
||||
RF2data.set("period", (int)rf2rd.period);
|
||||
RF2data.set("address", (unsigned long)rf2rd.address);
|
||||
RF2data.set("switchType", (int)rf2rd.switchType);
|
||||
|
||||
trc(F("Adv data RF2toMQTT"));
|
||||
pub(subjectRF2toMQTT, RF2data);
|
||||
}
|
||||
}
|
||||
@@ -123,61 +120,50 @@ void MQTTtoRF2(char *topicOri, char *datacallback)
|
||||
{
|
||||
pos = pos + +strlen(RF2codeKey);
|
||||
valueCODE = (topic.substring(pos, pos + 8)).toInt();
|
||||
trc(F("RF2 code:"));
|
||||
trc(valueCODE);
|
||||
Log.notice(F("RF2 code: %l" CR), valueCODE);
|
||||
}
|
||||
int pos2 = topic.lastIndexOf(RF2periodKey);
|
||||
if (pos2 != -1)
|
||||
{
|
||||
pos2 = pos2 + strlen(RF2periodKey);
|
||||
valuePERIOD = (topic.substring(pos2, pos2 + 3)).toInt();
|
||||
trc(F("RF2 Period:"));
|
||||
trc(valuePERIOD);
|
||||
Log.notice(F("RF2 Period: %d" CR), valuePERIOD);
|
||||
}
|
||||
int pos3 = topic.lastIndexOf(RF2unitKey);
|
||||
if (pos3 != -1)
|
||||
{
|
||||
pos3 = pos3 + strlen(RF2unitKey);
|
||||
valueUNIT = (topic.substring(pos3, topic.indexOf("/", pos3))).toInt();
|
||||
trc(F("Unit:"));
|
||||
trc(valueUNIT);
|
||||
Log.notice(F("Unit: %d" CR), valueUNIT);
|
||||
}
|
||||
int pos4 = topic.lastIndexOf(RF2groupKey);
|
||||
if (pos4 != -1)
|
||||
{
|
||||
pos4 = pos4 + strlen(RF2groupKey);
|
||||
valueGROUP = (topic.substring(pos4, pos4 + 1)).toInt();
|
||||
trc(F("RF2 Group:"));
|
||||
trc(valueGROUP);
|
||||
Log.notice(F("RF2 Group: %d" CR),valueGROUP);
|
||||
}
|
||||
int pos5 = topic.lastIndexOf(RF2dimKey);
|
||||
if (pos5 != -1)
|
||||
{
|
||||
isDimCommand = true;
|
||||
valueDIM = atoi(datacallback);
|
||||
trc(F("RF2 Dim:"));
|
||||
trc(valueDIM);
|
||||
Log.notice(F("RF2 Dim: %d" CR), valueDIM);
|
||||
}
|
||||
|
||||
if ((topic == subjectMQTTtoRF2) || (valueCODE != 0) || (valueUNIT != -1) || (valuePERIOD != 0))
|
||||
{
|
||||
trc(F("MQTTtoRF2"));
|
||||
Log.trace(F("MQTTtoRF2" CR));
|
||||
if (valueCODE == 0)
|
||||
valueCODE = 8233378;
|
||||
if (valueUNIT == -1)
|
||||
valueUNIT = 0;
|
||||
if (valuePERIOD == 0)
|
||||
valuePERIOD = 272;
|
||||
trc(valueCODE);
|
||||
trc(valueUNIT);
|
||||
trc(valuePERIOD);
|
||||
trc(valueGROUP);
|
||||
trc(boolSWITCHTYPE);
|
||||
trc(valueDIM);
|
||||
NewRemoteReceiver::disable();
|
||||
trc(F("Creating transmitter"));
|
||||
Log.trace(F("Creating transmitter" CR));
|
||||
NewRemoteTransmitter transmitter(valueCODE, RF_EMITTER_PIN, valuePERIOD);
|
||||
trc(F("Sending data"));
|
||||
Log.trace(F("Sending data" CR));
|
||||
if (valueGROUP)
|
||||
{
|
||||
if (isDimCommand)
|
||||
@@ -200,7 +186,7 @@ void MQTTtoRF2(char *topicOri, char *datacallback)
|
||||
transmitter.sendUnit(valueUNIT, boolSWITCHTYPE);
|
||||
}
|
||||
}
|
||||
trc(F("Data sent"));
|
||||
Log.trace(F("Data sent" CR));
|
||||
NewRemoteReceiver::enable();
|
||||
|
||||
// Publish state change back to MQTT
|
||||
@@ -218,7 +204,7 @@ void MQTTtoRF2(char *topicOri, char *datacallback)
|
||||
MQTTswitchType = String(boolSWITCHTYPE);
|
||||
MQTTdimLevel = String(valueDIM);
|
||||
String MQTTRF2string;
|
||||
trc(F("Adv data MQTTtoRF2 push state via RF2toMQTT"));
|
||||
Log.trace(F("Adv data MQTTtoRF2 push state via RF2toMQTT" CR));
|
||||
if (isDimCommand)
|
||||
{
|
||||
MQTTRF2string = subjectRF2toMQTT + String("/") + RF2codeKey + MQTTAddress + String("/") + RF2unitKey + MQTTunit + String("/") + RF2groupKey + MQTTgroupBit + String("/") + RF2dimKey + String("/") + RF2periodKey + MQTTperiod;
|
||||
@@ -239,11 +225,11 @@ void MQTTtoRF2(char *topicOri, JsonObject &RF2data)
|
||||
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTtoRF2))
|
||||
{
|
||||
trc(F("MQTTtoRF2 json"));
|
||||
Log.trace(F("MQTTtoRF2 json" CR));
|
||||
int boolSWITCHTYPE = RF2data["switchType"] | 99;
|
||||
if (boolSWITCHTYPE != 99)
|
||||
{
|
||||
trc(F("MQTTtoRF2 switch type ok"));
|
||||
Log.trace(F("MQTTtoRF2 switch type ok" CR));
|
||||
bool isDimCommand = boolSWITCHTYPE == 2;
|
||||
unsigned long valueCODE = RF2data["address"];
|
||||
int valueUNIT = RF2data["unit"] | -1;
|
||||
@@ -252,22 +238,16 @@ void MQTTtoRF2(char *topicOri, JsonObject &RF2data)
|
||||
int valueDIM = RF2data["dim"] | -1;
|
||||
if ((valueCODE != 0) || (valueUNIT != -1) || (valuePERIOD != 0))
|
||||
{
|
||||
trc(F("MQTTtoRF2"));
|
||||
Log.trace(F("MQTTtoRF2" CR));
|
||||
if (valueCODE == 0)
|
||||
valueCODE = 8233378;
|
||||
if (valueUNIT == -1)
|
||||
valueUNIT = 0;
|
||||
if (valuePERIOD == 0)
|
||||
valuePERIOD = 272;
|
||||
trc(valueCODE);
|
||||
trc(valueUNIT);
|
||||
trc(valuePERIOD);
|
||||
trc(valueGROUP);
|
||||
trc(boolSWITCHTYPE);
|
||||
trc(valueDIM);
|
||||
NewRemoteReceiver::disable();
|
||||
NewRemoteTransmitter transmitter(valueCODE, RF_EMITTER_PIN, valuePERIOD);
|
||||
trc(F("Sending"));
|
||||
Log.trace(F("Sending" CR));
|
||||
if (valueGROUP)
|
||||
{
|
||||
if (isDimCommand)
|
||||
@@ -290,7 +270,7 @@ void MQTTtoRF2(char *topicOri, JsonObject &RF2data)
|
||||
transmitter.sendUnit(valueUNIT, boolSWITCHTYPE);
|
||||
}
|
||||
}
|
||||
trc(F("MQTTtoRF2 OK"));
|
||||
Log.notice(F("MQTTtoRF2 OK" CR));
|
||||
NewRemoteReceiver::enable();
|
||||
|
||||
// Publish state change back to MQTT
|
||||
@@ -299,7 +279,7 @@ void MQTTtoRF2(char *topicOri, JsonObject &RF2data)
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("MQTTtoRF2 failed json read"));
|
||||
Log.error(F("MQTTtoRF2 failed json read" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+16
-20
@@ -78,7 +78,7 @@ void eeprom_setup()
|
||||
// if checksum bad init GC else use GC values
|
||||
if (gc_checksum() != pGC->checksum)
|
||||
{
|
||||
trc(F("Factory reset"));
|
||||
Log.trace(F("Factory reset" CR));
|
||||
memset(pGC, 0, sizeof(*pGC));
|
||||
strcpy_P(pGC->encryptkey, ENCRYPTKEY);
|
||||
strcpy_P(pGC->rfmapname, RFM69AP_NAME);
|
||||
@@ -109,7 +109,7 @@ void setupRFM69(void)
|
||||
// Initialize radio
|
||||
if (!radio.initialize(pGC->rfmfrequency, pGC->nodeid, pGC->networkid))
|
||||
{
|
||||
trc(F("ZgatewayRFM69 initialization failed"));
|
||||
Log.error(F("ZgatewayRFM69 initialization failed" CR));
|
||||
}
|
||||
|
||||
if (GC_IS_RFM69HCW)
|
||||
@@ -121,7 +121,6 @@ void setupRFM69(void)
|
||||
if (pGC->encryptkey[0] != '\0')
|
||||
radio.encrypt(pGC->encryptkey);
|
||||
|
||||
trc(F("ZgatewayRFM69 Listening and transmitting at"));
|
||||
switch (pGC->rfmfrequency)
|
||||
{
|
||||
case RF69_433MHZ:
|
||||
@@ -140,13 +139,13 @@ void setupRFM69(void)
|
||||
freq = -1;
|
||||
break;
|
||||
}
|
||||
trc(freq);
|
||||
Log.notice(F("ZgatewayRFM69 Listening and transmitting at: %d" CR), freq);
|
||||
|
||||
size_t len = snprintf_P(RadioConfig, sizeof(RadioConfig), JSONtemplate,
|
||||
freq, GC_IS_RFM69HCW, pGC->networkid, GC_POWER_LEVEL);
|
||||
if (len >= sizeof(RadioConfig))
|
||||
{
|
||||
trc(F("\n\n*** RFM69 config truncated ***\n"));
|
||||
Log.trace(F("\n\n*** RFM69 config truncated ***\n" CR));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +154,7 @@ bool RFM69toMQTT(void)
|
||||
//check if something was received (could be an interrupt from the radio)
|
||||
if (radio.receiveDone())
|
||||
{
|
||||
trc(F("Creating RFM69 buffer"));
|
||||
Log.trace(F("Creating RFM69 buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &RFM69data = jsonBuffer.createObject();
|
||||
uint8_t data[RF69_MAX_DATA_LEN + 1]; // For the null character
|
||||
@@ -175,8 +174,7 @@ bool RFM69toMQTT(void)
|
||||
}
|
||||
//updateClients(senderId, rssi, (const char *)data);
|
||||
|
||||
trc(F("Data received"));
|
||||
trc((const char *)data);
|
||||
Log.trace(F("Data received: %s" CR), (const char *)data);
|
||||
|
||||
char buff[sizeof(subjectRFM69toMQTT) + 4];
|
||||
sprintf(buff, "%s/%d", subjectRFM69toMQTT, SENDERID);
|
||||
@@ -199,7 +197,7 @@ void MQTTtoRFM69(char *topicOri, char *datacallback)
|
||||
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTtoRFM69))
|
||||
{
|
||||
trc(F("MQTTtoRFM69 data analysis"));
|
||||
Log.trace(F("MQTTtoRFM69 data analysis" CR));
|
||||
char data[RF69_MAX_DATA_LEN + 1];
|
||||
memcpy(data, (void *)datacallback, RF69_MAX_DATA_LEN);
|
||||
data[RF69_MAX_DATA_LEN] = '\0';
|
||||
@@ -212,12 +210,11 @@ void MQTTtoRFM69(char *topicOri, char *datacallback)
|
||||
{
|
||||
pos = pos + +strlen(RFM69receiverKey);
|
||||
valueRCV = (topic.substring(pos, pos + 3)).toInt();
|
||||
trc(F("RFM69 receiver ID:"));
|
||||
trc(valueRCV);
|
||||
Log.notice(F("RFM69 receiver ID: %d" CR), valueRCV);
|
||||
}
|
||||
if (radio.sendWithRetry(valueRCV, data, strlen(data), 10))
|
||||
{
|
||||
trc(F(" OK "));
|
||||
Log.notice(F(" OK " CR));
|
||||
// Acknowledgement to the GTWRF topic
|
||||
char buff[sizeof(subjectGTWRFM69toMQTT) + 4];
|
||||
sprintf(buff, "%s/%d", subjectGTWRFM69toMQTT, radio.SENDERID);
|
||||
@@ -225,7 +222,7 @@ void MQTTtoRFM69(char *topicOri, char *datacallback)
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("RFM69 sending failed"));
|
||||
Log.error(F("RFM69 sending failed" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -237,27 +234,26 @@ void MQTTtoRFM69(char *topicOri, JsonObject &RFM69data)
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTtoRFM69))
|
||||
{
|
||||
const char *data = RFM69data["data"];
|
||||
trc(F("MQTTtoRFM69 json data analysis"));
|
||||
Log.trace(F("MQTTtoRFM69 json data analysis" CR));
|
||||
if (data)
|
||||
{
|
||||
trc(F("MQTTtoRFM69 data ok"));
|
||||
Log.trace(F("MQTTtoRFM69 data ok" CR));
|
||||
int valueRCV = RFM69data["receiverid"] | defaultRFM69ReceiverId; //default receiver id value
|
||||
trc(F("RFM69 receiver ID:"));
|
||||
trc(valueRCV);
|
||||
Log.notice(F("RFM69 receiver ID: %d" CR), valueRCV);
|
||||
if (radio.sendWithRetry(valueRCV, data, strlen(data), 10))
|
||||
{
|
||||
trc(F(" OK "));
|
||||
Log.notice(F(" OK " CR));
|
||||
// Acknowledgement to the GTWRF topic
|
||||
pub(subjectGTWRFM69toMQTT, RFM69data); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("MQTTtoRFM69 sending failed"));
|
||||
Log.error(F("MQTTtoRFM69 sending failed" CR));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("MQTTtoRFM69 failed json read"));
|
||||
Log.error(F("MQTTtoRFM69 failed json read" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+25
-25
@@ -37,8 +37,8 @@ unsigned char _uartpos = 0;
|
||||
|
||||
void setupSRFB()
|
||||
{
|
||||
trc(F("ZgatewaySRFB setup done "));
|
||||
trc("Serial Baud" + String(SERIAL_BAUD));
|
||||
Log.trace(F("ZgatewaySRFB setup done " CR));
|
||||
Log.trace(F("Serial Baud: %l" CR),SERIAL_BAUD);
|
||||
}
|
||||
|
||||
void _rfbSend(byte *message)
|
||||
@@ -60,7 +60,7 @@ void _rfbSend(byte *message, int times)
|
||||
|
||||
char buffer[RF_MESSAGE_SIZE];
|
||||
_rfbToChar(message, buffer);
|
||||
trc(F("[RFBRIDGE] Sending MESSAGE '%s' %d time(s)\n"));
|
||||
Log.notice(F("[RFBRIDGE] Sending MESSAGE" CR));
|
||||
|
||||
for (int i = 0; i < times; i++)
|
||||
{
|
||||
@@ -120,7 +120,7 @@ void _rfbDecode()
|
||||
{
|
||||
_rfbToChar(&_uartbuf[1], buffer);
|
||||
|
||||
trc(F("Creating SRFB buffer"));
|
||||
Log.trace(F("Creating SRFB buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &SRFBdata = jsonBuffer.createObject();
|
||||
SRFBdata.set("raw", (char *)buffer);
|
||||
@@ -147,13 +147,13 @@ void _rfbDecode()
|
||||
|
||||
if (!isAduplicate(MQTTvalue) && MQTTvalue != 0)
|
||||
{ // conditions to avoid duplications of RF -->MQTT
|
||||
trc(F("Adv data SRFBtoMQTT"));
|
||||
Log.trace(F("Adv data SRFBtoMQTT" CR));
|
||||
pub(subjectSRFBtoMQTT, SRFBdata);
|
||||
trc(F("Store val"));
|
||||
Log.trace(F("Store val: %lu" CR), MQTTvalue);
|
||||
storeValue(MQTTvalue);
|
||||
if (repeatSRFBwMQTT)
|
||||
{
|
||||
trc(F("Publish SRFB for rpt"));
|
||||
Log.trace(F("Publish SRFB for rpt" CR));
|
||||
pub(subjectMQTTtoSRFB, SRFBdata);
|
||||
}
|
||||
}
|
||||
@@ -163,7 +163,7 @@ void _rfbDecode()
|
||||
|
||||
void _rfbAck()
|
||||
{
|
||||
trc(F("[RFBRIDGE] Sending ACK\n"));
|
||||
Log.trace(F("[RFBRIDGE] Sending ACK\n" CR));
|
||||
Serial.println();
|
||||
Serial.write(RF_CODE_START);
|
||||
Serial.write(RF_CODE_ACK);
|
||||
@@ -195,7 +195,7 @@ bool _rfbToChar(byte *in, char *out)
|
||||
{
|
||||
for (unsigned char p = 0; p < RF_MESSAGE_SIZE; p++)
|
||||
{
|
||||
sprintf_P(&out[p * 2], PSTR("%02X"), in[p]);
|
||||
sprintf_P(&out[p * 2], PSTR("%02X" CR), in[p]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -220,8 +220,7 @@ void MQTTtoSRFB(char *topicOri, char *datacallback)
|
||||
{
|
||||
pos = pos + +strlen(SRFBRptKey);
|
||||
valueRPT = (topic.substring(pos, pos + 1)).toInt();
|
||||
trc(F("SRFB Repeat:"));
|
||||
trc(valueRPT);
|
||||
Log.notice(F("SRFB Repeat: %d" CR),valueRPT);
|
||||
}
|
||||
|
||||
int pos2 = topic.lastIndexOf(SRFBminipulselengthKey);
|
||||
@@ -229,8 +228,7 @@ void MQTTtoSRFB(char *topicOri, char *datacallback)
|
||||
{
|
||||
pos2 = pos2 + strlen(SRFBminipulselengthKey);
|
||||
valueMiniPLSL = (topic.substring(pos2, pos2 + 3)).toInt();
|
||||
trc(F("RF Mini Pulse Lgth:"));
|
||||
trc(valueMiniPLSL);
|
||||
Log.notice(F("RF Mini Pulse Lgth: %d" CR),valueMiniPLSL);
|
||||
}
|
||||
|
||||
int pos3 = topic.lastIndexOf(SRFBmaxipulselengthKey);
|
||||
@@ -238,8 +236,7 @@ void MQTTtoSRFB(char *topicOri, char *datacallback)
|
||||
{
|
||||
pos3 = pos3 + strlen(SRFBmaxipulselengthKey);
|
||||
valueMaxiPLSL = (topic.substring(pos3, pos3 + 2)).toInt();
|
||||
trc(F("RF Maxi Pulse Lgth:"));
|
||||
trc(valueMaxiPLSL);
|
||||
Log.notice(F("RF Maxi Pulse Lgth: %d" CR), valueMaxiPLSL);
|
||||
}
|
||||
|
||||
int pos4 = topic.lastIndexOf(SRFBsyncKey);
|
||||
@@ -247,11 +244,10 @@ void MQTTtoSRFB(char *topicOri, char *datacallback)
|
||||
{
|
||||
pos4 = pos4 + strlen(SRFBsyncKey);
|
||||
valueSYNC = (topic.substring(pos4, pos4 + 2)).toInt();
|
||||
trc(F("RF sync:"));
|
||||
trc(valueSYNC);
|
||||
Log.notice(F("RF sync: %d" CR), valueSYNC);
|
||||
}
|
||||
|
||||
trc(F("MQTTtoSRFB prts"));
|
||||
Log.trace(F("MQTTtoSRFB prts" CR));
|
||||
if (valueRPT == 0)
|
||||
valueRPT = 1;
|
||||
if (valueMiniPLSL == 0)
|
||||
@@ -298,8 +294,7 @@ void MQTTtoSRFB(char *topicOri, char *datacallback)
|
||||
{
|
||||
pos = pos + +strlen(SRFBRptKey);
|
||||
valueRPT = (topic.substring(pos, pos + 1)).toInt();
|
||||
trc(F("SRFB Repeat:"));
|
||||
trc(valueRPT);
|
||||
Log.notice(F("SRFB Repeat: %d" CR), valueRPT);
|
||||
}
|
||||
if (valueRPT == 0)
|
||||
valueRPT = 1;
|
||||
@@ -321,10 +316,10 @@ void MQTTtoSRFB(char *topicOri, JsonObject &SRFBdata)
|
||||
int valueRPT = SRFBdata["repeat"] | 1;
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTtoSRFB))
|
||||
{
|
||||
trc(F("MQTTtoSRFB json"));
|
||||
Log.trace(F("MQTTtoSRFB json" CR));
|
||||
if (raw)
|
||||
{ // send raw in priority when defined in the json
|
||||
trc(F("MQTTtoSRFB raw ok"));
|
||||
Log.trace(F("MQTTtoSRFB raw ok" CR));
|
||||
byte message_b[RF_MESSAGE_SIZE];
|
||||
_rfbToArray(raw, message_b);
|
||||
_rfbSend(message_b, valueRPT);
|
||||
@@ -334,7 +329,7 @@ void MQTTtoSRFB(char *topicOri, JsonObject &SRFBdata)
|
||||
unsigned long data = SRFBdata["value"];
|
||||
if (data != 0)
|
||||
{
|
||||
trc(F("MQTTtoSRFB data ok"));
|
||||
Log.notice(F("MQTTtoSRFB data ok" CR));
|
||||
int valueMiniPLSL = SRFBdata["val_Tlow"];
|
||||
int valueMaxiPLSL = SRFBdata["val_Thigh"];
|
||||
int valueSYNC = SRFBdata["delay"];
|
||||
@@ -348,6 +343,11 @@ void MQTTtoSRFB(char *topicOri, JsonObject &SRFBdata)
|
||||
if (valueSYNC == 0)
|
||||
valueSYNC = 9500;
|
||||
|
||||
Log.notice(F("SRFB Repeat: %d" CR),valueRPT);
|
||||
Log.notice(F("RF Mini Pulse Lgth: %d" CR),valueMiniPLSL);
|
||||
Log.notice(F("RF Maxi Pulse Lgth: %d" CR), valueMaxiPLSL);
|
||||
Log.notice(F("RF sync: %d" CR), valueSYNC);
|
||||
|
||||
byte hex_valueMiniPLSL[2];
|
||||
hex_valueMiniPLSL[0] = (int)((valueMiniPLSL >> 8) & 0xFF);
|
||||
hex_valueMiniPLSL[1] = (int)(valueMiniPLSL & 0xFF);
|
||||
@@ -372,14 +372,14 @@ void MQTTtoSRFB(char *topicOri, JsonObject &SRFBdata)
|
||||
memcpy(message_b + 4, hex_valueMaxiPLSL, 2);
|
||||
memcpy(message_b + 6, hex_data, 3);
|
||||
|
||||
trc(F("MQTTtoSRFB OK"));
|
||||
Log.notice(F("MQTTtoSRFB OK" CR));
|
||||
_rfbSend(message_b, valueRPT);
|
||||
// Acknowledgement to the GTWRF topic
|
||||
pub(subjectGTWSRFBtoMQTT, SRFBdata); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("MQTTtoSRFB error decoding value"));
|
||||
Log.error(F("MQTTtoSRFB error decoding value" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,11 +48,10 @@ void PairedDeviceAdded(byte newID)
|
||||
|
||||
void setupWeatherStation()
|
||||
{
|
||||
trc(F("RF_WS_RECEIVER_PIN "));
|
||||
trc(String(RF_WS_RECEIVER_PIN));
|
||||
Log.notice(F("RF_WS_RECEIVER_PIN %d" CR),RF_WS_RECEIVER_PIN);
|
||||
wsdr.begin();
|
||||
wsdr.pair(NULL, PairedDeviceAdded);
|
||||
trc(F("ZgatewayWeatherStation setup done "));
|
||||
Log.trace(F("ZgatewayWeatherStation setup done " CR));
|
||||
}
|
||||
|
||||
void sendWindSpeedData(byte id, float wind_speed, byte battery_status)
|
||||
@@ -67,7 +66,7 @@ void sendWindSpeedData(byte id, float wind_speed, byte battery_status)
|
||||
RFdata.set("wind_speed", wind_speed);
|
||||
RFdata.set("battery", bitRead(battery_status, 0) == 0 ? "OK" : "Low");
|
||||
pub(subjectRFtoMQTT, RFdata);
|
||||
trc(F("Store wind speed val"));
|
||||
Log.trace(F("Store wind speed val: %lu" CR), MQTTvalue);
|
||||
storeValue(MQTTvalue);
|
||||
}
|
||||
}
|
||||
@@ -84,7 +83,7 @@ void sendRainData(byte id, float rain_volume, byte battery_status)
|
||||
RFdata.set("rain_volume", rain_volume);
|
||||
RFdata.set("battery", bitRead(battery_status, 1) == 0 ? "OK" : "Low");
|
||||
pub(subjectRFtoMQTT, RFdata);
|
||||
trc(F("Store rain_volume"));
|
||||
Log.trace(F("Store rain_volume: %lu" CR), MQTTvalue);
|
||||
storeValue(MQTTvalue);
|
||||
}
|
||||
}
|
||||
@@ -102,7 +101,7 @@ void sendWindData(byte id, int wind_direction, float wind_gust, byte battery_sta
|
||||
RFdata.set("wind_gust", wind_gust);
|
||||
RFdata.set("battery", bitRead(battery_status, 0) == 0 ? "OK" : "Low");
|
||||
pub(subjectRFtoMQTT, RFdata);
|
||||
trc(F("Store wind data val"));
|
||||
Log.trace(F("Store wind data val: %lu" CR), MQTTvalue);
|
||||
storeValue(MQTTvalue);
|
||||
}
|
||||
}
|
||||
@@ -120,7 +119,7 @@ void sendTemperatureData(byte id, float temperature, int humidity, byte battery_
|
||||
RFdata.set("humidity", humidity);
|
||||
RFdata.set("battery", bitRead(battery_status, 0) == 0 ? "OK" : "Low");
|
||||
pub(subjectRFtoMQTT, RFdata);
|
||||
trc(F("Store temperature val"));
|
||||
Log.trace(F("Store temp val: %lu" CR), MQTTvalue);
|
||||
storeValue(MQTTvalue);
|
||||
}
|
||||
}
|
||||
@@ -131,22 +130,22 @@ void ZgatewayWeatherStationtoMQTT()
|
||||
switch (newData)
|
||||
{
|
||||
case 'T':
|
||||
trc(F("Temperature"));
|
||||
Log.trace(F("Temperature" CR));
|
||||
sendTemperatureData(wsdr.sensorID(), wsdr.readTemperature(), wsdr.readHumidity(), wsdr.batteryStatus());
|
||||
break;
|
||||
|
||||
case 'S':
|
||||
trc(F("Wind speed"));
|
||||
Log.trace(F("Wind speed" CR));
|
||||
sendWindSpeedData(wsdr.sensorID(), wsdr.readWindSpeed(), wsdr.batteryStatus());
|
||||
break;
|
||||
|
||||
case 'G':
|
||||
trc(F("Wind direction"));
|
||||
Log.trace(F("Wind direction" CR));
|
||||
sendWindData(wsdr.sensorID(), wsdr.readWindDirection(), wsdr.readWindGust(), wsdr.batteryStatus());
|
||||
break;
|
||||
|
||||
case 'R':
|
||||
trc(F("Rain volume"));
|
||||
Log.trace(F("Rain volume" CR));
|
||||
sendRainData(wsdr.sensorID(), wsdr.readRainVolume(), wsdr.batteryStatus());
|
||||
break;
|
||||
|
||||
|
||||
+55
-55
@@ -117,7 +117,7 @@ void createDiscovery(char *sensor_type,
|
||||
|
||||
void pubMqttDiscovery()
|
||||
{
|
||||
trc(F("omgStatusDiscovery"));
|
||||
Log.trace(F("omgStatusDiscovery" CR));
|
||||
createDiscovery("binary_sensor", //set Type
|
||||
will_Topic, Gateway_Name, (char *)getUniqueId("", "").c_str(), //set state_topic,name,uniqueId
|
||||
will_Topic, "connectivity", "", //set availability_topic,device_class,value_template,
|
||||
@@ -142,7 +142,7 @@ void pubMqttDiscovery()
|
||||
|
||||
#ifdef ZsensorBME280
|
||||
#define BMEparametersCount 6
|
||||
trc(F("bme280Discovery"));
|
||||
Log.trace(F("bme280Discovery" CR));
|
||||
char *BMEsensor[BMEparametersCount][8] = {
|
||||
{"sensor", "tempc", "bme", "temperature", "{{ value_json.tempc }}", "", "", "°C"},
|
||||
{"sensor", "tempf", "bme", "temperature", "{{ value_json.tempf }}", "", "", "°F"},
|
||||
@@ -155,8 +155,8 @@ void pubMqttDiscovery()
|
||||
|
||||
for (int i = 0; i < BMEparametersCount; i++)
|
||||
{
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(BMEsensor[i][1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(BMEsensor[i][1]);
|
||||
createDiscovery(BMEsensor[i][0],
|
||||
BMETOPIC, BMEsensor[i][1], (char *)getUniqueId(BMEsensor[i][1], BMEsensor[i][2]).c_str(),
|
||||
will_Topic, BMEsensor[i][3], BMEsensor[i][4],
|
||||
@@ -167,15 +167,15 @@ void pubMqttDiscovery()
|
||||
|
||||
#ifdef ZsensorDHT
|
||||
#define DHTparametersCount 2
|
||||
trc(F("DHTDiscovery"));
|
||||
Log.trace(F("DHTDiscovery" CR));
|
||||
char *DHTsensor[DHTparametersCount][8] = {
|
||||
{"sensor", "tempc", "dht", "temperature", "{{ value_json.temp }}", "", "", "°C"},
|
||||
{"sensor", "hum", "dht", "humidity", "{{ value_json.hum }}", "", "", "%"}};
|
||||
|
||||
for (int i = 0; i < DHTparametersCount; i++)
|
||||
{
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(DHTsensor[i][1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(DHTsensor[i][1]);
|
||||
createDiscovery(DHTsensor[i][0],
|
||||
DHTTOPIC, DHTsensor[i][1], (char *)getUniqueId(DHTsensor[i][1], DHTsensor[i][2]).c_str(),
|
||||
will_Topic, DHTsensor[i][3], DHTsensor[i][4],
|
||||
@@ -185,12 +185,12 @@ void pubMqttDiscovery()
|
||||
#endif
|
||||
|
||||
#ifdef ZsensorADC
|
||||
trc(F("ADCDiscovery"));
|
||||
Log.trace(F("ADCDiscovery" CR));
|
||||
char *ADCsensor[8] = {"sensor", "adc", "", "", "{{ value_json.adc }}", "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(ADCsensor[1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(ADCsensor[1]);
|
||||
createDiscovery(ADCsensor[0],
|
||||
ADCTOPIC, ADCsensor[1], (char *)getUniqueId(ADCsensor[1], ADCsensor[2]).c_str(),
|
||||
will_Topic, ADCsensor[3], ADCsensor[4],
|
||||
@@ -200,7 +200,7 @@ void pubMqttDiscovery()
|
||||
|
||||
#ifdef ZsensorBH1750
|
||||
#define BH1750parametersCount 3
|
||||
trc(F("BH1750Discovery"));
|
||||
Log.trace(F("BH1750Discovery" CR));
|
||||
char *BH1750sensor[BH1750parametersCount][8] = {
|
||||
{"sensor", "lux", "BH1750", "illuminance", "{{ value_json.lux }}", "", "", "lu"},
|
||||
{"sensor", "ftCd", "BH1750", "", "{{ value_json.ftCd }}", "", "", ""},
|
||||
@@ -210,8 +210,8 @@ void pubMqttDiscovery()
|
||||
|
||||
for (int i = 0; i < BH1750parametersCount; i++)
|
||||
{
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(BH1750sensor[i][1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(BH1750sensor[i][1]);
|
||||
createDiscovery(BH1750sensor[i][0],
|
||||
subjectBH1750toMQTT, BH1750sensor[i][1], (char *)getUniqueId(BH1750sensor[i][1], BH1750sensor[i][2]).c_str(),
|
||||
will_Topic, BH1750sensor[i][3], BH1750sensor[i][4],
|
||||
@@ -222,7 +222,7 @@ void pubMqttDiscovery()
|
||||
|
||||
#ifdef ZsensorTSL2561
|
||||
#define TSL2561parametersCount 3
|
||||
trc(F("TSL2561Discovery"));
|
||||
Log.trace(F("TSL2561Discovery" CR));
|
||||
char *TSL2561sensor[TSL2561parametersCount][8] = {
|
||||
{"sensor", "lux", "TSL2561", "illuminance", "{{ value_json.lux }}", "", "", "lu"},
|
||||
{"sensor", "ftcd", "TSL2561", "", "{{ value_json.ftcd }}", "", "", ""},
|
||||
@@ -232,8 +232,8 @@ void pubMqttDiscovery()
|
||||
|
||||
for (int i = 0; i < TSL2561parametersCount; i++)
|
||||
{
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(TSL2561sensor[i][1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(TSL2561sensor[i][1]);
|
||||
createDiscovery(TSL2561sensor[i][0],
|
||||
subjectTSL12561toMQTT, TSL2561sensor[i][1], (char *)getUniqueId(TSL2561sensor[i][1], TSL2561sensor[i][2]).c_str(),
|
||||
will_Topic, TSL2561sensor[i][3], TSL2561sensor[i][4],
|
||||
@@ -243,12 +243,12 @@ void pubMqttDiscovery()
|
||||
#endif
|
||||
|
||||
#ifdef ZsensorHCSR501
|
||||
trc(F("HCSR501Discovery"));
|
||||
Log.trace(F("HCSR501Discovery" CR));
|
||||
char *HCSR501sensor[8] = {"binary_sensor", "hcsr501", "", "", "{{value_json.hcsr501}}", "true", "false", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(HCSR501sensor[1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(HCSR501sensor[1]);
|
||||
createDiscovery(HCSR501sensor[0],
|
||||
subjectHCSR501toMQTT, HCSR501sensor[1], (char *)getUniqueId(HCSR501sensor[1], HCSR501sensor[2]).c_str(),
|
||||
will_Topic, HCSR501sensor[3], HCSR501sensor[4],
|
||||
@@ -257,12 +257,12 @@ void pubMqttDiscovery()
|
||||
#endif
|
||||
|
||||
#ifdef ZsensorGPIOInput
|
||||
trc(F("GPIOInputDiscovery"));
|
||||
Log.trace(F("GPIOInputDiscovery" CR));
|
||||
char *GPIOInputsensor[8] = {"binary_sensor", "GPIOInput", "", "", "{{value_json.gpio}}", "HIGH", "LOW", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(GPIOInputsensor[1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(GPIOInputsensor[1]);
|
||||
createDiscovery(GPIOInputsensor[0],
|
||||
subjectGPIOInputtoMQTT, GPIOInputsensor[1], (char *)getUniqueId(GPIOInputsensor[1], GPIOInputsensor[2]).c_str(),
|
||||
will_Topic, GPIOInputsensor[3], GPIOInputsensor[4],
|
||||
@@ -272,7 +272,7 @@ void pubMqttDiscovery()
|
||||
|
||||
#ifdef ZsensorINA226
|
||||
#define INA226parametersCount 3
|
||||
trc(F("INA226Discovery"));
|
||||
Log.trace(F("INA226Discovery" CR));
|
||||
char *INA226sensor[INA226parametersCount][8] = {
|
||||
{"sensor", "volt", "INA226", "", "{{ value_json.volt }}", "", "", "V"},
|
||||
{"sensor", "current", "INA226", "", "{{ value_json.current }}", "", "", "A"},
|
||||
@@ -282,8 +282,8 @@ void pubMqttDiscovery()
|
||||
|
||||
for (int i = 0; i < INA226parametersCount; i++)
|
||||
{
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(INA226sensor[i][1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(INA226sensor[i][1]);
|
||||
createDiscovery(INA226sensor[i][0],
|
||||
subjectINA226toMQTT, INA226sensor[i][1], (char *)getUniqueId(INA226sensor[i][1], INA226sensor[i][2]).c_str(),
|
||||
will_Topic, INA226sensor[i][3], INA226sensor[i][4],
|
||||
@@ -293,12 +293,12 @@ void pubMqttDiscovery()
|
||||
#endif
|
||||
|
||||
#ifdef ZactuatorONOFF
|
||||
trc(F("actuatorONOFFDiscovery"));
|
||||
Log.trace(F("actuatorONOFFDiscovery" CR));
|
||||
char *actuatorONOFF[8] = {"switch", "actuatorONOFF", "", "", "", "{\"cmd\":1}", "{\"cmd\":0}", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(actuatorONOFF[1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(actuatorONOFF[1]);
|
||||
createDiscovery(actuatorONOFF[0],
|
||||
subjectGTWONOFFtoMQTT, actuatorONOFF[1], (char *)getUniqueId(actuatorONOFF[1], actuatorONOFF[2]).c_str(),
|
||||
will_Topic, actuatorONOFF[3], actuatorONOFF[4],
|
||||
@@ -308,12 +308,12 @@ void pubMqttDiscovery()
|
||||
|
||||
#ifdef ZgatewayRF
|
||||
// Sensor to display RF received value
|
||||
trc(F("gatewayRFDiscovery"));
|
||||
Log.trace(F("gatewayRFDiscovery" CR));
|
||||
char *gatewayRF[8] = {"sensor", "gatewayRF", "", "", "{{ value_json.value }}", "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayRF[1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewayRF[1]);
|
||||
createDiscovery(gatewayRF[0],
|
||||
subjectRFtoMQTT, gatewayRF[1], (char *)getUniqueId(gatewayRF[1], gatewayRF[2]).c_str(),
|
||||
will_Topic, gatewayRF[3], gatewayRF[4],
|
||||
@@ -323,12 +323,12 @@ void pubMqttDiscovery()
|
||||
|
||||
#ifdef ZgatewayRF2
|
||||
// Sensor to display RF received value
|
||||
trc(F("gatewayRF2Discovery"));
|
||||
Log.trace(F("gatewayRF2Discovery" CR));
|
||||
char *gatewayRF2[8] = {"sensor", "gatewayRF2", "", "", "{{ value_json.value }}", "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayRF2[1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewayRF2[1]);
|
||||
createDiscovery(gatewayRF2[0],
|
||||
subjectRF2toMQTT, gatewayRF2[1], (char *)getUniqueId(gatewayRF2[1], gatewayRF2[2]).c_str(),
|
||||
will_Topic, gatewayRF2[3], gatewayRF2[4],
|
||||
@@ -338,12 +338,12 @@ void pubMqttDiscovery()
|
||||
|
||||
#ifdef ZgatewayRFM69
|
||||
// Sensor to display RF received value
|
||||
trc(F("gatewayRFM69Discovery"));
|
||||
Log.trace(F("gatewayRFM69Discovery" CR));
|
||||
char *gatewayRFM69[8] = {"sensor", "gatewayRFM69", "", "", "{{ value_json.value }}", "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayRFM69[1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewayRFM69[1]);
|
||||
createDiscovery(gatewayRFM69[0],
|
||||
subjectRFM69toMQTT, gatewayRFM69[1], (char *)getUniqueId(gatewayRFM69[1], gatewayRFM69[2]).c_str(),
|
||||
will_Topic, gatewayRFM69[3], gatewayRFM69[4],
|
||||
@@ -353,12 +353,12 @@ void pubMqttDiscovery()
|
||||
|
||||
#ifdef ZgatewayLORA
|
||||
// Sensor to display RF received value
|
||||
trc(F("gatewayLORADiscovery"));
|
||||
Log.trace(F("gatewayLORADiscovery" CR));
|
||||
char *gatewayLORA[8] = {"sensor", "gatewayLORA", "", "", "{{ value_json.message }}", "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayLORA[1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewayLORA[1]);
|
||||
createDiscovery(gatewayLORA[0],
|
||||
subjectLORAtoMQTT, gatewayLORA[1], (char *)getUniqueId(gatewayLORA[1], gatewayLORA[2]).c_str(),
|
||||
will_Topic, gatewayLORA[3], gatewayLORA[4],
|
||||
@@ -368,12 +368,12 @@ void pubMqttDiscovery()
|
||||
|
||||
#ifdef ZgatewaySRFB
|
||||
// Sensor to display RF received value
|
||||
trc(F("gatewaySRFBDiscovery"));
|
||||
Log.trace(F("gatewaySRFBDiscovery" CR));
|
||||
char *gatewaySRFB[8] = {"sensor", "gatewaySRFB", "", "", "{{ value_json.value }}", "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewaySRFB[1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewaySRFB[1]);
|
||||
createDiscovery(gatewaySRFB[0],
|
||||
subjectSRFBtoMQTT, gatewaySRFB[1], (char *)getUniqueId(gatewaySRFB[1], gatewaySRFB[2]).c_str(),
|
||||
will_Topic, gatewaySRFB[3], gatewaySRFB[4],
|
||||
@@ -383,12 +383,12 @@ void pubMqttDiscovery()
|
||||
|
||||
#ifdef ZgatewayPilight
|
||||
// Sensor to display RF received value
|
||||
trc(F("gatewayPilightDiscovery"));
|
||||
Log.trace(F("gatewayPilightDiscovery" CR));
|
||||
char *gatewayPilight[8] = {"sensor", "gatewayPilight", "", "", "{{ value_json.message }}", "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayPilight[1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewayPilight[1]);
|
||||
createDiscovery(gatewayPilight[0],
|
||||
subjectPilighttoMQTT, gatewayPilight[1], (char *)getUniqueId(gatewayPilight[1], gatewayPilight[2]).c_str(),
|
||||
will_Topic, gatewayPilight[3], gatewayPilight[4],
|
||||
@@ -398,12 +398,12 @@ void pubMqttDiscovery()
|
||||
|
||||
#ifdef ZgatewayIR
|
||||
// Sensor to display IR received value
|
||||
trc(F("gatewayIRDiscovery"));
|
||||
Log.trace(F("gatewayIRDiscovery" CR));
|
||||
char *gatewayIR[8] = {"sensor", "gatewayIR", "", "", "{{ value_json.value }}", "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayIR[1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewayIR[1]);
|
||||
createDiscovery(gatewayIR[0],
|
||||
subjectIRtoMQTT, gatewayIR[1], (char *)getUniqueId(gatewayIR[1], gatewayIR[2]).c_str(),
|
||||
will_Topic, gatewayIR[3], gatewayIR[4],
|
||||
@@ -413,12 +413,12 @@ void pubMqttDiscovery()
|
||||
|
||||
#ifdef Zgateway2G
|
||||
// Sensor to display 2G received value
|
||||
trc(F("gateway2GDiscovery"));
|
||||
Log.trace(F("gateway2GDiscovery" CR));
|
||||
char *gateway2G[8] = {"sensor", "gateway2G", "", "", "{{ value_json.message }}", "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gateway2G[1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gateway2G[1]);
|
||||
createDiscovery(gateway2G[0],
|
||||
subject2GtoMQTT, gateway2G[1], (char *)getUniqueId(gateway2G[1], gateway2G[2]).c_str(),
|
||||
will_Topic, gateway2G[3], gateway2G[4],
|
||||
@@ -428,12 +428,12 @@ void pubMqttDiscovery()
|
||||
|
||||
#ifdef ZgatewayBT
|
||||
// Sensor to display BT received value
|
||||
trc(F("gatewayBTDiscovery"));
|
||||
Log.trace(F("gatewayBTDiscovery" CR));
|
||||
char *gatewayBT[8] = {"sensor", "gatewayBT", "", "", "{{ value_json.id }}", "", "", ""};
|
||||
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
|
||||
|
||||
trc(F("CreateDiscoverySensor"));
|
||||
trc(gatewayBT[1]);
|
||||
Log.trace(F("CreateDiscoverySensor" CR));
|
||||
//trc(gatewayBT[1]);
|
||||
createDiscovery(gatewayBT[0],
|
||||
subjectBTtoMQTT, gatewayBT[1], (char *)getUniqueId(gatewayBT[1], gatewayBT[2]).c_str(),
|
||||
will_Topic, gatewayBT[3], gatewayBT[4],
|
||||
|
||||
+7
-2
@@ -37,6 +37,11 @@ ADC_MODE(ADC_TOUT);
|
||||
//Time used to wait for an interval before resending adc value
|
||||
unsigned long timeadc = 0;
|
||||
|
||||
void setupADC()
|
||||
{
|
||||
Log.notice(F("Reading ADC on pin: %d" CR), ADC_PIN);
|
||||
}
|
||||
|
||||
void MeasureADC()
|
||||
{
|
||||
if (millis() > (timeadc + TimeBetweenReadingADC))
|
||||
@@ -49,13 +54,13 @@ void MeasureADC()
|
||||
int val = analogRead(ADC_PIN);
|
||||
if (isnan(val))
|
||||
{
|
||||
trc(F("Failed to read from ADC !"));
|
||||
Log.error(F("Failed to read from ADC !" CR));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (val >= persistedadc + ThresholdReadingADC || val <= persistedadc - ThresholdReadingADC)
|
||||
{
|
||||
trc(F("Creating ADC buffer"));
|
||||
Log.trace(F("Creating ADC buffer" CR));
|
||||
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(1);
|
||||
StaticJsonBuffer<JSON_MSG_CALC_BUFFER> jsonBuffer;
|
||||
JsonObject &ADCdata = jsonBuffer.createObject();
|
||||
|
||||
@@ -44,6 +44,7 @@
|
||||
|
||||
void setupZsensorBH1750()
|
||||
{
|
||||
Log.notice(F("Setup BH1750 on adress: %H" CR), BH1750_i2c_addr);
|
||||
Wire.begin();
|
||||
Wire.beginTransmission(BH1750_i2c_addr);
|
||||
Wire.write(0x10); // Set resolution to 1 Lux
|
||||
@@ -56,7 +57,7 @@ void MeasureLightIntensity()
|
||||
{
|
||||
if (millis() > (timebh1750 + TimeBetweenReadingBH1750))
|
||||
{ //retriving value of Lux, FtCd and Wattsm2 from BH1750
|
||||
trc(F("Creating BH1750 buffer"));
|
||||
Log.trace(F("Creating BH1750 buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &BH1750data = jsonBuffer.createObject();
|
||||
|
||||
@@ -73,7 +74,7 @@ void MeasureLightIntensity()
|
||||
Wire.requestFrom(BH1750_i2c_addr, 2);
|
||||
if (Wire.available() != 2)
|
||||
{
|
||||
trc(F("Failed to read from LightSensor BH1750!"));
|
||||
Log.error(F("Failed to read from LightSensor BH1750!" CR));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -93,7 +94,7 @@ void MeasureLightIntensity()
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Same lux don't send it"));
|
||||
Log.trace(F("Same lux don't send it" CR));
|
||||
}
|
||||
|
||||
// Generate FtCd
|
||||
@@ -103,7 +104,7 @@ void MeasureLightIntensity()
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Same ftcd don't send it"));
|
||||
Log.trace(F("Same ftcd don't send it" CR));
|
||||
}
|
||||
|
||||
// Generate Watts/m2
|
||||
@@ -113,7 +114,7 @@ void MeasureLightIntensity()
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Same wattsm2 don't send it"));
|
||||
Log.trace(F("Same wattsm2 don't send it" CR));
|
||||
}
|
||||
if (BH1750data.size() > 0)
|
||||
pub(subjectBH1750toMQTT, BH1750data);
|
||||
|
||||
+13
-13
@@ -50,8 +50,8 @@ BME280 mySensor;
|
||||
void setupZsensorBME280()
|
||||
{
|
||||
mySensor.settings.commInterface = I2C_MODE;
|
||||
mySensor.settings.I2CAddress = 0x76; // Bosch BME280 I2C Address
|
||||
|
||||
mySensor.settings.I2CAddress = BME280_i2c_addr;
|
||||
Log.notice(F("Setup BME280 on adress: %H" CR), BME280_i2c_addr);
|
||||
//***Operation settings*****************************//
|
||||
|
||||
// runMode Setting - Values:
|
||||
@@ -101,8 +101,7 @@ void setupZsensorBME280()
|
||||
mySensor.settings.humidOverSample = 1;
|
||||
|
||||
delay(10); // Gives the Sensor enough time to turn on (The BME280 requires 2ms to start up)
|
||||
Serial.print("Bosch BME280 Initialized - Result of .begin(): 0x");
|
||||
Serial.println(mySensor.begin(), HEX);
|
||||
Log.notice(F("Bosch BME280 Initialized - Result of .begin(): 0x %h" CR),mySensor.begin());
|
||||
}
|
||||
|
||||
void MeasureTempHumAndPressure()
|
||||
@@ -129,11 +128,11 @@ void MeasureTempHumAndPressure()
|
||||
// Check if reads failed and exit early (to try again).
|
||||
if (isnan(BmeTempC) || isnan(BmeTempF) || isnan(BmeHum) || isnan(BmePa) || isnan(BmeAltiM) || isnan(BmeAltiFt))
|
||||
{
|
||||
trc(F("Failed to read from Weather Sensor BME280!"));
|
||||
Log.error(F("Failed to read from BME280!" CR));
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Creating BME280 buffer"));
|
||||
Log.trace(F("Creating BME280 buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &BME280data = jsonBuffer.createObject();
|
||||
// Generate Temperature in degrees C
|
||||
@@ -143,7 +142,7 @@ void MeasureTempHumAndPressure()
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Same Degrees C don't send it"));
|
||||
Log.trace(F("Same Degrees C don't send it" CR));
|
||||
}
|
||||
|
||||
// Generate Temperature in degrees F
|
||||
@@ -153,7 +152,7 @@ void MeasureTempHumAndPressure()
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Same Degrees F don't send it"));
|
||||
Log.trace(F("Same Degrees F don't send it" CR));
|
||||
}
|
||||
|
||||
// Generate Humidity in percent
|
||||
@@ -163,7 +162,7 @@ void MeasureTempHumAndPressure()
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Same Humidity don't send it"));
|
||||
Log.trace(F("Same Humidity don't send it" CR));
|
||||
}
|
||||
|
||||
// Generate Pressure in Pa
|
||||
@@ -173,18 +172,18 @@ void MeasureTempHumAndPressure()
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Same Pressure don't send it"));
|
||||
Log.trace(F("Same Pressure don't send it" CR));
|
||||
}
|
||||
|
||||
// Generate Altitude in Meter
|
||||
if (BmeAltiM != persisted_bme_altim || bme280_always)
|
||||
{
|
||||
trc(F("Sending Altitude Meter to MQTT"));
|
||||
Log.trace(F("Sending Altitude Meter to MQTT" CR));
|
||||
BME280data.set("altim", (float)BmeAltiM);
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Same Altitude Meter don't send it"));
|
||||
Log.trace(F("Same Altitude Meter don't send it" CR));
|
||||
}
|
||||
|
||||
// Generate Altitude in Feet
|
||||
@@ -194,11 +193,12 @@ void MeasureTempHumAndPressure()
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Same Altitude Feet don't send it"));
|
||||
Log.trace(F("Same Altitude Feet don't send it" CR));
|
||||
}
|
||||
if (BME280data.size() > 0)
|
||||
pub(BMETOPIC, BME280data);
|
||||
}
|
||||
|
||||
persisted_bme_tempc = BmeTempC;
|
||||
persisted_bme_tempf = BmeTempF;
|
||||
persisted_bme_hum = BmeHum;
|
||||
|
||||
+9
-4
@@ -38,6 +38,11 @@ DHT dht(DHT_RECEIVER_PIN, DHT_SENSOR_TYPE);
|
||||
//Time used to wait for an interval before resending temp and hum
|
||||
unsigned long timedht = 0;
|
||||
|
||||
void setupDHT()
|
||||
{
|
||||
Log.notice(F("Reading DHT on pin: %d" CR), DHT_RECEIVER_PIN);
|
||||
}
|
||||
|
||||
void MeasureTempAndHum()
|
||||
{
|
||||
if (millis() > (timedht + TimeBetweenReadingDHT))
|
||||
@@ -51,11 +56,11 @@ void MeasureTempAndHum()
|
||||
// Check if any reads failed and exit early (to try again).
|
||||
if (isnan(h) || isnan(t))
|
||||
{
|
||||
trc(F("Failed to read from DHT sensor!"));
|
||||
Log.error(F("Failed to read from DHT sensor!" CR));
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Creating DHT buffer"));
|
||||
Log.trace(F("Creating DHT buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &DHTdata = jsonBuffer.createObject();
|
||||
if (h != persistedh || dht_always)
|
||||
@@ -64,7 +69,7 @@ void MeasureTempAndHum()
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Same hum don't send it"));
|
||||
Log.trace(F("Same hum don't send it" CR));
|
||||
}
|
||||
if (t != persistedt || dht_always)
|
||||
{
|
||||
@@ -72,7 +77,7 @@ void MeasureTempAndHum()
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Same temp don't send it"));
|
||||
Log.trace(F("Same temp don't send it" CR));
|
||||
}
|
||||
if (DHTdata.size() > 0)
|
||||
pub(DHTTOPIC, DHTdata);
|
||||
|
||||
+22
-17
@@ -41,11 +41,11 @@ static String ds1820_addr[OW_MAX_SENSORS];
|
||||
|
||||
void setupZsensorDS1820()
|
||||
{
|
||||
trc(String("DS1820: configured pin ") + DS1820_OWBUS_PIN + String(" for 1-wire bus"));
|
||||
Log.trace(F("DS1820: configured pin: %d for 1-wire bus" CR), DS1820_OWBUS_PIN );
|
||||
ds1820.begin();
|
||||
|
||||
// locate device(s) on 1-wire bus
|
||||
trc("DS1820: Found " + String(ds1820.getDeviceCount(), DEC) + String(" devices."));
|
||||
Log.notice(F("DS1820: Found %d devices" CR), ds1820.getDeviceCount());
|
||||
|
||||
// determine device addresses on 1-wire bus
|
||||
if (owbus.search(ds1820_address))
|
||||
@@ -90,10 +90,11 @@ void setupZsensorDS1820()
|
||||
ds1820.setResolution(ds1820_address, DS1820_RESOLUTION);
|
||||
}
|
||||
ds1820_resolution[ds1820_count] = ds1820.getResolution(ds1820_address);
|
||||
trc(String("DS1820: Device ") + ds1820_count
|
||||
+ String(", Type ") + ds1820_type[ds1820_count]
|
||||
+ String(", Address ") + ds1820_addr[ds1820_count]
|
||||
+ String(", Resolution ") + ds1820_resolution[ds1820_count]);
|
||||
Log.trace(F("DS1820: Device %d, Type: %s, Address: %s, Resolution: %d" CR),
|
||||
ds1820_count,
|
||||
(char *)ds1820_type[ds1820_count].c_str(),
|
||||
(char *)ds1820_addr[ds1820_count].c_str(),
|
||||
ds1820_resolution[ds1820_count]);
|
||||
ds1820_count++;
|
||||
}
|
||||
} while (owbus.search(ds1820_address));
|
||||
@@ -101,7 +102,7 @@ void setupZsensorDS1820()
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("DS1820: Failed to enumerate sensors on 1-wire bus. Check your pin assignment!"));
|
||||
Log.error(F("DS1820: Failed to enumerate sensors on 1-wire bus. Check your pin assignment!" CR));
|
||||
}
|
||||
|
||||
// make requestTemperatures() non-blocking
|
||||
@@ -120,7 +121,7 @@ void MeasureDS1820Temp()
|
||||
// calling getTempC() to make reading temperatures non-blocking
|
||||
if (!triggeredConversion && (millis() > (timeDS1820 + (DS1820_INTERVAL_SEC*1000) - DS1820_CONV_TIME)))
|
||||
{
|
||||
trc(F("DS1820: Trigger temperature conversion..."));
|
||||
Log.trace(F("DS1820: Trigger temperature conversion..." CR));
|
||||
ds1820.requestTemperatures();
|
||||
triggeredConversion = true;
|
||||
}
|
||||
@@ -131,11 +132,11 @@ void MeasureDS1820Temp()
|
||||
|
||||
if (ds1820_count < 1)
|
||||
{
|
||||
trc(F("DS1820: Failed to identify any temperature sensors on 1-wire bus during setup!"));
|
||||
Log.error(F("DS1820: Failed to identify any temperature sensors on 1-wire bus during setup!" CR));
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(String("DS1820: Reading temperature(s) from ") + ds1820_count + String(" sensor(s)..."));
|
||||
Log.trace(F("DS1820: Reading temperature(s) from %d sensor(s)..." CR), ds1820_count);
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &DS1820data = jsonBuffer.createObject();
|
||||
|
||||
@@ -144,24 +145,28 @@ void MeasureDS1820Temp()
|
||||
current_temp[i] = round(ds1820.getTempC(ds1820_devices[i])*10)/10.0;
|
||||
if (current_temp[i] == -127)
|
||||
{
|
||||
trc(String("DS1820: Device ") + ds1820_addr[i] + String(" currently disconnected!"));
|
||||
Log.error(F("DS1820: Device %s currently disconnected!" CR), (char *)ds1820_addr[i].c_str() );
|
||||
}
|
||||
else if (DS1820_ALWAYS || current_temp[i] != persisted_temp[i])
|
||||
{
|
||||
if (DS1820_FAHRENHEIT) {
|
||||
trc(String("DS1820: Temperature (") + ds1820_addr[i] + String("): ")
|
||||
+ DallasTemperature::toFahrenheit(current_temp[i]) + String(" F"));
|
||||
Log.notice(F("DS1820: Temperature %s %d F" CR),
|
||||
(char *)ds1820_addr[i].c_str(),
|
||||
DallasTemperature::toFahrenheit(current_temp[i]));
|
||||
DS1820data.set("temp", (float)DallasTemperature::toFahrenheit(current_temp[i]));
|
||||
DS1820data.set("unit", "F");
|
||||
} else {
|
||||
trc(String("DS1820: Temperature (") + ds1820_addr[i]
|
||||
+ String("): ") + current_temp[i] + String(" C"));
|
||||
Log.notice(F("DS1820: Temperature %s %d C" CR),
|
||||
(char *)ds1820_addr[i].c_str(),
|
||||
current_temp[i]);
|
||||
DS1820data.set("temp", (float)DallasTemperature::toFahrenheit(current_temp[i]));
|
||||
DS1820data.set("unit", "F");
|
||||
DS1820data.set("temp", (float)current_temp[i]);
|
||||
DS1820data.set("unit", "C");
|
||||
}
|
||||
if (DS1820_DETAILS) {
|
||||
DS1820data.set("type", ds1820_type[i]);
|
||||
DS1820data.set("res", ds1820_resolution[i] + String("bit"));
|
||||
DS1820data.set("res", ds1820_resolution[i] + String("bit" CR));
|
||||
DS1820data.set("addr", ds1820_addr[i]);
|
||||
}
|
||||
pub(OW_TOPIC, DS1820data);
|
||||
@@ -169,7 +174,7 @@ void MeasureDS1820Temp()
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(String("DS1820: Temperature for device ") + ds1820_addr[i] + String(" didn't change, don't publish it."));
|
||||
Log.trace(F("DS1820: Temperature for device %s didn't change, don't publish it." CR), (char *)ds1820_addr[i].c_str());
|
||||
}
|
||||
persisted_temp[i] = current_temp[i];
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ int lastInputState = 3;
|
||||
|
||||
void setupGPIOInput()
|
||||
{
|
||||
Log.notice(F("Reading GPIO at pin: %d" CR), GPIOInput_PIN);
|
||||
pinMode(GPIOInput_PIN, INPUT_PULLUP); // declare GPIOInput pin as input_pullup to prevent floating. Pin will be high when not connected to ground
|
||||
}
|
||||
|
||||
@@ -65,18 +66,16 @@ void MeasureGPIOInput()
|
||||
if (reading != InputState)
|
||||
{
|
||||
InputState = reading;
|
||||
trc(F("Creating GPIOInput buffer"));
|
||||
Log.trace(F("Creating GPIOInput buffer" CR));
|
||||
const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(1);
|
||||
StaticJsonBuffer<JSON_MSG_CALC_BUFFER> jsonBuffer;
|
||||
JsonObject &GPIOdata = jsonBuffer.createObject();
|
||||
if (InputState == HIGH)
|
||||
{
|
||||
trc(F("GPIO HIGH"));
|
||||
GPIOdata.set("gpio", "HIGH");
|
||||
}
|
||||
if (InputState == LOW)
|
||||
{
|
||||
trc(F("GPIO LOW"));
|
||||
GPIOdata.set("gpio", "LOW");
|
||||
}
|
||||
if (GPIOdata.size() > 0)
|
||||
|
||||
@@ -66,7 +66,7 @@ void MeasureGPIOKeyCode()
|
||||
InputStateGPIOKeyCode = reading;
|
||||
sprintf(hex, "%02x", InputStateGPIOKeyCode);
|
||||
hex[2] = 0;
|
||||
Serial.printf("GPIOKeyCode %s\n", hex);
|
||||
Log.notice(F("GPIOKeyCode %H" CR), hex);
|
||||
pub(subjectGPIOKeyCodetoMQTT, hex);
|
||||
lastLatchStateGPIOKeyCode = latch;
|
||||
}
|
||||
@@ -74,7 +74,7 @@ void MeasureGPIOKeyCode()
|
||||
if (latch != lastLatchStateGPIOKeyCode)
|
||||
{
|
||||
lastLatchStateGPIOKeyCode = latch;
|
||||
Serial.printf("GPIOKeyCode latch %d\n", latch);
|
||||
Log.notice(F("GPIOKeyCode latch %d" CR), latch);
|
||||
if (latch == 0)
|
||||
pub(subjectGPIOKeyCodeStatetoMQTT, "done");
|
||||
}
|
||||
|
||||
@@ -35,6 +35,8 @@ unsigned long timeHCSR04 = 0;
|
||||
|
||||
void setupHCSR04()
|
||||
{
|
||||
Log.notice(F("HCSR04 trigger pin: %d" CR), HCSR04_TRI_PIN);
|
||||
Log.notice(F("HCSR04 echo pin: %d" CR), HCSR04_ECH_PIN);
|
||||
pinMode(HCSR04_TRI_PIN, OUTPUT); // declare HC SR-04 trigger pin as output
|
||||
pinMode(HCSR04_ECH_PIN, INPUT); // declare HC SR-04 echo pin as input
|
||||
}
|
||||
@@ -44,7 +46,7 @@ void MeasureDistance()
|
||||
if (millis() > (timeHCSR04 + TimeBetweenReadingHCSR04))
|
||||
{
|
||||
timeHCSR04 = millis();
|
||||
trc(F("Creating HCSR04 buffer"));
|
||||
Log.trace(F("Creating HCSR04 buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &HCSR04data = jsonBuffer.createObject();
|
||||
digitalWrite(HCSR04_TRI_PIN, LOW);
|
||||
@@ -55,7 +57,7 @@ void MeasureDistance()
|
||||
unsigned long duration = pulseIn(HCSR04_ECH_PIN, HIGH);
|
||||
if (isnan(duration))
|
||||
{
|
||||
trc(F("Failed to read from HC SR04 sensor!"));
|
||||
Log.error(F("Failed to read from HC SR04 sensor!" CR));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -65,17 +67,17 @@ void MeasureDistance()
|
||||
if (d > distance)
|
||||
{
|
||||
HCSR04data.set("direction", "away");
|
||||
trc(F("HC SR04 Distance changed"));
|
||||
Log.trace(F("HC SR04 Distance changed" CR));
|
||||
}
|
||||
else if (d < distance)
|
||||
{
|
||||
HCSR04data.set("direction", "towards");
|
||||
trc(F("HC SR04 Distance changed"));
|
||||
Log.trace(F("HC SR04 Distance changed" CR));
|
||||
}
|
||||
else if (HCSR04_always)
|
||||
{
|
||||
HCSR04data.set("direction", "static");
|
||||
trc(F("HC SR04 Distance hasn't changed"));
|
||||
Log.trace(F("HC SR04 Distance hasn't changed" CR));
|
||||
}
|
||||
distance = d;
|
||||
if (HCSR04data.size() > 0)
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
|
||||
void setupHCSR501()
|
||||
{
|
||||
Log.notice(F("HCSR501 pin: %d" CR), HCSR501_PIN);
|
||||
pinMode(HCSR501_PIN, INPUT); // declare HC SR-501 pin as input
|
||||
}
|
||||
|
||||
@@ -53,7 +54,6 @@ void MeasureHCSR501()
|
||||
{
|
||||
//turned on
|
||||
HCSR501data.set("hcsr501", "true");
|
||||
trc(F("HC SR501 Motion started"));
|
||||
pirState = HIGH;
|
||||
}
|
||||
}
|
||||
@@ -63,7 +63,6 @@ void MeasureHCSR501()
|
||||
{
|
||||
// turned off
|
||||
HCSR501data.set("hcsr501", "false");
|
||||
trc(F("HC SR501 Motion ended"));
|
||||
pirState = LOW;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,16 +55,13 @@ void MeasureINA226()
|
||||
if (millis() > (timeINA226 + TimeBetweenReadingINA226))
|
||||
{ //retriving value of temperature and humidity of the box from DHT every xUL
|
||||
timeINA226 = millis();
|
||||
trc(F("Creating INA226 buffer"));
|
||||
Log.trace(F("Creating INA226 buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &INA226data = jsonBuffer.createObject();
|
||||
// Topic on which we will send data
|
||||
trc(F("Retrieving electrical data"));
|
||||
Log.trace(F("Retrieving electrical data" CR));
|
||||
// Bus Spannung, read-only, 16Bit, 0...40.96V max., LSB 1.25mV
|
||||
trc(F(" Volt: "));
|
||||
float volt = readRegister(0x02) * 0.00125;
|
||||
trc(volt);
|
||||
trc(F(" V, Current: "));
|
||||
// Seite 24: Shunt Spannung +- 81,92mV mit 16 Bit, LSB 2,5uV
|
||||
int shuntvolt = readRegister(0x01);
|
||||
if (shuntvolt && 0x8000)
|
||||
@@ -74,11 +71,7 @@ void MeasureINA226()
|
||||
shuntvolt *= -1; // negativ machen
|
||||
}
|
||||
float current = shuntvolt * 0.0000025 / rShunt; // * LSB / R
|
||||
trc(current);
|
||||
trc(F(" A, Power: "));
|
||||
float power = abs(volt * current);
|
||||
trc(power);
|
||||
trc(F(" W"));
|
||||
|
||||
char volt_c[7];
|
||||
char current_c[7];
|
||||
|
||||
+14
-14
@@ -51,26 +51,26 @@ void displaySensorDetails(void)
|
||||
{
|
||||
sensor_t sensor;
|
||||
tsl.getSensor(&sensor);
|
||||
trc(F("------------------------------------"));
|
||||
trc("Sensor: " + String(sensor.name));
|
||||
trc("Driver Ver: " + String(sensor.version));
|
||||
trc("Unique ID: " + String(sensor.sensor_id));
|
||||
trc("Max Value: " + String(sensor.max_value) + " lux");
|
||||
trc("Min Value: " + String(sensor.min_value) + " lux");
|
||||
trc("Resolution: " + String(sensor.resolution) + " lux");
|
||||
trc(F("------------------------------------"));
|
||||
trc(F(""));
|
||||
Log.trace(F("------------------------------------" CR));
|
||||
Log.trace(("Sensor: %s" CR), sensor.name);
|
||||
Log.trace(("Driver Ver: %s" CR), sensor.version);
|
||||
Log.trace(("Unique ID: %s" CR), sensor.sensor_id);
|
||||
Log.trace(("Max Value: %s lux" CR), sensor.max_value);
|
||||
Log.trace(("Min Value: %s lux" CR), sensor.min_value);
|
||||
Log.trace(("Resolution: %s lux" CR), sensor.resolution);
|
||||
Log.trace(F("------------------------------------" CR));
|
||||
delay(500);
|
||||
}
|
||||
|
||||
void setupZsensorTSL2561()
|
||||
{
|
||||
Log.notice(F("Setup TSL2561 on adress: %H" CR), TSL2561_ADDR_FLOAT);
|
||||
Wire.begin();
|
||||
Wire.beginTransmission(TSL2561_ADDR_FLOAT);
|
||||
|
||||
if (!tsl.begin())
|
||||
{
|
||||
trc(F("No TSL2561 detected"));
|
||||
Log.error(F("No TSL2561 detected" CR));
|
||||
}
|
||||
|
||||
// enable auto ranging
|
||||
@@ -82,7 +82,7 @@ void setupZsensorTSL2561()
|
||||
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* medium resolution and speed */
|
||||
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);
|
||||
|
||||
trc(F("TSL2561 Initialized. Printing detials now."));
|
||||
Log.trace(F("TSL2561 Initialized. Printing detials now." CR));
|
||||
displaySensorDetails();
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ void MeasureLightIntensityTSL2561()
|
||||
static uint32_t persisted_lux;
|
||||
timetsl2561 = millis();
|
||||
|
||||
trc(F("Creating TSL2561 buffer"));
|
||||
Log.trace(F("Creating TSL2561 buffer" CR));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &TSL2561data = jsonBuffer.createObject();
|
||||
|
||||
@@ -114,12 +114,12 @@ void MeasureLightIntensityTSL2561()
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Same lux value, do not send"));
|
||||
Log.trace(F("Same lux value, do not send" CR));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Failed to read from TSL2561"));
|
||||
Log.error(F("Failed to read from TSL2561" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,5 +50,6 @@ extern void BME280toMQTT();
|
||||
|
||||
//Time used to wait for an interval before resending measured values
|
||||
unsigned long timebme280 = 0;
|
||||
int BME280_i2c_addr = 0x76; // Bosch BME280 I2C Address
|
||||
|
||||
#endif
|
||||
+116
-210
@@ -40,6 +40,7 @@ unsigned long ReceivedSignal[array_size][2] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
|
||||
|
||||
#include <PubSubClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <ArduinoLog.h>
|
||||
|
||||
// Modules config inclusion
|
||||
#if defined(ZgatewayRF) || defined(ZgatewayRF2) || defined(ZgatewayPilight)
|
||||
@@ -214,89 +215,6 @@ bool to_bool(String const &s)
|
||||
return s != "0";
|
||||
}
|
||||
|
||||
//trace
|
||||
void trc(String msg)
|
||||
{
|
||||
#ifdef TRACE
|
||||
Serial.println(msg);
|
||||
digitalWrite(led_info, HIGH);
|
||||
#endif
|
||||
#ifdef subjectTRACEtoMQTT
|
||||
pubMQTT(subjectTRACEtoMQTT, msg);
|
||||
#endif
|
||||
}
|
||||
|
||||
void trc(int msg)
|
||||
{
|
||||
#ifdef TRACE
|
||||
Serial.println(msg);
|
||||
digitalWrite(led_info, HIGH);
|
||||
#endif
|
||||
#ifdef subjectTRACEtoMQTT
|
||||
pubMQTT(subjectTRACEtoMQTT, msg);
|
||||
#endif
|
||||
}
|
||||
|
||||
void trc(unsigned int msg)
|
||||
{
|
||||
#ifdef TRACE
|
||||
Serial.println(msg);
|
||||
digitalWrite(led_info, HIGH);
|
||||
#endif
|
||||
#ifdef subjectTRACEtoMQTT
|
||||
pubMQTT(subjectTRACEtoMQTT, msg);
|
||||
#endif
|
||||
}
|
||||
|
||||
void trc(long msg)
|
||||
{
|
||||
#ifdef TRACE
|
||||
Serial.println(msg);
|
||||
digitalWrite(led_info, HIGH);
|
||||
#endif
|
||||
#ifdef subjectTRACEtoMQTT
|
||||
pubMQTT(subjectTRACEtoMQTT, msg);
|
||||
#endif
|
||||
}
|
||||
|
||||
void trc(unsigned long msg)
|
||||
{
|
||||
#ifdef TRACE
|
||||
Serial.println(msg);
|
||||
digitalWrite(led_info, HIGH);
|
||||
#endif
|
||||
#ifdef subjectTRACEtoMQTT
|
||||
pubMQTT(subjectTRACEtoMQTT, msg);
|
||||
#endif
|
||||
}
|
||||
|
||||
void trc(double msg)
|
||||
{
|
||||
#ifdef TRACE
|
||||
Serial.println(msg);
|
||||
digitalWrite(led_info, HIGH);
|
||||
#endif
|
||||
#ifdef subjectTRACEtoMQTT
|
||||
pubMQTT(subjectTRACEtoMQTT, msg);
|
||||
#endif
|
||||
}
|
||||
|
||||
void trc(float msg)
|
||||
{
|
||||
#ifdef TRACE
|
||||
Serial.println(msg);
|
||||
digitalWrite(led_info, HIGH);
|
||||
#endif
|
||||
#ifdef subjectTRACEtoMQTT
|
||||
pubMQTT(subjectTRACEtoMQTT, msg);
|
||||
#endif
|
||||
}
|
||||
|
||||
void trc(JsonObject &data)
|
||||
{
|
||||
data.printTo(Serial);
|
||||
}
|
||||
|
||||
void pub(char *topicori, char *payload, bool retainFlag)
|
||||
{
|
||||
String topic = String(mqtt_topic) + String(topicori);
|
||||
@@ -305,6 +223,8 @@ void pub(char *topicori, char *payload, bool retainFlag)
|
||||
|
||||
void pub(char *topicori, JsonObject &data)
|
||||
{
|
||||
Log.notice(F("Subject: %s" CR),topicori);
|
||||
logJson(data);
|
||||
if (client.connected())
|
||||
{
|
||||
digitalWrite(led_receive, HIGH);
|
||||
@@ -318,20 +238,18 @@ void pub(char *topicori, JsonObject &data)
|
||||
#endif
|
||||
|
||||
#ifdef jsonPublishing
|
||||
Log.trace(F("jsonPublishing" CR));
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
char JSONmessageBuffer[data.measureLength() + 1];
|
||||
#else
|
||||
char JSONmessageBuffer[JSON_MSG_BUFFER];
|
||||
#endif
|
||||
trc(F("Pub json into:"));
|
||||
trc(topic);
|
||||
data.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
|
||||
trc(JSONmessageBuffer);
|
||||
pubMQTT(topic, JSONmessageBuffer);
|
||||
#endif
|
||||
|
||||
#ifdef simplePublishing
|
||||
trc(F("Pub data per topic"));
|
||||
Log.trace(F("simplePublishing" CR));
|
||||
// Loop through all the key-value pairs in obj
|
||||
for (JsonPair &p : data)
|
||||
{
|
||||
@@ -340,8 +258,6 @@ void pub(char *topicori, JsonObject &data)
|
||||
#endif
|
||||
if (p.value.is<unsigned long>() && strcmp(p.key, "rssi") != 0)
|
||||
{ //test rssi , bypass solution due to the fact that a int is considered as an unsigned long
|
||||
trc(p.key);
|
||||
trc(p.value.as<unsigned long>());
|
||||
if (strcmp(p.key, "value") == 0)
|
||||
{ // if data is a value we don't integrate the name into the topic
|
||||
pubMQTT(topic, p.value.as<unsigned long>());
|
||||
@@ -353,20 +269,14 @@ void pub(char *topicori, JsonObject &data)
|
||||
}
|
||||
else if (p.value.is<int>())
|
||||
{
|
||||
trc(p.key);
|
||||
trc(p.value.as<int>());
|
||||
pubMQTT(topic + "/" + String(p.key), p.value.as<int>());
|
||||
}
|
||||
else if (p.value.is<float>())
|
||||
{
|
||||
trc(p.key);
|
||||
trc(p.value.as<float>());
|
||||
pubMQTT(topic + "/" + String(p.key), p.value.as<float>());
|
||||
}
|
||||
else if (p.value.is<char *>())
|
||||
{
|
||||
trc(p.key);
|
||||
trc(p.value.as<const char *>());
|
||||
pubMQTT(topic + "/" + String(p.key), p.value.as<const char *>());
|
||||
}
|
||||
}
|
||||
@@ -374,7 +284,7 @@ void pub(char *topicori, JsonObject &data)
|
||||
}
|
||||
else
|
||||
{
|
||||
trc("client not connected can't pub");
|
||||
Log.warning(F("client not connected can't pub" CR));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -383,13 +293,12 @@ void pub(char *topicori, char *payload)
|
||||
if (client.connected())
|
||||
{
|
||||
String topic = String(mqtt_topic) + String(topicori);
|
||||
trc(F("Pub ack into:"));
|
||||
trc(topic);
|
||||
Log.trace(F("Pub ack %s into: %s" CR), payload, topic.c_str());
|
||||
pubMQTT(topic, payload);
|
||||
}
|
||||
else
|
||||
{
|
||||
trc("client not connected can't pub");
|
||||
Log.warning(F("client not connected can't pub" CR));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -402,15 +311,13 @@ void pub_custom_topic(char *topicori, JsonObject &data, boolean retain)
|
||||
#else
|
||||
char JSONmessageBuffer[JSON_MSG_BUFFER];
|
||||
#endif
|
||||
trc(F("Pub json discovery into:"));
|
||||
trc(topicori);
|
||||
data.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
|
||||
trc(JSONmessageBuffer);
|
||||
Log.trace(F("Pub json :%s into custom topic: %s" CR), JSONmessageBuffer, topicori);
|
||||
pubMQTT(topicori, JSONmessageBuffer, retain);
|
||||
}
|
||||
else
|
||||
{
|
||||
trc("client not connected can't pub");
|
||||
Log.warning(F("client not connected can't pub" CR));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -510,6 +417,17 @@ void pubMQTT(String topic, unsigned long payload)
|
||||
client.publish((char *)topic.c_str(), val);
|
||||
}
|
||||
|
||||
void logJson(JsonObject &jsondata)
|
||||
{
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
char JSONmessageBuffer[jsondata.measureLength() + 1];
|
||||
#else
|
||||
char JSONmessageBuffer[JSON_MSG_BUFFER];
|
||||
#endif
|
||||
jsondata.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
|
||||
Log.notice(F("Received json : %s" CR),JSONmessageBuffer);
|
||||
}
|
||||
|
||||
bool cmpToMainTopic(char * topicOri, char * toAdd){
|
||||
char topic[mqtt_topic_max_size];
|
||||
strcpy(topic,mqtt_topic);
|
||||
@@ -533,13 +451,13 @@ void reconnect()
|
||||
// Loop until we're reconnected
|
||||
while (!client.connected())
|
||||
{
|
||||
trc(F("MQTT connection...")); //F function enable to decrease sram usage
|
||||
Log.notice(F("MQTT connection..." CR)); //F function enable to decrease sram usage
|
||||
char topic[mqtt_topic_max_size];
|
||||
strcpy(topic,mqtt_topic);
|
||||
strcat(topic,will_Topic);
|
||||
if (client.connect(gateway_name, mqtt_user, mqtt_pass, topic, will_QoS, will_Retain, will_Message))
|
||||
{
|
||||
trc(F("Connected to broker"));
|
||||
Log.notice(F("Connected to broker" CR));
|
||||
failure_number = 0;
|
||||
// Once connected, publish an announcement...
|
||||
pub(will_Topic, Gateway_AnnouncementMsg, will_Retain);
|
||||
@@ -557,25 +475,23 @@ void reconnect()
|
||||
#ifdef ZgatewayIR
|
||||
client.subscribe(subjectMultiGTWIR); // subject on which other OMG will publish, this OMG will store these msg and by the way don't republish them if they have been already published
|
||||
#endif
|
||||
trc(F("Subscription OK to the subjects"));
|
||||
Log.trace(F("Subscription OK to the subjects" CR));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
failure_number++; // we count the failure
|
||||
trc(F("failure_number"));
|
||||
trc(failure_number);
|
||||
Log.warning(F("failure_number: %d" CR),failure_number);
|
||||
if (failure_number > maxMQTTretry && !connectedOnce)
|
||||
{
|
||||
#ifndef ESPWifiManualSetup
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
trc(F("failed connecting first time to mqtt, reset wifi manager & erase network credentials"));
|
||||
Log.error(F("Failed connecting 1st time to mqtt, reset wifi manager & erase network credentials" CR));
|
||||
setup_wifimanager(true);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
trc(F("failed, rc="));
|
||||
trc(client.state());
|
||||
Log.warning(F("failed, rc=%d" CR),client.state());
|
||||
delay(5000);
|
||||
// On ESP32 force wifi begin https://github.com/espressif/arduino-esp32/issues/2501
|
||||
#if defined(ESP32)
|
||||
@@ -591,7 +507,7 @@ void callback(char *topic, byte *payload, unsigned int length)
|
||||
// In order to republish this payload, a copy must be made
|
||||
// as the orignal payload buffer will be overwritten whilst
|
||||
// constructing the PUBLISH packet.
|
||||
trc(F("Hey I got a callback "));
|
||||
Log.trace(F("Hey I got a callback " CR));
|
||||
// Allocate the correct amount of memory for the payload copy
|
||||
byte *p = (byte *)malloc(length + 1);
|
||||
// Copy the payload to the new buffer
|
||||
@@ -614,6 +530,8 @@ void setup()
|
||||
{
|
||||
//Launch serial for debugging purposes
|
||||
Serial.begin(SERIAL_BAUD);
|
||||
Log.begin(LOG_LEVEL, &Serial);
|
||||
Log.notice(F(CR "************* WELCOME TO OpenMQTTGateway **************" CR));
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
#ifdef ESP8266
|
||||
@@ -629,11 +547,8 @@ void setup()
|
||||
setup_wifimanager(false);
|
||||
#endif
|
||||
|
||||
trc(F("OpenMQTTGateway mac: "));
|
||||
trc(WiFi.macAddress());
|
||||
|
||||
trc(F("OpenMQTTGateway ip: "));
|
||||
trc(WiFi.localIP().toString());
|
||||
Log.trace(F("OpenMQTTGateway mac: %s" CR),WiFi.macAddress().c_str());
|
||||
Log.trace(F("OpenMQTTGateway ip: %s" CR),WiFi.localIP().toString().c_str());
|
||||
|
||||
// Port defaults to 8266
|
||||
ArduinoOTA.setPort(ota_port);
|
||||
@@ -645,26 +560,26 @@ void setup()
|
||||
ArduinoOTA.setPassword(ota_password);
|
||||
|
||||
ArduinoOTA.onStart([]() {
|
||||
trc(F("Start"));
|
||||
Log.trace(F("Start OTA" CR));
|
||||
});
|
||||
ArduinoOTA.onEnd([]() {
|
||||
trc(F("\nEnd"));
|
||||
Log.trace(F("\nEnd OTA" CR));
|
||||
});
|
||||
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
||||
Log.trace(F("Progress: %u%%\r" CR), (progress / (total / 100)));
|
||||
});
|
||||
ArduinoOTA.onError([](ota_error_t error) {
|
||||
Serial.printf("Error[%u]: ", error);
|
||||
if (error == OTA_AUTH_ERROR)
|
||||
trc(F("Auth Failed"));
|
||||
Log.error(F("Auth Failed" CR));
|
||||
else if (error == OTA_BEGIN_ERROR)
|
||||
trc(F("Begin Failed"));
|
||||
Log.error(F("Begin Failed" CR));
|
||||
else if (error == OTA_CONNECT_ERROR)
|
||||
trc(F("Connect Failed"));
|
||||
Log.error(F("Connect Failed" CR));
|
||||
else if (error == OTA_RECEIVE_ERROR)
|
||||
trc(F("Receive Failed"));
|
||||
Log.error(F("Receive Failed" CR));
|
||||
else if (error == OTA_END_ERROR)
|
||||
trc(F("End Failed"));
|
||||
Log.error(F("End Failed" CR));
|
||||
});
|
||||
ArduinoOTA.begin();
|
||||
|
||||
@@ -685,22 +600,20 @@ void setup()
|
||||
digitalWrite(led_info, LOW);
|
||||
|
||||
#if defined(MDNS_SD) && defined(ESP8266)
|
||||
trc(F("Connecting to MQTT by mDNS without mqtt hostname"));
|
||||
Log.trace(F("Connecting to MQTT by mDNS without mqtt hostname" CR));
|
||||
connectMQTTmdns();
|
||||
#else
|
||||
long port;
|
||||
port = strtol(mqtt_port, NULL, 10);
|
||||
trc(port);
|
||||
Log.trace(F("Port: %l" CR),port);
|
||||
#ifdef mqtt_server_name // if name is defined we define the mqtt server by its name
|
||||
trc(F("Connecting to MQTT with mqtt hostname"));
|
||||
IPAddress mqtt_server_ip;
|
||||
WiFi.hostByName(mqtt_server_name, mqtt_server_ip);
|
||||
client.setServer(mqtt_server_ip, port);
|
||||
trc(mqtt_server_ip.toString());
|
||||
Log.trace(F("Mqtt server connection by host name: %s" CR),mqtt_server_ip.toString());
|
||||
#else // if not by its IP adress
|
||||
trc(F("Connecting to MQTT by IP adress"));
|
||||
client.setServer(mqtt_server, port);
|
||||
trc(mqtt_server);
|
||||
Log.trace(F("Mqtt server connection by IP: %s" CR),mqtt_server);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -773,14 +686,18 @@ void setup()
|
||||
#ifdef ZsensorDS1820
|
||||
setupZsensorDS1820();
|
||||
#endif
|
||||
|
||||
trc(F("MQTT_MAX_PACKET_SIZE"));
|
||||
trc(MQTT_MAX_PACKET_SIZE);
|
||||
#ifdef ZsensorADC
|
||||
setupADC();
|
||||
#endif
|
||||
#ifdef ZsensorDHT
|
||||
setupDHT();
|
||||
#endif
|
||||
Log.trace(F("MQTT_MAX_PACKET_SIZE: %d" CR),MQTT_MAX_PACKET_SIZE);
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
if (MQTT_MAX_PACKET_SIZE == 128)
|
||||
trc(F("WRONG PUBSUBCLIENT LIBRARY USED PLEASE INSTALL THE ONE FROM RELEASE PAGE"));
|
||||
Log.error(F("WRONG PUBSUBCLIENT LIBRARY USED PLEASE INSTALL THE ONE FROM RELEASE PAGE" CR));
|
||||
#endif
|
||||
trc(F("Setup OpenMQTTGateway end"));
|
||||
Log.notice(F("Setup OpenMQTTGateway end" CR));
|
||||
}
|
||||
|
||||
#if defined(ESPWifiManualSetup)
|
||||
@@ -790,8 +707,7 @@ void setup_wifi()
|
||||
int failureAttempt = 0; //DIRTY FIX ESP32 waiting for https://github.com/espressif/arduino-esp32/issues/653
|
||||
WiFi.mode(WIFI_STA);
|
||||
// We start by connecting to a WiFi network
|
||||
trc(F("Connecting to "));
|
||||
trc(wifi_ssid);
|
||||
Log.trace(F("Connecting to %s" CR),wifi_ssid);
|
||||
#ifdef ESPWifiAdvancedSetup
|
||||
IPAddress ip_adress(ip);
|
||||
IPAddress gateway_adress(gateway);
|
||||
@@ -799,7 +715,7 @@ void setup_wifi()
|
||||
IPAddress dns_adress(Dns);
|
||||
if (!WiFi.config(ip_adress, gateway_adress, subnet_adress, dns_adress))
|
||||
{
|
||||
trc("STA Failed to configure");
|
||||
Log.error(F("Wifi STA Failed to configure" CR));
|
||||
}
|
||||
WiFi.begin(wifi_ssid, wifi_password);
|
||||
#else
|
||||
@@ -809,12 +725,12 @@ void setup_wifi()
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(500);
|
||||
trc(F("."));
|
||||
Log.trace(F("." CR));
|
||||
failureAttempt++; //DIRTY FIX ESP32
|
||||
if (failureAttempt > 30)
|
||||
setup_wifi(); //DIRTY FIX ESP32
|
||||
}
|
||||
trc(F("WiFi ok with manual config credentials"));
|
||||
Log.notice(F("WiFi ok with manual config credentials" CR));
|
||||
}
|
||||
|
||||
#elif defined(ESP8266) || defined(ESP32)
|
||||
@@ -828,7 +744,7 @@ bool shouldSaveConfig = true;
|
||||
//callback notifying us of the need to save config
|
||||
void saveConfigCallback()
|
||||
{
|
||||
trc(F("Should save config"));
|
||||
Log.trace(F("Should save config" CR));
|
||||
shouldSaveConfig = true;
|
||||
}
|
||||
|
||||
@@ -841,13 +757,13 @@ void checkButton()
|
||||
delay(50);
|
||||
if (digitalRead(TRIGGER_PIN) == LOW)
|
||||
{
|
||||
trc(F("Trigger button Pressed"));
|
||||
Log.trace(F("Trigger button Pressed" CR));
|
||||
// still holding button for 3000 ms, reset settings, code not ideaa for production
|
||||
delay(3000); // reset delay hold
|
||||
if (digitalRead(TRIGGER_PIN) == LOW)
|
||||
{
|
||||
trc(F("Button Held"));
|
||||
trc(F("Erasing ESP Config, restarting"));
|
||||
Log.trace(F("Button Held" CR));
|
||||
Log.notice(F("Erasing ESP Config, restarting" CR));
|
||||
setup_wifimanager(true);
|
||||
}
|
||||
}
|
||||
@@ -862,8 +778,7 @@ void eraseAndRestart()
|
||||
WiFi.disconnect(true, true);
|
||||
#endif
|
||||
|
||||
trc("Formatting requested, result:");
|
||||
trc(SPIFFS.format());
|
||||
Log.trace(F("Formatting requested, result: %d" CR),SPIFFS.format());
|
||||
|
||||
#if defined(ESP8266)
|
||||
ESP.reset();
|
||||
@@ -883,27 +798,27 @@ void setup_wifimanager(bool reset_settings)
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
//read configuration from FS json
|
||||
trc(F("mounting FS..."));
|
||||
Log.trace(F("mounting FS..." CR));
|
||||
|
||||
if (SPIFFS.begin())
|
||||
{
|
||||
trc(F("mounted file system"));
|
||||
Log.trace(F("mounted file system" CR));
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("failed to mount FS -> formating"));
|
||||
Log.warning(F("failed to mount FS -> formating" CR));
|
||||
SPIFFS.format();
|
||||
if (SPIFFS.begin())
|
||||
trc(F("mounted file system after formating"));
|
||||
Log.trace(F("mounted file system after formating" CR));
|
||||
}
|
||||
if (SPIFFS.exists("/config.json"))
|
||||
{
|
||||
//file exists, reading and loading
|
||||
trc(F("reading config file"));
|
||||
Log.trace(F("reading config file" CR));
|
||||
File configFile = SPIFFS.open("/config.json", "r");
|
||||
if (configFile)
|
||||
{
|
||||
trc(F("opened config file"));
|
||||
Log.trace(F("opened config file" CR));
|
||||
size_t size = configFile.size();
|
||||
// Allocate a buffer to store contents of the file.
|
||||
std::unique_ptr<char[]> buf(new char[size]);
|
||||
@@ -913,7 +828,7 @@ void setup_wifimanager(bool reset_settings)
|
||||
json.printTo(Serial);
|
||||
if (json.success())
|
||||
{
|
||||
trc(F("\nparsed json"));
|
||||
Log.trace(F("\nparsed json" CR));
|
||||
if (json.containsKey("mqtt_server"))
|
||||
strcpy(mqtt_server, json["mqtt_server"]);
|
||||
if (json.containsKey("mqtt_port"))
|
||||
@@ -929,7 +844,7 @@ void setup_wifimanager(bool reset_settings)
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("failed to load json config"));
|
||||
Log.warning(F("failed to load json config" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -956,7 +871,7 @@ void setup_wifimanager(bool reset_settings)
|
||||
|
||||
//set static ip
|
||||
#ifdef NetworkAdvancedSetup
|
||||
trc(F("Adv wifi cfg"));
|
||||
Log.trace(F("Adv wifi cfg" CR));
|
||||
IPAddress gateway_adress(gateway);
|
||||
IPAddress subnet_adress(subnet);
|
||||
IPAddress ip_adress(ip);
|
||||
@@ -979,7 +894,7 @@ void setup_wifimanager(bool reset_settings)
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
if (!wifiManager.autoConnect(WifiManager_ssid, WifiManager_password))
|
||||
{
|
||||
trc(F("failed to connect and hit timeout"));
|
||||
Log.warning(F("failed to connect and hit timeout" CR));
|
||||
delay(3000);
|
||||
//reset and try again, or maybe put it to deep sleep
|
||||
#if defined(ESP8266)
|
||||
@@ -991,7 +906,7 @@ void setup_wifimanager(bool reset_settings)
|
||||
}
|
||||
|
||||
//if you get here you have connected to the WiFi
|
||||
trc(F("connected...yeey :)"));
|
||||
Log.notice(F("Wifi connected" CR));
|
||||
|
||||
//read updated parameters
|
||||
strcpy(mqtt_server, custom_mqtt_server.getValue());
|
||||
@@ -1004,7 +919,7 @@ void setup_wifimanager(bool reset_settings)
|
||||
//save the custom parameters to FS
|
||||
if (shouldSaveConfig)
|
||||
{
|
||||
trc(F("saving config"));
|
||||
Log.trace(F("saving config" CR));
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject &json = jsonBuffer.createObject();
|
||||
json["mqtt_server"] = mqtt_server;
|
||||
@@ -1017,7 +932,7 @@ void setup_wifimanager(bool reset_settings)
|
||||
File configFile = SPIFFS.open("/config.json", "w");
|
||||
if (!configFile)
|
||||
{
|
||||
trc(F("failed to open config file for writing"));
|
||||
Log.error(F("failed to open config file for writing" CR));
|
||||
}
|
||||
|
||||
json.printTo(Serial);
|
||||
@@ -1030,21 +945,19 @@ void setup_wifimanager(bool reset_settings)
|
||||
void setup_ethernet()
|
||||
{
|
||||
#ifdef NetworkAdvancedSetup
|
||||
trc(F("Adv eth cfg"));
|
||||
Log.trace(F("Adv eth cfg" CR));
|
||||
Ethernet.begin(mac, ip, Dns, gateway, subnet);
|
||||
#else
|
||||
trc(F("Spl eth cfg"));
|
||||
Log.trace(F("Spl eth cfg" CR));
|
||||
Ethernet.begin(mac, ip);
|
||||
#endif
|
||||
if (Ethernet.hardwareStatus() == EthernetNoHardware)
|
||||
{
|
||||
trc("Ethernet shield was not found.");
|
||||
Log.error(F("Ethernet shield was not found." CR));
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("ip: "));
|
||||
Serial.println(Ethernet.localIP());
|
||||
trc(F("Eth ok"));
|
||||
Log.trace(F("ip: %s " CR),Ethernet.localIP());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1054,38 +967,34 @@ void connectMQTTmdns()
|
||||
{
|
||||
if (!MDNS.begin("ESP_MQTT"))
|
||||
{
|
||||
trc(F("Error setting up MDNS responder!"));
|
||||
Log.error(F("Error setting up MDNS responder!" CR));
|
||||
while (1)
|
||||
{
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
trc(F("Browsing for service MQTT "));
|
||||
Log.trace(F("Browsing for MQTT service" CR));
|
||||
int n = MDNS.queryService("mqtt", "tcp");
|
||||
if (n == 0)
|
||||
{
|
||||
trc(F("no services found"));
|
||||
Log.error(F("no services found" CR));
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(n);
|
||||
trc(F(" service(s) found"));
|
||||
Log.trace(F("%d service(s) found" CR),n);
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
// Print details for each service found
|
||||
trc(i + 1);
|
||||
trc(MDNS.hostname(i));
|
||||
trc(MDNS.IP(i).toString());
|
||||
trc(MDNS.port(i));
|
||||
Log.trace(F("Service %d %s found" CR),i, MDNS.hostname(i).c_str());
|
||||
Log.trace(F("IP %s Port %d" CR),MDNS.IP(i).toString().c_str(), MDNS.port(i));
|
||||
}
|
||||
if (n == 1)
|
||||
{
|
||||
trc(F("One MQTT server found setting parameters"));
|
||||
Log.trace(F("One MQTT server found setting parameters" CR));
|
||||
client.setServer(MDNS.IP(0), int(MDNS.port(0)));
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("Several MQTT servers found, please deactivate mDNS and set your default server"));
|
||||
Log.error(F("Several MQTT servers found, please deactivate mDNS and set your default server" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1180,7 +1089,7 @@ void loop()
|
||||
#ifdef ZgatewayBT
|
||||
#ifndef ESP32
|
||||
if (BTtoMQTT())
|
||||
trc(F("BTtoMQTT OK"));
|
||||
Log.trace(F("BTtoMQTT OK" CR));
|
||||
#endif
|
||||
#endif
|
||||
#ifdef ZgatewaySRFB
|
||||
@@ -1191,11 +1100,11 @@ void loop()
|
||||
#endif
|
||||
#ifdef Zgateway2G
|
||||
if (_2GtoMQTT())
|
||||
trc(F("2GtoMQTT OK"));
|
||||
Log.trace(F("2GtoMQTT OK" CR));
|
||||
#endif
|
||||
#ifdef ZgatewayRFM69
|
||||
if (RFM69toMQTT())
|
||||
trc(F("RFM69toMQTT OK"));
|
||||
Log.trace(F("RFM69toMQTT OK" CR));
|
||||
#endif
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
stateMeasures();
|
||||
@@ -1219,10 +1128,10 @@ void loop()
|
||||
{
|
||||
lastNTWKReconnectAttempt = now;
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
trc("wifi disconnected");
|
||||
Log.warning(F("wifi disconnected" CR));
|
||||
delay(10000); // add a delay to avoid ESP32 crash and reset
|
||||
#if defined(ESPWifiManualSetup)
|
||||
trc(F("restarting ESP"));
|
||||
Log.notice(F("restarting ESP" CR));
|
||||
#ifdef ESP32
|
||||
ESP.restart();
|
||||
#endif
|
||||
@@ -1232,12 +1141,12 @@ void loop()
|
||||
#else
|
||||
if (!connectedOnce)
|
||||
{
|
||||
trc(F("reseting wifi manager"));
|
||||
Log.notice(F("reseting wifi manager" CR));
|
||||
setup_wifimanager(true); // if we didn't connected once to mqtt we reset and start in AP mode again to have a chance to change the parameters
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
trc("eth disconnected");
|
||||
Log.warning(F("eth disconnected" CR));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -1248,14 +1157,13 @@ void stateMeasures()
|
||||
{
|
||||
unsigned long now = millis();
|
||||
if (now > (timer_sys_measures + TimeBetweenReadingSYS))
|
||||
{ //retriving value of memory ram every TimeBetweenReadingSYS
|
||||
{
|
||||
timer_sys_measures = millis();
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject &SYSdata = jsonBuffer.createObject();
|
||||
trc(F("Uptime (s)"));
|
||||
unsigned long uptime = millis() / 1000;
|
||||
trc(uptime);
|
||||
SYSdata["uptime"] = uptime;
|
||||
Log.trace(F("retriving value of system characteristics Uptime (s):%u" CR),uptime);
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
uint32_t freeMem;
|
||||
freeMem = ESP.getFreeHeap();
|
||||
@@ -1345,7 +1253,6 @@ void stateMeasures()
|
||||
#endif
|
||||
|
||||
SYSdata["modules"] = modules;
|
||||
trc(SYSdata);
|
||||
pub(subjectSYStoMQTT, SYSdata);
|
||||
}
|
||||
}
|
||||
@@ -1356,17 +1263,15 @@ void storeValue(unsigned long MQTTvalue)
|
||||
unsigned long now = millis();
|
||||
// find oldest value of the buffer
|
||||
int o = getMin();
|
||||
trc(F("Min ind: "));
|
||||
trc(o);
|
||||
Log.trace(F("Min ind: %d" CR),o);
|
||||
// replace it by the new one
|
||||
ReceivedSignal[o][0] = MQTTvalue;
|
||||
ReceivedSignal[o][1] = now;
|
||||
trc(F("store code :"));
|
||||
trc(String(ReceivedSignal[o][0]) + "/" + String(ReceivedSignal[o][1]));
|
||||
trc(F("Col: val/timestamp"));
|
||||
Log.trace(F("store code : %u / %u" CR),ReceivedSignal[o][0],ReceivedSignal[o][1]);
|
||||
Log.trace(F("Col: val/timestamp" CR));
|
||||
for (int i = 0; i < array_size; i++)
|
||||
{
|
||||
trc(String(i) + ":" + String(ReceivedSignal[i][0]) + "/" + String(ReceivedSignal[i][1]));
|
||||
Log.trace(F("mem code : %u / %u" CR),ReceivedSignal[i][0],ReceivedSignal[i][1]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1387,7 +1292,7 @@ int getMin()
|
||||
|
||||
bool isAduplicate(unsigned long value)
|
||||
{
|
||||
trc(F("isAdupl?"));
|
||||
Log.trace(F("isAdupl?" CR));
|
||||
// check if the value has been already sent during the last time_avoid_duplicate
|
||||
for (int i = 0; i < array_size; i++)
|
||||
{
|
||||
@@ -1396,7 +1301,7 @@ bool isAduplicate(unsigned long value)
|
||||
unsigned long now = millis();
|
||||
if (now - ReceivedSignal[i][1] < time_avoid_duplicate)
|
||||
{ // change
|
||||
trc(F("no pub. dupl"));
|
||||
Log.notice(F("no pub. dupl" CR));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1412,7 +1317,6 @@ void receivingMQTT(char *topicOri, char *datacallback)
|
||||
|
||||
if (strstr(topicOri, subjectMultiGTWKey) != NULL) // storing received value so as to avoid publishing this value if it has been already sent by this or another OpenMQTTGateway
|
||||
{
|
||||
trc(F("Store str"));
|
||||
unsigned long data = 0;
|
||||
#ifdef jsonPublishing
|
||||
if (jsondata.success())
|
||||
@@ -1426,13 +1330,15 @@ void receivingMQTT(char *topicOri, char *datacallback)
|
||||
if (data != 0)
|
||||
{
|
||||
storeValue(data);
|
||||
trc(F("JSON str"));
|
||||
}
|
||||
}
|
||||
|
||||
if (jsondata.success())
|
||||
{ // json object ok -> json decoding
|
||||
#ifdef ZgatewayPilight // ZgatewayPilight is only defined with json publishing
|
||||
// log the received json
|
||||
logJson(jsondata);
|
||||
|
||||
#ifdef ZgatewayPilight // ZgatewayPilight is only defined with json publishing due to its numerous parameters
|
||||
MQTTtoPilight(topicOri, jsondata);
|
||||
#endif
|
||||
#ifdef jsonReceiving
|
||||
@@ -1460,15 +1366,15 @@ void receivingMQTT(char *topicOri, char *datacallback)
|
||||
#ifdef ZgatewayBT
|
||||
MQTTtoBT(topicOri, jsondata);
|
||||
#endif
|
||||
#ifdef ZactuatorFASTLED
|
||||
MQTTtoFASTLED(topicOri, jsondata);
|
||||
#endif
|
||||
#endif
|
||||
#ifdef ZactuatorONOFF // outside the jsonpublishing macro due to the fact that we need to use simplepublishing with HA discovery
|
||||
MQTTtoONOFF(topicOri, jsondata);
|
||||
#endif
|
||||
digitalWrite(led_send, HIGH);
|
||||
|
||||
#ifdef ZactuatorFASTLED
|
||||
MQTTtoFASTLEDJSON(topicOri, jsondata);
|
||||
#endif
|
||||
MQTTtoSYS(topicOri, jsondata);
|
||||
}
|
||||
else
|
||||
@@ -1495,13 +1401,14 @@ void receivingMQTT(char *topicOri, char *datacallback)
|
||||
#ifdef ZgatewayRFM69
|
||||
MQTTtoRFM69(topicOri, datacallback);
|
||||
#endif
|
||||
#ifdef ZactuatorFASTLED
|
||||
MQTTtoFASTLED(topicOri, datacallback);
|
||||
#endif
|
||||
#endif
|
||||
#ifdef ZactuatorONOFF
|
||||
MQTTtoONOFF(topicOri, datacallback);
|
||||
#endif
|
||||
#ifdef ZactuatorFASTLED
|
||||
MQTTtoFASTLED(topicOri, datacallback);
|
||||
#endif
|
||||
|
||||
digitalWrite(led_send, HIGH);
|
||||
}
|
||||
//YELLOW OFF
|
||||
@@ -1512,13 +1419,12 @@ void MQTTtoSYS(char *topicOri, JsonObject &SYSdata)
|
||||
{ // json object decoding
|
||||
if (cmpToMainTopic(topicOri,subjectMQTTtoSYSset))
|
||||
{
|
||||
trc(F("MQTTtoSYS json set"));
|
||||
Log.trace(F("MQTTtoSYS json" CR));
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
if (SYSdata.containsKey("cmd"))
|
||||
{
|
||||
trc(F("Command"));
|
||||
const char *cmd = SYSdata["cmd"];
|
||||
trc(cmd);
|
||||
Log.notice(F("Command: %s" CR),cmd);
|
||||
if (strstr(cmd, restartCmd) != NULL)
|
||||
{ //restart
|
||||
#if defined(ESP8266)
|
||||
@@ -1535,7 +1441,7 @@ if (cmpToMainTopic(topicOri,subjectMQTTtoSYSset))
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("wrong command"));
|
||||
Log.warning(F("wrong command" CR));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -61,6 +61,7 @@ extra_configs =
|
||||
|
||||
[libraries]
|
||||
arduinojson = ArduinoJson@5.13.4
|
||||
arduinolog = 1technophile/Arduino-Log#39b0916
|
||||
pubsubclient = PubSubClient@2.7
|
||||
rc-switch = 1technophile/rc-switch#90adb34
|
||||
newremoteswitch = 1technophile/NewRemoteSwitch#8eb980e
|
||||
@@ -91,6 +92,7 @@ framework = arduino
|
||||
lib_deps =
|
||||
${libraries.pubsubclient}
|
||||
${libraries.arduinojson}
|
||||
${libraries.arduinolog}
|
||||
build_flags =
|
||||
-w ; supress all warnings
|
||||
'-DjsonPublishing=true'
|
||||
|
||||
Reference in new issue
Block a user