mirror of
https://github.com/anklimov/lighthub
synced 2025-12-06 03:39:49 +03:00
from IS-STM32Duino
This commit is contained in:
8
.idea/markdown-exported-files.xml
generated
Normal file
8
.idea/markdown-exported-files.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="MarkdownExportedFiles">
|
||||
<htmlFiles />
|
||||
<imageFiles />
|
||||
<otherFiles />
|
||||
</component>
|
||||
</project>
|
||||
@@ -23,30 +23,35 @@ e-mail anklimov@gmail.com
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#ifndef DHT_DISABLE
|
||||
#if defined(ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||
#include <DHTesp.h>
|
||||
#else
|
||||
#include "DHT.h"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
extern PubSubClient mqttClient;
|
||||
//DHT dht();
|
||||
|
||||
static volatile unsigned long nextPollMillisValue[5];
|
||||
static volatile int nextPollMillisPin[5] = {0,0,0,0,0};
|
||||
|
||||
#if defined(__AVR__)
|
||||
static volatile long encoder_value[6];
|
||||
static volatile long counter_value[6];
|
||||
#endif
|
||||
|
||||
#if defined(ESP8266)
|
||||
static volatile long encoder_value[6];
|
||||
static volatile long counter_value[6];
|
||||
#endif
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
static volatile long encoder_value[6];
|
||||
static volatile long counter_value[6];
|
||||
#endif
|
||||
|
||||
#if defined(__SAM3X8E__) || defined(ARDUINO_ARCH_STM32F1)
|
||||
static short encoder_irq_map[54];
|
||||
static long encoder_value[54];
|
||||
static int encoders_count;
|
||||
static short counter_irq_map[54];
|
||||
static long counter_value[54];
|
||||
static int counters_count;
|
||||
#endif
|
||||
|
||||
Input::Input(char * name) //Constructor
|
||||
{
|
||||
if (name)
|
||||
@@ -57,13 +62,13 @@ Input::Input(char * name) //Constructor
|
||||
}
|
||||
|
||||
|
||||
Input::Input(int pin) //Constructor
|
||||
Input::Input(int pin)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
|
||||
Input::Input(aJsonObject * obj) //Constructor
|
||||
Input::Input(aJsonObject * obj)
|
||||
{
|
||||
inputObj= obj;
|
||||
Parse();
|
||||
@@ -86,9 +91,9 @@ void Input::Parse()
|
||||
aJsonObject *s;
|
||||
|
||||
s = aJson.getObjectItem(inputObj, "T");
|
||||
if (s) inType = s->valueint;
|
||||
if (s) inType = static_cast<uint8_t>(s->valueint);
|
||||
|
||||
pin = atoi(inputObj->name);
|
||||
pin = static_cast<uint8_t>(atoi(inputObj->name));
|
||||
|
||||
s = aJson.getObjectItem(inputObj, "S");
|
||||
if (!s) {
|
||||
@@ -108,18 +113,17 @@ int Input::poll() {
|
||||
if (!isValid()) return -1;
|
||||
if (inType & IN_DHT22)
|
||||
dht22Poll();
|
||||
else if(inType & IN_ENCODER)
|
||||
encoderPoll();
|
||||
/* example
|
||||
else if (inType & IN_ANALOG)
|
||||
analogPoll(); */
|
||||
else if (inType & IN_COUNTER)
|
||||
counterPoll();
|
||||
else if (inType & IN_UPTIME)
|
||||
uptimePoll();
|
||||
else
|
||||
contactPoll();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Input::encoderPoll() {
|
||||
if (store->nextPollMillis > millis())
|
||||
void Input::counterPoll() {
|
||||
if(nextPollTime()>millis())
|
||||
return;
|
||||
if (store->logicState == 0) {
|
||||
#if defined(__AVR__)
|
||||
@@ -131,31 +135,31 @@ void Input::encoderPoll() {
|
||||
} else {
|
||||
Serial.print(F("IRQ:"));
|
||||
Serial.print(pin);
|
||||
Serial.print(F(" Encoder type. INCORRECT Interrupt number!!!"));
|
||||
Serial.print(F(" Counter type. INCORRECT Interrupt number!!!"));
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__SAM3X8E__)
|
||||
attachInterruptPinIrq(pin,encoders_count);
|
||||
encoder_irq_map[encoders_count]=pin;
|
||||
encoders_count++;
|
||||
attachInterruptPinIrq(pin,counters_count);
|
||||
counter_irq_map[counters_count]=pin;
|
||||
counters_count++;
|
||||
#endif
|
||||
store->logicState = 1;
|
||||
return;
|
||||
}
|
||||
long encoderValue = encoder_value[pin];
|
||||
Serial.print(F("IN:"));Serial.print(pin);Serial.print(F(" Encoder type. val="));Serial.print(encoderValue);
|
||||
long counterValue = counter_value[pin];
|
||||
Serial.print(F("IN:"));Serial.print(pin);Serial.print(F(" Counter type. val="));Serial.print(counterValue);
|
||||
|
||||
aJsonObject *emit = aJson.getObjectItem(inputObj, "emit");
|
||||
if (emit) {
|
||||
char valstr[10];
|
||||
char addrstr[100] = "";
|
||||
strcat(addrstr, emit->valuestring);
|
||||
sprintf(valstr, "%d", encoderValue);
|
||||
sprintf(valstr, "%d", counterValue);
|
||||
mqttClient.publish(addrstr, valstr);
|
||||
store->nextPollMillis = millis() + DHT_POLL_DELAY_DEFAULT;
|
||||
Serial.print(F(" NextPollMillis="));Serial.println(store->nextPollMillis);
|
||||
setNextPollTime(millis() + DHT_POLL_DELAY_DEFAULT);
|
||||
Serial.print(F(" NextPollMillis="));Serial.println(nextPollTime());
|
||||
}
|
||||
else
|
||||
Serial.print(F(" No emit data!"));
|
||||
@@ -172,22 +176,22 @@ void Input::attachInterruptPinIrq(int realPin, int irq) {
|
||||
#endif
|
||||
switch(irq){
|
||||
case 0:
|
||||
attachInterrupt(real_irq, onEncoderChanged0, RISING);
|
||||
attachInterrupt(real_irq, onCounterChanged0, RISING);
|
||||
break;
|
||||
case 1:
|
||||
attachInterrupt(real_irq, onEncoderChanged1, RISING);
|
||||
attachInterrupt(real_irq, onCounterChanged1, RISING);
|
||||
break;
|
||||
case 2:
|
||||
attachInterrupt(real_irq, onEncoderChanged2, RISING);
|
||||
attachInterrupt(real_irq, onCounterChanged2, RISING);
|
||||
break;
|
||||
case 3:
|
||||
attachInterrupt(real_irq, onEncoderChanged3, RISING);
|
||||
attachInterrupt(real_irq, onCounterChanged3, RISING);
|
||||
break;
|
||||
case 4:
|
||||
attachInterrupt(real_irq, onEncoderChanged4, RISING);
|
||||
attachInterrupt(real_irq, onCounterChanged4, RISING);
|
||||
break;
|
||||
case 5:
|
||||
attachInterrupt(real_irq, onEncoderChanged5, RISING);
|
||||
attachInterrupt(real_irq, onCounterChanged5, RISING);
|
||||
break;
|
||||
default:
|
||||
Serial.print(F("Incorrect irq:"));Serial.println(irq);
|
||||
@@ -197,14 +201,27 @@ void Input::attachInterruptPinIrq(int realPin, int irq) {
|
||||
|
||||
void Input::dht22Poll() {
|
||||
#ifndef DHT_DISABLE
|
||||
if (store->nextPollMillis > millis())
|
||||
if(nextPollTime()>millis())
|
||||
return;
|
||||
#if defined(ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||
DHTesp dhtSensor;
|
||||
dhtSensor.setup(pin, DHTesp::DHT22);
|
||||
TempAndHumidity dhtSensorData = dhtSensor.getTempAndHumidity();
|
||||
float temp = dhtSensorData.temperature;
|
||||
float humidity = dhtSensorData.humidity;
|
||||
#else
|
||||
DHT dht(pin, DHT22);
|
||||
float temp = dht.readTemperature();
|
||||
float humidity = dht.readHumidity();
|
||||
#endif
|
||||
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("%"));
|
||||
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] = "";
|
||||
@@ -215,28 +232,53 @@ void Input::dht22Poll() {
|
||||
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;
|
||||
setNextPollTime(millis() + DHT_POLL_DELAY_DEFAULT);
|
||||
Serial.print(" NextPollMillis=");
|
||||
Serial.println(nextPollTime());
|
||||
} else
|
||||
setNextPollTime(millis() + DHT_POLL_DELAY_DEFAULT / 3);
|
||||
#endif
|
||||
}
|
||||
|
||||
void Input::printFloatValueToStr(float temp, char *valstr) {
|
||||
#if defined(ESP8266)
|
||||
sprintf(valstr, "%2.1f", temp);
|
||||
unsigned long Input::nextPollTime() const {
|
||||
for(int i=0;i<5;i++){
|
||||
if(nextPollMillisPin[i]==pin)
|
||||
return nextPollMillisValue[i];
|
||||
else if(nextPollMillisPin[i]==0) {
|
||||
nextPollMillisPin[i]=pin;
|
||||
return nextPollMillisValue[i] = 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Input::setNextPollTime(unsigned long pollTime) {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (nextPollMillisPin[i] == pin) {
|
||||
nextPollMillisValue[i] = pollTime;
|
||||
return;
|
||||
} else if (nextPollMillisPin[i] == 0) {
|
||||
nextPollMillisPin[i] == pin;
|
||||
nextPollMillisValue[i] = pollTime;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Input::printFloatValueToStr(float value, char *valstr) {
|
||||
#if defined(ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||
sprintf(valstr, "%2.1f", value);
|
||||
#endif
|
||||
#if defined(__AVR__)
|
||||
sprintf(valstr, "%d", (int)temp);
|
||||
int fractional = 10.0*((float)abs(temp)-(float)abs((int)temp));
|
||||
sprintf(valstr, "%d", (int)value);
|
||||
int fractional = 10.0*((float)abs(value)-(float)abs((int)value));
|
||||
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);
|
||||
sprintf(valstr, "%2.1f",value);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -245,7 +287,7 @@ void Input::contactPoll() {
|
||||
#if defined(ARDUINO_ARCH_STM32F1)
|
||||
WiringPinMode inputPinMode;
|
||||
#endif
|
||||
#if defined(__SAM3X8E__)||defined(__AVR__)||defined(ESP8266)
|
||||
#if defined(__SAM3X8E__)||defined(__AVR__)||defined(ESP8266)||defined(ARDUINO_ARCH_ESP32)
|
||||
uint32_t inputPinMode;
|
||||
#endif
|
||||
|
||||
@@ -271,6 +313,19 @@ void Input::contactPoll() {
|
||||
store->bounce = SAME_STATE_ATTEMPTS;
|
||||
}
|
||||
|
||||
void Input::uptimePoll() {
|
||||
if(nextPollTime()>millis())
|
||||
return;
|
||||
aJsonObject *emit = aJson.getObjectItem(inputObj, "emit");
|
||||
if (emit) {
|
||||
char valstr[11];
|
||||
// printUlongValueToStr(valstr,millis());
|
||||
printUlongValueToStr(valstr,millis());
|
||||
mqttClient.publish(emit->valuestring, valstr);
|
||||
}
|
||||
setNextPollTime(millis() +UPTIME_POLL_DELAY_DEFAULT);
|
||||
}
|
||||
|
||||
void Input::onContactChanged(int val)
|
||||
{
|
||||
Serial.print(F("IN:")); Serial.print(pin);Serial.print(F("="));Serial.println(val);
|
||||
@@ -309,32 +364,47 @@ void Input::onContactChanged(int val)
|
||||
}
|
||||
}
|
||||
|
||||
void Input::onEncoderChanged(int i) {
|
||||
void Input::onCounterChanged(int i) {
|
||||
#if defined(__SAM3X8E__)
|
||||
encoder_value[encoder_irq_map[i]]++;
|
||||
counter_value[counter_irq_map[i]]++;
|
||||
#endif
|
||||
|
||||
#if defined(__AVR__)
|
||||
encoder_value[i]++;
|
||||
counter_value[i]++;
|
||||
#endif
|
||||
}
|
||||
|
||||
void Input::onEncoderChanged0() {
|
||||
onEncoderChanged(0);
|
||||
void Input::onCounterChanged0() {
|
||||
onCounterChanged(0);
|
||||
}
|
||||
void Input::onEncoderChanged1() {
|
||||
onEncoderChanged(1);
|
||||
void Input::onCounterChanged1() {
|
||||
onCounterChanged(1);
|
||||
}
|
||||
void Input::onEncoderChanged2() {
|
||||
onEncoderChanged(2);
|
||||
void Input::onCounterChanged2() {
|
||||
onCounterChanged(2);
|
||||
}
|
||||
void Input::onEncoderChanged3() {
|
||||
onEncoderChanged(3);
|
||||
void Input::onCounterChanged3() {
|
||||
onCounterChanged(3);
|
||||
}
|
||||
void Input::onEncoderChanged4() {
|
||||
onEncoderChanged(4);
|
||||
void Input::onCounterChanged4() {
|
||||
onCounterChanged(4);
|
||||
}
|
||||
void Input::onEncoderChanged5() {
|
||||
onEncoderChanged(5);
|
||||
void Input::onCounterChanged5() {
|
||||
onCounterChanged(5);
|
||||
}
|
||||
|
||||
void Input::printUlongValueToStr(char *valstr, unsigned long value) {
|
||||
char buf[11];
|
||||
int i=0;
|
||||
for(;value>0;i++){
|
||||
unsigned long mod = value - ((unsigned long)(value/10))*10;
|
||||
buf[i]=mod+48;
|
||||
value = (unsigned long)(value/10);
|
||||
}
|
||||
|
||||
for(int n=0;n<=i;n++){
|
||||
valstr[n]=buf[i-n-1];
|
||||
}
|
||||
valstr[i]='\0';
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,8 @@ e-mail anklimov@gmail.com
|
||||
#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 IN_ENCODER 8
|
||||
#define IN_COUNTER 8
|
||||
#define IN_UPTIME 16
|
||||
|
||||
#define SAME_STATE_ATTEMPTS 3
|
||||
|
||||
@@ -72,7 +73,7 @@ typedef union {
|
||||
int8_t bounce;
|
||||
int8_t currentValue;
|
||||
};
|
||||
unsigned long nextPollMillis;
|
||||
|
||||
} inStore;
|
||||
|
||||
class Input {
|
||||
@@ -94,13 +95,13 @@ public:
|
||||
|
||||
int poll();
|
||||
|
||||
static void inline onEncoderChanged(int i);
|
||||
static void onEncoderChanged0();
|
||||
static void onEncoderChanged1();
|
||||
static void onEncoderChanged2();
|
||||
static void onEncoderChanged3();
|
||||
static void onEncoderChanged4();
|
||||
static void onEncoderChanged5();
|
||||
static void inline onCounterChanged(int i);
|
||||
static void onCounterChanged0();
|
||||
static void onCounterChanged1();
|
||||
static void onCounterChanged2();
|
||||
static void onCounterChanged3();
|
||||
static void onCounterChanged4();
|
||||
static void onCounterChanged5();
|
||||
|
||||
|
||||
|
||||
@@ -111,10 +112,17 @@ protected:
|
||||
|
||||
void dht22Poll();
|
||||
|
||||
void printFloatValueToStr(float temp, char *valstr);
|
||||
void printFloatValueToStr(float value, char *valstr);
|
||||
|
||||
void encoderPoll();
|
||||
void counterPoll();
|
||||
|
||||
void attachInterruptPinIrq(int realPin, int irq);
|
||||
|
||||
unsigned long nextPollTime() const;
|
||||
void setNextPollTime(unsigned long pollTime);
|
||||
|
||||
|
||||
void uptimePoll();
|
||||
|
||||
void printUlongValueToStr(char *valstr, unsigned long value);
|
||||
};
|
||||
|
||||
@@ -88,6 +88,7 @@ WiFiClient ethClient;
|
||||
#include <WiFi.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <EEPROM.h>
|
||||
#include "Ethernet3.h"
|
||||
WiFiClient ethClient;
|
||||
#endif
|
||||
|
||||
@@ -105,6 +106,14 @@ WiFiClient ethClient;
|
||||
EthernetClient ethClient;
|
||||
#endif
|
||||
|
||||
#ifndef SYSLOG_DISABLE
|
||||
#include <EthernetUdp.h>
|
||||
#include <Syslog.h>
|
||||
EthernetUDP udpSyslogClient;
|
||||
Syslog udpSyslog(udpSyslogClient, SYSLOG_PROTO_IETF);
|
||||
unsigned long nextSyslogPingTime;
|
||||
#endif
|
||||
|
||||
lan_status lanStatus = INITIAL_STATE;
|
||||
|
||||
const char outprefix[] PROGMEM = OUTTOPIC;
|
||||
@@ -120,6 +129,7 @@ aJsonObject *mqttArr = NULL;
|
||||
aJsonObject *modbusArr = NULL;
|
||||
aJsonObject *owArr = NULL;
|
||||
aJsonObject *dmxArr = NULL;
|
||||
aJsonObject *udpSyslogArr = NULL;
|
||||
|
||||
unsigned long nextPollingCheck = 0;
|
||||
unsigned long nextInputCheck = 0;
|
||||
@@ -158,7 +168,7 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
|
||||
debugSerial.print(topic);
|
||||
debugSerial.print(F("] "));
|
||||
if (!payload) return;
|
||||
payload[length] = 0;
|
||||
payload[length] = 0;
|
||||
|
||||
int fr = freeRam();
|
||||
if (fr < 250) {
|
||||
@@ -172,8 +182,8 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
|
||||
debugSerial.println();
|
||||
|
||||
if(!strcmp(topic,CMDTOPIC)) {
|
||||
cmd_parse((char *)payload);
|
||||
return;
|
||||
cmd_parse((char *)payload);
|
||||
return;
|
||||
}
|
||||
|
||||
boolean retaining = (lanStatus == RETAINING_COLLECTING);
|
||||
@@ -191,17 +201,17 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
|
||||
return;
|
||||
}
|
||||
char subtopic[MQTT_SUBJECT_LENGTH] = "";
|
||||
// int cmd = 0;
|
||||
// int cmd = 0;
|
||||
//cmd = txt2cmd((char *) payload);
|
||||
char *t;
|
||||
if (t = strrchr(topic, '/'))
|
||||
strncpy(subtopic, t + 1, MQTT_SUBJECT_LENGTH - 1);
|
||||
Item item(subtopic);
|
||||
if (item.isValid()) {
|
||||
if (item.itemType == CH_GROUP && retaining)
|
||||
return; //Do not restore group channels - they consist not relevant data
|
||||
Item item(subtopic);
|
||||
if (item.isValid()) {
|
||||
if (item.itemType == CH_GROUP && retaining)
|
||||
return; //Do not restore group channels - they consist not relevant data
|
||||
item.Ctrl((char *)payload, !retaining);
|
||||
} //valid item
|
||||
} //valid item
|
||||
}
|
||||
|
||||
void printIPAddress(IPAddress ipAddress) {
|
||||
@@ -230,7 +240,7 @@ lan_status lanLoop() {
|
||||
|
||||
#ifdef NOETHER
|
||||
lanStatus=DO_NOTHING;//-14;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
switch (lanStatus) {
|
||||
case INITIAL_STATE:
|
||||
@@ -347,60 +357,77 @@ void ip_ready_config_loaded_connecting_to_broker() {
|
||||
char *user = ∅
|
||||
char passwordBuf[16] = "";
|
||||
char *password = passwordBuf;
|
||||
|
||||
#ifndef SYSLOG_DISABLE
|
||||
debugSerial.println("debugSerial:");
|
||||
delay(100);
|
||||
char *syslogServer = aJson.getArrayItem(udpSyslogArr, 0)->valuestring;
|
||||
int syslogPort = aJson.getArrayItem(udpSyslogArr, 1)->valueint;
|
||||
char *syslogDeviceHostname = aJson.getArrayItem(udpSyslogArr, 2)->valuestring;
|
||||
char *syslogAppname = aJson.getArrayItem(udpSyslogArr, 3)->valuestring;
|
||||
debugSerial.println("debugSerial:");
|
||||
debugSerial.println(syslogServer);
|
||||
debugSerial.println(syslogPort);
|
||||
debugSerial.println(syslogDeviceHostname);
|
||||
debugSerial.println(syslogAppname);
|
||||
udpSyslog.server(syslogServer, syslogPort);
|
||||
udpSyslog.deviceHostname(syslogDeviceHostname);
|
||||
udpSyslog.appName(syslogAppname);
|
||||
udpSyslog.defaultPriority(LOG_KERN);
|
||||
udpSyslog.log(LOG_INFO, "UDP Syslog initialized!");
|
||||
#endif
|
||||
|
||||
if (!mqttClient.connected() && mqttArr && ((n = aJson.getArraySize(mqttArr)) > 1)) {
|
||||
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;
|
||||
if (!loadFlash(OFFSET_MQTT_PWD, passwordBuf, sizeof(passwordBuf)) && (n >= 5)) {
|
||||
password = aJson.getArrayItem(mqttArr, 4)->valuestring;
|
||||
debugSerial.println(F("Using MQTT password from config"));
|
||||
}
|
||||
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;
|
||||
if (!loadFlash(OFFSET_MQTT_PWD, passwordBuf, sizeof(passwordBuf)) && (n >= 5)) {
|
||||
password = aJson.getArrayItem(mqttArr, 4)->valuestring;
|
||||
debugSerial.println(F("Using MQTT password from config"));
|
||||
}
|
||||
|
||||
mqttClient.setServer(servername, port);
|
||||
mqttClient.setCallback(mqttCallback);
|
||||
mqttClient.setServer(servername, port);
|
||||
mqttClient.setCallback(mqttCallback);
|
||||
|
||||
debugSerial.print(F("Attempting MQTT connection to "));
|
||||
debugSerial.print(servername);
|
||||
debugSerial.print(F(":"));
|
||||
debugSerial.print(port);
|
||||
debugSerial.print(F(" user:"));
|
||||
debugSerial.print(user);
|
||||
debugSerial.print(F(" ..."));
|
||||
debugSerial.print(F("Attempting MQTT connection to "));
|
||||
debugSerial.print(servername);
|
||||
debugSerial.print(F(":"));
|
||||
debugSerial.print(port);
|
||||
debugSerial.print(F(" user:"));
|
||||
debugSerial.print(user);
|
||||
debugSerial.print(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)) {
|
||||
mqttErrorRate = 0;
|
||||
debugSerial.print(F("connected as "));
|
||||
debugSerial.println(client_id);
|
||||
wdt_en();
|
||||
configOk = true;
|
||||
// ... Temporary subscribe to status topic
|
||||
char buf[MQTT_TOPIC_LENGTH];
|
||||
wdt_dis(); //potential unsafe for ethernetIdle(), but needed to avoid cyclic reboot if mosquitto out of order
|
||||
if (mqttClient.connect(client_id, user, password)) {
|
||||
mqttErrorRate = 0;
|
||||
debugSerial.print(F("connected as "));
|
||||
debugSerial.println(client_id);
|
||||
wdt_en();
|
||||
configOk = true;
|
||||
// ... Temporary subscribe to status topic
|
||||
char buf[MQTT_TOPIC_LENGTH];
|
||||
|
||||
strncpy_P(buf, outprefix, sizeof(buf));
|
||||
strncat(buf, "#", sizeof(buf));
|
||||
mqttClient.subscribe(buf);
|
||||
strncpy_P(buf, outprefix, sizeof(buf));
|
||||
strncat(buf, "#", sizeof(buf));
|
||||
mqttClient.subscribe(buf);
|
||||
|
||||
//Subscribing for command topics
|
||||
strncpy_P(buf, inprefix, sizeof(buf));
|
||||
strncat(buf, "#", sizeof(buf));
|
||||
mqttClient.subscribe(buf);
|
||||
//Subscribing for command topics
|
||||
strncpy_P(buf, inprefix, sizeof(buf));
|
||||
strncat(buf, "#", sizeof(buf));
|
||||
mqttClient.subscribe(buf);
|
||||
|
||||
//restoreState();
|
||||
// if (_once) {DMXput(); _once=0;}
|
||||
lanStatus = RETAINING_COLLECTING;//4;
|
||||
nextLanCheckTime = millis() + 5000;
|
||||
debugSerial.println(F("Awaiting for retained topics"));
|
||||
} else {
|
||||
debugSerial.print(F("failed, rc="));
|
||||
debugSerial.print(mqttClient.state());
|
||||
debugSerial.println(F(" try again in 5 seconds"));
|
||||
nextLanCheckTime = millis() + 5000;
|
||||
//restoreState();
|
||||
// if (_once) {DMXput(); _once=0;}
|
||||
lanStatus = RETAINING_COLLECTING;//4;
|
||||
nextLanCheckTime = millis() + 5000;
|
||||
debugSerial.println(F("Awaiting for retained topics"));
|
||||
} else {
|
||||
debugSerial.print(F("failed, rc="));
|
||||
debugSerial.print(mqttClient.state());
|
||||
debugSerial.println(F(" try again in 5 seconds"));
|
||||
nextLanCheckTime = millis() + 5000;
|
||||
#ifdef RESTART_LAN_ON_MQTT_ERRORS
|
||||
mqttErrorRate++;
|
||||
mqttErrorRate++;
|
||||
if(mqttErrorRate>50){
|
||||
debugSerial.print(F("Too many MQTT connection errors. Restart LAN"));
|
||||
mqttErrorRate=0;
|
||||
@@ -412,9 +439,9 @@ void ip_ready_config_loaded_connecting_to_broker() {
|
||||
}
|
||||
#endif
|
||||
|
||||
lanStatus = RECONNECT;//12;
|
||||
}
|
||||
}
|
||||
lanStatus = RECONNECT;//12;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void onInitialStateInitLAN() {
|
||||
@@ -484,7 +511,7 @@ void onInitialStateInitLAN() {
|
||||
} else Ethernet.begin(mac, ip, dns);
|
||||
} else Ethernet.begin(mac, ip);
|
||||
}
|
||||
else {
|
||||
else {
|
||||
debugSerial.println("No IP data found in flash");
|
||||
wdt_dis();
|
||||
#if defined(__AVR__) || defined(__SAM3X8E__)
|
||||
@@ -510,7 +537,7 @@ void onInitialStateInitLAN() {
|
||||
printIPAddress(Ethernet.localIP());
|
||||
lanStatus = HAVE_IP_ADDRESS;//1;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef ARDUINO_ARCH_STM32F1
|
||||
@@ -587,17 +614,20 @@ void cmdFunctionHelp(int arg_cnt, char **args)
|
||||
//(char* tokens)
|
||||
{
|
||||
printFirmwareVersionAndBuildOptions();
|
||||
#ifndef SYSLOG_DISABLE
|
||||
// udpSyslog.logf(LOG_INFO, "free RAM: %d",freeRam());
|
||||
#endif
|
||||
debugSerial.print(F(" free RAM: "));debugSerial.print(freeRam());
|
||||
debugSerial.println(F(" Use the commands: 'help' - this text\n"
|
||||
"'mac de:ad:be:ef:fe:00' set and store MAC-address in EEPROM\n"
|
||||
"'ip [ip[,dns[,gw[,subnet]]]]' - set static IP\n"
|
||||
"'save' - save config in NVRAM\n"
|
||||
"'get' [config addr]' - get config from pre-configured URL and store addr\n"
|
||||
"'load' - load config from NVRAM\n"
|
||||
"'pwd' - define MQTT password\n"
|
||||
"'kill' - test watchdog\n"
|
||||
"'clear' - clear EEPROM\n"
|
||||
"'reboot' - reboot controller"));
|
||||
"'mac de:ad:be:ef:fe:00' set and store MAC-address in EEPROM\n"
|
||||
"'ip [ip[,dns[,gw[,subnet]]]]' - set static IP\n"
|
||||
"'save' - save config in NVRAM\n"
|
||||
"'get' [config addr]' - get config from pre-configured URL and store addr\n"
|
||||
"'load' - load config from NVRAM\n"
|
||||
"'pwd' - define MQTT password\n"
|
||||
"'kill' - test watchdog\n"
|
||||
"'clear' - clear EEPROM\n"
|
||||
"'reboot' - reboot controller"));
|
||||
}
|
||||
|
||||
void cmdFunctionKill(int arg_cnt, char **args) {
|
||||
@@ -613,7 +643,7 @@ void cmdFunctionReboot(int arg_cnt, char **args) {
|
||||
}
|
||||
|
||||
void applyConfig() {
|
||||
if (!root) return;
|
||||
if (!root) return;
|
||||
|
||||
#ifdef _dmxin
|
||||
int itemsCount;
|
||||
@@ -659,37 +689,38 @@ void applyConfig() {
|
||||
items = aJson.getObjectItem(root, "items");
|
||||
|
||||
// Digital output related Items initialization
|
||||
pollingItem=NULL;
|
||||
if (items) {
|
||||
aJsonObject * item = items->child;
|
||||
while (items && item)
|
||||
if (item->type == aJson_Array && aJson.getArraySize(item)>1) {
|
||||
Item it(item);
|
||||
if (it.isValid()) {
|
||||
int pin=it.getArg();
|
||||
int cmd = it.getCmd();
|
||||
switch (it.itemType) {
|
||||
case CH_THERMO:
|
||||
if (cmd<1) it.setCmd(CMD_OFF);
|
||||
case CH_RELAY:
|
||||
{
|
||||
int k;
|
||||
pinMode(pin, OUTPUT);
|
||||
digitalWrite(pin, k = ((cmd == CMD_ON) ? HIGH : LOW));
|
||||
debugSerial.print(F("Pin:"));
|
||||
debugSerial.print(pin);
|
||||
debugSerial.print(F("="));
|
||||
debugSerial.println(k);
|
||||
}
|
||||
break;
|
||||
} //switch
|
||||
} //isValid
|
||||
item = item->next;
|
||||
} //if
|
||||
pollingItem = items->child;
|
||||
}
|
||||
pollingItem=NULL;
|
||||
if (items) {
|
||||
aJsonObject * item = items->child;
|
||||
while (items && item)
|
||||
if (item->type == aJson_Array && aJson.getArraySize(item)>1) {
|
||||
Item it(item);
|
||||
if (it.isValid()) {
|
||||
int pin=it.getArg();
|
||||
int cmd = it.getCmd();
|
||||
switch (it.itemType) {
|
||||
case CH_THERMO:
|
||||
if (cmd<1) it.setCmd(CMD_OFF);
|
||||
case CH_RELAY:
|
||||
{
|
||||
int k;
|
||||
pinMode(pin, OUTPUT);
|
||||
digitalWrite(pin, k = ((cmd == CMD_ON) ? HIGH : LOW));
|
||||
debugSerial.print(F("Pin:"));
|
||||
debugSerial.print(pin);
|
||||
debugSerial.print(F("="));
|
||||
debugSerial.println(k);
|
||||
}
|
||||
break;
|
||||
} //switch
|
||||
} //isValid
|
||||
item = item->next;
|
||||
} //if
|
||||
pollingItem = items->child;
|
||||
}
|
||||
inputs = aJson.getObjectItem(root, "in");
|
||||
mqttArr = aJson.getObjectItem(root, "mqtt");
|
||||
udpSyslogArr = aJson.getObjectItem(root, "syslog");
|
||||
printConfigSummary();
|
||||
}
|
||||
|
||||
@@ -705,6 +736,8 @@ void printConfigSummary() {
|
||||
printBool(mqttArr);
|
||||
debugSerial.print(F("1-wire "));
|
||||
printBool(owArr);
|
||||
debugSerial.print(F("udp syslog "));
|
||||
printBool(udpSyslogArr);
|
||||
}
|
||||
|
||||
void cmdFunctionLoad(int arg_cnt, char **args) {
|
||||
@@ -843,11 +876,11 @@ void cmdFunctionClearEEPROM(int arg_cnt, char **args){
|
||||
void cmdFunctionPwd(int arg_cnt, char **args)
|
||||
//(char* tokens)
|
||||
{ char empty[]="";
|
||||
if (arg_cnt)
|
||||
saveFlash(OFFSET_MQTT_PWD,args[1]);
|
||||
else saveFlash(OFFSET_MQTT_PWD,empty);
|
||||
debugSerial.println(F("Password updated"));
|
||||
}
|
||||
if (arg_cnt)
|
||||
saveFlash(OFFSET_MQTT_PWD,args[1]);
|
||||
else saveFlash(OFFSET_MQTT_PWD,empty);
|
||||
debugSerial.println(F("Password updated"));
|
||||
}
|
||||
|
||||
void cmdFunctionSetMac(int arg_cnt, char **args) {
|
||||
|
||||
@@ -879,30 +912,30 @@ void printBool(bool arg) { (arg) ? debugSerial.println(F("on")) : debugSerial.pr
|
||||
|
||||
|
||||
void saveFlash(short n, char *str) {
|
||||
short i;
|
||||
short len=strlen(str);
|
||||
if (len>31) len=31;
|
||||
for(int i=0;i<len;i++) EEPROM.write(n+i,str[i]);
|
||||
EEPROM.write(n+len,0);
|
||||
short i;
|
||||
short len=strlen(str);
|
||||
if (len>31) len=31;
|
||||
for(int i=0;i<len;i++) EEPROM.write(n+i,str[i]);
|
||||
EEPROM.write(n+len,0);
|
||||
}
|
||||
|
||||
int loadFlash(short n, char *str, short l) {
|
||||
short i;
|
||||
uint8_t ch = EEPROM.read(n);
|
||||
if (!ch || (ch == 0xff)) return 0;
|
||||
for (i=0;i<l-1 && (str[i] = EEPROM.read(n++));i++);
|
||||
str[i]=0;
|
||||
return 1;
|
||||
short i;
|
||||
uint8_t ch = EEPROM.read(n);
|
||||
if (!ch || (ch == 0xff)) return 0;
|
||||
for (i=0;i<l-1 && (str[i] = EEPROM.read(n++));i++);
|
||||
str[i]=0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void saveFlash(short n, IPAddress& ip) {
|
||||
for(int i=0;i<4;i++) EEPROM.write(n++,ip[i]);
|
||||
for(int i=0;i<4;i++) EEPROM.write(n++,ip[i]);
|
||||
}
|
||||
|
||||
int ipLoadFromFlash(short n, IPAddress &ip) {
|
||||
for(int i=0;i<4;i++) ip[i]=EEPROM.read(n++);
|
||||
if (ip[0] && (ip[0] != 0xff)) return 1;
|
||||
return 0;
|
||||
for(int i=0;i<4;i++) ip[i]=EEPROM.read(n++);
|
||||
if (ip[0] && (ip[0] != 0xff)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
lan_status getConfig(int arg_cnt, char **args)
|
||||
@@ -915,7 +948,7 @@ lan_status getConfig(int arg_cnt, char **args)
|
||||
strncpy(configServer, args[1], sizeof(configServer) - 1);
|
||||
saveFlash(OFFSET_CONFIGSERVER, configServer);
|
||||
} else if (!loadFlash(OFFSET_CONFIGSERVER, configServer))
|
||||
strncpy_P(configServer,configserver,sizeof(configServer));
|
||||
strncpy_P(configServer,configserver,sizeof(configServer));
|
||||
|
||||
snprintf(URI, sizeof(URI), "/%02x-%02x-%02x-%02x-%02x-%02x.config.json", mac[0], mac[1], mac[2], mac[3], mac[4],
|
||||
mac[5]);
|
||||
@@ -1059,7 +1092,7 @@ lan_status getConfig(int arg_cnt, char **args)
|
||||
|
||||
void preTransmission() {
|
||||
#ifdef CONTROLLINO
|
||||
// set DE and RE on HIGH
|
||||
// set DE and RE on HIGH
|
||||
PORTJ |= B01100000;
|
||||
#else
|
||||
digitalWrite(TXEnablePin, 1);
|
||||
@@ -1086,7 +1119,7 @@ void setup_main() {
|
||||
loadConfigFromEEPROM(0, NULL);
|
||||
|
||||
#ifdef _modbus
|
||||
#ifdef CONTROLLINO
|
||||
#ifdef CONTROLLINO
|
||||
//set PORTJ pin 5,6 direction (RE,DE)
|
||||
DDRJ |= B01100000;
|
||||
//set RE,DE on LOW
|
||||
@@ -1129,6 +1162,8 @@ void setup_main() {
|
||||
void printFirmwareVersionAndBuildOptions() {
|
||||
debugSerial.print(F("\nLazyhome.ru LightHub controller "));
|
||||
debugSerial.println(F(QUOTE(PIO_SRC_REV)));
|
||||
debugSerial.print(F("C++ version:"));
|
||||
debugSerial.println(F(QUOTE(__cplusplus)));
|
||||
#ifdef CONTROLLINO
|
||||
debugSerial.println(F("(+)CONTROLLINO"));
|
||||
#endif
|
||||
@@ -1254,7 +1289,7 @@ void loop_main() {
|
||||
#endif
|
||||
|
||||
#ifdef _dmxin
|
||||
// unsigned long lastpacket = DMXSerial.noDataSince();
|
||||
// unsigned long lastpacket = DMXSerial.noDataSince();
|
||||
DMXCheck();
|
||||
#endif
|
||||
// if (lastpacket && (lastpacket%10==0)) debugSerial.println(lastpacket);
|
||||
@@ -1274,6 +1309,12 @@ void loop_main() {
|
||||
#if defined (_espdmx)
|
||||
dmxout.update();
|
||||
#endif
|
||||
|
||||
#ifndef SYSLOG_DISABLE
|
||||
// debugSerial.print(F("#"));
|
||||
// udpSyslog.log(LOG_INFO, "Ping syslog:");
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
void owIdle(void) {
|
||||
@@ -1296,10 +1337,10 @@ void owIdle(void) {
|
||||
#endif
|
||||
}
|
||||
void ethernetIdle(void){
|
||||
wdt_res();
|
||||
inputLoop();
|
||||
wdt_res();
|
||||
inputLoop();
|
||||
// debugSerial.print(".");
|
||||
};
|
||||
};
|
||||
|
||||
void modbusIdle(void) {
|
||||
wdt_res();
|
||||
@@ -1308,7 +1349,7 @@ void modbusIdle(void) {
|
||||
#ifdef _artnet
|
||||
if (artnet) artnet->read();
|
||||
#endif
|
||||
inputLoop();
|
||||
inputLoop();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
#include <SPI.h>
|
||||
#include <Ethernet3.h>
|
||||
//#include <Ethernet3.h>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -136,13 +136,14 @@
|
||||
#endif
|
||||
|
||||
#define DHT_POLL_DELAY_DEFAULT 15000
|
||||
#define UPTIME_POLL_DELAY_DEFAULT 30000
|
||||
|
||||
#ifdef ARDUINO_ARCH_STM32F1
|
||||
#define strncpy_P strncpy
|
||||
#endif
|
||||
|
||||
#ifndef debugSerial
|
||||
#define debugSerial Serial1
|
||||
#define debugSerial Serial
|
||||
#endif
|
||||
|
||||
#ifndef Wiz5500
|
||||
|
||||
@@ -18,7 +18,7 @@ env_default =
|
||||
; megaatmega2560-5500
|
||||
; due-5500
|
||||
; controllino
|
||||
stm32
|
||||
; stm32
|
||||
|
||||
build_dir = /tmp/pioenvs
|
||||
libdeps_dir = /tmp/piolibdeps
|
||||
@@ -48,7 +48,9 @@ lib_deps =
|
||||
platform = ststm32
|
||||
framework = arduino
|
||||
board = nucleo_f103rb
|
||||
upload_protocol = st-link
|
||||
upload_protocol = stlink
|
||||
debug_tool = stlink
|
||||
extra_scripts = pre:!pre_stm32.sh
|
||||
;lib_ldf_mode = chain+
|
||||
build_flags = !sh build_flags_stm32.sh
|
||||
lib_deps =
|
||||
@@ -62,7 +64,11 @@ lib_deps =
|
||||
https://github.com/knolleary/pubsubclient.git
|
||||
Adafruit Unified Sensor
|
||||
DHT sensor library
|
||||
|
||||
; https://github.com/anklimov/DMXSerial
|
||||
; Syslog
|
||||
; https://github.com/No3x/Syslog.git
|
||||
https://github.com/arcao/Syslog.git
|
||||
; UIPEthernet
|
||||
|
||||
[env:due]
|
||||
platform = atmelsam
|
||||
@@ -88,6 +94,7 @@ lib_deps =
|
||||
SdFat
|
||||
Adafruit Unified Sensor
|
||||
DHT sensor library
|
||||
https://github.com/arcao/Syslog.git
|
||||
|
||||
|
||||
[env:megaatmega2560]
|
||||
@@ -136,6 +143,7 @@ lib_deps =
|
||||
DHT sensor library for ESPx
|
||||
DHT sensor library
|
||||
WifiManager
|
||||
https://github.com/arcao/Syslog.git
|
||||
|
||||
[env:megaatmega2560-net]
|
||||
platform = atmelavr
|
||||
@@ -145,21 +153,21 @@ upload_port = net:192.168.88.2:23000
|
||||
build_flags = !sh build_flags_mega2560-net.sh
|
||||
;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/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/Ethernet
|
||||
https://github.com/PaulStoffregen/SPI.git
|
||||
https://github.com/knolleary/pubsubclient.git
|
||||
https://github.com/anklimov/Artnet.git
|
||||
FastLED
|
||||
Adafruit Unified Sensor
|
||||
DHT sensor library
|
||||
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/Ethernet
|
||||
https://github.com/PaulStoffregen/SPI.git
|
||||
https://github.com/knolleary/pubsubclient.git
|
||||
https://github.com/anklimov/Artnet.git
|
||||
FastLED
|
||||
Adafruit Unified Sensor
|
||||
DHT sensor library
|
||||
|
||||
[env:due-5500]
|
||||
platform = atmelsam
|
||||
|
||||
4
pre_stm32.sh
Normal file
4
pre_stm32.sh
Normal file
@@ -0,0 +1,4 @@
|
||||
#! /bin/bash
|
||||
rm /tmp/piolibdeps -Rf
|
||||
mkdir /tmp/piolibdeps_stm32
|
||||
ln -s /tmp/piolibdeps_stm32 /tmp/piolibdeps
|
||||
Reference in New Issue
Block a user