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
+52
-113
No files matched your search
+16
-15
@@ -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,6 +714,7 @@ void stateMeasures(){
|
||||
unsigned long uptime = millis()/1000;
|
||||
trc(uptime);
|
||||
SYSdata["uptime"] = uptime;
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
trc("Remaining memory");
|
||||
uint32_t freeMem;
|
||||
freeMem = ESP.getFreeHeap();
|
||||
@@ -728,6 +728,7 @@ void stateMeasures(){
|
||||
String SSID = WiFi.SSID();
|
||||
SYSdata["SSID"] = SSID;
|
||||
trc(SSID);
|
||||
#endif
|
||||
trc("Activated modules");
|
||||
String modules = "";
|
||||
#ifdef ZgatewayRF
|
||||
|
||||
+18
-64
@@ -173,22 +173,23 @@ 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>
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
#define STRING_MSG "OK+DISC:"
|
||||
#define RESPONSE_MSG "OK+DISIS"
|
||||
#define RESP_END_MSG "OK+DISCE"
|
||||
#define SETUP_MSG "OK+RESET"
|
||||
#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);
|
||||
SoftwareSerial softserial(BT_RX, BT_TX);
|
||||
|
||||
String returnedString = "";
|
||||
unsigned long timebt = 0;
|
||||
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}};
|
||||
// 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() {
|
||||
void setupBT() {
|
||||
softserial.begin(9600);
|
||||
softserial.print(F("AT+ROLE1"));
|
||||
delay(100);
|
||||
@@ -197,11 +198,9 @@ void setupBT() {
|
||||
softserial.print(F("AT+RESET"));
|
||||
delay(100);
|
||||
trc(F("ZgatewayBT HM1X setup done "));
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ZgatewayBT_v6xx
|
||||
#define QUESTION_MSG "AT+DISA?"
|
||||
boolean BTtoMQTT() {
|
||||
boolean BTtoMQTT() {
|
||||
|
||||
//extract serial data from module in hexa format
|
||||
while (softserial.available() > 0) {
|
||||
@@ -289,61 +288,16 @@ boolean BTtoMQTT() {
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void strupp(char* beg)
|
||||
{
|
||||
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
|
||||
#endif
|
||||
|
||||
boolean process_data(int offset, char * rest_data, char * mac_adress){
|
||||
trc(F("Creating BLE buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
|
||||
+1
-3
@@ -480,9 +480,7 @@ 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"];
|
||||
|
||||
+7
-26
@@ -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);
|
||||
int valuePRT = RFdata["protocol"]|1;
|
||||
int valuePLSL = RFdata["delay"]|350;
|
||||
int valueBITS = RFdata["length"]|24;
|
||||
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"));
|
||||
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