code cleanup and improvment (#339)
* replace string by char array comparison * take mac adress from user_config for arduino ethernet shield * remove code for managing HM10 with version < v6xx * add sys measure call for arduino mega * code size decrease and optim
This commit is contained in:
1 parent
afe4678350
commit
c7fe43eb97
5 files changed
+165
-226
No files matched your search
+29
-28
@@ -71,6 +71,8 @@
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
#define array_size 12
|
||||
unsigned long ReceivedSignal[array_size][2] ={{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}};
|
||||
//Time used to wait for an interval before checking system measures
|
||||
unsigned long timer_sys_measures = 0;
|
||||
#else // boards with smaller memory
|
||||
#define array_size 4
|
||||
unsigned long ReceivedSignal[array_size][2] ={{0,0},{0,0},{0,0},{0,0}};
|
||||
@@ -179,20 +181,6 @@ unsigned long timer_led_receive = 0;
|
||||
unsigned long timer_led_send = 0;
|
||||
unsigned long timer_led_error = 0;
|
||||
|
||||
//Time used to wait for an interval before checking system measures
|
||||
unsigned long timer_sys_measures = 0;
|
||||
|
||||
//Wifi manager parameters
|
||||
//flag for saving data
|
||||
bool shouldSaveConfig = true;
|
||||
//do we have been connected once to mqtt
|
||||
|
||||
//callback notifying us of the need to save config
|
||||
void saveConfigCallback () {
|
||||
trc("Should save config");
|
||||
shouldSaveConfig = true;
|
||||
}
|
||||
|
||||
boolean reconnect() {
|
||||
|
||||
// Loop until we're reconnected
|
||||
@@ -444,6 +432,17 @@ void setup_wifi() {
|
||||
}
|
||||
|
||||
#elif defined(ESP8266) && !defined(ESPWifiManualSetup)
|
||||
//Wifi manager parameters
|
||||
//flag for saving data
|
||||
bool shouldSaveConfig = true;
|
||||
//do we have been connected once to mqtt
|
||||
|
||||
//callback notifying us of the need to save config
|
||||
void saveConfigCallback () {
|
||||
trc("Should save config");
|
||||
shouldSaveConfig = true;
|
||||
}
|
||||
|
||||
void setup_wifimanager(){
|
||||
#ifdef cleanFS
|
||||
//clean FS, for testing
|
||||
@@ -704,7 +703,7 @@ void loop()
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
void stateMeasures(){
|
||||
unsigned long now = millis();
|
||||
if (now > (timer_sys_measures + TimeBetweenReadingSYS)) {//retriving value of memory ram every TimeBetweenReadingSYS
|
||||
@@ -715,19 +714,21 @@ void stateMeasures(){
|
||||
unsigned long uptime = millis()/1000;
|
||||
trc(uptime);
|
||||
SYSdata["uptime"] = uptime;
|
||||
trc("Remaining memory");
|
||||
uint32_t freeMem;
|
||||
freeMem = ESP.getFreeHeap();
|
||||
SYSdata["freeMem"] = freeMem;
|
||||
trc(freeMem);
|
||||
trc("RSSI");
|
||||
long rssi = WiFi.RSSI();
|
||||
SYSdata["rssi"] = rssi;
|
||||
trc(rssi);
|
||||
trc("SSID");
|
||||
String SSID = WiFi.SSID();
|
||||
SYSdata["SSID"] = SSID;
|
||||
trc(SSID);
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
trc("Remaining memory");
|
||||
uint32_t freeMem;
|
||||
freeMem = ESP.getFreeHeap();
|
||||
SYSdata["freeMem"] = freeMem;
|
||||
trc(freeMem);
|
||||
trc("RSSI");
|
||||
long rssi = WiFi.RSSI();
|
||||
SYSdata["rssi"] = rssi;
|
||||
trc(rssi);
|
||||
trc("SSID");
|
||||
String SSID = WiFi.SSID();
|
||||
SYSdata["SSID"] = SSID;
|
||||
trc(SSID);
|
||||
#endif
|
||||
trc("Activated modules");
|
||||
String modules = "";
|
||||
#ifdef ZgatewayRF
|
||||
|
||||
+114
-160
@@ -173,175 +173,129 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht
|
||||
|
||||
#else // arduino or ESP8266 working with HM10/11
|
||||
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
#define STRING_MSG "OK+DISC:"
|
||||
#define RESPONSE_MSG "OK+DISIS"
|
||||
#define RESP_END_MSG "OK+DISCE"
|
||||
#define SETUP_MSG "OK+RESET"
|
||||
|
||||
SoftwareSerial softserial(BT_RX, BT_TX);
|
||||
|
||||
String returnedString = "";
|
||||
unsigned long timebt = 0;
|
||||
|
||||
// this struct define which parts of the hexadecimal chain we extract and what to do with these parts
|
||||
struct decompose d[6] = {{"mac",16,12,true},{"typ",28,2,false},{"rsi",30,2,false},{"rdl",32,2,false},{"sty",44,4,true},{"rda",34,60,false}};
|
||||
|
||||
void setupBT() {
|
||||
softserial.begin(9600);
|
||||
softserial.print(F("AT+ROLE1"));
|
||||
delay(100);
|
||||
softserial.print(F("AT+IMME1"));
|
||||
delay(100);
|
||||
softserial.print(F("AT+RESET"));
|
||||
delay(100);
|
||||
trc(F("ZgatewayBT HM1X setup done "));
|
||||
}
|
||||
|
||||
#ifdef ZgatewayBT_v6xx
|
||||
#define QUESTION_MSG "AT+DISA?"
|
||||
boolean BTtoMQTT() {
|
||||
|
||||
//extract serial data from module in hexa format
|
||||
while (softserial.available() > 0) {
|
||||
int a = softserial.read();
|
||||
if (a < 16) {
|
||||
returnedString = returnedString + "0";
|
||||
}
|
||||
returnedString = returnedString + String(a,HEX);
|
||||
}
|
||||
|
||||
if (millis() > (timebt + TimeBtw_Read)) {//retriving data
|
||||
timebt = millis();
|
||||
#if defined(ESP8266)
|
||||
yield();
|
||||
#endif
|
||||
if (returnedString != "") {
|
||||
size_t pos = 0;
|
||||
while ((pos = returnedString.lastIndexOf(delimiter)) != -1) {
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
#define STRING_MSG "OK+DISC:"
|
||||
#define QUESTION_MSG "AT+DISA?"
|
||||
#define RESPONSE_MSG "OK+DISIS"
|
||||
#define RESP_END_MSG "OK+DISCE"
|
||||
#define SETUP_MSG "OK+RESET"
|
||||
|
||||
SoftwareSerial softserial(BT_RX, BT_TX);
|
||||
|
||||
String returnedString = "";
|
||||
unsigned long timebt = 0;
|
||||
|
||||
// this struct define which parts of the hexadecimal chain we extract and what to do with these parts
|
||||
struct decompose d[6] = {{"mac",16,12,true},{"typ",28,2,false},{"rsi",30,2,false},{"rdl",32,2,false},{"sty",44,4,true},{"rda",34,60,false}};
|
||||
|
||||
void setupBT() {
|
||||
softserial.begin(9600);
|
||||
softserial.print(F("AT+ROLE1"));
|
||||
delay(100);
|
||||
softserial.print(F("AT+IMME1"));
|
||||
delay(100);
|
||||
softserial.print(F("AT+RESET"));
|
||||
delay(100);
|
||||
trc(F("ZgatewayBT HM1X setup done "));
|
||||
}
|
||||
|
||||
boolean BTtoMQTT() {
|
||||
|
||||
//extract serial data from module in hexa format
|
||||
while (softserial.available() > 0) {
|
||||
int a = softserial.read();
|
||||
if (a < 16) {
|
||||
returnedString = returnedString + "0";
|
||||
}
|
||||
returnedString = returnedString + String(a,HEX);
|
||||
}
|
||||
|
||||
if (millis() > (timebt + TimeBtw_Read)) {//retriving data
|
||||
timebt = millis();
|
||||
#if defined(ESP8266)
|
||||
yield();
|
||||
#endif
|
||||
String token = returnedString.substring(pos);
|
||||
returnedString.remove(pos,returnedString.length() );
|
||||
char token_char[token.length()+1];
|
||||
token.toCharArray(token_char, token.length()+1);
|
||||
trc(token);
|
||||
if ( token.length() > 60){// we extract data only if we have detailled infos
|
||||
for(int i =0; i<6;i++)
|
||||
{
|
||||
extract_char(token_char,d[i].extract,d[i].start, d[i].len ,d[i].reverse, false);
|
||||
if (i == 3) d[5].len = (int)strtol(d[i].extract, NULL, 16) * 2; // extracting the length of the rest data
|
||||
}
|
||||
|
||||
if((strlen(d[0].extract)) == 12) // if a mac adress is detected we publish it
|
||||
{
|
||||
trc(F("Creating BLE buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& BLEdata = jsonBuffer.createObject();
|
||||
#ifdef subjectHomePresence
|
||||
String HomePresenceId;
|
||||
for (int i = 0; i<12; i++){
|
||||
HomePresenceId += String(d[0].extract[i]);
|
||||
if(((i-1) % 2 == 0) && (i!=11)) HomePresenceId += ":";
|
||||
if (returnedString != "") {
|
||||
size_t pos = 0;
|
||||
while ((pos = returnedString.lastIndexOf(delimiter)) != -1) {
|
||||
#if defined(ESP8266)
|
||||
yield();
|
||||
#endif
|
||||
String token = returnedString.substring(pos);
|
||||
returnedString.remove(pos,returnedString.length() );
|
||||
char token_char[token.length()+1];
|
||||
token.toCharArray(token_char, token.length()+1);
|
||||
trc(token);
|
||||
if ( token.length() > 60){// we extract data only if we have detailled infos
|
||||
for(int i =0; i<6;i++)
|
||||
{
|
||||
extract_char(token_char,d[i].extract,d[i].start, d[i].len ,d[i].reverse, false);
|
||||
if (i == 3) d[5].len = (int)strtol(d[i].extract, NULL, 16) * 2; // extracting the length of the rest data
|
||||
}
|
||||
trc(F("HomePresenceId"));
|
||||
trc(HomePresenceId);
|
||||
BLEdata.set("id", (char *)HomePresenceId.c_str());
|
||||
#endif
|
||||
strupp(d[0].extract);
|
||||
String topic = subjectBTtoMQTT + String(d[0].extract);
|
||||
int rssi = (int)strtol(d[2].extract, NULL, 16) - 256;
|
||||
BLEdata.set("rssi", (int)rssi);
|
||||
#ifdef subjectHomePresence
|
||||
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);
|
||||
Service_data = Service_data.substring(14);
|
||||
#ifdef pubBLEServiceData
|
||||
BLEdata.set("servicedata", (char *)Service_data.c_str());
|
||||
#endif
|
||||
pub((char *)topic.c_str(),BLEdata);
|
||||
if (strcmp(d[4].extract, "fe95") == 0) {
|
||||
int pos = -1;
|
||||
pos = strpos(d[5].extract,"209800");
|
||||
if (pos != -1) {
|
||||
trc("mi flora data reading");
|
||||
boolean result = process_data(pos - 38,(char *)Service_data.c_str(),d[0].extract);
|
||||
}
|
||||
pos = -1;
|
||||
pos = strpos(d[5].extract,"20aa01");
|
||||
if (pos != -1){
|
||||
trc("mi jia data reading");
|
||||
boolean result = process_data(pos - 40,(char *)Service_data.c_str(),d[0].extract);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if((strlen(d[0].extract)) == 12) // if a mac adress is detected we publish it
|
||||
{
|
||||
trc(F("Creating BLE buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& BLEdata = jsonBuffer.createObject();
|
||||
#ifdef subjectHomePresence
|
||||
String HomePresenceId;
|
||||
for (int i = 0; i<12; i++){
|
||||
HomePresenceId += String(d[0].extract[i]);
|
||||
if(((i-1) % 2 == 0) && (i!=11)) HomePresenceId += ":";
|
||||
}
|
||||
trc(F("HomePresenceId"));
|
||||
trc(HomePresenceId);
|
||||
BLEdata.set("id", (char *)HomePresenceId.c_str());
|
||||
#endif
|
||||
strupp(d[0].extract);
|
||||
String topic = subjectBTtoMQTT + String(d[0].extract);
|
||||
int rssi = (int)strtol(d[2].extract, NULL, 16) - 256;
|
||||
BLEdata.set("rssi", (int)rssi);
|
||||
#ifdef subjectHomePresence
|
||||
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);
|
||||
Service_data = Service_data.substring(14);
|
||||
#ifdef pubBLEServiceData
|
||||
BLEdata.set("servicedata", (char *)Service_data.c_str());
|
||||
#endif
|
||||
pub((char *)topic.c_str(),BLEdata);
|
||||
if (strcmp(d[4].extract, "fe95") == 0) {
|
||||
int pos = -1;
|
||||
pos = strpos(d[5].extract,"209800");
|
||||
if (pos != -1) {
|
||||
trc("mi flora data reading");
|
||||
boolean result = process_data(pos - 38,(char *)Service_data.c_str(),d[0].extract);
|
||||
}
|
||||
pos = -1;
|
||||
pos = strpos(d[5].extract,"20aa01");
|
||||
if (pos != -1){
|
||||
trc("mi jia data reading");
|
||||
boolean result = process_data(pos - 40,(char *)Service_data.c_str(),d[0].extract);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
returnedString = ""; //init data string
|
||||
return false;
|
||||
}
|
||||
}
|
||||
returnedString = ""; //init data string
|
||||
softserial.print(F(QUESTION_MSG));
|
||||
return false;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
softserial.print(F(QUESTION_MSG));
|
||||
return false;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void strupp(char* beg)
|
||||
{
|
||||
while (*beg = toupper(*beg))
|
||||
++beg;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef ZgatewayBT_v6xx
|
||||
#define QUESTION_MSG "AT+DISI?"
|
||||
boolean BTtoMQTT() {
|
||||
while (softserial.available() > 0) {
|
||||
#if defined(ESP8266)
|
||||
yield();
|
||||
#endif
|
||||
String discResult = softserial.readString();
|
||||
if (discResult.indexOf(STRING_MSG)>=0){
|
||||
discResult.replace(RESPONSE_MSG,"");
|
||||
discResult.replace(RESP_END_MSG,"");
|
||||
float device_number = discResult.length()/78.0;
|
||||
if (device_number == (int)device_number){ // to avoid publishing partial values we detect if the serial data has been fully read = a multiple of 78
|
||||
trc(F("Sending BT data to MQTT HM1X Version<v6xx"));
|
||||
#if defined(ESP8266)
|
||||
yield();
|
||||
#endif
|
||||
for (int i=0;i<(int)device_number;i++){
|
||||
String onedevice = discResult.substring(0,78);
|
||||
onedevice.replace(STRING_MSG,"");
|
||||
String mac = onedevice.substring(53,65);
|
||||
pub(subjectBTtoMQTT + mac,onedevice.substring(66,70));
|
||||
discResult = discResult.substring(78);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (discResult.indexOf(SETUP_MSG)>=0)
|
||||
{
|
||||
trc(F("Connection OK to HM-10"));
|
||||
}
|
||||
}
|
||||
if (millis() > (timebt + TimeBtw_Read)) {//retriving value of adresses and rssi
|
||||
timebt = millis();
|
||||
#if defined(ESP8266)
|
||||
yield();
|
||||
#endif
|
||||
softserial.print(F(QUESTION_MSG));
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
void strupp(char* beg)
|
||||
{
|
||||
while (*beg = toupper(*beg))
|
||||
++beg;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
boolean process_data(int offset, char * rest_data, char * mac_adress){
|
||||
|
||||
+2
-4
@@ -479,10 +479,8 @@ void IRtoMQTT(){
|
||||
|
||||
#ifdef jsonPublishing
|
||||
void MQTTtoIR(char * topicOri, JsonObject& IRdata) {
|
||||
|
||||
String topic = topicOri;
|
||||
|
||||
if (topic == subjectMQTTtoIR) {
|
||||
|
||||
if (strcmp(topicOri,subjectMQTTtoIR) == 0){
|
||||
trc(F("MQTTtoIR json data analysis"));
|
||||
unsigned long data = IRdata["value"];
|
||||
const char * raw = IRdata["raw"];
|
||||
|
||||
+10
-29
@@ -41,13 +41,12 @@ void setupRF(){
|
||||
mySwitch.enableReceive(RF_RECEIVER_PIN);
|
||||
trc(F("RF_RECEIVER_PIN "));
|
||||
trc(RF_RECEIVER_PIN);
|
||||
trc(F("ZgatewayRF setup done "));
|
||||
trc(F("RF setup ok"));
|
||||
}
|
||||
|
||||
void RFtoMQTT(){
|
||||
|
||||
if (mySwitch.available()){
|
||||
trc(F("Creating RF buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& RFdata = jsonBuffer.createObject();
|
||||
trc(F("Rcv. RF"));
|
||||
@@ -68,13 +67,12 @@ void RFtoMQTT(){
|
||||
|
||||
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"));
|
||||
#ifdef ZmqttDiscovery //component creation for HA
|
||||
RFtoMQTTdiscovery(MQTTvalue);
|
||||
#endif
|
||||
pub(subjectRFtoMQTT,RFdata);
|
||||
if (repeatRFwMQTT){
|
||||
trc(F("Publish RF for repeat"));
|
||||
trc(F("Pub RF for rpt"));
|
||||
pub(subjectMQTTtoRF,RFdata);
|
||||
}
|
||||
}
|
||||
@@ -159,34 +157,17 @@ void MQTTtoRF(char * topicOri, char * datacallback) {
|
||||
void MQTTtoRF(char * topicOri, JsonObject& RFdata) { // json object decoding
|
||||
|
||||
if (strcmp(topicOri,subjectMQTTtoRF) == 0){
|
||||
trc(F("MQTTtoRF json data analysis"));
|
||||
trc(F("MQTTtoRF json 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
|
||||
}
|
||||
int valuePRT = RFdata["protocol"]|1;
|
||||
int valuePLSL = RFdata["delay"]|350;
|
||||
int valueBITS = RFdata["length"]|24;
|
||||
mySwitch.setProtocol(valuePRT,valuePLSL);
|
||||
mySwitch.send(data, valueBITS);
|
||||
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"));
|
||||
trc(F("MQTTtoRF Fail read json"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
-5
@@ -386,13 +386,18 @@ void createDiscovery(char * sensor_type,
|
||||
String getMacAddress()
|
||||
{
|
||||
uint8_t baseMac[6];
|
||||
#ifdef ESP8266
|
||||
WiFi.macAddress(baseMac);
|
||||
#else
|
||||
esp_read_mac(baseMac, ESP_MAC_WIFI_STA);
|
||||
#endif
|
||||
char baseMacChr[13] = {0};
|
||||
#if defined(ESP8266)
|
||||
WiFi.macAddress(baseMac);
|
||||
sprintf(baseMacChr, "%02X%02X%02X%02X%02X%02X", baseMac[0], baseMac[1], baseMac[2], baseMac[3], baseMac[4], baseMac[5]);
|
||||
#elif defined(ESP32)
|
||||
esp_read_mac(baseMac, ESP_MAC_WIFI_STA);
|
||||
sprintf(baseMacChr, "%02X%02X%02X%02X%02X%02X", baseMac[0], baseMac[1], baseMac[2], baseMac[3], baseMac[4], baseMac[5]);
|
||||
#else
|
||||
sprintf(baseMacChr, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
#endif
|
||||
|
||||
|
||||
return String(baseMacChr);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user