From 141bb9c65710f366e8a5dd14405e0caba31a9b6e Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Mon, 4 Mar 2019 04:49:35 +0300 Subject: [PATCH] MQTT topics config & CallBack refactoring --- check_custom_build_flags_esp32.sh | 2 + check_custom_build_flags_esp8266.sh | 1 + check_custom_build_flags_nrf52840.sh | 2 +- check_custom_build_flags_stm32.sh | 2 + lighthub/inputs.cpp | 12 +- lighthub/item.cpp | 10 +- lighthub/lighthub.ino | 8 +- lighthub/main.cpp | 178 ++++++++++++++++++--------- lighthub/main.h | 6 +- lighthub/options.h | 17 +++ lighthub/utils.cpp | 85 +++++++++++++ lighthub/utils.h | 10 ++ platformio.ini | 3 +- 13 files changed, 258 insertions(+), 78 deletions(-) diff --git a/check_custom_build_flags_esp32.sh b/check_custom_build_flags_esp32.sh index 14db68b..ea38d5b 100755 --- a/check_custom_build_flags_esp32.sh +++ b/check_custom_build_flags_esp32.sh @@ -3,6 +3,8 @@ export FLAGS="$FLAGS -DPIO_SRC_REV="$(git log --pretty=format:%h_%ad -1 --date=s export FLAGS="$FLAGS -DDMX_DISABLE" export FLAGS="$FLAGS -DMODBUS_DISABLE" export FLAGS="$FLAGS -DOWIRE_DISABLE" +export FLAGS="$FLAGS -DDHT_DISABLE" +export FLAGS="$FLAGS -DCOUNTER_DISABLE" #export FLAGS="$FLAGS -std=gnu++11" CUSTOM_BUILD_FLAGS_FILE=custom-build-flags/build_flags_esp32.sh if [ -f $CUSTOM_BUILD_FLAGS_FILE ]; then diff --git a/check_custom_build_flags_esp8266.sh b/check_custom_build_flags_esp8266.sh index ef6d14a..d8e709a 100755 --- a/check_custom_build_flags_esp8266.sh +++ b/check_custom_build_flags_esp8266.sh @@ -1,5 +1,6 @@ #! /bin/bash export FLAGS="$FLAGS -DMODBUS_DISABLE" +export FLAGS="$FLAGS -DCOUNTER_DISABLE" export FLAGS="$FLAGS -DPIO_SRC_REV="$(git log --pretty=format:%h_%ad -1 --date=short) CUSTOM_BUILD_FLAGS_FILE=custom-build-flags/build_flags_esp8266.sh if [ -f $CUSTOM_BUILD_FLAGS_FILE ]; then diff --git a/check_custom_build_flags_nrf52840.sh b/check_custom_build_flags_nrf52840.sh index 9770f37..ca88af5 100755 --- a/check_custom_build_flags_nrf52840.sh +++ b/check_custom_build_flags_nrf52840.sh @@ -6,7 +6,7 @@ export FLAGS="$FLAGS -DMODBUS_DISABLE" export FLAGS="$FLAGS -DOWIRE_DISABLE" export FLAGS="$FLAGS -std=gnu++11" export FLAGS="$FLAGS -DWIFI_MANAGER_DISABLE" -export FLAGS="$FLAGS -DDHT_COUNTER_DISABLE" +export FLAGS="$FLAGS -DCOUNTER_DISABLE" CUSTOM_BUILD_FLAGS_FILE=custom-build-flags/build_flags_nrf52840.sh if [ -f $CUSTOM_BUILD_FLAGS_FILE ]; then diff --git a/check_custom_build_flags_stm32.sh b/check_custom_build_flags_stm32.sh index b6169c0..6364e8c 100755 --- a/check_custom_build_flags_stm32.sh +++ b/check_custom_build_flags_stm32.sh @@ -4,6 +4,8 @@ export FLAGS="$FLAGS -DPIO_SRC_REV="$(git log --pretty=format:%h_%ad -1 --date=s export FLAGS="$FLAGS -DDMX_DISABLE" export FLAGS="$FLAGS -DMODBUS_DISABLE" export FLAGS="$FLAGS -DOWIRE_DISABLE" +export FLAGS="$FLAGS -DDHT_DISABLE" +export FLAGS="$FLAGS -DCOUNTER_DISABLE" if [ -f $CUSTOM_BUILD_FLAGS_FILE ]; then source $CUSTOM_BUILD_FLAGS_FILE fi diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index 94b6aa1..947d42b 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -23,7 +23,7 @@ e-mail anklimov@gmail.com #include "utils.h" #include -#ifndef DHT_COUNTER_DISABLE +#ifndef DHT_DISABLE #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) #include @@ -34,7 +34,7 @@ e-mail anklimov@gmail.com extern PubSubClient mqttClient; -#ifndef DHT_COUNTER_DISABLE +#ifndef COUNTER_DISABLE static volatile unsigned long nextPollMillisValue[5]; static volatile int nextPollMillisPin[5] = {0,0,0,0,0}; #if defined(ARDUINO_ARCH_AVR) @@ -110,7 +110,7 @@ int Input::poll() { if (!isValid()) return -1; if (0) ; - #ifndef DHT_COUNTER_DISABLE + #ifndef DHT_DISABLE else if (inType & IN_DHT22) dht22Poll(); else if (inType & IN_COUNTER) @@ -127,7 +127,7 @@ int Input::poll() { // contactPoll(); } -#ifndef DHT_COUNTER_DISABLE +#ifndef COUNTER_DISABLE void Input::counterPoll() { if(nextPollTime()>millis()) return; @@ -172,7 +172,7 @@ void Input::counterPoll() { } #endif -#ifndef DHT_COUNTER_DISABLE +#ifndef COUNTER_DISABLE void Input::attachInterruptPinIrq(int realPin, int irq) { pinMode(realPin, INPUT); int real_irq; @@ -389,7 +389,7 @@ void Input::analogPoll() { // Mapping if (inputMap && inputMap->type == aJson_Array) { - int max; + int max; if (aJson.getArraySize(inputMap)>=4) mappedInputVal = map (mappedInputVal, aJson.getArrayItem(inputMap, 0)->valueint, diff --git a/lighthub/item.cpp b/lighthub/item.cpp index 42d88bb..ac14fb2 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -42,7 +42,7 @@ extern aJsonObject *pollingItem; extern PubSubClient mqttClient; //extern char outprefix[]; -const char outprefix[] PROGMEM = OUTTOPIC; +//const char outprefix[] PROGMEM = OUTTOPIC; static unsigned long lastctrl = 0; static aJsonObject *lastobj = NULL; @@ -884,7 +884,9 @@ int Item::checkFM() { char *outch; char addrstr[32]; - strcpy_P(addrstr, outprefix); + //strcpy_P(addrstr, outprefix); + setTopic(addrstr,sizeof(addrstr),T_OUT); + strncat(addrstr, itemArr->name, sizeof(addrstr) - 1); strncat(addrstr, "_stat", sizeof(addrstr) - 1); @@ -1164,7 +1166,9 @@ int Item::SendStatus(short cmd, short n, int *Par, boolean deffered) { //char addrbuf[17]; char valstr[16] = ""; - strcpy_P(addrstr, outprefix); + //strcpy_P(addrstr, outprefix); + setTopic(addrstr,sizeof(addrstr),T_OUT); + strncat(addrstr, itemArr->name, sizeof(addrstr)); diff --git a/lighthub/lighthub.ino b/lighthub/lighthub.ino index 467cc94..163e489 100644 --- a/lighthub/lighthub.ino +++ b/lighthub/lighthub.ino @@ -1,7 +1,11 @@ #include "main.h" void setup(){ - setup_main(); + //if (millis()>1000) + setup_main(); + //else Serial.println("Hello"); + //delay(1000); } void loop(){ - loop_main(); + //if (millis()>10000) + loop_main(); } diff --git a/lighthub/main.cpp b/lighthub/main.cpp index e637f17..8dbfbbe 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -115,13 +115,13 @@ unsigned long nextSyslogPingTime; lan_status lanStatus = INITIAL_STATE; -const char outprefix[] PROGMEM = OUTTOPIC; -const char inprefix[] PROGMEM = INTOPIC; + const char configserver[] PROGMEM = CONFIG_SERVER; unsigned int UniqueID[5] = {0,0,0,0,0}; - +char *deviceName = NULL; +aJsonObject *topics = NULL; aJsonObject *root = NULL; aJsonObject *items = NULL; aJsonObject *inputs = NULL; @@ -181,29 +181,63 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) { debugSerial<<((char) payload[i]); debugSerial<name,sizeof(topic)); strncat(topic,"/",sizeof(topic)); strncat_P(topic,datatype_P,sizeof(topic)); @@ -419,7 +449,9 @@ void onMQTTConnect(){ if (format[0]) { - strncpy_P(topic, outprefix, sizeof(topic)); + //strncpy_P(topic, outprefix, sizeof(topic)); + setTopic(topic,sizeof(topic),T_OUT); + strncat(topic,item->name,sizeof(topic)); strncat(topic,"/",sizeof(topic)); strncat_P(topic,format_P,sizeof(topic)); @@ -427,7 +459,8 @@ void onMQTTConnect(){ } item = item->next; } //if - strncpy_P(topic, outprefix, sizeof(topic)); + //strncpy_P(topic, outprefix, sizeof(topic)); + setTopic(topic,sizeof(topic),T_OUT); strncat_P(topic, nodes_P, sizeof(topic)); mqttClient.publish(topic,buf,true); } @@ -441,18 +474,26 @@ void ip_ready_config_loaded_connecting_to_broker() { char *user = ∅ char passwordBuf[16] = ""; char *password = passwordBuf; + int syslogPort = 514; + char syslogDeviceHostname[16]; + if (mqttArr && (aJson.getArraySize(mqttArr))) deviceName = aJson.getArrayItem(mqttArr, 0)->valuestring; + #ifdef SYSLOG_ENABLE - debugSerial<<"debugSerial:"; + //debugSerial<<"debugSerial:"; delay(100); - if (udpSyslogArr && aJson.getArraySize(udpSyslogArr)) { + if (udpSyslogArr && (n = aJson.getArraySize(udpSyslogArr))) { char *syslogServer = aJson.getArrayItem(udpSyslogArr, 0)->valuestring; - int syslogPort = aJson.getArrayItem(udpSyslogArr, 1)->valueint; + if (n>1) syslogPort = aJson.getArrayItem(udpSyslogArr, 1)->valueint; + + inet_ntoa_r(Ethernet.localIP(),syslogDeviceHostname,sizeof(syslogDeviceHostname)); +/* char *syslogDeviceHostname = aJson.getArrayItem(udpSyslogArr, 2)->valuestring; char *syslogAppname = aJson.getArrayItem(udpSyslogArr, 3)->valuestring; - debugSerial< 1)) { - char *client_id = aJson.getArrayItem(mqttArr, 0)->valuestring; + // char *client_id = aJson.getArrayItem(mqttArr, 0)->valuestring; char *servername = aJson.getArrayItem(mqttArr, 1)->valuestring; if (n >= 3) port = aJson.getArrayItem(mqttArr, 2)->valueint; if (n >= 4) user = aJson.getArrayItem(mqttArr, 3)->valuestring; @@ -477,26 +518,35 @@ void ip_ready_config_loaded_connecting_to_broker() { strncpy_P(willMessage,disconnected_P,sizeof(willMessage)); - strncpy_P(willTopic, outprefix, sizeof(willTopic)); + // strncpy_P(willTopic, outprefix, sizeof(willTopic)); + setTopic(willTopic,sizeof(willTopic),T_OUT); + strncat_P(willTopic, state_P, sizeof(willTopic)); debugSerial<> 4, HEX); @@ -326,6 +333,84 @@ int inet_aton(const char* aIPAddrString, IPAddress& aResult) } } +/** + * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used. + * + * @param addr ip address in network order to convert + * @param buf target buffer where the string is stored + * @param buflen length of buf + * @return either pointer to buf which now holds the ASCII + * representation of addr or NULL if buf was too small + */ +char *inet_ntoa_r(IPAddress addr, char *buf, int buflen) +{ + short n; + char intbuf[4]; + + +buf[0]=0; +for(n = 0; n < 4; n++) { + if (addr[n]>255) addr[n]=-1; + itoa(addr[n],intbuf,10); + strncat(buf,intbuf,buflen); + if (n<3) strncat(buf,".",buflen); +} + return buf; +} + + +void printIPAddress(IPAddress ipAddress) { + for (byte i = 0; i < 4; i++) +#ifdef WITH_PRINTEX_LIB + (i < 3) ? debugSerial << (ipAddress[i]) << F(".") : debugSerial << (ipAddress[i])<type == aJson_Object) + { + _root = aJson.getObjectItem(topics, "root"); + switch (tt) { + case T_OUT: + _l2 = aJson.getObjectItem(topics, "out"); + break; + case T_BCST: + _l2 = aJson.getObjectItem(topics, "bcst"); + break; + } + + + } +if (_root) strncpy(buf,_root->valuestring,buflen); + else strncpy_P(buf,homeTopic,buflen); +strncat(buf,"/",buflen); + +if (_l2) strncat(buf,_l2->valuestring,buflen); + else + switch (tt) { + case T_DEV: + strncat(buf,deviceName,buflen); + break; + case T_OUT: + strncat_P(buf,outTopic,buflen); + break; + case T_BCST: + strncat_P(buf,inTopic,buflen); ///// + break; + } +strncat(buf,"/",buflen); +if (suffix) strncat(buf,suffix,buflen); + +return buf; + +} #pragma message(VAR_NAME_VALUE(debugSerial)) #pragma message(VAR_NAME_VALUE(SERIAL_BAUD)) diff --git a/lighthub/utils.h b/lighthub/utils.h index 8e361a0..d3c69a5 100644 --- a/lighthub/utils.h +++ b/lighthub/utils.h @@ -25,6 +25,7 @@ e-mail anklimov@gmail.com #include #include +#include "aJSON.h" #include "options.h" #ifdef WITH_PRINTEX_LIB #include "PrintEx.h" @@ -33,6 +34,12 @@ using namespace ios; #include "Streaming.h" #endif +enum topicType { + T_DEV = 1, + T_BCST= 2, + T_OUT = 3 + }; + void PrintBytes(uint8_t* addr, uint8_t count, bool newline); void SetBytes(uint8_t* addr, uint8_t count, char * out); void SetAddr(char * out, uint8_t* addr); @@ -44,3 +51,6 @@ int log(const char *str, ...); void printFloatValueToStr(float value, char *valstr); void ReadUniqueID( unsigned int * pdwUniqueID ); int inet_aton(const char* aIPAddrString, IPAddress& aResult); +char *inet_ntoa_r(IPAddress addr, char *buf, int buflen); +void printIPAddress(IPAddress ipAddress); +char* setTopic(char* buf, int8_t buflen, topicType tt, char* suffix = NULL); diff --git a/platformio.ini b/platformio.ini index 8e846e3..86ae76c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,7 +9,7 @@ ; http://docs.platformio.org/page/projectconf.html [platformio] src_dir = lighthub -env_default = megaatmega2560 +env_default = megaatmega2560-net ;monitor_speed = 115200 ; megaatmega2560 ; megaatmega2560-net @@ -254,7 +254,6 @@ lib_deps = https://github.com/arcao/Syslog.git Streaming - [env:due-5500] platform = atmelsam framework = arduino