MQTT topics config & CallBack refactoring

This commit is contained in:
2019-03-04 04:49:35 +03:00
parent 23e493097e
commit 141bb9c657
13 changed files with 258 additions and 78 deletions

View File

@@ -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 -DDMX_DISABLE"
export FLAGS="$FLAGS -DMODBUS_DISABLE" export FLAGS="$FLAGS -DMODBUS_DISABLE"
export FLAGS="$FLAGS -DOWIRE_DISABLE" export FLAGS="$FLAGS -DOWIRE_DISABLE"
export FLAGS="$FLAGS -DDHT_DISABLE"
export FLAGS="$FLAGS -DCOUNTER_DISABLE"
#export FLAGS="$FLAGS -std=gnu++11" #export FLAGS="$FLAGS -std=gnu++11"
CUSTOM_BUILD_FLAGS_FILE=custom-build-flags/build_flags_esp32.sh CUSTOM_BUILD_FLAGS_FILE=custom-build-flags/build_flags_esp32.sh
if [ -f $CUSTOM_BUILD_FLAGS_FILE ]; then if [ -f $CUSTOM_BUILD_FLAGS_FILE ]; then

View File

@@ -1,5 +1,6 @@
#! /bin/bash #! /bin/bash
export FLAGS="$FLAGS -DMODBUS_DISABLE" 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) 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 CUSTOM_BUILD_FLAGS_FILE=custom-build-flags/build_flags_esp8266.sh
if [ -f $CUSTOM_BUILD_FLAGS_FILE ]; then if [ -f $CUSTOM_BUILD_FLAGS_FILE ]; then

View File

@@ -6,7 +6,7 @@ export FLAGS="$FLAGS -DMODBUS_DISABLE"
export FLAGS="$FLAGS -DOWIRE_DISABLE" export FLAGS="$FLAGS -DOWIRE_DISABLE"
export FLAGS="$FLAGS -std=gnu++11" export FLAGS="$FLAGS -std=gnu++11"
export FLAGS="$FLAGS -DWIFI_MANAGER_DISABLE" 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 CUSTOM_BUILD_FLAGS_FILE=custom-build-flags/build_flags_nrf52840.sh
if [ -f $CUSTOM_BUILD_FLAGS_FILE ]; then if [ -f $CUSTOM_BUILD_FLAGS_FILE ]; then

View File

@@ -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 -DDMX_DISABLE"
export FLAGS="$FLAGS -DMODBUS_DISABLE" export FLAGS="$FLAGS -DMODBUS_DISABLE"
export FLAGS="$FLAGS -DOWIRE_DISABLE" export FLAGS="$FLAGS -DOWIRE_DISABLE"
export FLAGS="$FLAGS -DDHT_DISABLE"
export FLAGS="$FLAGS -DCOUNTER_DISABLE"
if [ -f $CUSTOM_BUILD_FLAGS_FILE ]; then if [ -f $CUSTOM_BUILD_FLAGS_FILE ]; then
source $CUSTOM_BUILD_FLAGS_FILE source $CUSTOM_BUILD_FLAGS_FILE
fi fi

View File

@@ -23,7 +23,7 @@ e-mail anklimov@gmail.com
#include "utils.h" #include "utils.h"
#include <PubSubClient.h> #include <PubSubClient.h>
#ifndef DHT_COUNTER_DISABLE #ifndef DHT_DISABLE
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#include <DHTesp.h> #include <DHTesp.h>
@@ -34,7 +34,7 @@ e-mail anklimov@gmail.com
extern PubSubClient mqttClient; extern PubSubClient mqttClient;
#ifndef DHT_COUNTER_DISABLE #ifndef COUNTER_DISABLE
static volatile unsigned long nextPollMillisValue[5]; static volatile unsigned long nextPollMillisValue[5];
static volatile int nextPollMillisPin[5] = {0,0,0,0,0}; static volatile int nextPollMillisPin[5] = {0,0,0,0,0};
#if defined(ARDUINO_ARCH_AVR) #if defined(ARDUINO_ARCH_AVR)
@@ -110,7 +110,7 @@ int Input::poll() {
if (!isValid()) return -1; if (!isValid()) return -1;
if (0) ; if (0) ;
#ifndef DHT_COUNTER_DISABLE #ifndef DHT_DISABLE
else if (inType & IN_DHT22) else if (inType & IN_DHT22)
dht22Poll(); dht22Poll();
else if (inType & IN_COUNTER) else if (inType & IN_COUNTER)
@@ -127,7 +127,7 @@ int Input::poll() {
// contactPoll(); // contactPoll();
} }
#ifndef DHT_COUNTER_DISABLE #ifndef COUNTER_DISABLE
void Input::counterPoll() { void Input::counterPoll() {
if(nextPollTime()>millis()) if(nextPollTime()>millis())
return; return;
@@ -172,7 +172,7 @@ void Input::counterPoll() {
} }
#endif #endif
#ifndef DHT_COUNTER_DISABLE #ifndef COUNTER_DISABLE
void Input::attachInterruptPinIrq(int realPin, int irq) { void Input::attachInterruptPinIrq(int realPin, int irq) {
pinMode(realPin, INPUT); pinMode(realPin, INPUT);
int real_irq; int real_irq;

View File

@@ -42,7 +42,7 @@ extern aJsonObject *pollingItem;
extern PubSubClient mqttClient; extern PubSubClient mqttClient;
//extern char outprefix[]; //extern char outprefix[];
const char outprefix[] PROGMEM = OUTTOPIC; //const char outprefix[] PROGMEM = OUTTOPIC;
static unsigned long lastctrl = 0; static unsigned long lastctrl = 0;
static aJsonObject *lastobj = NULL; static aJsonObject *lastobj = NULL;
@@ -884,7 +884,9 @@ int Item::checkFM() {
char *outch; char *outch;
char addrstr[32]; 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, itemArr->name, sizeof(addrstr) - 1);
strncat(addrstr, "_stat", 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 addrbuf[17];
char valstr[16] = ""; char valstr[16] = "";
strcpy_P(addrstr, outprefix); //strcpy_P(addrstr, outprefix);
setTopic(addrstr,sizeof(addrstr),T_OUT);
strncat(addrstr, itemArr->name, sizeof(addrstr)); strncat(addrstr, itemArr->name, sizeof(addrstr));

View File

@@ -1,7 +1,11 @@
#include "main.h" #include "main.h"
void setup(){ void setup(){
//if (millis()>1000)
setup_main(); setup_main();
//else Serial.println("Hello");
//delay(1000);
} }
void loop(){ void loop(){
//if (millis()>10000)
loop_main(); loop_main();
} }

View File

@@ -115,13 +115,13 @@ unsigned long nextSyslogPingTime;
lan_status lanStatus = INITIAL_STATE; lan_status lanStatus = INITIAL_STATE;
const char outprefix[] PROGMEM = OUTTOPIC;
const char inprefix[] PROGMEM = INTOPIC;
const char configserver[] PROGMEM = CONFIG_SERVER; const char configserver[] PROGMEM = CONFIG_SERVER;
unsigned int UniqueID[5] = {0,0,0,0,0}; unsigned int UniqueID[5] = {0,0,0,0,0};
char *deviceName = NULL;
aJsonObject *topics = NULL;
aJsonObject *root = NULL; aJsonObject *root = NULL;
aJsonObject *items = NULL; aJsonObject *items = NULL;
aJsonObject *inputs = NULL; aJsonObject *inputs = NULL;
@@ -181,29 +181,63 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
debugSerial<<((char) payload[i]); debugSerial<<((char) payload[i]);
debugSerial<<endl; debugSerial<<endl;
if(!strcmp(topic,CMDTOPIC)) {
cmd_parse((char *)payload);
return;
}
short intopic = 0; short intopic = 0;
short pfxlen = 0;
char * itemName;
{ {
char buf[MQTT_TOPIC_LENGTH + 1]; char buf[MQTT_TOPIC_LENGTH + 1];
strncpy_P(buf, inprefix, sizeof(buf)); // strncpy_P(buf, inprefix, sizeof(buf));
intopic = strncmp(topic, buf, strlen(inprefix));
if (lanStatus == RETAINING_COLLECTING)
{
setTopic(buf,sizeof(buf),T_OUT);
pfxlen = strlen(buf);
intopic = strncmp(topic, buf, pfxlen);
}
else
{
setTopic(buf,sizeof(buf),T_BCST);
pfxlen = strlen(buf);
intopic = strncmp(topic, buf, pfxlen );
if (intopic)
{
setTopic(buf,sizeof(buf),T_DEV);
pfxlen = strlen(buf);
intopic = strncmp(topic, buf, pfxlen);
} }
}
}
// in Retaining status - trying to restore previous state from retained output topic. Retained input topics are not relevant. // in Retaining status - trying to restore previous state from retained output topic. Retained input topics are not relevant.
if ((lanStatus == RETAINING_COLLECTING) && !intopic) { if (intopic) {
debugSerial<<F("Skipping.."); debugSerial<<F("Skipping..");
return; return;
} }
char subtopic[MQTT_SUBJECT_LENGTH] = "";
// int cmd = 0; itemName=topic+pfxlen;
//cmd = txt2cmd((char *) payload);
if(!strcmp(itemName,CMDTOPIC)) {
cmd_parse((char *)payload);
return;
}
//char subtopic[MQTT_SUBJECT_LENGTH] = "";
char *t; char *t;
if (t = strrchr(topic, '/')) if (t = strchr(itemName, '/'))
strncpy(subtopic, t + 1, MQTT_SUBJECT_LENGTH - 1); {
Item item(subtopic); *t = 0;
t++;
debugSerial<<F("Subtopic:")<<t<<endl;
//strncpy(subtopic, t + 1, MQTT_SUBJECT_LENGTH - 1);
}
debugSerial<<F("Item:")<<itemName<<endl;
Item item(itemName);
if (item.isValid()) { if (item.isValid()) {
if (item.itemType == CH_GROUP && (lanStatus == RETAINING_COLLECTING)) if (item.itemType == CH_GROUP && (lanStatus == RETAINING_COLLECTING))
return; //Do not restore group channels - they consist not relevant data return; //Do not restore group channels - they consist not relevant data
@@ -211,14 +245,7 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
} //valid item } //valid item
} }
void printIPAddress(IPAddress ipAddress) {
for (byte i = 0; i < 4; i++)
#ifdef WITH_PRINTEX_LIB
(i < 3) ? debugSerial << (ipAddress[i]) << F(".") : debugSerial << (ipAddress[i])<<F(", ");
#else
(i < 3) ? debugSerial << _DEC(ipAddress[i]) << F(".") : debugSerial << _DEC(ipAddress[i]) << F(", ");
#endif
}
void printMACAddress() { void printMACAddress() {
debugSerial<<F("Configured MAC:"); debugSerial<<F("Configured MAC:");
@@ -230,10 +257,6 @@ void printMACAddress() {
#endif #endif
} }
void restoreState() {
// Once connected, publish an announcement... // Once connected, publish an announcement...
//mqttClient.publish("/myhome/out/RestoreState", "ON");
};
lan_status lanLoop() { lan_status lanLoop() {
@@ -263,7 +286,8 @@ lan_status lanLoop() {
char buf[MQTT_TOPIC_LENGTH]; char buf[MQTT_TOPIC_LENGTH];
//Unsubscribe from status topics.. //Unsubscribe from status topics..
strncpy_P(buf, outprefix, sizeof(buf)); //strncpy_P(buf, outprefix, sizeof(buf));
setTopic(buf,sizeof(buf),T_OUT);
strncat(buf, "#", sizeof(buf)); strncat(buf, "#", sizeof(buf));
mqttClient.unsubscribe(buf); mqttClient.unsubscribe(buf);
@@ -353,24 +377,28 @@ void onMQTTConnect(){
char buf[128] = ""; char buf[128] = "";
// High level homie topics publishing // High level homie topics publishing
strncpy_P(topic, outprefix, sizeof(topic)); //strncpy_P(topic, outprefix, sizeof(topic));
setTopic(topic,sizeof(topic),T_OUT);
strncat_P(topic, state_P, sizeof(topic)); strncat_P(topic, state_P, sizeof(topic));
strncpy_P(buf, ready_P, sizeof(buf)); strncpy_P(buf, ready_P, sizeof(buf));
mqttClient.publish(topic,buf,true); mqttClient.publish(topic,buf,true);
strncpy_P(topic, outprefix, sizeof(topic)); //strncpy_P(topic, outprefix, sizeof(topic));
setTopic(topic,sizeof(topic),T_OUT);
strncat_P(topic, name_P, sizeof(topic)); strncat_P(topic, name_P, sizeof(topic));
strncpy_P(buf, nameval_P, sizeof(buf)); strncpy_P(buf, nameval_P, sizeof(buf));
mqttClient.publish(topic,buf,true); mqttClient.publish(topic,buf,true);
strncpy_P(topic, outprefix, sizeof(topic)); //strncpy_P(topic, outprefix, sizeof(topic));
setTopic(topic,sizeof(topic),T_OUT);
strncat_P(topic, stats_P, sizeof(topic)); strncat_P(topic, stats_P, sizeof(topic));
strncpy_P(buf, statsval_P, sizeof(buf)); strncpy_P(buf, statsval_P, sizeof(buf));
mqttClient.publish(topic,buf,true); mqttClient.publish(topic,buf,true);
#ifndef NO_HOMIE #ifndef NO_HOMIE
strncpy_P(topic, outprefix, sizeof(topic)); // strncpy_P(topic, outprefix, sizeof(topic));
setTopic(topic,sizeof(topic),T_OUT);
strncat_P(topic, homie_P, sizeof(topic)); strncat_P(topic, homie_P, sizeof(topic));
strncpy_P(buf, homiever_P, sizeof(buf)); strncpy_P(buf, homiever_P, sizeof(buf));
mqttClient.publish(topic,buf,true); mqttClient.publish(topic,buf,true);
@@ -411,7 +439,9 @@ void onMQTTConnect(){
break; break;
} //switch } //switch
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,item->name,sizeof(topic));
strncat(topic,"/",sizeof(topic)); strncat(topic,"/",sizeof(topic));
strncat_P(topic,datatype_P,sizeof(topic)); strncat_P(topic,datatype_P,sizeof(topic));
@@ -419,7 +449,9 @@ void onMQTTConnect(){
if (format[0]) 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,item->name,sizeof(topic));
strncat(topic,"/",sizeof(topic)); strncat(topic,"/",sizeof(topic));
strncat_P(topic,format_P,sizeof(topic)); strncat_P(topic,format_P,sizeof(topic));
@@ -427,7 +459,8 @@ void onMQTTConnect(){
} }
item = item->next; item = item->next;
} //if } //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)); strncat_P(topic, nodes_P, sizeof(topic));
mqttClient.publish(topic,buf,true); mqttClient.publish(topic,buf,true);
} }
@@ -441,18 +474,26 @@ void ip_ready_config_loaded_connecting_to_broker() {
char *user = &empty; char *user = &empty;
char passwordBuf[16] = ""; char passwordBuf[16] = "";
char *password = passwordBuf; char *password = passwordBuf;
int syslogPort = 514;
char syslogDeviceHostname[16];
if (mqttArr && (aJson.getArraySize(mqttArr))) deviceName = aJson.getArrayItem(mqttArr, 0)->valuestring;
#ifdef SYSLOG_ENABLE #ifdef SYSLOG_ENABLE
debugSerial<<"debugSerial:"; //debugSerial<<"debugSerial:";
delay(100); delay(100);
if (udpSyslogArr && aJson.getArraySize(udpSyslogArr)) { if (udpSyslogArr && (n = aJson.getArraySize(udpSyslogArr))) {
char *syslogServer = aJson.getArrayItem(udpSyslogArr, 0)->valuestring; 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 *syslogDeviceHostname = aJson.getArrayItem(udpSyslogArr, 2)->valuestring;
char *syslogAppname = aJson.getArrayItem(udpSyslogArr, 3)->valuestring; char *syslogAppname = aJson.getArrayItem(udpSyslogArr, 3)->valuestring;
debugSerial<<F("Syslog params:")<<syslogServer<<syslogPort<<syslogDeviceHostname<<syslogAppname; */
debugSerial<<F("Syslog params:")<<syslogServer<<":"<<syslogPort<<":"<<syslogDeviceHostname<<":"<<deviceName<<endl;
udpSyslog.server(syslogServer, syslogPort); udpSyslog.server(syslogServer, syslogPort);
udpSyslog.deviceHostname(syslogDeviceHostname); udpSyslog.deviceHostname(syslogDeviceHostname);
udpSyslog.appName(syslogAppname); if (deviceName) udpSyslog.appName(deviceName);
udpSyslog.defaultPriority(LOG_KERN); udpSyslog.defaultPriority(LOG_KERN);
udpSyslog.log(LOG_INFO, F("UDP Syslog initialized!")); udpSyslog.log(LOG_INFO, F("UDP Syslog initialized!"));
debugSerial<<F("UDP Syslog initialized!\n"); debugSerial<<F("UDP Syslog initialized!\n");
@@ -460,7 +501,7 @@ void ip_ready_config_loaded_connecting_to_broker() {
#endif #endif
if (!mqttClient.connected() && mqttArr && ((n = aJson.getArraySize(mqttArr)) > 1)) { if (!mqttClient.connected() && mqttArr && ((n = aJson.getArraySize(mqttArr)) > 1)) {
char *client_id = aJson.getArrayItem(mqttArr, 0)->valuestring; // char *client_id = aJson.getArrayItem(mqttArr, 0)->valuestring;
char *servername = aJson.getArrayItem(mqttArr, 1)->valuestring; char *servername = aJson.getArrayItem(mqttArr, 1)->valuestring;
if (n >= 3) port = aJson.getArrayItem(mqttArr, 2)->valueint; if (n >= 3) port = aJson.getArrayItem(mqttArr, 2)->valueint;
if (n >= 4) user = aJson.getArrayItem(mqttArr, 3)->valuestring; 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(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)); strncat_P(willTopic, state_P, sizeof(willTopic));
debugSerial<<F("\nAttempting MQTT connection to ")<<servername<<F(":")<<port<<F(" user:")<<user<<F(" ..."); debugSerial<<F("\nAttempting MQTT connection to ")<<servername<<F(":")<<port<<F(" user:")<<user<<F(" ...");
wdt_dis(); //potential unsafe for ethernetIdle(), but needed to avoid cyclic reboot if mosquitto out of order wdt_dis(); //potential unsafe for ethernetIdle(), but needed to avoid cyclic reboot if mosquitto out of order
if (mqttClient.connect(client_id, user, password,willTopic,MQTTQOS1,true,willMessage)) { if (mqttClient.connect(deviceName, user, password,willTopic,MQTTQOS1,true,willMessage)) {
mqttErrorRate = 0; mqttErrorRate = 0;
debugSerial<<F("connected as ")<<client_id <<endl; debugSerial<<F("connected as ")<<deviceName <<endl;
wdt_en(); wdt_en();
configOk = true; configOk = true;
// ... Temporary subscribe to status topic // ... Temporary subscribe to status topic
char buf[MQTT_TOPIC_LENGTH]; char buf[MQTT_TOPIC_LENGTH];
strncpy_P(buf, outprefix, sizeof(buf)); // strncpy_P(buf, outprefix, sizeof(buf));
setTopic(buf,sizeof(buf),T_OUT);
strncat(buf, "#", sizeof(buf)); strncat(buf, "#", sizeof(buf));
mqttClient.subscribe(buf); mqttClient.subscribe(buf);
//Subscribing for command topics //Subscribing for command topics
strncpy_P(buf, inprefix, sizeof(buf)); //strncpy_P(buf, inprefix, sizeof(buf));
setTopic(buf,sizeof(buf),T_BCST);
strncat(buf, "#", sizeof(buf));
Serial.println(buf);
mqttClient.subscribe(buf);
setTopic(buf,sizeof(buf),T_DEV);
strncat(buf, "#", sizeof(buf)); strncat(buf, "#", sizeof(buf));
Serial.println(buf); Serial.println(buf);
mqttClient.subscribe(buf); mqttClient.subscribe(buf);
@@ -695,7 +745,8 @@ void Changed(int i, DeviceAddress addr, float currentTemp) {
} }
#endif #endif
strcpy_P(addrstr, outprefix); //strcpy_P(addrstr, outprefix);
setTopic(addrstr,sizeof(addrstr),T_OUT);
strncat(addrstr, owEmitString, sizeof(addrstr)); strncat(addrstr, owEmitString, sizeof(addrstr));
mqttClient.publish(addrstr, valstr); mqttClient.publish(addrstr, valstr);
} }
@@ -792,6 +843,7 @@ void applyConfig() {
} }
#endif #endif
items = aJson.getObjectItem(root, "items"); items = aJson.getObjectItem(root, "items");
topics = aJson.getObjectItem(root, "topics");
// Digital output related Items initialization // Digital output related Items initialization
pollingItem=NULL; pollingItem=NULL;
@@ -852,7 +904,7 @@ void printConfigSummary() {
void cmdFunctionLoad(int arg_cnt, char **args) { void cmdFunctionLoad(int arg_cnt, char **args) {
loadConfigFromEEPROM(); loadConfigFromEEPROM();
restoreState(); // restoreState();
} }
int loadConfigFromEEPROM() int loadConfigFromEEPROM()
@@ -882,7 +934,7 @@ int loadConfigFromEEPROM()
void cmdFunctionReq(int arg_cnt, char **args) { void cmdFunctionReq(int arg_cnt, char **args) {
mqttConfigRequest(arg_cnt, args); mqttConfigRequest(arg_cnt, args);
restoreState(); // restoreState();
} }
@@ -1297,10 +1349,16 @@ void printFirmwareVersionAndBuildOptions() {
#else #else
debugSerial<<F("\n(+)OWIRE"); debugSerial<<F("\n(+)OWIRE");
#endif #endif
#ifndef DHT_COUNTER_DISABLE #ifndef DHT_DISABLE
debugSerial<<F("\n(+)DHT COUNTER"); debugSerial<<F("\n(+)DHT");
#else #else
debugSerial<<F("\n(-)DHT COUNTER"); debugSerial<<F("\n(-)DHT");
#endif
#ifndef COUNTER_DISABLE
debugSerial<<F("\n(+)COUNTER");
#else
debugSerial<<F("\n(-)COUNTER");
#endif #endif
#ifdef SD_CARD_INSERTED #ifdef SD_CARD_INSERTED
@@ -1323,7 +1381,7 @@ debugSerial<<endl;
// WDT_Disable( WDT ) ; // WDT_Disable( WDT ) ;
Serial.println("Reading 128 bits unique identifier \n\r" ) ; Serial.println(F("Reading 128 bits unique identifier") ) ;
ReadUniqueID( UniqueID ) ; ReadUniqueID( UniqueID ) ;
Serial.print ("ID: ") ; Serial.print ("ID: ") ;
@@ -1333,6 +1391,9 @@ debugSerial<<endl;
} }
void publishStat(){ void publishStat(){
long fr = freeRam(); long fr = freeRam();
char topic[64]; char topic[64];
@@ -1340,15 +1401,14 @@ void publishStat(){
long ut = millis()/1000; long ut = millis()/1000;
// debugSerial<<F("\nfree RAM: ")<<fr; // debugSerial<<F("\nfree RAM: ")<<fr;
setTopic(topic,sizeof(topic),T_DEV);
strncpy_P(topic, outprefix, sizeof(topic));
strncat_P(topic, stats_P, sizeof(topic)); strncat_P(topic, stats_P, sizeof(topic));
strncat(topic, "/", sizeof(topic)); strncat(topic, "/", sizeof(topic));
strncat_P(topic, freeheap_P, sizeof(topic)); strncat_P(topic, freeheap_P, sizeof(topic));
mqttClient.publish(topic,itoa(fr,intbuf,10),true); mqttClient.publish(topic,itoa(fr,intbuf,10),true);
strncpy_P(topic, outprefix, sizeof(topic)); setTopic(topic,sizeof(topic),T_DEV);
strncat_P(topic, stats_P, sizeof(topic)); strncat_P(topic, stats_P, sizeof(topic));
strncat(topic, "/", sizeof(topic)); strncat(topic, "/", sizeof(topic));
strncat_P(topic, uptime_P, sizeof(topic)); strncat_P(topic, uptime_P, sizeof(topic));
@@ -1427,9 +1487,9 @@ void loop_main() {
#ifndef MODBUS_DISABLE #ifndef MODBUS_DISABLE
if (lanStatus != RETAINING_COLLECTING) pollingLoop(); if (lanStatus != RETAINING_COLLECTING) pollingLoop();
#endif #endif
#ifdef _owire //#ifdef _owire
thermoLoop(); thermoLoop();
#endif //#endif
} }

View File

@@ -161,17 +161,13 @@ enum lan_status {
DO_NOTHING = -14 DO_NOTHING = -14
}; };
//void watchdogSetup(void); //void watchdogSetup(void);
void mqttCallback(char *topic, byte *payload, unsigned int length); void mqttCallback(char *topic, byte *payload, unsigned int length);
void printIPAddress(IPAddress ipAddress);
void printMACAddress(); void printMACAddress();
void restoreState();
lan_status lanLoop(); lan_status lanLoop();
#ifndef OWIRE_DISABLE #ifndef OWIRE_DISABLE

View File

@@ -65,6 +65,10 @@
#define CONFIG_SERVER QUOTE(MY_CONFIG_SERVER) #define CONFIG_SERVER QUOTE(MY_CONFIG_SERVER)
#endif #endif
#ifndef HOMETOPIC
#define HOMETOPIC "/myhome"
#endif
/*
#ifndef OUTTOPIC #ifndef OUTTOPIC
#define OUTTOPIC "/myhome/s_out/" #define OUTTOPIC "/myhome/s_out/"
#endif #endif
@@ -76,6 +80,19 @@
#ifndef INTOPIC #ifndef INTOPIC
#define INTOPIC "/myhome/in/" #define INTOPIC "/myhome/in/"
#endif #endif
*/
#ifndef OUTTOPIC
#define OUTTOPIC "s_out"
#endif
#ifndef CMDTOPIC
#define CMDTOPIC "command"
#endif
#ifndef INTOPIC
#define INTOPIC "in"
#endif
#define MQTT_SUBJECT_LENGTH 20 #define MQTT_SUBJECT_LENGTH 20
#define MQTT_TOPIC_LENGTH 20 #define MQTT_TOPIC_LENGTH 20

View File

@@ -32,6 +32,13 @@ extern "C" {
} }
#endif #endif
const char outTopic[] PROGMEM = OUTTOPIC;
const char inTopic[] PROGMEM = INTOPIC;
const char homeTopic[] PROGMEM = HOMETOPIC;
extern char *deviceName;
extern aJsonObject *topics;
void PrintBytes(uint8_t *addr, uint8_t count, bool newline) { void PrintBytes(uint8_t *addr, uint8_t count, bool newline) {
for (uint8_t i = 0; i < count; i++) { for (uint8_t i = 0; i < count; i++) {
Serial.print(addr[i] >> 4, HEX); Serial.print(addr[i] >> 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])<<F(", ");
#else
(i < 3) ? debugSerial << _DEC(ipAddress[i]) << F(".") : debugSerial << _DEC(ipAddress[i]) << F(", ");
#endif
}
char* setTopic(char* buf, int8_t buflen, topicType tt, char* suffix=NULL)
{
aJsonObject *_root = NULL;
aJsonObject *_l2 = NULL;
if (topics && topics->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(debugSerial))
#pragma message(VAR_NAME_VALUE(SERIAL_BAUD)) #pragma message(VAR_NAME_VALUE(SERIAL_BAUD))

View File

@@ -25,6 +25,7 @@ e-mail anklimov@gmail.com
#include <Arduino.h> #include <Arduino.h>
#include <IPAddress.h> #include <IPAddress.h>
#include "aJSON.h"
#include "options.h" #include "options.h"
#ifdef WITH_PRINTEX_LIB #ifdef WITH_PRINTEX_LIB
#include "PrintEx.h" #include "PrintEx.h"
@@ -33,6 +34,12 @@ using namespace ios;
#include "Streaming.h" #include "Streaming.h"
#endif #endif
enum topicType {
T_DEV = 1,
T_BCST= 2,
T_OUT = 3
};
void PrintBytes(uint8_t* addr, uint8_t count, bool newline); void PrintBytes(uint8_t* addr, uint8_t count, bool newline);
void SetBytes(uint8_t* addr, uint8_t count, char * out); void SetBytes(uint8_t* addr, uint8_t count, char * out);
void SetAddr(char * out, uint8_t* addr); void SetAddr(char * out, uint8_t* addr);
@@ -44,3 +51,6 @@ int log(const char *str, ...);
void printFloatValueToStr(float value, char *valstr); void printFloatValueToStr(float value, char *valstr);
void ReadUniqueID( unsigned int * pdwUniqueID ); void ReadUniqueID( unsigned int * pdwUniqueID );
int inet_aton(const char* aIPAddrString, IPAddress& aResult); 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);

View File

@@ -9,7 +9,7 @@
; http://docs.platformio.org/page/projectconf.html ; http://docs.platformio.org/page/projectconf.html
[platformio] [platformio]
src_dir = lighthub src_dir = lighthub
env_default = megaatmega2560 env_default = megaatmega2560-net
;monitor_speed = 115200 ;monitor_speed = 115200
; megaatmega2560 ; megaatmega2560
; megaatmega2560-net ; megaatmega2560-net
@@ -254,7 +254,6 @@ lib_deps =
https://github.com/arcao/Syslog.git https://github.com/arcao/Syslog.git
Streaming Streaming
[env:due-5500] [env:due-5500]
platform = atmelsam platform = atmelsam
framework = arduino framework = arduino