Publish refactoring (#298)

* factoring of the publishing function
* integration of json publishing to MQTT
* receiving and transcode json messages from mqtt
* replace the TRACE runtime condition by a buildtime one so as to decrease program size
* remove retainFlag parameter not necessary for the moment
* Increase the scope of tests for arduino mega and esp32
* Update arduinojson library to 5.13.3
* add macros to choose between simple or json publishing or both
* differentiate build test for ATmega2560
* correct wrong sprintf definition
* change IDE version for continuous integration
* let default warning level in arduino ide
* correct pins for ESP32
* reorganize user_config
This commit is contained in:
Florian authored and GitHub committed 2018-11-05 22:02:27 +01:00
1 parent f77df7347f
commit d8dba15a81
182 files changed
+2701 -20768

No files matched your search

+61 -30
View File
@@ -44,43 +44,41 @@ void setupRF(){
trc(F("ZgatewayRF setup done "));
}
boolean RFtoMQTT(){
void RFtoMQTT(){
if (mySwitch.available()){
trc(F("Creating RF buffer"));
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
JsonObject& RFdata = jsonBuffer.createObject();
trc(F("Rcv. RF"));
#ifdef ESP32
String taskMessage = "RF Task running on core ";
taskMessage = taskMessage + xPortGetCoreID();
trc(taskMessage);
#endif
unsigned long MQTTvalue = 0;
String MQTTprotocol;
String MQTTbits;
String MQTTlength;
MQTTvalue = mySwitch.getReceivedValue();
MQTTprotocol = String(mySwitch.getReceivedProtocol());
MQTTbits = String(mySwitch.getReceivedBitlength());
MQTTlength = String(mySwitch.getReceivedDelay());
RFdata.set("value", (unsigned long)mySwitch.getReceivedValue());
RFdata.set("protocol",(int)mySwitch.getReceivedProtocol());
RFdata.set("length", (int)mySwitch.getReceivedBitlength());
RFdata.set("delay", (int)mySwitch.getReceivedDelay());
mySwitch.resetAvailable();
trc(F("LED MNG"));
digitalWrite(led_receive, LOW);
timer_led_receive = millis();
unsigned long MQTTvalue = RFdata.get<unsigned long>("value");
if (!isAduplicate(MQTTvalue) && MQTTvalue!=0) {// conditions to avoid duplications of RF -->MQTT
trc(F("Adv data RFtoMQTT"));
client.publish(subjectRFtoMQTTprotocol,(char *)MQTTprotocol.c_str());
client.publish(subjectRFtoMQTTbits,(char *)MQTTbits.c_str());
client.publish(subjectRFtoMQTTlength,(char *)MQTTlength.c_str());
trc(F("Sending RFtoMQTT"));
String value = String(MQTTvalue);
trc(value);
boolean result = client.publish(subjectRFtoMQTT,(char *)value.c_str());
trc(F("Adv data RFtoMQTT"));
pub(subjectRFtoMQTT,RFdata);
if (repeatRFwMQTT){
trc(F("Publish RF for repeat"));
client.publish(subjectMQTTtoRF,(char *)value.c_str());
pub(subjectMQTTtoRF,RFdata);
}
return result;
}
}
return false;
}
#ifdef simplePublishing
void MQTTtoRF(char * topicOri, char * datacallback) {
unsigned long data = strtoul(datacallback, NULL, 10); // we will not be able to pass values > 4294967295
@@ -119,9 +117,7 @@ void MQTTtoRF(char * topicOri, char * datacallback) {
mySwitch.setProtocol(1,350);
mySwitch.send(data, 24);
// Acknowledgement to the GTWRF topic
boolean result = client.publish(subjectGTWRFtoMQTT, datacallback);
if (result)trc(F("Ack pub."));
pub(subjectGTWRFtoMQTT, datacallback);
} else if ((valuePRT != 0) || (valuePLSL != 0)|| (valueBITS != 0)){
trc(F("MQTTtoRF usr par."));
if (valuePRT == 0) valuePRT = 1;
@@ -133,12 +129,47 @@ void MQTTtoRF(char * topicOri, char * datacallback) {
mySwitch.setProtocol(valuePRT,valuePLSL);
mySwitch.send(data, valueBITS);
// Acknowledgement to the GTWRF topic
boolean result = client.publish(subjectGTWRFtoMQTT, datacallback);// 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
if (result){
trc(F("MQTTtoRF ack pub."));
trc(data);
}
}
pub(subjectGTWRFtoMQTT, datacallback);// 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
}
}
#endif
#ifdef jsonPublishing
void MQTTtoRF(char * topicOri, JsonObject& RFdata) { // json object decoding
String topic = topicOri;
if (topic == subjectMQTTtoRF) {
trc(F("MQTTtoRF json data analysis"));
unsigned long data = RFdata["value"];
if (data != 0) {
trc(F("MQTTtoRF data ok"));
int valuePRT = RFdata["protocol"];
int valuePLSL = RFdata["delay"];
int valueBITS = RFdata["length"];
if ((valuePRT == 0) && (valuePLSL == 0) && (valueBITS == 0)){
trc(F("MQTTtoRF dflt"));
mySwitch.setProtocol(1,350);
mySwitch.send(data, 24);
// Acknowledgement to the GTWRF topic
pub(subjectGTWRFtoMQTT, RFdata);
} else if ((valuePRT != 0) || (valuePLSL != 0)|| (valueBITS != 0)){
trc(F("MQTTtoRF usr par."));
if (valuePRT == 0) valuePRT = 1;
if (valuePLSL == 0) valuePLSL = 350;
if (valueBITS == 0) valueBITS = 24;
trc(valuePRT);
trc(valuePLSL);
trc(valueBITS);
mySwitch.setProtocol(valuePRT,valuePLSL);
mySwitch.send(data, valueBITS);
// Acknowledgement to the GTWRF topic
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
}
}else{
trc(F("MQTTtoRF Fail reading from json"));
}
}
}
#endif
#endif