#1 some code factoring

This commit is contained in:
1technophile authored and GitHub committed 2017-01-06 21:16:27 +01:00
1 parent 5fec66ddc5
commit 71c7210bef
1 file changed
+26 -16
+26 -16
View File
@@ -52,6 +52,11 @@ RCSwitch mySwitch = RCSwitch();
// array to store previous received RFs codes and their timestamps // array to store previous received RFs codes and their timestamps
long ReceivedRF[10][2] ={{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}}; long ReceivedRF[10][2] ={{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}};
#define subjectMQTTtoX "home/commands/#"
//RF MQTT Subjects
#define subject433toMQTT "home/433toMQTT"
#define subjectMQTTto433 "home/MQTTto433/"
//adding this to bypass to problem of the arduino builder issue 50 //adding this to bypass to problem of the arduino builder issue 50
void callback(char*topic, byte* payload,unsigned int length); void callback(char*topic, byte* payload,unsigned int length);
WiFiClient espClient; WiFiClient espClient;
@@ -126,10 +131,8 @@ boolean reconnect() {
// Once connected, publish an announcement... // Once connected, publish an announcement...
client.publish("outTopic","hello world"); client.publish("outTopic","hello world");
trc("connected"); trc("connected");
//Topic subscribed so as to get data
String topicNameRec = String("home/MQTTto433/");
//Subscribing to topic(s) //Subscribing to topic(s)
subscribing(topicNameRec); subscribing(subjectMQTTtoX);
} else { } else {
trc("failed, rc="); trc("failed, rc=");
trc(String(client.state())); trc(String(client.state()));
@@ -159,20 +162,34 @@ void loop()
client.loop(); client.loop();
} }
// Receive loop, if data received by RF433 send it by MQTT to MQTTsubject // Receive loop, if data received by RF433 send it by MQTT to subject433toMQTT
if (mySwitch.available()) { if (mySwitch.available()) {
// Topic on which we will send data // Topic on which we will send data
trc("Receiving 433Mhz signal"); trc("Receiving 433Mhz signal");
String MQTTsubject = "home/433toMQTT"; unsigned long MQTTvalue;
long MQTTvalue;
MQTTvalue=mySwitch.getReceivedValue(); MQTTvalue=mySwitch.getReceivedValue();
mySwitch.resetAvailable(); mySwitch.resetAvailable();
if (client.connected()) { if (client.connected()) {
long now = millis();
if (!isAduplicate(MQTTvalue)) {// conditions to avoid duplications of RF -->MQTT if (!isAduplicate(MQTTvalue)) {// conditions to avoid duplications of RF -->MQTT
trc("Sending 433Mhz signal to MQTT"); trc("Sending 433Mhz signal to MQTT");
trc(String(MQTTvalue)); trc(String(MQTTvalue));
sendMQTT(MQTTsubject,String(MQTTvalue)); sendMQTT(subject433toMQTT,String(MQTTvalue));
storeValue(MQTTvalue);
}
} else {
if (reconnect()) {
trc("Sending 433Mhz signal to MQTT after reconnect");
trc(String(MQTTvalue));
sendMQTT(subject433toMQTT,String(MQTTvalue));
storeValue(MQTTvalue);
lastReconnectAttempt = 0;
}
}
}
}
void storeValue(long MQTTvalue){
long now = millis();
// find oldest value of the buffer // find oldest value of the buffer
int o = getMin(); int o = getMin();
trc("Minimum index: " + String(o)); trc("Minimum index: " + String(o));
@@ -186,14 +203,7 @@ void loop()
trc(String(i) + ":" + String(ReceivedRF[i][0])+"/"+String(ReceivedRF[i][1])); trc(String(i) + ":" + String(ReceivedRF[i][0])+"/"+String(ReceivedRF[i][1]));
} }
} }
} else {
if (reconnect()) {
sendMQTT(MQTTsubject,String(MQTTvalue));
lastReconnectAttempt = 0;
}
}
}
}
int getMin() int getMin()
{ {
int minimum = ReceivedRF[0][1]; int minimum = ReceivedRF[0][1];