mirror of
https://github.com/anklimov/lighthub
synced 2025-12-06 03:39:49 +03:00
Retained MQTT topics logic developed, unused sources (SD & extra 1-wire) moved to spare folders to save static RAM
This commit is contained in:
@@ -17,13 +17,13 @@ bool shouldSaveConfig = false;
|
||||
|
||||
//callback notifying us of the need to save config
|
||||
void saveConfigCallback () {
|
||||
Serial.println("Should save config");
|
||||
Serial.println(F("Should save config"));
|
||||
shouldSaveConfig = true;
|
||||
}
|
||||
|
||||
|
||||
void espSetup () {
|
||||
Serial.println("Setting up Wifi");
|
||||
Serial.println(F("Setting up Wifi"));
|
||||
shouldSaveConfig = true;
|
||||
//WiFiManager
|
||||
|
||||
@@ -37,7 +37,7 @@ void espSetup () {
|
||||
wifiManager.setMinimumSignalQuality();
|
||||
|
||||
if (!wifiManager.autoConnect()) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
Serial.println(F("failed to connect and hit timeout"));
|
||||
delay(3000);
|
||||
//reset and try again, or maybe put it to deep sleep
|
||||
ESP.reset();
|
||||
@@ -45,7 +45,7 @@ if (!wifiManager.autoConnect()) {
|
||||
}
|
||||
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.println("connected...yeey :)");
|
||||
Serial.println(F("connected...yeey :)"));
|
||||
|
||||
//read updated parameters
|
||||
strcpy(mqtt_password, custom_mqtt_password.getValue());
|
||||
|
||||
@@ -71,7 +71,7 @@ void Input::Parse()
|
||||
|
||||
|
||||
s = aJson.getObjectItem(inputObj,"S");
|
||||
if (!s) { Serial.print("In: ");Serial.print(pin);Serial.print("/");Serial.println(inType);
|
||||
if (!s) { Serial.print(F("In: "));Serial.print(pin);Serial.print(F("/"));Serial.println(inType);
|
||||
aJson.addNumberToObject(inputObj,"S", 0);
|
||||
s = aJson.getObjectItem(inputObj,"S");
|
||||
}
|
||||
@@ -110,7 +110,7 @@ int Input::Poll()
|
||||
|
||||
void Input::Changed (int val)
|
||||
{
|
||||
Serial.print(pin);Serial.print("=");Serial.println(val);
|
||||
Serial.print(pin);Serial.print(F("="));Serial.println(val);
|
||||
aJsonObject * item = aJson.getObjectItem(inputObj,"item");
|
||||
aJsonObject * scmd = aJson.getObjectItem(inputObj,"scmd");
|
||||
aJsonObject * rcmd = aJson.getObjectItem(inputObj,"rcmd");
|
||||
@@ -121,11 +121,11 @@ void Input::Changed (int val)
|
||||
|
||||
if (val)
|
||||
{ //send set command
|
||||
if (!scmd) mqttClient.publish(emit->valuestring,"ON"); else if (strlen(scmd->valuestring)) mqttClient.publish(emit->valuestring,scmd->valuestring);
|
||||
if (!scmd) mqttClient.publish(emit->valuestring,"ON",true); else if (strlen(scmd->valuestring)) mqttClient.publish(emit->valuestring,scmd->valuestring,true);
|
||||
}
|
||||
else
|
||||
{ //send reset command
|
||||
if (!rcmd) mqttClient.publish(emit->valuestring,"OFF"); else if (strlen(rcmd->valuestring)) mqttClient.publish(emit->valuestring,rcmd->valuestring);
|
||||
if (!rcmd) mqttClient.publish(emit->valuestring,"OFF",true); else if (strlen(rcmd->valuestring)) mqttClient.publish(emit->valuestring,rcmd->valuestring,true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ GIT: https://github.com/anklimov/lighthub
|
||||
e-mail anklimov@gmail.com
|
||||
|
||||
*/
|
||||
|
||||
#include "options.h"
|
||||
#include "item.h"
|
||||
#include "aJSON.h"
|
||||
|
||||
@@ -41,8 +41,8 @@ extern aJsonObject *modbusitem;
|
||||
int modbusSet(int addr, uint16_t _reg, int _mask, uint16_t value);
|
||||
|
||||
extern PubSubClient mqttClient;
|
||||
//extern const char* outprefix;
|
||||
const char outprefix[] PROGMEM = "/myhome/s_out/";
|
||||
//extern char outprefix[];
|
||||
const char outprefix[] PROGMEM = OUTTOPIC;
|
||||
|
||||
static unsigned long lastctrl = 0;
|
||||
static aJsonObject *lastobj = NULL;
|
||||
@@ -252,7 +252,7 @@ int Item::Ctrl(short cmd, short n, int *Par, boolean send) {
|
||||
case CH_VC:
|
||||
case CH_DIMMER:
|
||||
case CH_MODBUS:
|
||||
SendCmd(0, 1, Par); // Send back parameter for channel above this line
|
||||
if (send) SendCmd(0, 1, Par); // Send back parameter for channel above this line
|
||||
case CH_THERMO:
|
||||
case CH_VCTEMP:
|
||||
setVal(Par[0]); // Store value
|
||||
@@ -279,7 +279,7 @@ int Item::Ctrl(short cmd, short n, int *Par, boolean send) {
|
||||
Par[1] = st.s;
|
||||
Par[2] = st.v;
|
||||
params = 3;
|
||||
SendCmd(0, params, Par); // Send restored triplet
|
||||
SendCmd(0, params, Par); // Send restored triplet. In any cases
|
||||
break;
|
||||
|
||||
|
||||
@@ -291,15 +291,15 @@ int Item::Ctrl(short cmd, short n, int *Par, boolean send) {
|
||||
|
||||
Par[0] = st.aslong;
|
||||
params = 1;
|
||||
SendCmd(0, params, Par); // Send restored parameter
|
||||
SendCmd(0, params, Par); // Send restored parameter, even if send=false - no problem, loop will be supressed at next hop
|
||||
break;
|
||||
case CH_THERMO:
|
||||
Par[0] = st.aslong;
|
||||
params = 0;
|
||||
SendCmd(CMD_ON); // Just ON (switch)
|
||||
if (send) SendCmd(CMD_ON); // Just ON (switch)
|
||||
break;
|
||||
default:
|
||||
SendCmd(cmd); // Just send ON
|
||||
if (send) SendCmd(cmd); // Just send ON
|
||||
}//itemtype
|
||||
else {// Default settings
|
||||
Serial.print(st.aslong);
|
||||
@@ -325,7 +325,7 @@ int Item::Ctrl(short cmd, short n, int *Par, boolean send) {
|
||||
}
|
||||
|
||||
|
||||
setCmd(cmd);
|
||||
if (send) setCmd(cmd);
|
||||
} else { //Double ON - apply special preset - clean white full power
|
||||
switch (itemType) {
|
||||
case CH_RGBW:
|
||||
@@ -341,7 +341,7 @@ int Item::Ctrl(short cmd, short n, int *Par, boolean send) {
|
||||
setVal(st.aslong);
|
||||
|
||||
//Send to OH
|
||||
SendCmd(0, 3, Par);
|
||||
if (send) SendCmd(0, 3, Par);
|
||||
break;
|
||||
} //itemtype
|
||||
} //else
|
||||
@@ -359,7 +359,7 @@ int Item::Ctrl(short cmd, short n, int *Par, boolean send) {
|
||||
Par[1] = 0;
|
||||
Par[2] = 0;
|
||||
setCmd(cmd);
|
||||
SendCmd(cmd);
|
||||
if (send) SendCmd(cmd); ///?
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -370,7 +370,7 @@ int Item::Ctrl(short cmd, short n, int *Par, boolean send) {
|
||||
Par[1] = 0;
|
||||
Par[2] = 0;
|
||||
setCmd(cmd);
|
||||
SendCmd(CMD_OFF);
|
||||
SendCmd(CMD_OFF); //HALT to OFF mapping - send in any cases
|
||||
Serial.println(F(" Halted"));
|
||||
}
|
||||
|
||||
@@ -770,7 +770,7 @@ int Item::SendCmd(short cmd, short n, int *Par) {
|
||||
Serial.print(addrstr);
|
||||
Serial.print(F("->"));
|
||||
Serial.println(valstr);
|
||||
mqttClient.publish(addrstr, valstr);
|
||||
mqttClient.publish(addrstr, valstr,true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ class Item
|
||||
Item(char * name);
|
||||
Item(aJsonObject * obj);
|
||||
boolean isValid ();
|
||||
virtual int Ctrl(short cmd, short n=0, int * Par=NULL, boolean send=false);
|
||||
virtual int Ctrl(short cmd, short n=0, int * Par=NULL, boolean send=true);
|
||||
int getArg(short n=0);
|
||||
boolean getEnableCMD(int delta);
|
||||
//int getVal(short n); //From VAL array. Negative if no array
|
||||
|
||||
@@ -149,11 +149,6 @@ EthernetClient ethClient;
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef PIO_SRC_REV
|
||||
#define PIO_SRC_REV v0.98
|
||||
#endif
|
||||
|
||||
|
||||
#include "item.h"
|
||||
#include "inputs.h"
|
||||
|
||||
@@ -173,13 +168,10 @@ extern Artnet *artnet;
|
||||
#define GIST 2
|
||||
//#define serverip "192.168.88.2"
|
||||
//IPAddress server(192, 168, 88, 2); //TODO - configure it
|
||||
//char* inprefix=("/myhome/in/");
|
||||
//char* outprefix=("/myhome/s_out/");
|
||||
//char* subprefix=("/myhome/in/#");
|
||||
|
||||
#define inprefix "/myhome/in/"
|
||||
const char outprefix[] PROGMEM = "/myhome/s_out/";
|
||||
#define subprefix "/myhome/in/#"
|
||||
//const char inprefix[] PROGMEM = "/myhome/in/"
|
||||
const char outprefix[] PROGMEM = OUTTOPIC;
|
||||
const char inprefix[] PROGMEM = INTOPIC;
|
||||
|
||||
aJsonObject *root = NULL;
|
||||
aJsonObject *items = NULL;
|
||||
@@ -213,11 +205,13 @@ PubSubClient mqttClient(ethClient);
|
||||
void watchdogSetup(void) {} //Do not remove - strong re-definition WDT Init for DUE
|
||||
|
||||
// MQTT Callback routine
|
||||
#define sublen 20
|
||||
#define topiclen 20
|
||||
void callback(char *topic, byte *payload, unsigned int length) {
|
||||
payload[length] = 0;
|
||||
Serial.print(F("\n["));
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
Serial.print(F("] "));
|
||||
|
||||
int fr = freeRam();
|
||||
if (fr < 250) {
|
||||
@@ -225,17 +219,28 @@ void callback(char *topic, byte *payload, unsigned int length) {
|
||||
return;
|
||||
}
|
||||
|
||||
#define sublen 20
|
||||
char subtopic[sublen] = "";
|
||||
int cmd = 0;
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
Serial.print((char) payload[i]);
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// short intopic = strncmp(topic,F(inprefix),strlen(inprefix));
|
||||
// short outtopic = strncmp(topic,F(outprefix),strlen(outprefix));
|
||||
boolean retaining = (lanStatus == 4); //Todo - named constant
|
||||
//Check if topic = Command topic
|
||||
short intopic=0;
|
||||
{
|
||||
char buf[topiclen+1];
|
||||
strncpy_P(buf,inprefix,sizeof(buf));
|
||||
intopic = strncmp(topic,buf,strlen(inprefix));
|
||||
}
|
||||
|
||||
// in Retaining status - trying to restore previous state from retained output topic. Retained input topics are not relevant.
|
||||
if (retaining && !intopic) {
|
||||
Serial.println(F("Skipping.."));
|
||||
return;
|
||||
}
|
||||
|
||||
char subtopic[sublen] = "";
|
||||
int cmd = 0;
|
||||
|
||||
cmd = txt2cmd((char *) payload);
|
||||
char *t;
|
||||
@@ -270,7 +275,7 @@ void callback(char *topic, byte *payload, unsigned int length) {
|
||||
while (payload && i < 3)
|
||||
Par[i++] = getInt((char **) &payload);
|
||||
|
||||
item.Ctrl(0, i, Par);
|
||||
item.Ctrl(0, i, Par, !retaining);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -280,13 +285,13 @@ void callback(char *topic, byte *payload, unsigned int length) {
|
||||
|
||||
case CMD_ON:
|
||||
|
||||
if (item.getEnableCMD(500))
|
||||
item.Ctrl(cmd); //Accept ON command not earlier then 500 ms after set settings (Homekit hack)
|
||||
if (item.getEnableCMD(500) || lanStatus == 4)
|
||||
item.Ctrl(cmd, 0, NULL, !retaining); //Accept ON command not earlier then 500 ms after set settings (Homekit hack)
|
||||
else Serial.println("on Skipped");
|
||||
|
||||
break;
|
||||
default: //some known command
|
||||
item.Ctrl(cmd);
|
||||
item.Ctrl(cmd, 0, NULL, !retaining);
|
||||
|
||||
} //ctrl
|
||||
} //valid json
|
||||
@@ -320,7 +325,7 @@ void printMACAddress() {
|
||||
|
||||
void restoreState() {
|
||||
// Once connected, publish an announcement... // Once connected, publish an announcement...
|
||||
mqttClient.publish("/myhome/out/RestoreState", "ON");
|
||||
//mqttClient.publish("/myhome/out/RestoreState", "ON");
|
||||
};
|
||||
|
||||
|
||||
@@ -397,13 +402,23 @@ if((wifiMulti.run() == WL_CONNECTED)) lanStatus=1;
|
||||
Serial.println(client_id);
|
||||
|
||||
|
||||
// ... and resubscribe
|
||||
mqttClient.subscribe(subprefix);
|
||||
// ... Temporary subscribe to status topic
|
||||
char buf[topiclen];
|
||||
|
||||
strncpy_P(buf,outprefix,sizeof(buf));
|
||||
strncat(buf,"#",sizeof(buf));
|
||||
mqttClient.subscribe(buf);
|
||||
|
||||
restoreState();
|
||||
//Subscribing for command topics
|
||||
strncpy_P(buf,inprefix,sizeof(buf));
|
||||
strncat(buf,"#",sizeof(buf));
|
||||
mqttClient.subscribe(buf);
|
||||
|
||||
//restoreState();
|
||||
// if (_once) {DMXput(); _once=0;}
|
||||
lanStatus = 3;
|
||||
lanStatus = 4;
|
||||
lanCheck = millis() + 5000;
|
||||
Serial.println(F("Awaiting for retained topics"));
|
||||
} else {
|
||||
Serial.print(F("failed, rc="));
|
||||
Serial.print(mqttClient.state());
|
||||
@@ -414,6 +429,21 @@ if((wifiMulti.run() == WL_CONNECTED)) lanStatus=1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 4: //retaining ... Collecting
|
||||
if (millis() > lanCheck) {
|
||||
char buf[topiclen];
|
||||
|
||||
//Unsubscribe from status topics..
|
||||
strncpy_P(buf,outprefix,sizeof(buf));
|
||||
strncat(buf,"#",sizeof(buf));
|
||||
mqttClient.unsubscribe(buf);
|
||||
|
||||
lanStatus = 3;
|
||||
Serial.println(F("Accepting commands..."));
|
||||
break;
|
||||
}
|
||||
|
||||
case 3: //operation
|
||||
if (!mqttClient.connected()) lanStatus = 2;
|
||||
break;
|
||||
@@ -505,7 +535,7 @@ void Changed(int i, DeviceAddress addr, int val) {
|
||||
if (owEmit) {
|
||||
strncpy(addrbuf, owEmit, sizeof(addrbuf));
|
||||
Serial.print(owEmit);
|
||||
Serial.print("=");
|
||||
Serial.print(F("="));
|
||||
Serial.println(val);
|
||||
}
|
||||
owItem = aJson.getObjectItem(owObj, "item")->valuestring;
|
||||
@@ -866,7 +896,7 @@ int getConfig(int arg_cnt, char **args)
|
||||
response = htclient.responseBody();
|
||||
htclient.stop();
|
||||
wdt_res();
|
||||
Serial.print("HTTP Status code: ");
|
||||
Serial.print(F("HTTP Status code: "));
|
||||
Serial.println(responseStatusCode);
|
||||
//Serial.print("GET Response: ");
|
||||
|
||||
@@ -1043,7 +1073,7 @@ void owIdle(void) {
|
||||
|
||||
wdt_res();
|
||||
return;///
|
||||
Serial.print("o");
|
||||
Serial.print(F("o"));
|
||||
|
||||
if (lanLoop() == 1) mqttClient.loop();
|
||||
//if (owReady) owLoop();
|
||||
|
||||
@@ -1,4 +1,16 @@
|
||||
// Configuration of drivers enabled
|
||||
#ifndef PIO_SRC_REV
|
||||
#define PIO_SRC_REV v0.99
|
||||
#endif
|
||||
|
||||
#ifndef OUTTOPIC
|
||||
#define OUTTOPIC "/myhome/s_out/"
|
||||
#endif
|
||||
|
||||
#ifndef INTOPIC
|
||||
#define INTOPIC "/myhome/in/"
|
||||
#endif
|
||||
|
||||
#ifndef DMX_DISABLE
|
||||
#define _dmxin
|
||||
#define _dmxout
|
||||
@@ -12,7 +24,9 @@
|
||||
#define _modbus
|
||||
#endif
|
||||
|
||||
#ifdef ARTNET_ENABLE
|
||||
#define _artnet
|
||||
#endif
|
||||
|
||||
#if defined(ESP8266)
|
||||
#define __ESP__
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
[platformio]
|
||||
src_dir = lighthub
|
||||
env_default =
|
||||
; megaatmega2560
|
||||
due
|
||||
megaatmega2560
|
||||
; due
|
||||
; esp8266
|
||||
|
||||
[env:due]
|
||||
|
||||
Reference in New Issue
Block a user