mirror of
https://github.com/anklimov/lighthub
synced 2025-12-06 11:49:51 +03:00
MQTT topics config & CallBack refactoring
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -23,7 +23,7 @@ e-mail anklimov@gmail.com
|
||||
#include "utils.h"
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#ifndef DHT_COUNTER_DISABLE
|
||||
#ifndef DHT_DISABLE
|
||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||
#include <DHTesp.h>
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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));
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
#include "main.h"
|
||||
void setup(){
|
||||
//if (millis()>1000)
|
||||
setup_main();
|
||||
//else Serial.println("Hello");
|
||||
//delay(1000);
|
||||
}
|
||||
void loop(){
|
||||
//if (millis()>10000)
|
||||
loop_main();
|
||||
}
|
||||
|
||||
@@ -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<<endl;
|
||||
|
||||
if(!strcmp(topic,CMDTOPIC)) {
|
||||
cmd_parse((char *)payload);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
short intopic = 0;
|
||||
short pfxlen = 0;
|
||||
char * itemName;
|
||||
{
|
||||
char buf[MQTT_TOPIC_LENGTH + 1];
|
||||
strncpy_P(buf, inprefix, sizeof(buf));
|
||||
intopic = strncmp(topic, buf, strlen(inprefix));
|
||||
// strncpy_P(buf, inprefix, sizeof(buf));
|
||||
|
||||
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.
|
||||
if ((lanStatus == RETAINING_COLLECTING) && !intopic) {
|
||||
if (intopic) {
|
||||
debugSerial<<F("Skipping..");
|
||||
return;
|
||||
}
|
||||
char subtopic[MQTT_SUBJECT_LENGTH] = "";
|
||||
// int cmd = 0;
|
||||
//cmd = txt2cmd((char *) payload);
|
||||
|
||||
itemName=topic+pfxlen;
|
||||
|
||||
if(!strcmp(itemName,CMDTOPIC)) {
|
||||
cmd_parse((char *)payload);
|
||||
return;
|
||||
}
|
||||
//char subtopic[MQTT_SUBJECT_LENGTH] = "";
|
||||
char *t;
|
||||
if (t = strrchr(topic, '/'))
|
||||
strncpy(subtopic, t + 1, MQTT_SUBJECT_LENGTH - 1);
|
||||
Item item(subtopic);
|
||||
if (t = strchr(itemName, '/'))
|
||||
{
|
||||
*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.itemType == CH_GROUP && (lanStatus == RETAINING_COLLECTING))
|
||||
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
|
||||
}
|
||||
|
||||
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() {
|
||||
debugSerial<<F("Configured MAC:");
|
||||
@@ -230,10 +257,6 @@ void printMACAddress() {
|
||||
#endif
|
||||
}
|
||||
|
||||
void restoreState() {
|
||||
// Once connected, publish an announcement... // Once connected, publish an announcement...
|
||||
//mqttClient.publish("/myhome/out/RestoreState", "ON");
|
||||
};
|
||||
|
||||
lan_status lanLoop() {
|
||||
|
||||
@@ -263,7 +286,8 @@ lan_status lanLoop() {
|
||||
char buf[MQTT_TOPIC_LENGTH];
|
||||
|
||||
//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));
|
||||
mqttClient.unsubscribe(buf);
|
||||
|
||||
@@ -353,24 +377,28 @@ void onMQTTConnect(){
|
||||
char buf[128] = "";
|
||||
|
||||
// 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));
|
||||
strncpy_P(buf, ready_P, sizeof(buf));
|
||||
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));
|
||||
strncpy_P(buf, nameval_P, sizeof(buf));
|
||||
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));
|
||||
strncpy_P(buf, statsval_P, sizeof(buf));
|
||||
mqttClient.publish(topic,buf,true);
|
||||
|
||||
#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));
|
||||
strncpy_P(buf, homiever_P, sizeof(buf));
|
||||
mqttClient.publish(topic,buf,true);
|
||||
@@ -411,7 +439,9 @@ void onMQTTConnect(){
|
||||
break;
|
||||
} //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,"/",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<<F("Syslog params:")<<syslogServer<<syslogPort<<syslogDeviceHostname<<syslogAppname;
|
||||
*/
|
||||
debugSerial<<F("Syslog params:")<<syslogServer<<":"<<syslogPort<<":"<<syslogDeviceHostname<<":"<<deviceName<<endl;
|
||||
udpSyslog.server(syslogServer, syslogPort);
|
||||
udpSyslog.deviceHostname(syslogDeviceHostname);
|
||||
udpSyslog.appName(syslogAppname);
|
||||
if (deviceName) udpSyslog.appName(deviceName);
|
||||
udpSyslog.defaultPriority(LOG_KERN);
|
||||
udpSyslog.log(LOG_INFO, F("UDP Syslog initialized!"));
|
||||
debugSerial<<F("UDP Syslog initialized!\n");
|
||||
@@ -460,7 +501,7 @@ void ip_ready_config_loaded_connecting_to_broker() {
|
||||
#endif
|
||||
|
||||
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;
|
||||
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<<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
|
||||
if (mqttClient.connect(client_id, user, password,willTopic,MQTTQOS1,true,willMessage)) {
|
||||
if (mqttClient.connect(deviceName, user, password,willTopic,MQTTQOS1,true,willMessage)) {
|
||||
mqttErrorRate = 0;
|
||||
debugSerial<<F("connected as ")<<client_id <<endl;
|
||||
debugSerial<<F("connected as ")<<deviceName <<endl;
|
||||
wdt_en();
|
||||
configOk = true;
|
||||
// ... Temporary subscribe to status topic
|
||||
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));
|
||||
mqttClient.subscribe(buf);
|
||||
|
||||
//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));
|
||||
Serial.println(buf);
|
||||
mqttClient.subscribe(buf);
|
||||
@@ -695,7 +745,8 @@ void Changed(int i, DeviceAddress addr, float currentTemp) {
|
||||
}
|
||||
#endif
|
||||
|
||||
strcpy_P(addrstr, outprefix);
|
||||
//strcpy_P(addrstr, outprefix);
|
||||
setTopic(addrstr,sizeof(addrstr),T_OUT);
|
||||
strncat(addrstr, owEmitString, sizeof(addrstr));
|
||||
mqttClient.publish(addrstr, valstr);
|
||||
}
|
||||
@@ -792,6 +843,7 @@ void applyConfig() {
|
||||
}
|
||||
#endif
|
||||
items = aJson.getObjectItem(root, "items");
|
||||
topics = aJson.getObjectItem(root, "topics");
|
||||
|
||||
// Digital output related Items initialization
|
||||
pollingItem=NULL;
|
||||
@@ -852,7 +904,7 @@ void printConfigSummary() {
|
||||
|
||||
void cmdFunctionLoad(int arg_cnt, char **args) {
|
||||
loadConfigFromEEPROM();
|
||||
restoreState();
|
||||
// restoreState();
|
||||
}
|
||||
|
||||
int loadConfigFromEEPROM()
|
||||
@@ -882,7 +934,7 @@ int loadConfigFromEEPROM()
|
||||
|
||||
void cmdFunctionReq(int arg_cnt, char **args) {
|
||||
mqttConfigRequest(arg_cnt, args);
|
||||
restoreState();
|
||||
// restoreState();
|
||||
}
|
||||
|
||||
|
||||
@@ -1297,10 +1349,16 @@ void printFirmwareVersionAndBuildOptions() {
|
||||
#else
|
||||
debugSerial<<F("\n(+)OWIRE");
|
||||
#endif
|
||||
#ifndef DHT_COUNTER_DISABLE
|
||||
debugSerial<<F("\n(+)DHT COUNTER");
|
||||
#ifndef DHT_DISABLE
|
||||
debugSerial<<F("\n(+)DHT");
|
||||
#else
|
||||
debugSerial<<F("\n(-)DHT COUNTER");
|
||||
debugSerial<<F("\n(-)DHT");
|
||||
#endif
|
||||
|
||||
#ifndef COUNTER_DISABLE
|
||||
debugSerial<<F("\n(+)COUNTER");
|
||||
#else
|
||||
debugSerial<<F("\n(-)COUNTER");
|
||||
#endif
|
||||
|
||||
#ifdef SD_CARD_INSERTED
|
||||
@@ -1323,7 +1381,7 @@ debugSerial<<endl;
|
||||
|
||||
// WDT_Disable( WDT ) ;
|
||||
|
||||
Serial.println("Reading 128 bits unique identifier \n\r" ) ;
|
||||
Serial.println(F("Reading 128 bits unique identifier") ) ;
|
||||
ReadUniqueID( UniqueID ) ;
|
||||
|
||||
Serial.print ("ID: ") ;
|
||||
@@ -1333,6 +1391,9 @@ debugSerial<<endl;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void publishStat(){
|
||||
long fr = freeRam();
|
||||
char topic[64];
|
||||
@@ -1340,15 +1401,14 @@ void publishStat(){
|
||||
long ut = millis()/1000;
|
||||
|
||||
// debugSerial<<F("\nfree RAM: ")<<fr;
|
||||
|
||||
strncpy_P(topic, outprefix, sizeof(topic));
|
||||
setTopic(topic,sizeof(topic),T_DEV);
|
||||
strncat_P(topic, stats_P, sizeof(topic));
|
||||
strncat(topic, "/", sizeof(topic));
|
||||
strncat_P(topic, freeheap_P, sizeof(topic));
|
||||
|
||||
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(topic, "/", sizeof(topic));
|
||||
strncat_P(topic, uptime_P, sizeof(topic));
|
||||
@@ -1427,9 +1487,9 @@ void loop_main() {
|
||||
#ifndef MODBUS_DISABLE
|
||||
if (lanStatus != RETAINING_COLLECTING) pollingLoop();
|
||||
#endif
|
||||
#ifdef _owire
|
||||
//#ifdef _owire
|
||||
thermoLoop();
|
||||
#endif
|
||||
//#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -161,17 +161,13 @@ enum lan_status {
|
||||
DO_NOTHING = -14
|
||||
};
|
||||
|
||||
|
||||
//void watchdogSetup(void);
|
||||
|
||||
void mqttCallback(char *topic, byte *payload, unsigned int length);
|
||||
|
||||
|
||||
void printIPAddress(IPAddress ipAddress);
|
||||
|
||||
void printMACAddress();
|
||||
|
||||
void restoreState();
|
||||
|
||||
lan_status lanLoop();
|
||||
|
||||
#ifndef OWIRE_DISABLE
|
||||
|
||||
@@ -65,6 +65,10 @@
|
||||
#define CONFIG_SERVER QUOTE(MY_CONFIG_SERVER)
|
||||
#endif
|
||||
|
||||
#ifndef HOMETOPIC
|
||||
#define HOMETOPIC "/myhome"
|
||||
#endif
|
||||
/*
|
||||
#ifndef OUTTOPIC
|
||||
#define OUTTOPIC "/myhome/s_out/"
|
||||
#endif
|
||||
@@ -76,6 +80,19 @@
|
||||
#ifndef INTOPIC
|
||||
#define INTOPIC "/myhome/in/"
|
||||
#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_TOPIC_LENGTH 20
|
||||
|
||||
@@ -32,6 +32,13 @@ extern "C" {
|
||||
}
|
||||
#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) {
|
||||
for (uint8_t i = 0; i < count; i++) {
|
||||
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(SERIAL_BAUD))
|
||||
|
||||
@@ -25,6 +25,7 @@ e-mail anklimov@gmail.com
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <IPAddress.h>
|
||||
#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);
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user