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:
1 parent
f77df7347f
commit
d8dba15a81
182 files changed
+2701
-20768
No files matched your search
+184
-110
@@ -39,106 +39,6 @@ void setupSRFB(){
|
||||
trc("Serial Baud" + String(SERIAL_BAUD));
|
||||
}
|
||||
|
||||
void MQTTtoSRFB(char * topicOri, char * datacallback) {
|
||||
|
||||
// RF DATA ANALYSIS
|
||||
String topic = topicOri;
|
||||
int valueRPT = 0;
|
||||
|
||||
if (topic == subjectMQTTtoSRFB){
|
||||
|
||||
int valueMiniPLSL = 0;
|
||||
int valueMaxiPLSL = 0;
|
||||
int valueSYNC = 0;
|
||||
|
||||
int pos = topic.lastIndexOf(SRFBRptKey);
|
||||
if (pos != -1){
|
||||
pos = pos + +strlen(SRFBRptKey);
|
||||
valueRPT = (topic.substring(pos,pos + 1)).toInt();
|
||||
trc(F("SRFB Repeat:"));
|
||||
trc(valueRPT);
|
||||
}
|
||||
|
||||
int pos2 = topic.lastIndexOf(SRFBminipulselengthKey);
|
||||
if (pos2 != -1) {
|
||||
pos2 = pos2 + strlen(SRFBminipulselengthKey);
|
||||
valueMiniPLSL = (topic.substring(pos2,pos2 + 3)).toInt();
|
||||
trc(F("RF Mini Pulse Lgth:"));
|
||||
trc(valueMiniPLSL);
|
||||
}
|
||||
|
||||
int pos3 = topic.lastIndexOf(SRFBmaxipulselengthKey);
|
||||
if (pos3 != -1){
|
||||
pos3 = pos3 + strlen(SRFBmaxipulselengthKey);
|
||||
valueMaxiPLSL = (topic.substring(pos3,pos3 + 2)).toInt();
|
||||
trc(F("RF Maxi Pulse Lgth:"));
|
||||
trc(valueMaxiPLSL);
|
||||
}
|
||||
|
||||
int pos4 = topic.lastIndexOf(SRFBsyncKey);
|
||||
if (pos4 != -1){
|
||||
pos4 = pos4 + strlen(SRFBsyncKey);
|
||||
valueSYNC = (topic.substring(pos4,pos4 + 2)).toInt();
|
||||
trc(F("RF sync:"));
|
||||
trc(valueSYNC);
|
||||
}
|
||||
|
||||
trc(F("MQTTtoSRFB prts"));
|
||||
if (valueRPT == 0) valueRPT = 1;
|
||||
if (valueMiniPLSL == 0) valueMiniPLSL = 320;
|
||||
if (valueMaxiPLSL == 0) valueMaxiPLSL = 900;
|
||||
if (valueSYNC == 0) valueSYNC = 9500;
|
||||
|
||||
byte hex_valueMiniPLSL[2];
|
||||
hex_valueMiniPLSL[0] = (int)((valueMiniPLSL >> 8) & 0xFF) ;
|
||||
hex_valueMiniPLSL[1] = (int)(valueMiniPLSL & 0xFF) ;
|
||||
|
||||
byte hex_valueMaxiPLSL[2];
|
||||
hex_valueMaxiPLSL[0] = (int)((valueMaxiPLSL >> 8) & 0xFF) ;
|
||||
hex_valueMaxiPLSL[1] = (int)(valueMaxiPLSL & 0xFF) ;
|
||||
|
||||
byte hex_valueSYNC[2];
|
||||
hex_valueSYNC[0] = (int)((valueSYNC >> 8) & 0xFF) ;
|
||||
hex_valueSYNC[1] = (int)(valueSYNC & 0xFF) ;
|
||||
|
||||
unsigned long data = strtoul(datacallback, NULL, 10); // we will not be able to pass values > 4294967295
|
||||
byte hex_data[3];
|
||||
hex_data[0] = (unsigned long)((data >> 16) & 0xFF) ;
|
||||
hex_data[1] = (unsigned long)((data >> 8) & 0xFF) ;
|
||||
hex_data[2] = (unsigned long)(data & 0xFF) ;
|
||||
|
||||
byte message_b[RF_MESSAGE_SIZE];
|
||||
|
||||
memcpy(message_b, hex_valueSYNC, 2);
|
||||
memcpy(message_b + 2, hex_valueMiniPLSL, 2);
|
||||
memcpy(message_b + 4, hex_valueMaxiPLSL, 2);
|
||||
memcpy(message_b + 6, hex_data, 3);
|
||||
|
||||
_rfbSend(message_b, valueRPT);
|
||||
// Acknowledgement to the GTWRF topic
|
||||
boolean result = client.publish(subjectGTWSRFBtoMQTT, 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("MQTTtoSRFB ack pub."));
|
||||
}
|
||||
if (topic == subjectMQTTtoSRFBRaw){
|
||||
|
||||
int pos = topic.lastIndexOf(SRFBRptKey);
|
||||
if (pos != -1){
|
||||
pos = pos + +strlen(SRFBRptKey);
|
||||
valueRPT = (topic.substring(pos,pos + 1)).toInt();
|
||||
trc(F("SRFB Repeat:"));
|
||||
trc(valueRPT);
|
||||
}
|
||||
if (valueRPT == 0) valueRPT = 1;
|
||||
|
||||
byte message_b[RF_MESSAGE_SIZE];
|
||||
_rfbToArray(datacallback,message_b);
|
||||
_rfbSend(message_b, valueRPT);
|
||||
// Acknowledgement to the GTWRF topic
|
||||
boolean result = client.publish(subjectGTWSRFBtoMQTT, 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("MQTTtoSRFBRaw ack pub."));
|
||||
}
|
||||
}
|
||||
|
||||
void _rfbSend(byte * message) {
|
||||
Serial.println();
|
||||
Serial.write(RF_CODE_START);
|
||||
@@ -201,24 +101,37 @@ void _rfbDecode() {
|
||||
|
||||
if (action == RF_CODE_RFIN) {
|
||||
_rfbToChar(&_uartbuf[1], buffer);
|
||||
client.publish(subjectSRFBtoMQTTRaw,buffer);
|
||||
|
||||
trc(F("Creating SRFB buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& SRFBdata = jsonBuffer.createObject();
|
||||
SRFBdata.set("raw", (char *)buffer);
|
||||
|
||||
char val[8]= {0};
|
||||
extract_char(buffer, val, 12 ,8, false,true);
|
||||
client.publish(subjectSRFBtoMQTT,val);
|
||||
extract_char(buffer, val, 12 ,8, false,true);
|
||||
SRFBdata.set("value", (char *)val);
|
||||
|
||||
char val_Tsyn[4]= {0};
|
||||
extract_char(buffer, val_Tsyn, 0 ,4, false, true);
|
||||
client.publish(subjectSRFBtoMQTTTsyn,val_Tsyn);
|
||||
|
||||
extract_char(buffer, val_Tsyn , 0 ,4, false, true);
|
||||
SRFBdata.set("delay", (char *)val_Tsyn);
|
||||
|
||||
char val_Thigh[4]= {0};
|
||||
extract_char(buffer, val_Thigh, 4 ,4, false, true);
|
||||
client.publish(subjectSRFBtoMQTTThigh,val_Thigh);
|
||||
extract_char(buffer, val_Thigh , 4 ,4, false, true);
|
||||
SRFBdata.set("val_Thigh", (char *)val_Thigh);
|
||||
|
||||
char val_Tlow[4]= {0};
|
||||
extract_char(buffer, val_Tlow, 8 ,4, false, true);
|
||||
client.publish(subjectSRFBtoMQTTTlow,val_Tlow);
|
||||
extract_char(buffer,val_Tlow , 8 ,4, false, true);
|
||||
SRFBdata.set("val_Tlow", (char *)val_Tlow);
|
||||
|
||||
unsigned long MQTTvalue = SRFBdata.get<unsigned long>("value");
|
||||
if (!isAduplicate(MQTTvalue) && MQTTvalue!=0) {// conditions to avoid duplications of RF -->MQTT
|
||||
trc(F("Adv data SRFBtoMQTT"));
|
||||
pub(subjectSRFBtoMQTT,SRFBdata);
|
||||
if (repeatSRFBwMQTT){
|
||||
trc(F("Publish SRFB for repeat"));
|
||||
pub(subjectMQTTtoSRFB,SRFBdata);
|
||||
}
|
||||
}
|
||||
_rfbAck();
|
||||
}
|
||||
}
|
||||
@@ -256,4 +169,165 @@ bool _rfbToChar(byte * in, char * out) {
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef simplePublishing
|
||||
void MQTTtoSRFB(char * topicOri, char * datacallback) {
|
||||
|
||||
// RF DATA ANALYSIS
|
||||
String topic = topicOri;
|
||||
int valueRPT = 0;
|
||||
|
||||
if (topic == subjectMQTTtoSRFB){
|
||||
|
||||
int valueMiniPLSL = 0;
|
||||
int valueMaxiPLSL = 0;
|
||||
int valueSYNC = 0;
|
||||
|
||||
int pos = topic.lastIndexOf(SRFBRptKey);
|
||||
if (pos != -1){
|
||||
pos = pos + +strlen(SRFBRptKey);
|
||||
valueRPT = (topic.substring(pos,pos + 1)).toInt();
|
||||
trc(F("SRFB Repeat:"));
|
||||
trc(valueRPT);
|
||||
}
|
||||
|
||||
int pos2 = topic.lastIndexOf(SRFBminipulselengthKey);
|
||||
if (pos2 != -1) {
|
||||
pos2 = pos2 + strlen(SRFBminipulselengthKey);
|
||||
valueMiniPLSL = (topic.substring(pos2,pos2 + 3)).toInt();
|
||||
trc(F("RF Mini Pulse Lgth:"));
|
||||
trc(valueMiniPLSL);
|
||||
}
|
||||
|
||||
int pos3 = topic.lastIndexOf(SRFBmaxipulselengthKey);
|
||||
if (pos3 != -1){
|
||||
pos3 = pos3 + strlen(SRFBmaxipulselengthKey);
|
||||
valueMaxiPLSL = (topic.substring(pos3,pos3 + 2)).toInt();
|
||||
trc(F("RF Maxi Pulse Lgth:"));
|
||||
trc(valueMaxiPLSL);
|
||||
}
|
||||
|
||||
int pos4 = topic.lastIndexOf(SRFBsyncKey);
|
||||
if (pos4 != -1){
|
||||
pos4 = pos4 + strlen(SRFBsyncKey);
|
||||
valueSYNC = (topic.substring(pos4,pos4 + 2)).toInt();
|
||||
trc(F("RF sync:"));
|
||||
trc(valueSYNC);
|
||||
}
|
||||
|
||||
trc(F("MQTTtoSRFB prts"));
|
||||
if (valueRPT == 0) valueRPT = 1;
|
||||
if (valueMiniPLSL == 0) valueMiniPLSL = 320;
|
||||
if (valueMaxiPLSL == 0) valueMaxiPLSL = 900;
|
||||
if (valueSYNC == 0) valueSYNC = 9500;
|
||||
|
||||
byte hex_valueMiniPLSL[2];
|
||||
hex_valueMiniPLSL[0] = (int)((valueMiniPLSL >> 8) & 0xFF) ;
|
||||
hex_valueMiniPLSL[1] = (int)(valueMiniPLSL & 0xFF) ;
|
||||
|
||||
byte hex_valueMaxiPLSL[2];
|
||||
hex_valueMaxiPLSL[0] = (int)((valueMaxiPLSL >> 8) & 0xFF) ;
|
||||
hex_valueMaxiPLSL[1] = (int)(valueMaxiPLSL & 0xFF) ;
|
||||
|
||||
byte hex_valueSYNC[2];
|
||||
hex_valueSYNC[0] = (int)((valueSYNC >> 8) & 0xFF) ;
|
||||
hex_valueSYNC[1] = (int)(valueSYNC & 0xFF) ;
|
||||
|
||||
unsigned long data = strtoul(datacallback, NULL, 10); // we will not be able to pass values > 4294967295
|
||||
byte hex_data[3];
|
||||
hex_data[0] = (unsigned long)((data >> 16) & 0xFF) ;
|
||||
hex_data[1] = (unsigned long)((data >> 8) & 0xFF) ;
|
||||
hex_data[2] = (unsigned long)(data & 0xFF) ;
|
||||
|
||||
byte message_b[RF_MESSAGE_SIZE];
|
||||
|
||||
memcpy(message_b, hex_valueSYNC, 2);
|
||||
memcpy(message_b + 2, hex_valueMiniPLSL, 2);
|
||||
memcpy(message_b + 4, hex_valueMaxiPLSL, 2);
|
||||
memcpy(message_b + 6, hex_data, 3);
|
||||
|
||||
_rfbSend(message_b, valueRPT);
|
||||
// Acknowledgement to the GTWRF topic
|
||||
pub(subjectGTWSRFBtoMQTT, 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 (topic == subjectMQTTtoSRFBRaw){
|
||||
|
||||
int pos = topic.lastIndexOf(SRFBRptKey);
|
||||
if (pos != -1){
|
||||
pos = pos + +strlen(SRFBRptKey);
|
||||
valueRPT = (topic.substring(pos,pos + 1)).toInt();
|
||||
trc(F("SRFB Repeat:"));
|
||||
trc(valueRPT);
|
||||
}
|
||||
if (valueRPT == 0) valueRPT = 1;
|
||||
|
||||
byte message_b[RF_MESSAGE_SIZE];
|
||||
_rfbToArray(datacallback,message_b);
|
||||
_rfbSend(message_b, valueRPT);
|
||||
// Acknowledgement to the GTWRF topic
|
||||
pub(subjectGTWSRFBtoMQTT, 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 MQTTtoSRFB(char * topicOri, JsonObject& SRFBdata) {
|
||||
|
||||
// RF DATA ANALYSIS
|
||||
String topic = topicOri;
|
||||
const char * raw = SRFBdata["raw"];
|
||||
int valueRPT = SRFBdata["repeat"]|1;
|
||||
if (topic == subjectMQTTtoSRFB){
|
||||
trc(F("MQTTtoSRFB json data analysis"));
|
||||
if (raw){ // send raw in priority when defined in the json
|
||||
trc(F("MQTTtoSRFB raw ok"));
|
||||
byte message_b[RF_MESSAGE_SIZE];
|
||||
_rfbToArray(raw,message_b);
|
||||
_rfbSend(message_b, valueRPT);
|
||||
}else{
|
||||
unsigned long data = SRFBdata["value"];
|
||||
if (data != 0) {
|
||||
trc(F("MQTTtoSRFB data ok"));
|
||||
int valueMiniPLSL = SRFBdata["val_Tlow"];
|
||||
int valueMaxiPLSL =SRFBdata["val_Thigh"];
|
||||
int valueSYNC = SRFBdata["delay"];
|
||||
|
||||
if (valueRPT == 0) valueRPT = 1;
|
||||
if (valueMiniPLSL == 0) valueMiniPLSL = 320;
|
||||
if (valueMaxiPLSL == 0) valueMaxiPLSL = 900;
|
||||
if (valueSYNC == 0) valueSYNC = 9500;
|
||||
|
||||
byte hex_valueMiniPLSL[2];
|
||||
hex_valueMiniPLSL[0] = (int)((valueMiniPLSL >> 8) & 0xFF) ;
|
||||
hex_valueMiniPLSL[1] = (int)(valueMiniPLSL & 0xFF) ;
|
||||
|
||||
byte hex_valueMaxiPLSL[2];
|
||||
hex_valueMaxiPLSL[0] = (int)((valueMaxiPLSL >> 8) & 0xFF) ;
|
||||
hex_valueMaxiPLSL[1] = (int)(valueMaxiPLSL & 0xFF) ;
|
||||
|
||||
byte hex_valueSYNC[2];
|
||||
hex_valueSYNC[0] = (int)((valueSYNC >> 8) & 0xFF) ;
|
||||
hex_valueSYNC[1] = (int)(valueSYNC & 0xFF) ;
|
||||
|
||||
byte hex_data[3];
|
||||
hex_data[0] = (unsigned long)((data >> 16) & 0xFF) ;
|
||||
hex_data[1] = (unsigned long)((data >> 8) & 0xFF) ;
|
||||
hex_data[2] = (unsigned long)(data & 0xFF) ;
|
||||
|
||||
byte message_b[RF_MESSAGE_SIZE];
|
||||
|
||||
memcpy(message_b, hex_valueSYNC, 2);
|
||||
memcpy(message_b + 2, hex_valueMiniPLSL, 2);
|
||||
memcpy(message_b + 4, hex_valueMaxiPLSL, 2);
|
||||
memcpy(message_b + 6, hex_data, 3);
|
||||
|
||||
trc(F("MQTTtoSRFB send"));
|
||||
_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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
Reference in new issue
Block a user