sometimes strange partial response are given by serial

Avoid publishing them
This commit is contained in:
1technophile committed 2017-05-19 22:24:36 +02:00
1 parent 642c0938f2
commit 37842bd0c4
1 file changed
+25 -22
+25 -22
View File
@@ -28,6 +28,7 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht
#define STRING_MSG "OK+DISC:" #define STRING_MSG "OK+DISC:"
#define QUESTION_MSG "AT+DISI?" #define QUESTION_MSG "AT+DISI?"
#define RESPONSE_MSG "OK+DISIS" #define RESPONSE_MSG "OK+DISIS"
#define RESP_END_MSG "OK+DISCE"
#define SETUP_MSG "OK+RESET" #define SETUP_MSG "OK+RESET"
#define TimeBtw_Read 10000 #define TimeBtw_Read 10000
@@ -47,32 +48,34 @@ void setupBT() {
boolean BTtoMQTT() { boolean BTtoMQTT() {
while (softserial.available() > 0) { while (softserial.available() > 0) {
#ifdef ESP8266
yield();
#endif
String discResult = softserial.readString(); String discResult = softserial.readString();
if (discResult.indexOf(STRING_MSG)>=0){ if (discResult.indexOf(STRING_MSG)>=0){
trc(F("Sending BT data to MQTT"));
discResult.replace(RESPONSE_MSG,""); discResult.replace(RESPONSE_MSG,"");
int device_number = discResult.length()/78; discResult.replace(RESP_END_MSG,"");
for (int i=0;i<device_number;i++){ float device_number = discResult.length()/78.0;
String onedevice = discResult.substring(0,78); if (device_number == (int)device_number){ // to avoid publishing partial values we detect if the serial data as been fully read = a multiple of 78
onedevice.replace(STRING_MSG,""); trc(F("Sending BT data to MQTT"));
/*String company = onedevice.substring(0,8); #ifdef ESP8266
String uuid = onedevice.substring(9,41); yield();
String others = onedevice.substring(42,52);*/ #endif
String mac = onedevice.substring(53,65); for (int i=0;i<(int)device_number;i++){
String rssi = onedevice.substring(66,70); String onedevice = discResult.substring(0,78);
String mactopic = subjectBTtoMQTT + mac; onedevice.replace(STRING_MSG,"");
trc(mactopic + " " + rssi); /*String company = onedevice.substring(0,8);
client.publish((char *)mactopic.c_str(),(char *)rssi.c_str()); String uuid = onedevice.substring(9,41);
discResult = discResult.substring(78); String others = onedevice.substring(42,52);*/
#ifdef ESP8266 String mac = onedevice.substring(53,65);
yield(); String rssi = onedevice.substring(66,70);
#endif String mactopic = subjectBTtoMQTT + mac;
trc(mactopic + " " + rssi);
client.publish((char *)mactopic.c_str(),(char *)rssi.c_str());
discResult = discResult.substring(78);
#ifdef ESP8266
yield();
#endif
}
return true;
} }
return true;
} }
if (discResult.indexOf(SETUP_MSG)>=0) if (discResult.indexOf(SETUP_MSG)>=0)
{ {