Merge pull request #25 from livello/IS-DHT

(2)DHT22 Input support, thermo, esp refactoring, lan_status enum, wifi_manager, printing ip config, save current ip config
This commit is contained in:
2018-06-30 01:07:49 +03:00
committed by GitHub
26 changed files with 8256 additions and 8010 deletions

View File

@@ -13,7 +13,7 @@ Lighthub allows connecting together:
* Modbus RTU devices (Currently, are deployed two types of Modbus devices: AC Dimmer and Ventilation set (Based on [Vacon 10 controller](http://files.danfoss.com/download/Drives/Vacon-10-Quick-Guide-DPD00714F1-UK.pdf))
* Simple DMX wall sensor panel [like this](https://aliexpress.com/item/New-Ltech-D8-LED-rgb-RGBW-touch-panel-controller-DMX512-controller-DC12-24V-4-zones-4/32800199589.html)
![alt text](LightHub.png "LightHub application diagram")
![alt text](docs/LightHubAppDiagram.png "LightHub application diagram")
Where is possible both, to configure local control/mapping between inputs and outputs (light, floor heating thermostats) and remote control from MQTT enabled software. At the moment, LightHub tested with following set of complementary free software:
* [Openhab or Openhab2 Smarthome software](http://www.openhab.org/)
@@ -90,7 +90,9 @@ Prefered way to compile project is using platformio toolchain, suitable for Ardu
# Due compilation issue "USART0_Handler redefinition"
Please, open /variants/arduino_due_x/variant.cpp file, then add USART0_Handler method definition like this
```
void USART0_Handler(void) __attribute__((weak));
```
The normal path to find this file in platformio is:
.platformio/packages/framework-arduinosam/variants/arduino_due_x
@@ -114,7 +116,6 @@ cp build_flags_template.sh my_build_flags.sh
```
then edit, change or comment unnecessary sections and source it
```bash
nano my_build_flags.sh
source my_build_flags.sh
```
build and upload firmware for due|megaatmega2560|esp8266 board
@@ -144,7 +145,15 @@ platformio device monitor -b 115200
* MODBUS_DISABLE // disable Modbus support
* OWIRE_DISABLE // disable OneWire support
* ARTNET_ENABLE //Enable Artnet protocol support
* AVR_DMXOUT_PIN=18 // Set Pin for DMXOUT on megaatmega2560
* CONTROLLINO //Change Modbus port, direction pins and Wiznet SS pins to be working on [Controllino](http://controllino.biz/)
* LAN_INIT_DELAY=2000 // set lan init delay for Wiznet ethernet shield
* ESP_WIFI_AP=MYAP // esp wifi access point name
* ESP_WIFI_PWD=MYPWD // esp wifi access point password
* WIFI_MANAGER_DISABLE //Disable wifi manager for esp8266
* DHT_DISABLE //disable DHT Input support
* RESTART_LAN_ON_MQTT_ERRORS //reinit LAN if many mqtt errors occured
# Default compilation behavior:
@@ -160,8 +169,12 @@ platformio device monitor -b 115200
* Modbus support enabled
* OneWire support enabled
* Artnet disabled
* LAN_INIT_DELAY=500 //ms
* Defailt MQTT input topic: /myhome/in
* Default MQTT topic to publish device status: /myhome/s_out
* Default Alarm output topic /alarm
* DHT support enabled
* Wifi manager for esp8266 enabled
* RESTART_LAN_ON_MQTT_ERRORS disabled
If you've using Arduino IDE to compile & flash firmware, it will use Default options above and you will not able to configure additional compilers options except edit "options.h" file

View File

@@ -6,6 +6,7 @@
# nano my_build_flags.sh
# and source it
# source my_build_flags.sh
echo "==============================================Custom build flags are:====================================================="
export FLAGS="-DMY_CONFIG_SERVER=lazyhome.ru"
export FLAGS="$FLAGS -DWATCH_DOG_TICKER_DISABLE"
export FLAGS="$FLAGS -DUSE_1W_PIN=12"
@@ -17,5 +18,15 @@
export FLAGS="$FLAGS -DDMX_DISABLE"
export FLAGS="$FLAGS -DMODBUS_DISABLE"
export FLAGS="$FLAGS -DOWIRE_DISABLE"
export FLAGS="$FLAGS -DAVR_DMXOUT_PIN=18"
export FLAGS="$FLAGS -DLAN_INIT_DELAY=2000"
export FLAGS="$FLAGS -DCONTROLLINO"
export FLAGS="$FLAGS -DESP_WIFI_AP=MYAP"
export FLAGS="$FLAGS -DESP_WIFI_PWD=MYPWD"
export FLAGS="$FLAGS -DDHT_DISABLE"
export FLAGS="$FLAGS -DRESET_PIN=5"
export FLAGS="$FLAGS -DDHCP_RETRY_INTERVAL=60000"
export PLATFORMIO_BUILD_FLAGS="$FLAGS"
echo PLATFORMIO_BUILD_FLAGS=$PLATFORMIO_BUILD_FLAGS
echo "==============================================Custom build flags END====================================================="
unset FLAGS

Binary file not shown.

3
compiled/DUE/upload.bat Normal file
View File

@@ -0,0 +1,3 @@
mode com3:1200,n,8,1
pause
C:\Users\Akmal\.platformio\packages\tool-bossac\bossac.exe -i --port=com3 -U false -e -w -v -b C:\Users\Akmal\ownCloud\compiled\due\Wiz5500\firmware.bin -R

View File

@@ -0,0 +1,3 @@
mode com3:1200,n,8,1
pause
C:\Users\Akmal\.platformio\packages\tool-bossac\bossac.exe -i --port=com3 -U false -e -w -v -b C:\Users\Akmal\ownCloud\compiled\due\Wiz5500\firmware999.bin -R

1
compiled/mon.bat Normal file
View File

@@ -0,0 +1 @@
pio device monitor -b 115200

View File

@@ -0,0 +1,30 @@
{
"mqtt":["garden","192.168.10.114",1883,"test","test"],
"ow":{
"28FFADCE601705A3":{"emit":"t_soil1","item":"h_relay6"},
"286164123FF96F55":{"emit":"t_soil2"},
"28FFEF6D60170335":{"emit":"t_soil3"}
},
"items":{
"h_relay1":[6,39,1,1],
"h_relay2":[6,38,1,1],
"h_relay3":[6,37,1,1],
"h_relay4":[6,36,1,1],
"h_relay5":[6,35,1,1],
"h_relay6":[5,34,27,1],
"h_relay7":[6,33,1,1],
"h_relay8":[6,32,1,1],
"h_auto":[6,29,1,1]
},
"in":{
"5": {"T":4,"emit": "/myhome/s_out/t_dht1"},
"40":{"emit":"/myhome/s_out/g_in1","scmd":"CLOSED","rcmd":"OPEN"},
"41":{"emit":"/myhome/s_out/g_in2","scmd":"CLOSED","rcmd":"OPEN"},
"42":{"emit":"/myhome/s_out/g_in3","scmd":"CLOSED","rcmd":"OPEN"},
"43":{"emit":"/myhome/s_out/g_in4","scmd":"CLOSED","rcmd":"OPEN"},
"44":{"emit":"/myhome/s_out/g_in5","scmd":"CLOSED","rcmd":"OPEN"},
"45":{"emit":"/myhome/s_out/g_in6","scmd":"CLOSED","rcmd":"OPEN"},
"46":{"emit":"/myhome/s_out/g_in7","scmd":"CLOSED","rcmd":"OPEN"},
"47":{"emit":"/myhome/s_out/g_in8","scmd":"CLOSED","rcmd":"OPEN"}
}
}

View File

@@ -0,0 +1,9 @@
{
"mqtt": ["test_esp","192.168.10.1",1883,"test","test"],
"items":{
"h_speed0": [6,9],
"h_speed1": [6,10]},
"in": {
"5": {"T":4,"emit": "/myhome/s_out/t_vent_street"}
}
}

View File

Before

Width:  |  Height:  |  Size: 178 KiB

After

Width:  |  Height:  |  Size: 178 KiB

View File

@@ -29,8 +29,10 @@ e-mail anklimov@gmail.com
#endif
#if defined(__ESP__)
#ifndef DMX_DISABLE
DMXESPSerial dmxout;
#endif
#endif
uint8_t * DMXin = NULL;
int D_State=0;
@@ -137,7 +139,7 @@ for (short tch=0; tch<=3 ; tch++)
{
D_State |= (1<<tch);
updated=1;
//Serial.print("Changed :"); Serial.print(DMXin[tch*4+trh]); Serial.print(" => "); Serial.print(t);Serial.println();
//Serial.print("onContactChanged :"); Serial.print(DMXin[tch*4+trh]); Serial.print(" => "); Serial.print(t);Serial.println();
DMXin[base+trh]=t;
//DMXImmediateUpdate(tch,trh,t);
//break;

View File

@@ -1,54 +0,0 @@
#include "options.h"
#ifdef __ESP__
#include "esp.h"
ESP8266WiFiMulti wifiMulti;
WiFiClient ethClient;
char mqtt_password[16];
//default custom static IP
//char static_ip[16] = "10.0.1.56";
//char static_gw[16] = "10.0.1.1";
//char static_sn[16] = "255.255.255.0";
//flag for saving data
bool shouldSaveConfig = false;
//callback notifying us of the need to save config
void saveConfigCallback () {
Serial.println(F("Should save config"));
shouldSaveConfig = true;
}
void espSetup () {
Serial.println(F("Setting up Wifi"));
shouldSaveConfig = true;
//WiFiManager
WiFiManagerParameter custom_mqtt_password("", "mqtt password", mqtt_password, 16);
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.addParameter(&custom_mqtt_password);
wifiManager.setMinimumSignalQuality();
if (!wifiManager.autoConnect()) {
Serial.println(F("failed to connect and hit timeout"));
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println(F("connected...yeey :)"));
//read updated parameters
strcpy(mqtt_password, custom_mqtt_password.getValue());
}
#endif

View File

@@ -1,24 +0,0 @@
#include <ESP8266WiFi.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ESP8266WiFiMulti.h>
extern ESP8266WiFiMulti wifiMulti;
extern WiFiClient ethClient;
//WiFiManager wifiManager;
//define your default values here, if there are different values in config.json, they are overwritten.
//length should be max size + 1
extern char mqtt_password[16];
//default custom static IP
//char static_ip[16] = "10.0.1.56";
//char static_gw[16] = "10.0.1.1";
//char static_sn[16] = "255.255.255.0";
//flag for saving data
extern bool shouldSaveConfig;
void espSetup ();

View File

@@ -19,11 +19,15 @@ e-mail anklimov@gmail.com
*/
#include "inputs.h"
#include "aJSON.h"
#include "item.h"
#include <PubSubClient.h>
#ifndef DHT_DISABLE
#include "DHT.h"
#endif
extern PubSubClient mqttClient;
//DHT dht();
Input::Input(char * name) //Constructor
{
@@ -60,57 +64,109 @@ void Input::Parse()
inType = 0;
pin = 0;
if (inputObj && (inputObj->type==aJson_Object))
{
aJsonObject * s;
if (inputObj && (inputObj->type == aJson_Object)) {
aJsonObject *s;
s = aJson.getObjectItem(inputObj,"T");
s = aJson.getObjectItem(inputObj, "T");
if (s) inType = s->valueint;
pin = atoi(inputObj->name);
s = aJson.getObjectItem(inputObj,"S");
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");
s = aJson.getObjectItem(inputObj, "S");
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");
}
if (s) store= (inStore *) &s->valueint;
if (s) store = (inStore *) &s->valueint;
}
}
int Input::Poll()
{
boolean v;
int Input::poll() {
if (!isValid()) return -1;
if (inType & IN_ACTIVE_HIGH)
{ pinMode(pin, INPUT);
v = (digitalRead(pin)==HIGH);
}
else
{ pinMode(pin, INPUT_PULLUP);
v = (digitalRead(pin)==LOW);
}
if (v!=store->cur) // value changed
{
if (store->bounce) store->bounce--;
else //confirmed change
{
Changed(v);
store->cur=v;
}
}
else // no change
store->bounce=3;
if (inType & IN_PUSH_ON)
contactPoll();
else if (inType & IN_DHT22)
dht22Poll();
return 0;
}
void Input::Changed (int val)
void Input::dht22Poll() {
#ifndef DHT_DISABLE
if (store->nextPollMillis > millis())
return;
DHT dht(pin, DHT22);
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
aJsonObject *emit = aJson.getObjectItem(inputObj, "emit");
Serial.print(F("IN:"));Serial.print(pin);Serial.print(F(" DHT22 type. T="));Serial.print(temp);
Serial.print(F("°C H="));Serial.print(humidity);Serial.print(F("%"));
if (emit && temp && humidity && temp == temp && humidity == humidity) {
char valstr[10];
char addrstr[100] = "";
strcat(addrstr, emit->valuestring);
strcat(addrstr, "T");
printFloatValueToStr(temp, valstr);
mqttClient.publish(addrstr, valstr);
addrstr[strlen(addrstr) - 1] = 'H';
printFloatValueToStr(humidity, valstr);
mqttClient.publish(addrstr, valstr);
store->nextPollMillis = millis() + DHT_POLL_DELAY_DEFAULT;
Serial.print(" NextPollMillis=");Serial.println(store->nextPollMillis);
}
else
store->nextPollMillis = millis() + DHT_POLL_DELAY_DEFAULT/3;
#endif
}
void Input::printFloatValueToStr(float temp, char *valstr) {
#if defined(__ESP__)
sprintf(valstr, "%2.1f", temp);
#endif
#if defined(__AVR__)
sprintf(valstr, "%d", (int)temp);
int fractional = 10.0*((float)abs(temp)-(float)abs((int)temp));
int val_len =strlen(valstr);
valstr[val_len]='.';
valstr[val_len+1]='0'+fractional;
valstr[val_len+2]='\0';
#endif
#if defined(__SAM3X8E__)
sprintf(valstr, "%2.1f", temp);
#endif
}
void Input::contactPoll() {
boolean currentInputState;
uint8_t inputPinMode, inputOnLevel;
if (inType & IN_ACTIVE_HIGH) {
inputOnLevel = HIGH;
inputPinMode = INPUT;
} else {
inputOnLevel = LOW;
inputPinMode = INPUT_PULLUP;
}
pinMode(pin, inputPinMode);
currentInputState = (digitalRead(pin) == inputOnLevel);
if (currentInputState != store->currentValue) // value changed
{
if (store->bounce) store->bounce = store->bounce - 1;
else //confirmed change
{
onContactChanged(currentInputState);
store->currentValue = currentInputState;
}
} else // no change
store->bounce = SAME_STATE_ATTEMPTS;
}
void Input::onContactChanged(int val)
{
Serial.print(pin);Serial.print(F("="));Serial.println(val);
Serial.print(F("IN:")); 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");
@@ -136,11 +192,11 @@ void Input::Changed (int val)
{
if (val)
{ //send set command
if (!scmd) it.Ctrl(CMD_ON,0,NULL,true); else if (strlen(scmd->valuestring)) it.Ctrl(txt2cmd(scmd->valuestring),0,NULL,true);
if (!scmd) it.Ctrl(CMD_ON,0,NULL,true); else if (strlen(scmd->valuestring)) it.Ctrl(scmd->valuestring,true);
}
else
{ //send reset command
if (!rcmd) it.Ctrl(CMD_OFF,0,NULL,true); else if (strlen(rcmd->valuestring)) it.Ctrl(txt2cmd(rcmd->valuestring),0,NULL,true);
if (!rcmd) it.Ctrl(CMD_OFF,0,NULL,true); else if (strlen(rcmd->valuestring)) it.Ctrl(rcmd->valuestring,true);
}
}
}

View File

@@ -18,15 +18,17 @@ e-mail anklimov@gmail.com
*/
#include "aJSON.h"
#include <aJSON.h>
#define IN_ACTIVE_HIGH 128 // High level = PUSHED/ CLOSED/ ON othervise :Low Level
#define IN_ACTIVE_HIGH 2 // High level = PUSHED/ CLOSED/ ON othervise :Low Level
#define IN_ANALOG 64 // Analog input
#define IN_RE 32 // Rotary Encoder (for further use)
#define IN_PUSH_ON 0 // PUSH - ON, Release - OFF (ovverrided by pcmd/rcmd) - DEFAULT
#define IN_PUSH_TOGGLE 1 // Every physicall push toggle logical switch on/off
#define IN_DHT22 4
#define SAME_STATE_ATTEMPTS 3
// in syntaxis
// "pin": { "T":"N", "emit":"out_emit", item:"out_item", "scmd": "ON,OFF,TOGGLE,INCREASE,DECREASE", "rcmd": "ON,OFF,TOGGLE,INCREASE,DECREASE", "rcmd":"repeat_command" }
@@ -57,19 +59,20 @@ e-mail anklimov@gmail.com
//"pin1": { "T":"0", "emit":"/light1", item:"light1", "scmd": "ON", repcmd:"INCREASE"}
//"pin2": { "T":"0", "emit":"/light1", item:"light1", "scmd": "OFF", repcmd:"INCREASE"}
extern aJsonObject *inputs;
typedef union
{
long int aslong;
struct
{
struct {
int8_t reserve;
int8_t logicState;
int8_t bounce;
int8_t cur;
int8_t currentValue;
};
unsigned long nextPollMillis;
} inStore;
class Input
@@ -85,13 +88,16 @@ class Input
Input(char * name);
boolean isValid ();
void Changed (int val);
void onContactChanged(int val);
int Poll();
int poll();
protected:
void Parse();
void contactPoll();
void dht22Poll();
void printFloatValueToStr(float temp, char *valstr);
};

View File

@@ -199,6 +199,53 @@ boolean Item::getEnableCMD(int delta) {
}
#define MAXCTRLPAR 3
int Item::Ctrl(char * payload, boolean send){
int cmd = txt2cmd(payload);
switch (cmd) {
case 0: {
short i = 0;
int Par[3];
while (payload && i < 3)
Par[i++] = getInt((char **) &payload);
Ctrl(0, i, Par, send);
}
break;
case -1: //Not known command
case -2: //JSON input (not implemented yet
break;
case -3: //RGB color in #RRGGBB notation
{
CRGB rgb;
if (sscanf((const char*)payload, "#%2X%2X%2X", &rgb.r, &rgb.g, &rgb.b) == 3) {
int Par[3];
CHSV hsv = rgb2hsv_approximate(rgb);
Par[0] = map(hsv.h, 0, 255, 0, 365);
Par[1] = map(hsv.s, 0, 255, 0, 100);
Par[2] = map(hsv.v, 0, 255, 0, 100);
Ctrl(0, 3, Par, send);
}
break;
}
case CMD_ON:
// if (item.getEnableCMD(500) || lanStatus == 4)
Ctrl(cmd, 0, NULL,
send); //Accept ON command not earlier then 500 ms after set settings (Homekit hack)
// else Serial.println(F("on Skipped"));
break;
default: //some known command
Ctrl(cmd, 0, NULL, send);
} //ctrl
}
int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) {
@@ -286,6 +333,20 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) {
setCmd(cmd);
//retrive stored values
st.aslong = getVal();
// If command is ON but saved volume to low - setup mimimum volume
switch (itemType) {
case CH_DIMMER:
case CH_MODBUS:
if (st.aslong<MIN_VOLUME) st.aslong=INIT_VOLUME;
setVal(st.aslong);
break;
case CH_RGB:
case CH_RGBW:
if (st.aslong && (st.v<MIN_VOLUME)) st.v=INIT_VOLUME;
setVal(st.aslong);
}
if (st.aslong > 0) //Stored smthng
switch (itemType) {
@@ -295,8 +356,6 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) {
Par[0] = st.h;
Par[1] = st.s;
Par[2] = st.v;
if (!Par[2]) Par[2]=80; //If RGB value==0 set to 80%
setVal(st.aslong);
params = 3;
SendStatus(0, params, Par,true); // Send restored triplet. In any cases
break;
@@ -306,8 +365,6 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) {
case CH_DIMMER: //Everywhere, in flat VAL
case CH_MODBUS:
case CH_VC:
Par[0] = st.aslong;
params = 1;
SendStatus(0, params, Par, true); // Send restored parameter, even if send=false - no problem, loop will be supressed at next hop
@@ -616,8 +673,8 @@ int Item::isActive() {
case CH_PWM:
val = st.aslong;
} //switch
Serial.print(F(":="));
Serial.println(val);
//Serial.print(F(":="));
//Serial.println(val);
if (val) return 1; else return 0;
}
@@ -684,7 +741,7 @@ OFF
POLL 2101x10
[22:27:29] <= Response: 0A 03 14 00 23 00 00 27 10 13 88 0B 9C 00 32 00 F8 00 F2 06 FA 01 3F AD D0
[22:27:29] => Poll: 0A 03 08 34 00 0A 87 18
[22:27:29] => poll: 0A 03 08 34 00 0A 87 18
*/
@@ -696,8 +753,11 @@ void Item::mb_fail(short addr, short op, int val, int cmd) {
setVal(val);
}
#ifndef MODBUS_DISABLE
extern ModbusMaster node;
int Item::VacomSetFan(int8_t val, int8_t cmd) {
int addr = getArg();
Serial.print(F("VC#"));
@@ -712,8 +772,8 @@ int Item::VacomSetFan(int8_t val, int8_t cmd) {
}
modbusBusy = 1;
uint8_t j, result;
uint16_t data[1];
uint8_t j;//, result;
//uint16_t data[1];
modbusSerial.begin(9600, fmPar);
node.begin(addr, modbusSerial);
@@ -766,63 +826,6 @@ int Item::VacomSetHeat(int addr, int8_t val, int8_t cmd) {
modbusBusy = 0;
}
int Item::SendStatus(short cmd, short n, int *Par, boolean deffered) {
/// ToDo: relative patches, configuration
if (deffered) {
setCmd(cmd | CMD_REPORT);
Serial.println(F("Status deffered"));
// mqttClient.publish("/push", "1");
return 0;
// Todo: Parameters? Now expected that parameters already stored by setVal()
}
else { //publush to MQTT
char addrstr[32];
//char addrbuf[17];
char valstr[16] = "";
strcpy_P(addrstr, outprefix);
strncat(addrstr, itemArr->name, sizeof(addrstr));
switch (cmd) {
case CMD_ON:
strcpy(valstr, "ON");
break;
case CMD_OFF:
case CMD_HALT:
strcpy(valstr, "OFF");
break;
// TODO send Par
case 0:
case CMD_SET:
if (Par)
for (short i = 0; i < n; i++) {
char num[4];
snprintf(num, sizeof(num), "%d", Par[i]);
strncat(valstr, num, sizeof(valstr));
if (i != n - 1) {
strcpy(num, ",");
strncat(valstr, num, sizeof(valstr));
}
}
break;
default:
Serial.println(F("Unknown cmd "));
return -1;
}
Serial.print(F("Pub: "));
Serial.print(addrstr);
Serial.print(F("->"));
Serial.println(valstr);
mqttClient.publish(addrstr, valstr,true);
return 0;
}
}
int Item::modbusDimmerSet(int addr, uint16_t _reg, int _mask, uint16_t value) {
if (modbusBusy) {
@@ -856,7 +859,6 @@ int Item::modbusDimmerSet(int addr, uint16_t _reg, int _mask, uint16_t value) {
modbusBusy = 0;
}
int Item::checkFM() {
if (modbusBusy) return -1;
if (checkModbusRetry()) return -2;
@@ -987,7 +989,7 @@ int Item::checkModbusDimmer() {
uint16_t addr = getArg(0);
uint16_t reg = getArg(1);
short mask = getArg(2);
// short mask = getArg(2);
int data;
@@ -1118,3 +1120,59 @@ void Item::sendDelayedStatus(){
setCmd(cmd);
}
}
#endif
int Item::SendStatus(short cmd, short n, int *Par, boolean deffered) {
/// ToDo: relative patches, configuration
if (deffered) {
setCmd(cmd | CMD_REPORT);
Serial.println(F("Status deffered"));
// mqttClient.publish("/push", "1");
return 0;
// Todo: Parameters? Now expected that parameters already stored by setVal()
}
else { //publush to MQTT
char addrstr[32];
//char addrbuf[17];
char valstr[16] = "";
strcpy_P(addrstr, outprefix);
strncat(addrstr, itemArr->name, sizeof(addrstr));
switch (cmd) {
case CMD_ON:
strcpy(valstr, "ON");
break;
case CMD_OFF:
case CMD_HALT:
strcpy(valstr, "OFF");
break;
// TODO send Par
case 0:
case CMD_SET:
if (Par)
for (short i = 0; i < n; i++) {
char num[4];
snprintf(num, sizeof(num), "%d", Par[i]);
strncat(valstr, num, sizeof(valstr));
if (i != n - 1) {
strcpy(num, ",");
strncat(valstr, num, sizeof(valstr));
}
}
break;
default:
Serial.println(F("Unknown cmd "));
return -1;
}
Serial.print(F("Pub: "));
Serial.print(addrstr);
Serial.print(F("->"));
Serial.println(valstr);
mqttClient.publish(addrstr, valstr,true);
return 0;
}
}

View File

@@ -88,6 +88,8 @@ class Item
Item(aJsonObject * obj);
boolean isValid ();
virtual int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true);
virtual int Ctrl(char * payload, boolean send=true);
int getArg(short n=0);
boolean getEnableCMD(int delta);
//int getVal(short n); //From VAL array. Negative if no array
@@ -125,7 +127,7 @@ class Item
class PooledItem : public Item
{
public:
virtual int Changed() = 0;
virtual int onContactChanged() = 0;
virtual void Idle ();
protected:
int PoolingInterval;

File diff suppressed because it is too large Load Diff

View File

@@ -10,6 +10,10 @@
#define wdt_dis()
#endif
#ifndef DHCP_RETRY_INTERVAL
#define DHCP_RETRY_INTERVAL 60000
#endif
#if defined(__AVR__)
#define wdt_en() wdt_enable(WDTO_8S)
#define wdt_dis() wdt_disable()
@@ -61,9 +65,15 @@
#if defined(__ESP__)
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include "esp.h"
#include <EEPROM.h>
#include <ArduinoHttpClient.h>
#include <ESP8266HTTPClient.h>
#ifndef WIFI_MANAGER_DISABLE
#include <WiFiManager.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#endif
#endif
#ifdef _owire
@@ -96,21 +106,33 @@
extern Artnet *artnet;
#endif
enum lan_status {
INITIAL_STATE = 0,
HAVE_IP_ADDRESS = 1,
IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER = 2,
OPERATION = 3,
RETAINING_COLLECTING = 4,
AWAITING_ADDRESS = -10,
RECONNECT = 12,
READ_RE_CONFIG = -11,
DO_NOTHING = -14
};
//void watchdogSetup(void);
void mqttCallback(char *topic, byte *payload, unsigned int length);
#ifndef __ESP__
//#ifndef __ESP__
void printIPAddress();
void printIPAddress(IPAddress ipAddress);
#endif
//#endif
void printMACAddress();
void restoreState();
int lanLoop();
lan_status lanLoop();
void Changed(int i, DeviceAddress addr, int val);
@@ -146,9 +168,9 @@ int loadFlash(short n, char *str, short l=32);
void saveFlash(short n, IPAddress& ip);
int loadFlash(short n, IPAddress& ip);
int ipLoadFromFlash(short n, IPAddress &ip);
int getConfig(int arg_cnt=0, char **args=NULL);
lan_status getConfig(int arg_cnt=0, char **args=NULL);
void preTransmission();
@@ -178,8 +200,16 @@ void setupCmdArduino();
void setupMacAddress();
int getConfig(int arg_cnt, char **args);
void printFirmwareVersionAndBuildOptions();
bool IsThermostat(const aJsonObject *item);
bool disabledDisconnected(const aJsonObject *thermoExtensionArray, int thermoLatestCommand);
void resetHard();
void onInitialStateInitLAN();
void ip_ready_config_loaded_connecting_to_broker();
#endif //LIGHTHUB_MAIN_H

View File

@@ -17,6 +17,8 @@
#define THERMO_OVERHEAT_CELSIUS 38
#define FM_OVERHEAT_CELSIUS 40.
#define MIN_VOLUME 10
#define INIT_VOLUME 30
#define OFFSET_MAC 0
#define OFFSET_IP OFFSET_MAC+6
@@ -32,6 +34,10 @@
#define INTERVAL_POLLING 100
#define THERMOSTAT_CHECK_PERIOD 5000
#ifndef OW_UPDATE_INTERVAL
#define OW_UPDATE_INTERVAL 5000
#endif
#ifndef MODBUS_SERIAL_BAUD
#define MODBUS_SERIAL_BAUD 9600
#endif
@@ -57,6 +63,10 @@
#define OUTTOPIC "/myhome/s_out/"
#endif
#ifndef CMDTOPIC
#define CMDTOPIC "/myhome/in/command/"
#endif
#ifndef INTOPIC
#define INTOPIC "/myhome/in/"
#endif
@@ -81,6 +91,10 @@
#define _artnet
#endif
#ifndef LAN_INIT_DELAY
#define LAN_INIT_DELAY 500
#endif
#if defined(ESP8266)
#define __ESP__
#endif
@@ -105,10 +119,24 @@
#if defined(__ESP__)
#undef _dmxin
#undef _modbus
#ifndef DMX_DISABLE
#define _espdmx
#endif
#define modbusSerial Serial1
#endif
#ifndef _dmxout
#undef _artnet
#endif
#ifdef WIFI_MANAGER_DISABLE
#ifndef ESP_WIFI_AP
#define ESP_WIFI_AP mywifiap
#endif
#ifndef ESP_WIFI_PWD
#define ESP_WIFI_PWD mywifipass
#endif
#endif
#define DHT_POLL_DELAY_DEFAULT 15000

View File

@@ -41,7 +41,7 @@ unsigned long owTimer = 0;
owChangedType owChanged;
int owUpdate() {
unsigned long finish = millis() + 5000;
unsigned long finish = millis() + OW_UPDATE_INTERVAL;
short sr;
//net.setStrongPullup();

32
my_build_flags.py Normal file
View File

@@ -0,0 +1,32 @@
# from time import time
#
# from SCons.Script import DefaultEnvironment
#
# print("==============================================Custom build flags are:=====================================================")
# #FLAGS="-MY_CONFIG_SERVER=192.168.10.110"
# #FLAGS+=" -WATCH_DOG_TICKER_DISABLE"
# #FLAGS+=" -USE_1W_PIN=12"
# #FLAGS+=" -SD_CARD_INSERTED"
# #FLAGS+=" -SERIAL_BAUD=115200"
# #FLAGS+=" -Wiz5500"
# #FLAGS+=" -DISABLE_FREERAM_PRINT"
# #FLAGS+=" -CUSTOM_FIRMWARE_MAC=C4:3E:1f:03:1B:1B"
# #FLAGS+=" -DMX_DISABLE"
# FLAGS="MODBUS_DISABLE"
# #FLAGS+=" -OWIRE_DISABLE"
# #FLAGS+=" -ARTNET_ENABLE"
# #FLAGS+=" -CONTROLLINO"
# #FLAGS+=" -AVR_DMXOUT_PIN=18"
#
# print(FLAGS)
# print("==============================================Custom build flags END=====================================================")
#
# env = DefaultEnvironment()
# env.Append(CPPDEFINES=['MODBUS_DISABLE=1'])
from time import time
from SCons.Script import DefaultEnvironment
env = DefaultEnvironment()
env.Append(CPPDEFINES=['BUILD_TIMESTAMP=%d' % time()])

View File

@@ -10,19 +10,23 @@
[platformio]
src_dir = lighthub
env_default =
; due-5500
; controllino
megaatmega2560-net
; megaatmega2560-5500
; due
; esp8266
; megaatmega2560-5500
; due-5500
; controllino
build_dir = /tmp/pioenvs
libdeps_dir = /tmp/piolibdeps
[env:due]
platform = atmelsam
framework = arduino
board = due
lib_ldf_mode = chain+
build_flags = !echo -n "-DPIO_SRC_REV="$(git rev-parse --short HEAD)
extra_scripts = pre:my_build_flags.py
build_flags = !echo -n "-DPIO_SRC_REV="$(git log --pretty=format:%h_%ad -1 --date=short)
lib_deps =
https://github.com/sebnil/DueFlashStorage
https://github.com/anklimov/Arduino-Temperature-Control-Library.git
@@ -33,12 +37,14 @@ lib_deps =
https://github.com/anklimov/CmdArduino
https://github.com/anklimov/ModbusMaster
https://github.com/anklimov/Ethernet
; https://github.com/PaulStoffregen/SPI.git
https://github.com/anklimov/Ethernet2
https://github.com/knolleary/pubsubclient.git
https://github.com/anklimov/Artnet.git
FastLED
161
322
SD
SdFat
Adafruit Unified Sensor
DHT sensor library
[env:megaatmega2560]
@@ -46,7 +52,7 @@ platform = atmelavr
board = megaatmega2560
framework = arduino
;lib_ldf_mode = chain+
build_flags = !echo -n "-DPIO_SRC_REV="$(git rev-parse --short HEAD)
build_flags = !echo -n "-DPIO_SRC_REV="$(git log --pretty=format:%h_%ad -1 --date=short)
lib_deps =
https://github.com/anklimov/Arduino-Temperature-Control-Library.git
https://github.com/anklimov/DS2482_OneWire
@@ -57,12 +63,38 @@ lib_deps =
https://github.com/anklimov/ModbusMaster
https://github.com/anklimov/DMXSerial
https://github.com/anklimov/Ethernet
https://github.com/anklimov/Ethernet2
https://github.com/PaulStoffregen/SPI.git
https://github.com/knolleary/pubsubclient.git
https://github.com/anklimov/Artnet.git
FastLED
; 161
; 322
EEPROM
Adafruit Unified Sensor
DHT sensor library
[env:esp8266]
platform = espressif8266
framework = arduino
board = nodemcuv2
lib_ldf_mode = chain+
build_flags = !echo -n "-DMODBUS_DISABLE -DPIO_SRC_REV="$(git log --pretty=format:%h_%ad -1 --date=short)
;extra_scripts = pre:my_build_flags.py
lib_deps =
https://github.com/anklimov/Arduino-Temperature-Control-Library.git
https://github.com/anklimov/DS2482_OneWire
https://github.com/anklimov/ESP-Dmx
ESP8266HTTPClient
https://github.com/anklimov/aJson
https://github.com/anklimov/CmdArduino
https://github.com/anklimov/ModbusMaster
https://github.com/knolleary/pubsubclient.git
https://github.com/anklimov/Artnet.git
FastLED
Adafruit Unified Sensor
DHT sensor library for ESPx
DHT sensor library
WifiManager
[env:megaatmega2560-net]
platform = atmelavr
@@ -86,13 +118,39 @@ lib_deps =
https://github.com/anklimov/Artnet.git
FastLED
[env:due-5500]
platform = atmelsam
framework = arduino
board = due
lib_ldf_mode = chain+
extra_scripts = pre:my_build_flags.py
build_flags = !echo -n "-DPIO_SRC_REV="$(git log --pretty=format:%h_%ad -1 --date=short)
build_flags = -D Wiz5500 -D ARTNET_ENABLE
lib_deps =
https://github.com/sebnil/DueFlashStorage
https://github.com/anklimov/Arduino-Temperature-Control-Library.git
https://github.com/anklimov/DS2482_OneWire
https://github.com/anklimov/DmxDue
https://github.com/anklimov/ArduinoHttpClient
https://github.com/anklimov/aJson
https://github.com/anklimov/CmdArduino
https://github.com/anklimov/ModbusMaster
https://github.com/anklimov/Ethernet
https://github.com/anklimov/Ethernet2
https://github.com/knolleary/pubsubclient.git
https://github.com/anklimov/Artnet.git
FastLED
SD
SdFat
Adafruit Unified Sensor
DHT sensor library
[env:controllino]
platform = atmelavr
board = megaatmega2560
framework = arduino
build_flags = !echo -n "-DPIO_SRC_REV="$(git rev-parse --short HEAD)
;lib_ldf_mode = chain+
build_flags = !echo -n "-DPIO_SRC_REV="$(git log --pretty=format:%h_%ad -1 --date=short)
build_flags = -D CONTROLLINO -D CUSTOM_FIRMWARE_MAC=de:ad:be:ef:fe:07
lib_deps =
https://github.com/anklimov/Arduino-Temperature-Control-Library.git
@@ -104,77 +162,11 @@ lib_deps =
https://github.com/anklimov/ModbusMaster
https://github.com/anklimov/DMXSerial
https://github.com/anklimov/Ethernet
https://github.com/PaulStoffregen/SPI.git
https://github.com/knolleary/pubsubclient.git
https://github.com/anklimov/Artnet.git
FastLED
; 161
; 322
[env:due-5500]
platform = atmelsam
framework = arduino
board = due
lib_ldf_mode = chain+
build_flags = !echo -n "-DPIO_SRC_REV="$(git rev-parse --short HEAD)
build_flags = -D Wiz5500 -D ARTNET_ENABLE
lib_deps =
https://github.com/sebnil/DueFlashStorage
https://github.com/anklimov/Arduino-Temperature-Control-Library.git
https://github.com/anklimov/DS2482_OneWire
https://github.com/anklimov/DmxDue
https://github.com/anklimov/ArduinoHttpClient
https://github.com/anklimov/aJson
https://github.com/anklimov/CmdArduino
https://github.com/anklimov/ModbusMaster
https://github.com/anklimov/Ethernet2
; https://github.com/PaulStoffregen/SPI.git
https://github.com/knolleary/pubsubclient.git
https://github.com/anklimov/Artnet.git
https://github.com/brokentoaster/Syslog
FastLED
; 161
; 322
[env:megaatmega2560-5500]
platform = atmelavr
board = megaatmega2560
framework = arduino
lib_ldf_mode = chain+
build_flags = !echo -n "-DPIO_SRC_REV="$(git rev-parse --short HEAD)
build_flags = -D Wiz5500 -D AVR_DMXOUT_PIN=18
lib_deps =
https://github.com/anklimov/Arduino-Temperature-Control-Library.git
https://github.com/anklimov/DS2482_OneWire
https://github.com/anklimov/DmxSimple
https://github.com/anklimov/httpClient
https://github.com/anklimov/aJson
https://github.com/anklimov/CmdArduino
https://github.com/anklimov/ModbusMaster
https://github.com/anklimov/DMXSerial
https://github.com/anklimov/Ethernet2
https://github.com/PaulStoffregen/SPI.git
https://github.com/knolleary/pubsubclient.git
https://github.com/anklimov/Artnet.git
FastLED
EEPROM
; 161
; 322
[env:esp8266]
platform = espressif8266
framework = arduino
board = nodemcuv2
lib_ldf_mode = chain+
lib_deps =
https://github.com/anklimov/Arduino-Temperature-Control-Library.git
https://github.com/anklimov/DS2482_OneWire
https://github.com/anklimov/ESP-Dmx
https://github.com/arduino-libraries/ArduinoHttpClient
https://github.com/anklimov/aJson
https://github.com/anklimov/CmdArduino
https://github.com/anklimov/ModbusMaster
https://github.com/anklimov/DMXSerial
https://github.com/knolleary/pubsubclient.git
https://github.com/anklimov/Artnet.git
FastLED
Adafruit Unified Sensor
DHT sensor library