EEPROM refactoring& http API. Mega&due tested

This commit is contained in:
2021-07-06 00:21:26 +03:00
parent 32eacaeb07
commit 36a7242a60
33 changed files with 937 additions and 191 deletions

3
.gitignore vendored
View File

@@ -19,3 +19,6 @@ custom-build-flags/build_flags_nrf52840
.vscode/.browse.c_cpp.db
.vscode/c_cpp_properties.json
.vscode/launch.json
lighthub/modules/out_elevator.cpp
lighthub/modules/out_elevator.h
spare_files/*

View File

@@ -6,5 +6,8 @@
-DOTA
-DSTATUSLED
#-DPID_DISABLE
-DUARTBRIDGE_ENABLE
#-DUARTBRIDGE_ENABLE
-DARDUINO_OTA_MDNS_DISABLE
-DMDNS_ENABLE
-DMCP23017

View File

@@ -4,3 +4,5 @@
-DSYSLOG_ENABLE
-DWiz5100
#-DPID_DISABLE
-DARDUINO_OTA_MDNS_DISABLE
-DMDNS_ENABLE

View File

@@ -4,3 +4,5 @@
-DSTATUSLED
-DSYSLOG_ENABLE
#-DPID_DISABLE
-DARDUINO_OTA_MDNS_DISABLE
-DMDNS_ENABLE

View File

@@ -9,6 +9,9 @@
-DSYSLOG_ENABLE
# - udp errors
-DOTA
-DARDUINO_OTA_MDNS_DISABLE
-DMDNS_ENABLE
-DMCP23017
-DMODBUS_TX_PIN=13
#-DARTNET_ENABLE - udp rx errors ((

View File

@@ -8,3 +8,6 @@
-DUSE_1W_PIN=16
-DW5500_CS_PIN=15
#-DPID_DISABLE
-DARDUINO_OTA_MDNS_DISABLE
##-DMDNS_ENABLE
-DMCP23017

View File

@@ -9,3 +9,5 @@
-DSTATUSLED
-DMCP23017
#-DPID_DISABLE
-DARDUINO_OTA_MDNS_DISABLE
-DMDNS_ENABLE

View File

@@ -10,3 +10,6 @@
#-DSYSLOG_ENABLE
-DUSE_1W_PIN=16
#-DPID_DISABLE
-DARDUINO_OTA_MDNS_DISABLE
-DMDNS_ENABLE
-DMCP23017

View File

@@ -3,3 +3,5 @@
-DSYSLOG_ENABLE
-DWiz5100
#-DPID_DISABLE
-DARDUINO_OTA_MDNS_DISABLE
-DMDNS_ENABLE

View File

@@ -3,3 +3,5 @@
-DAVR_DMXOUT_PIN=18
-DSYSLOG_ENABLE
#-DPID_DISABLE
-DARDUINO_OTA_MDNS_DISABLE
-DMDNS_ENABLE

View File

@@ -16,3 +16,8 @@
-DOTA
-DMOTOR_DISABLE
#-DWiz5100
-DARDUINO_OTA_MDNS_DISABLE
-DMDNS_ENABLE
# Example of UARTBRIDGE configuration
#-DUARTBRIDGE_ENABLE

View File

@@ -1,4 +1,4 @@
-DWiz5500
#-DWiz5500
#-DW5500_CS_PIN=10
-DDMX_DISABLE
-DMODBUS_DISABLE
@@ -12,3 +12,4 @@
-DSYSLOG_ENABLE
-DMBUS_DISABLE
-DPID_DISABLE
-DMCP23017

View File

@@ -0,0 +1 @@
../tools/mac/arduinoOTA -d -address 192.168.88.70 -port 65280 -username arduino -password password -b /config

View File

@@ -0,0 +1 @@
../tools/mac/arduinoOTA -address elev.local -port 65280 -username arduino -password password -sketch config.json -b -upload /config

View File

@@ -1 +1 @@
../tools/mac/arduinoOTA -address 192.168.88.67 -port 65280 -username arduino -password password -sketch firmware.bin -b -upload /sketch
../tools/mac/arduinoOTA -address 192.168.11.213 -port 65280 -username arduino -password password -sketch firmware.bin -b -upload /sketch

View File

@@ -7,7 +7,7 @@ cp ../.pio/build/mega2560slim2/firmware.bin mega2560slim2
cp ../.pio/build/due-5100/firmware.bin due-5100
cp ../.pio/build/mega2560-5100/firmware.hex mega2560-5100
cp ../.pio/build/due-5500/firmware.bin due-5500
cp ../.pio/build/nrf52840-5500/firmware.hex nrf52840-5500
cp ../.pio/build/nrf52840/firmware.hex nrf52840-5500
cp ../.pio/build/esp32-wifi/firmware.bin esp32-wifi
cp ../.pio/build/stm32-enc2860/firmware.bin stm32-enc2860
cp ../.pio/build/esp8266-wifi/firmware.bin esp8266-wifi

159
lighthub/config.cpp Normal file
View File

@@ -0,0 +1,159 @@
#include "config.h"
//#if defined(ARDUINO_ARCH_AVR)
//#include <EEPROM.h>
//#endif
/*
void saveFlash(short n, char *str) {
short len=strlen(str);
if (len>MAXFLASHSTR-1) len=MAXFLASHSTR-1;
for(int i=0;i<len;i++) EEPROM.write(n+i,str[i]);
EEPROM.write(n+len,0);
#if defined(ARDUINO_ARCH_ESP8266)
// write the data to EEPROM
short res = EEPROM.commitReset();
Serial.println((res) ? "EEPROM Commit OK" : "Commit failed");
#endif
}
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;
}
void saveFlash(short n, IPAddress& ip) {
for(int i=0;i<4;i++) EEPROM.write(n++,ip[i]);
#if defined(ARDUINO_ARCH_ESP8266)
// write the data to EEPROM
short res = EEPROM.commitReset();
Serial.println((res) ? "EEPROM Commit OK" : "Commit failed");
#endif
}
int ipLoadFromFlash(short n, IPAddress &ip) {
for (int i = 0; i < 4; i++)
ip[i] = EEPROM.read(n++);
return (ip[0] && ((ip[0] != 0xff) || (ip[1] != 0xff) || (ip[2] != 0xff) || (ip[3] != 0xff)));
}
*/
bool systemConfig::isValidSysConf()
{
if (!stream) return false;
stream->seek(offsetof(systemConfigData,signature));
for (int i=0;i<EEPROM_SIGNATURE_LENGTH;i++)
if (stream->read()!=EEPROM_signature[i])
{
return false;
}
return true;
};
bool systemConfig::getMAC()
{
if (!stream || !isValidSysConf()) return false;
stream->seek(offsetof(systemConfigData,mac));
bool isMacValid = false;
for (short i = 0; i < 6; i++) {
mac[i] = stream->read();
if (mac[i] != 0 && mac[i] != 0xff) isMacValid = true;
}
return isMacValid;
}
bool systemConfig::getMQTTpwd(char * buffer, uint16_t bufLen)
{
}
bool systemConfig::setMQTTpwd(char * pwd)
{
}
bool systemConfig::setMAC(macAddress mac)
{
}
bool systemConfig::setServer(char* url)
{
}
bool systemConfig::getServer(char* url)
{
}
bool systemConfig::getIP(IPAddress& ip)
{
}
bool systemConfig::getMask(IPAddress& mask)
{
}
bool systemConfig::getDNS(IPAddress& dns)
{
}
bool systemConfig::getGW(IPAddress& gw)
{
}
bool systemConfig::setIP(IPAddress& ip)
{
}
bool systemConfig::setMask(IPAddress& mask)
{
}
bool systemConfig::setDNS(IPAddress& dns)
{
}
bool systemConfig::setGW(IPAddress& gw)
{
}
void systemConfig::clear()
{
return;
if (!stream) return ;
stream->seek(0);
for (unsigned int i = 0; i < stream->getSize(); i++) {
mac[i] = stream->write(255);
}
}
bool systemConfig::getSaveSuccedConfig()
{
return false;
}

86
lighthub/config.h Normal file
View File

@@ -0,0 +1,86 @@
#pragma once
#if defined(ESP8266) || defined(ESP32)
#include "FS.h"
#endif
#include <Arduino.h>
#include "flashstream.h"
#include <IPAddress.h>
#define MAXFLASHSTR 32
#define PWDFLASHSTR 16
#define EEPROM_SIGNATURE "LHCF"
#define EEPROM_SIGNATURE_LENGTH 4
const char EEPROM_signature[] = EEPROM_SIGNATURE;
#define SYSCONF_OFFSET 0
#define EEPROM_offset_NotAlligned SYSCONF_OFFSET+sizeof(systemConfigData)
#define EEPROM_offsetJSON EEPROM_offset_NotAlligned + (4 -(EEPROM_offset_NotAlligned & 3))
//#define EEPROM_offsetJSON IFLASH_PAGE_SIZE
#define EEPROM_FIX_PART_LEN EEPROM_offsetJSON-SYSCONF_OFFSET
typedef char flashstr[MAXFLASHSTR];
typedef char flashpwd[PWDFLASHSTR];
typedef uint8_t macAddress[6];
#pragma pack(push, 1)
typedef struct
{
char signature[4];
macAddress mac;
uint32_t ip;
uint32_t dns;
uint32_t gw;
uint32_t mask;
union {
uint8_t configFlags;
struct
{ uint8_t notGetConfigFromHTTP:1;
uint8_t saveToFlash:1;
};
};
flashstr configURL;
flashpwd MQTTpwd;
flashpwd OTApwd;
uint8_t serialDebugLevel;
uint16_t sysConfigHash;
uint16_t JSONHash;
} systemConfigData;
#pragma (pop)
class systemConfig {
private:
flashStream * stream;
public:
macAddress mac;
systemConfig() {stream=NULL;};
systemConfig(flashStream * fs){stream=fs;};
//systemConfigData data;
bool isValidSysConf();
//bool isValidJSON();
bool getMAC();
//inline macAddress * getMAC() {return &mac;};
bool getMQTTpwd(char * buffer, uint16_t bufLen);
bool setMQTTpwd(char * pwd = NULL);
bool setMAC(macAddress mac);
bool setServer(char* url);
bool getServer(char* url);
bool getIP(IPAddress& ip);
bool getMask(IPAddress& mask);
bool getDNS(IPAddress& dns);
bool getGW(IPAddress& gw);
bool setIP(IPAddress& ip);
bool setMask(IPAddress& mask);
bool setDNS(IPAddress& dns);
bool setGW(IPAddress& gw);
void clear();
bool getSaveSuccedConfig();
bool getLoadHTTPConfig();
//bool Save();
};

13
lighthub/flashstream.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include <flashstream.h>
#if defined(__SAM3X8E__)
DueFlashStorage EEPROM;
#endif
#ifdef NRF5
NRFFlashStorage EEPROM;
#endif
#ifdef ARDUINO_ARCH_STM32
NRFFlashStorage EEPROM;
#endif

134
lighthub/flashstream.h Normal file
View File

@@ -0,0 +1,134 @@
#pragma once
#ifndef _FLASHSTREAM_H_
#define _FLASHSTREAM_H_
#include <main.h>
#if defined(ESP32)
#include <FS.h>
#include <SPIFFS.h>
#endif
#if defined(ARDUINO_ARCH_AVR)
#include <EEPROM.h>
#endif
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP_EEPROM.h>
#endif
#if defined(__SAM3X8E__)
#include <DueFlashStorage.h>
extern DueFlashStorage EEPROM;
#endif
#ifdef NRF5
#include <NRFFlashStorage.h> //STUB
extern NRFFlashStorage EEPROM;
#endif
#ifdef ARDUINO_ARCH_STM32
#include <NRFFlashStorage.h> //STUB
extern NRFFlashStorage EEPROM;
#endif
#include <Stream.h>
#include <Arduino.h>
class seekableStream : public Stream
{
unsigned int streamSize;
public:
seekableStream(unsigned int size):Stream(),streamSize(size) {};
unsigned int getSize() {return streamSize;}
virtual unsigned int seek(unsigned int _pos = 0) = 0;
};
#if defined(ESP32)
class flashStream : public seekableStream
{
private:
File fs;
public:
flashStream(String _filename):seekableStream(65535) {fs = SPIFFS.open(_filename, "r+");}
virtual int available() { return 1; };
virtual int read() {return fs.read();};
virtual int peek() { return fs.peek();};
virtual unsigned int seek(unsigned int _pos = 0){return fs.seek(_pos,SeekSet);};
virtual void flush() {};
virtual size_t write(uint8_t ch) {return fs.write(ch);};
void putEOF(){write (255);};
virtual ~flashStream () {if (fs) fs.close();} ;
};
#else
class flashStream : public seekableStream
{
protected:
unsigned int pos;
unsigned int startPos;
unsigned int size;
public:
flashStream(unsigned int _startPos=0, unsigned int _size=4096 ):seekableStream(_size)
{
pos = _startPos; startPos = _startPos; size = _size;
#if defined(ESP8266)
size_t len = EEPROM.length();
if (len) EEPROM.commit();
EEPROM.begin(len+size); //Re-init
#endif
};
virtual unsigned int seek(unsigned int _pos = 0)
{ pos=min(startPos+_pos, startPos+size);
debugSerial<<F("Seek:")<<pos<<endl;
return pos;
};
virtual int available() { return 1;};
virtual int read() {int ch = peek(); pos++; return ch;};
virtual int peek()
{
return EEPROM.read(pos);
};
virtual void flush() {
#if defined(ESP8266)
EEPROM.commit();
#endif
};
#if defined(__SAM3X8E__)
virtual size_t write(const uint8_t *buffer, size_t size) override
{
debugSerial<<("Write from:")<<pos<<" "<<size<<" bytes"<<endl;
EEPROM.write(pos,(byte*)buffer,size);
pos+=size;
return size;
};
#endif
virtual size_t write(uint8_t ch)
{
#if defined(__AVR__)
//Serial.print (ch);
EEPROM.update(pos++,(char)ch);
return 1;
#endif
#if defined(__SAM3X8E__)
return EEPROM.write(pos++,(char)ch);
#endif
};
void putEOF(){write (255);
#if defined(ESP8266)
EEPROM.commit();
#endif
};
};
#endif
#endif

View File

@@ -34,8 +34,8 @@ e-mail anklimov@gmail.com
#endif
#ifdef MCP23017
#include "Adafruit_MCP23017.h"
Adafruit_MCP23017 mcp;
#include "Adafruit_MCP23X17.h"
Adafruit_MCP23X17 mcp;
#endif
extern PubSubClient mqttClient;
@@ -181,10 +181,10 @@ switch (inType)
case IN_I2C | IN_PUSH_ON | IN_ACTIVE_HIGH:
case IN_I2C | IN_PUSH_TOGGLE | IN_ACTIVE_HIGH:
mcp.begin(); //TBD - multiple chip
mcp.pinMode(pin, INPUT);
if (inputPinMode == INPUT_PULLUP) mcp.pullUp(0, HIGH); // turn on a 100K pullup internally
mcp.begin_I2C(); //TBD - multiple chip
// CHECK! mcp.pinMode(pin, INPUT);
// CHECK! if (inputPinMode == INPUT_PULLUP) mcp.pullUp(0, HIGH); // turn on a 100K pullup internally
mcp.pinMode(pin,inputPinMode);
store->state=IS_IDLE;
break;
#endif

View File

@@ -51,6 +51,10 @@ e-mail anklimov@gmail.com
#include "modules/out_multivent.h"
#include "modules/out_uartbridge.h"
#ifdef ELEVATOR_ENABLE
#include "modules/out_elevator.h"
#endif
short modbusBusy = 0;
bool isPendedModbusWrites = false;
@@ -104,7 +108,7 @@ int txt2subItem(char *payload) {
else if (strcmp_P(payload, TEMP_P) == 0) cmd = S_TEMP;
else if (strcmp_P(payload, VAL_P) == 0) cmd = S_VAL;
else if (strcmp_P(payload, DEL_P) == 0) cmd = S_DELAYED;
else if (strcmp_P(payload, _RAW_P) == 0) cmd = S_RAW;
return cmd;
}
@@ -191,6 +195,13 @@ void Item::Parse() {
// debugSerial<<F("AC driver created")<<endl;
break;
#endif
#ifdef ELEVATOR_ENABLE
case CH_ELEVATOR:
driver = new out_elevator (this);
// debugSerial<<F("AC driver created")<<endl;
break;
#endif
default: ;
}
// debugSerial << F(" Item:") << itemArr->name << F(" T:") << itemType << F(" =") << getArg() << endl;
@@ -489,6 +500,12 @@ if (subItem && strlen(subItem))
if (!suffixCode && defaultSuffixCode)
suffixCode = defaultSuffixCode;
if (suffixCode == S_RAW)
{ itemCmd ic;
ic.Str(payload);
ic.setSuffix(suffixCode);
return Ctrl(ic,subItem);
}
int i=0;
while (payload[i]) {payload[i]=toupper(payload[i]);i++;};
@@ -848,6 +865,7 @@ int Item::Ctrl(itemCmd cmd, char* subItem, bool allowRecursion)
if (chActive>0 && !cmd.getInt()) st.Cmd(CMD_OFF);
if (chActive==0 && cmd.getInt()) st.Cmd(CMD_ON);
setCmd(st.getCmd());
st.saveItem(this);
SendStatus(SEND_COMMAND | SEND_DEFFERED);
// continue processing as SET
@@ -913,8 +931,10 @@ if (driver) //New style modular code
if (!chActive) //if channel was'nt active before CMD_XON
{
debugSerial<<F("Turning XON\n");
res = driver->Ctrl(st.Cmd(CMD_ON), subItem);
st.Cmd(CMD_ON);
res = driver->Ctrl(st, subItem);
setCmd(CMD_XON);
st.saveItem(this);
SendStatus(SEND_COMMAND);
}
else
@@ -926,8 +946,10 @@ if (driver) //New style modular code
case CMD_HALT:
if (chActive) //if channel was active before CMD_HALT
{
res = driver->Ctrl(st.Cmd(CMD_OFF), subItem);
st.Cmd(CMD_OFF);
res = driver->Ctrl(st, subItem);
setCmd(CMD_HALT);
st.saveItem(this);
SendStatus(SEND_COMMAND);
return res;
}
@@ -940,8 +962,12 @@ if (driver) //New style modular code
case CMD_OFF:
if (getCmd() != CMD_HALT) //Halted, ignore OFF
{
res = driver->Ctrl(st.Cmd(CMD_OFF), subItem);
//debugSerial<<"OFF! ";
//st.debugOut();
st.Cmd(CMD_OFF);
res = driver->Ctrl(st, subItem);
setCmd(CMD_OFF);
st.saveItem(this);
SendStatus(SEND_COMMAND);
}
else
@@ -959,10 +985,15 @@ if (driver) //New style modular code
break;
case CMD_ON:
//debugSerial<<"ON!"<<endl;
if (chActive) break;
default: //another command
if (cmd.isCommand()) st.Cmd(cmd.getCmd());
//debugSerial<<"DEF!"<<endl;
//st.debugOut();
res = driver->Ctrl(st, subItem);
st.saveItem(this);
if (st.isCommand())
{
setCmd(st.getCmd());
@@ -1068,12 +1099,14 @@ switch (itemType) {
if (chActive>0) //if channel was active before CMD_HALT
{
setCmd(CMD_HALT);
cmd.saveItem(this); ///
SendStatus(SEND_COMMAND);
}
}
else
{
setCmd(st.getCmd());
st.saveItem(this);
SendStatus(SEND_COMMAND);
}
}
@@ -1261,7 +1294,7 @@ int Item::SendStatus(int sendFlags) {
char addrstr[48];
char valstr[20] = "";
char cmdstr[8] = "";
st.debugOut();
if (sendFlags & SEND_COMMAND)
{
// Preparing Command payload //////////////
@@ -1287,6 +1320,8 @@ int Item::SendStatus(int sendFlags) {
sendFlags &= ~SEND_COMMAND;
}
}
//debugSerial<<"C"<<cmdstr<<endl;
// publish to MQTT - OpenHab Legacy style to
// myhome/s_out/item - mix: value and command
// send only for OH bus supported types

View File

@@ -36,7 +36,8 @@ e-mail anklimov@gmail.com
#define S_TEMP 11
#define S_VAL 12
#define S_DELAYED 13
#define S_ADDITIONAL 13
#define S_RAW 14
#define S_ADDITIONAL 14
#define CH_DIMMER 0 //DMX 1-4 ch
#define CH_RGBW 1 //DMX 4 ch
@@ -54,6 +55,7 @@ e-mail anklimov@gmail.com
#define CH_PID 13
#define CH_MBUS 14
#define CH_UARTBRIDGE 15
#define CH_ELEVATOR 16
#define CH_MULTIVENT 18
//#define CHANNEL_TYPES 13

View File

@@ -616,6 +616,17 @@ long int itemCmd::getInt()
}
}
char* itemCmd::getString()
{
switch (cmd.itemArgType) {
case ST_STRING:
return param.asString;
default:
return NULL;
}
}
float itemCmd::getFloat()
{
@@ -875,7 +886,12 @@ itemCmd itemCmd::RGBW(uint8_t r, uint8_t g, uint8_t b, uint8_t w)
return *this;
}
itemCmd itemCmd::Str(char * str)
{
cmd.itemArgType=ST_STRING;
param.asString = str;
return *this;
}
itemCmd itemCmd::Cmd(uint8_t i)
@@ -906,18 +922,23 @@ bool itemCmd::loadItem(Item * item, bool includeCommand)
param.asInt32=item->getVal();
cmd.itemArgType= subtype;
if (includeCommand) cmd.cmdCode=item->getCmd();
//debugSerial<<F("Loaded :");
//debugOut();
debugSerial<<F("Loaded :");
debugOut();
return 1;
}
switch (item->itemVal->type)
{
case aJson_Int:
Int((int32_t)item->itemVal->valueint);
debugSerial<<F("Loaded Int:");
debugOut();
return true;
case aJson_Float:
Float(item->itemVal->valueint);
debugSerial<<F("Loaded Float:");
debugOut();
return true;
}

View File

@@ -183,6 +183,7 @@ public:
itemCmd HS(uint16_t h, uint8_t s);
itemCmd RGB(uint8_t r, uint8_t g, uint8_t b);
itemCmd RGBW(uint8_t r, uint8_t g, uint8_t b, uint8_t w);
itemCmd Str(char * str);
bool setH(uint16_t);
bool setS(uint8_t);
bool setColorTemp(int);
@@ -205,6 +206,7 @@ public:
long int getInt();
float getFloat();
char * getString();
long int getSingleInt();
short getPercents(bool inverse=false);
short getPercents255(bool inverse=false);

View File

@@ -63,10 +63,30 @@ SSL
ESP32
PWM Out
#if defined(ESP8266) || defined(ESP32)
#include "FS.h"
#endif
*/
#include "main.h"
#include "statusled.h"
#include "flashstream.h"
#include "config.h"
#if defined(ESP32)
flashStream sysConfStream("config.bin");
flashStream JSONStream("config.json");
#else
flashStream sysConfStream(SYSCONF_OFFSET,EEPROM_offsetJSON);
flashStream JSONStream(EEPROM_offsetJSON,MAX_JSON_CONF_SIZE);
#endif
systemConfig sysConf(&sysConfStream);
extern long timer0_overflow_count;
#ifdef WIFI_ENABLE
WiFiClient ethClient;
@@ -84,10 +104,21 @@ EthernetClient ethClient;
#include <ArduinoOTA.h>
#endif
#ifdef MDNS_ENABLE
#ifndef WIFI_ENABLE
EthernetUDP mdnsUDP;
#else
WiFiUDP mdnsUDP;
#endif
MDNS mdns(mdnsUDP);
#endif
/*
#if defined(__SAM3X8E__)
DueFlashStorage EEPROM;
#endif
#ifdef ARDUINO_ARCH_ESP32
NRFFlashStorage EEPROM;
#endif
@@ -99,6 +130,7 @@ NRFFlashStorage EEPROM;
#ifdef NRF5
NRFFlashStorage EEPROM;
#endif
*/
#ifdef SYSLOG_ENABLE
#include <Syslog.h>
@@ -109,7 +141,7 @@ NRFFlashStorage EEPROM;
WiFiUDP udpSyslogClient;
#endif
Syslog udpSyslog(udpSyslogClient, SYSLOG_PROTO_BSD);
unsigned long timerSyslogPingTime;
//unsigned long timerSyslogPingTime;
static char syslogDeviceHostname[16];
Streamlog debugSerial(&debugSerialPort,LOG_DEBUG,&udpSyslog);
@@ -175,7 +207,7 @@ int8_t configLocked = 0;
ModbusMaster node;
#endif
byte mac[6];
//byte mac[6];
PubSubClient mqttClient(ethClient);
@@ -350,15 +382,12 @@ else
void printMACAddress() {
//macAddress * mac = sysConf.getMAC();
infoSerial<<F("MAC:");
for (byte i = 0; i < 6; i++)
{
if (mac[i]<16) infoSerial<<"0";
#ifdef WITH_PRINTEX_LIB
(i < 5) ?infoSerial<<hex <<(mac[i])<<F(":"):infoSerial<<hex<<(mac[i])<<endl;
#else
(i < 5) ?infoSerial<<_HEX(mac[i])<<F(":"):infoSerial<<_HEX(mac[i])<<endl;
#endif
if (sysConf.mac[i]<16) infoSerial<<"0";
(i < 5) ?infoSerial<<_HEX(sysConf.mac[i])<<F(":"):infoSerial<<_HEX(sysConf.mac[i])<<endl;
}
}
@@ -385,15 +414,16 @@ if (element && element->type == aJson_String) return element->valuestring;
}
#ifdef OTA
#if (defined(ARDUINO_ARCH_ESP32) || defined(ESP8266))
// #if (defined(ARDUINO_ARCH_ESP32) || defined(ESP8266))
void setupOTA(void)
{
ArduinoOTA.begin(Ethernet.localIP(), "Lighthub", "password", InternalStorage);
ArduinoOTA.begin(Ethernet.localIP(), "Lighthub", "password", InternalStorage, sysConfStream, JSONStream);
ArduinoOTA.setCustomHandler(httpHandler);
infoSerial<<F("OTA initialized\n");
}
/*
#elif defined (ARDUINO_ARCH_AVR)
InternalStorageAVRClass flashStorage(EEPROM_offsetJSON);
void setupOTA(void)
@@ -405,17 +435,17 @@ if (element && element->type == aJson_String) return element->valuestring;
}
#else
InternalStorageClass flashStorage(EEPROM_offsetJSON);
//InternalStorageClass flashStorage(EEPROM_offsetJSON);
void setupOTA(void)
{
ArduinoOTA.begin(Ethernet.localIP(), "Lighthub", "password", flashStorage);
ArduinoOTA.begin(Ethernet.localIP(), "Lighthub", "password", InternalStorage);
ArduinoOTA.setCustomHandler(httpHandler);
infoSerial<<F("OTA initialized\n");
}
#endif
*/
#else
@@ -765,15 +795,22 @@ void ip_ready_config_loaded_connecting_to_broker() {
deviceName = getStringFromConfig(mqttArr, 0);
infoSerial<<F("Device Name:")<<deviceName<<endl;
#ifdef OTA
ArduinoOTA.setDeviceName(deviceName);
// #ifdef OTA
// ArduinoOTA.setDeviceName(deviceName);
// #endif
#ifdef MDNS_ENABLE
mdns.setName(deviceName);
#endif
//debugSerial<<F("N:")<<n<<endl;
char *servername = getStringFromConfig(mqttArr, 1);
if (n >= 3) port = aJson.getArrayItem(mqttArr, 2)->valueint;
if (n >= 4) user = getStringFromConfig(mqttArr, 3);
if (!loadFlash(OFFSET_MQTT_PWD, passwordBuf, sizeof(passwordBuf)) && (n >= 5))
//if (!loadFlash(OFFSET_MQTT_PWD, passwordBuf, sizeof(passwordBuf)) && (n >= 5))
if (!sysConf.getMQTTpwd(passwordBuf, sizeof(passwordBuf)) && (n >= 5))
{
password = getStringFromConfig(mqttArr, 4);
infoSerial<<F("Using MQTT password from config")<<endl;
@@ -861,7 +898,7 @@ void onInitialStateInitLAN() {
infoSerial<<F("WIFI AP/Password:")<<QUOTE(ESP_WIFI_AP)<<F("/")<<QUOTE(ESP_WIFI_PWD)<<endl;
#ifndef ARDUINO_ARCH_ESP32
wifi_set_macaddr(STATION_IF,mac); //ESP32 to check
wifi_set_macaddr(STATION_IF,sysConf.mac); //ESP32 to check
#endif
WiFi.begin(QUOTE(ESP_WIFI_AP), QUOTE(ESP_WIFI_PWD));
@@ -897,26 +934,26 @@ if (WiFi.status() == WL_CONNECTED) {
*/
#else // Ethernet connection
//macAddress * mac = sysConf.getMAC();
IPAddress ip, dns, gw, mask;
int res = 1;
infoSerial<<F("Starting lan")<<endl;
if (ipLoadFromFlash(OFFSET_IP, ip)) {
if (sysConf.getIP(ip)) {
infoSerial<<F("Loaded from flash IP:");
printIPAddress(ip);
if (ipLoadFromFlash(OFFSET_DNS, dns)) {
if (sysConf.getDNS(dns)) {
infoSerial<<F(" DNS:");
printIPAddress(dns);
if (ipLoadFromFlash(OFFSET_GW, gw)) {
if (sysConf.getGW(gw)) {
infoSerial<<F(" GW:");
printIPAddress(gw);
if (ipLoadFromFlash(OFFSET_MASK, mask)) {
if (sysConf.getMask(mask)) {
infoSerial<<F(" MASK:");
printIPAddress(mask);
Ethernet.begin(mac, ip, dns, gw, mask);
} else Ethernet.begin(mac, ip, dns, gw);
} else Ethernet.begin(mac, ip, dns);
} else Ethernet.begin(mac, ip);
Ethernet.begin(sysConf.mac, ip, dns, gw, mask);
} else Ethernet.begin(sysConf.mac, ip, dns, gw);
} else Ethernet.begin(sysConf.mac, ip, dns);
} else Ethernet.begin(sysConf.mac, ip);
infoSerial<<endl;
lanStatus = HAVE_IP_ADDRESS;
}
@@ -925,9 +962,9 @@ if (WiFi.status() == WL_CONNECTED) {
wdt_dis();
#if defined(ARDUINO_ARCH_STM32)
res = Ethernet.begin(mac);
res = Ethernet.begin(sysConf.mac);
#else
res = Ethernet.begin(mac, 12000);
res = Ethernet.begin(sysConf.mac, 12000);
#endif
wdt_en();
wdt_res();
@@ -944,6 +981,25 @@ if (WiFi.status() == WL_CONNECTED) {
infoSerial<<F("Got IP address:");
printIPAddress(Ethernet.localIP());
lanStatus = HAVE_IP_ADDRESS;
#ifdef MDNS_ENABLE
#ifndef OTA_PORT
#define OTA_PORT 65280
#endif
mdns.begin(Ethernet.localIP(), "lighthub");
mdns.addServiceRecord(("LightHub controller._http"),
OTA_PORT,
MDNSServiceTCP);
mdns.addServiceRecord("Lighthub TXT"
"._http",
OTA_PORT,
MDNSServiceTCP,
"\x7path=/2");
#endif
}
}
#endif
@@ -1158,6 +1214,7 @@ setupSyslog();
printConfigSummary();
configLoaded=true;
if (sysConf.getSaveSuccedConfig()) cmdFunctionSave(0,NULL);
configLocked--;
}
@@ -1200,10 +1257,14 @@ int loadConfigFromEEPROM()
char ch;
infoSerial<<F("Loading Config from EEPROM")<<endl;
ch = EEPROM.read(EEPROM_offsetJSON);
Serial.print(ch);
if (ch == '{') {
aJsonEEPROMStream as = aJsonEEPROMStream(EEPROM_offsetJSON);
//ch = EEPROM.read(EEPROM_offsetJSON);
//Serial.print(ch);
//if (ch == '{') {
JSONStream.seek();
if (JSONStream.peek() == '{') {
//aJsonEEPROMStream as = aJsonEEPROMStream(EEPROM_offsetJSON);
//flashStream fs = flashStream(EEPROM_offsetJSON);
aJsonStream as = aJsonStream(&JSONStream);
cleanConf();
root = aJson.parse(&as);
if (!root) {
@@ -1221,6 +1282,7 @@ int loadConfigFromEEPROM()
return 0;
}
/*
void cmdFunctionReq(int arg_cnt, char **args) {
mqttConfigRequest(arg_cnt, args);
}
@@ -1256,38 +1318,46 @@ int mqttConfigResp(char *as) {
return 1;
}
*/
#if defined(__SAM3X8E__)
#define saveBufLen 16000
//#define saveBufLen 16000
void cmdFunctionSave(int arg_cnt, char **args)
{
char* outBuf = (char*) malloc(saveBufLen); /* XXX: Dynamic size. */
char* outBuf = (char*) malloc(MAX_JSON_CONF_SIZE); /* XXX: Dynamic size. */
if (outBuf == NULL)
{
return;
}
infoSerial<<F("Saving config to EEPROM..");
aJsonStringStream stringStream(NULL, outBuf, saveBufLen);
infoSerial<<F("Saving config to EEPROM..")<<endl;
aJsonStringStream stringStream(NULL, outBuf, MAX_JSON_CONF_SIZE);
aJson.print(root, &stringStream);
int len = strlen(outBuf);
outBuf[len++]= EOF;
EEPROM.write(EEPROM_offsetJSON,(byte*) outBuf,len);
//EEPROM.write(EEPROM_offsetJSON,(byte*) outBuf,len);
JSONStream.seek();
size_t res = JSONStream.write((byte*) outBuf,len);
free (outBuf);
infoSerial<<F("Saved to EEPROM");
infoSerial<<res<< F("bytes from ")<<len<<F(" are saved to EEPROM")<<endl;
}
#else
void cmdFunctionSave(int arg_cnt, char **args)
{
aJsonEEPROMStream jsonEEPROMStream = aJsonEEPROMStream(EEPROM_offsetJSON);
//aJsonEEPROMStream jsonEEPROMStream = aJsonEEPROMStream(EEPROM_offsetJSON);
//flashStream fs = flashStream(EEPROM_offsetJSON);
JSONStream.seek();
aJsonStream jsonEEPROMStream = aJsonStream(&JSONStream);
infoSerial<<F("Saving config to EEPROM..");
aJson.print(root, &jsonEEPROMStream);
jsonEEPROMStream.putEOF();
JSONStream.putEOF();
infoSerial<<F("Saved to EEPROM");
infoSerial<<F("Saved to EEPROM")<<endl;
}
#endif
@@ -1307,23 +1377,23 @@ void cmdFunctionIp(int arg_cnt, char **args)
// switch (arg_cnt) {
// case 5:
if (arg_cnt>4 && _inet_aton(args[4], ip)) saveFlash(OFFSET_MASK, ip);
else saveFlash(OFFSET_MASK, ip0);
if (arg_cnt>4 && _inet_aton(args[4], ip)) sysConf.setMask(ip);
else sysConf.setMask(ip0);
// case 4:
if (arg_cnt>3 && _inet_aton(args[3], ip)) saveFlash(OFFSET_GW, ip);
else saveFlash(OFFSET_GW, ip0);
if (arg_cnt>3 && _inet_aton(args[3], ip)) sysConf.setGW(ip);
else sysConf.setGW(ip0);
// case 3:
if (arg_cnt>2 && _inet_aton(args[2], ip)) saveFlash(OFFSET_DNS, ip);
else saveFlash(OFFSET_DNS, ip0);
if (arg_cnt>2 && _inet_aton(args[2], ip)) sysConf.setDNS(ip);
else sysConf.setDNS(ip0);
// case 2:
if (arg_cnt>1 && _inet_aton(args[1], ip)) saveFlash(OFFSET_IP, ip);
else saveFlash(OFFSET_IP, ip0);
if (arg_cnt>1 && _inet_aton(args[1], ip)) sysConf.setIP(ip);
else sysConf.setIP(ip0);
// break;
// case 1: //dynamic IP
if (arg_cnt==1)
{
saveFlash(OFFSET_IP,ip0);
sysConf.setIP(ip0);
infoSerial<<F("Set dynamic IP\n");
}
/*
@@ -1345,32 +1415,37 @@ void cmdFunctionIp(int arg_cnt, char **args)
}
void cmdFunctionClearEEPROM(int arg_cnt, char **args){
/*
for (int i = OFFSET_MAC; i < OFFSET_MAC+EEPROM_FIX_PART_LEN+1; i++) //Fi[ part +{]
EEPROM.write(i, 0);
for (int i = 0; i < EEPROM_SIGNATURE_LENGTH; i++)
EEPROM.write(i+OFFSET_SIGNATURE,EEPROM_signature[i]);
*/
sysConf.clear();
infoSerial<<F("EEPROM cleared\n");
}
void cmdFunctionPwd(int arg_cnt, char **args)
{ char empty[]="";
{ //char empty[]="";
if (arg_cnt)
saveFlash(OFFSET_MQTT_PWD,args[1]);
else saveFlash(OFFSET_MQTT_PWD,empty);
sysConf.setMQTTpwd(args[1]);
else sysConf.setMQTTpwd();
infoSerial<<F("Password updated\n");
}
void cmdFunctionSetMac(int arg_cnt, char **args) {
char dummy;
uint8_t mac[6];
if (sscanf(args[1], "%x:%x:%x:%x:%x:%x%c", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5], &dummy) < 6) {
errorSerial<<F("could not parse: ")<<args[1];
return;
}
sysConf.setMAC(mac);
printMACAddress();
for (short i = 0; i < 6; i++) { EEPROM.write(i, mac[i]); }
//for (short i = 0; i < 6; i++) { EEPROM.write(i, mac[i]); }
infoSerial<<F("Updated\n");
}
@@ -1382,60 +1457,26 @@ void cmdFunctionGet(int arg_cnt, char **args) {
void printBool(bool arg) { (arg) ? infoSerial<<F("+") : infoSerial<<F("-"); }
void saveFlash(short n, char *str) {
short len=strlen(str);
if (len>MAXFLASHSTR-1) len=MAXFLASHSTR-1;
for(int i=0;i<len;i++) EEPROM.write(n+i,str[i]);
EEPROM.write(n+len,0);
#if defined(ARDUINO_ARCH_ESP8266)
// write the data to EEPROM
short res = EEPROM.commitReset();
Serial.println((res) ? "EEPROM Commit OK" : "Commit failed");
#endif
}
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;
}
void saveFlash(short n, IPAddress& ip) {
for(int i=0;i<4;i++) EEPROM.write(n++,ip[i]);
#if defined(ARDUINO_ARCH_ESP8266)
// write the data to EEPROM
short res = EEPROM.commitReset();
Serial.println((res) ? "EEPROM Commit OK" : "Commit failed");
#endif
}
int ipLoadFromFlash(short n, IPAddress &ip) {
for (int i = 0; i < 4; i++)
ip[i] = EEPROM.read(n++);
return (ip[0] && ((ip[0] != 0xff) || (ip[1] != 0xff) || (ip[2] != 0xff) || (ip[3] != 0xff)));
}
lan_status loadConfigFromHttp(int arg_cnt, char **args)
{
//macAddress * mac = sysConf.getMAC();
int responseStatusCode = 0;
char URI[64];
char configServer[32]="";
if (arg_cnt > 1) {
strncpy(configServer, args[1], sizeof(configServer) - 1);
saveFlash(OFFSET_CONFIGSERVER, configServer);
sysConf.setServer(configServer);
//saveFlash(OFFSET_CONFIGSERVER, configServer);
infoSerial<<configServer<<F(" Saved")<<endl;
} else if (!loadFlash(OFFSET_CONFIGSERVER, configServer))
} else if (!sysConf.getServer(configServer))
{
strncpy_P(configServer,configserver,sizeof(configServer));
infoSerial<<F(" Default config server used: ")<<configServer<<endl;
}
#ifndef DEVICE_NAME
snprintf(URI, sizeof(URI), "/cnf/%02x-%02x-%02x-%02x-%02x-%02x.config.json", mac[0], mac[1], mac[2], mac[3], mac[4],
mac[5]);
snprintf(URI, sizeof(URI), "/cnf/%02x-%02x-%02x-%02x-%02x-%02x.config.json", sysConf.mac[0], sysConf.mac[1], sysConf.mac[2], sysConf.mac[3], sysConf.mac[4],
sysConf.mac[5]);
#else
#ifndef FLASH_64KB
snprintf(URI, sizeof(URI), "/cnf/%s_config.json",QUOTE(DEVICE_NAME));
@@ -1554,11 +1595,21 @@ lan_status loadConfigFromHttp(int arg_cnt, char **args)
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) //|| defined (NRF5)
HTTPClient httpClient;
// WiFiClient wifiClient;
#if defined(WIFI_ENABLE)
WiFiClient configEthClient;
#else
EthernetClient configEthClient;
#endif
String fullURI = "http://";
fullURI+=configServer;
fullURI+=URI;
//httpClient.begin(wifiClient,fullURI);
#if defined(ARDUINO_ARCH_ESP8266)
httpClient.begin(configEthClient,fullURI);
#else
httpClient.begin(fullURI);
#endif
int httpResponseCode = httpClient.GET();
if (httpResponseCode > 0) {
infoSerial.printf("[HTTP] GET... code: %d\n", httpResponseCode);
@@ -1609,6 +1660,7 @@ void postTransmission() {
}
void setup_main() {
#if defined(__SAM3X8E__)
memset(&UniqueID,0,sizeof(UniqueID));
#endif
@@ -1622,23 +1674,42 @@ void setup_main() {
printFirmwareVersionAndBuildOptions();
//Checkin EEPROM integrity (signature)
for (int i=0;i<EEPROM_SIGNATURE_LENGTH;i++)
if (EEPROM.read(i+OFFSET_SIGNATURE)!=EEPROM_signature[i])
{
cmdFunctionClearEEPROM(0,NULL);
break;
}
if (!sysConf.isValidSysConf()) sysConf.clear();
// scan_i2c_bus();
#ifdef SD_CARD_INSERTED
sd_card_w5100_setup();
#endif
setupMacAddress();
/*
#if defined(ARDUINO_ARCH_ESP8266)
EEPROM.begin(ESP_EEPROM_SIZE);
#endif
*/
#if defined(ESP32)
//Initialize File System
if(SPIFFS.begin(true))
{
debugSerial<<("SPIFFS Initialize....ok")<<endl;
}
else
{
debugSerial<<("SPIFFS Initialization...failed")<<endl;
}
//Format File System
if(SPIFFS.format())
{
debugSerial<<("File System Formated")<<endl;
}
else
{
debugSerial<<("File System Formatting Error")<<endl;
}
#endif
#ifdef _modbus
#ifdef CONTROLLINO
@@ -1832,6 +1903,12 @@ infoSerial<<F("\n(+)UARTBRIDGE");
#else
infoSerial<<F("\n(-)UARTBRIDGE");
#endif
#ifdef MDNS_ENABLE
infoSerial<<F("\n(+)MDNS");
#else
infoSerial<<F("\n(-)MDNS");
#endif
infoSerial<<endl;
// WDT_Disable( WDT ) ;
@@ -1873,37 +1950,40 @@ void publishStat(){
void setupMacAddress() {
//Check MAC, stored in NVRAM
bool isMacValid = false;
//macAddress * mac = sysConf.getMAC();
//bool isMacValid = sysConf.getMAC(&mac);
/*
for (short i = 0; i < 6; i++) {
mac[i] = EEPROM.read(i);
if (mac[i] != 0 && mac[i] != 0xff) isMacValid = true;
}
if (!isMacValid) {
*/
if (!sysConf.getMAC()) {
infoSerial<<F("No MAC configured: set firmware's MAC\n");
#if defined (CUSTOM_FIRMWARE_MAC) //Forced MAC from compiler's directive
const char *macStr = QUOTE(CUSTOM_FIRMWARE_MAC);//colon(:) separated from build options
parseBytes(macStr, ':', mac, 6, 16);
parseBytes(macStr, ':', sysConf.mac, 6, 16);
mac[0]&=0xFE;
mac[0]|=2;
sysConf.mac[0]&=0xFE;
sysConf.mac[0]|=2;
#elif defined(WIFI_ENABLE)
//Using original MPU MAC
WiFi.begin();
WiFi.macAddress(mac);
WiFi.macAddress(sysConf.mac);
#elif defined(__SAM3X8E__)
//Lets make MAC from MPU serial#
mac[0]=0xDE;
sysConf.mac[0]=0xDE;
for (byte b = 0 ; b < 5 ; b++)
mac[b+1]=UniqueID.UID_Byte [b*3] + UniqueID.UID_Byte [b*3+1] + UniqueID.UID_Byte [b*3+2];
sysConf.mac[b+1]=UniqueID.UID_Byte [b*3] + UniqueID.UID_Byte [b*3+1] + UniqueID.UID_Byte [b*3+2];
#elif defined DEFAULT_FIRMWARE_MAC
uint8_t defaultMac[6] = DEFAULT_FIRMWARE_MAC;//comma(,) separated hex-array, hard-coded
memcpy(mac,defaultMac,6);
memcpy(sysConf.mac,defaultMac,6);
#endif
}
@@ -1922,7 +2002,7 @@ void setupCmdArduino() {
cmdAdd("mac", cmdFunctionSetMac);
#endif
cmdAdd("kill", cmdFunctionKill);
cmdAdd("req", cmdFunctionReq);
//cmdAdd("req", cmdFunctionReq);
cmdAdd("ip", cmdFunctionIp);
cmdAdd("pwd", cmdFunctionPwd);
cmdAdd("clear",cmdFunctionClearEEPROM);
@@ -1979,6 +2059,9 @@ void loop_main() {
dmxout.update();
#endif
#ifdef MDNS_ENABLE
mdns.run();
#endif
}
void owIdle(void) {
@@ -2021,7 +2104,9 @@ void modbusIdle(void) {
yield();
inputLoop();
}
#ifdef MDNS_ENABLE
mdns.run();
#endif
#ifdef _dmxin
DMXCheck();

View File

@@ -7,7 +7,7 @@
#endif
#if defined(__SAM3X8E__)
#include <DueFlashStorage.h>
//#include <DueFlashStorage.h>
#include <watchdog.h>
#include <ArduinoHttpClient.h>
#endif
@@ -16,13 +16,13 @@
#include "HTTPClientAVR.h"
#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include <EEPROM.h>
//#include <EEPROM.h>
#endif
#if defined(ARDUINO_ARCH_ESP8266)
#include <FS.h> //this needs to be first, or it all crashes and burns...
//#include "SPIFFS.h"
#include <ESP_EEPROM.h>
//#include <ESP_EEPROM.h>
//#include <ESP8266HTTPClient.h>
//#include <ArduinoHttpClient.h>
//#include "HttpClient.h"
@@ -38,7 +38,7 @@
#include <FS.h> //this needs to be first, or it all crashes and burns...
//#include "SPIFFS.h"
//#include <EEPROM.h>
#include <NRFFlashStorage.h>
//#include <NRFFlashStorage.h>
//#include "HttpClient.h"
//#include <ArduinoHttpClient.h>
//#include <HTTPClient.h>
@@ -53,14 +53,14 @@
#endif
#ifdef NRF5
#include <NRFFlashStorage.h>
//#include <NRFFlashStorage.h>
#include <ArduinoHttpClient.h>
#endif
#ifdef ARDUINO_ARCH_STM32
#include "HttpClient.h"
//#include "UIPEthernet.h"
#include <NRFFlashStorage.h>
//#include <NRFFlashStorage.h>
//#include <EEPROM.h>
#endif
@@ -178,6 +178,10 @@ extern Streamlog errorSerial;
#include "sd_card_w5100.h"
#endif
#ifdef MDNS_ENABLE
#include <ArduinoMDNS.h>
#endif
#include "Arduino.h"
#include "utils.h"
#include "textconst.h"
@@ -243,11 +247,11 @@ void cmdFunctionLoad(int arg_cnt, char **args);
int loadConfigFromEEPROM();
void cmdFunctionReq(int arg_cnt, char **args);
//void cmdFunctionReq(int arg_cnt, char **args);
int mqttConfigRequest(int arg_cnt, char **args);
//int mqttConfigRequest(int arg_cnt, char **args);
int mqttConfigResp(char *as);
//int mqttConfigResp(char *as);
void cmdFunctionSave(int arg_cnt, char **args);
@@ -256,7 +260,7 @@ void cmdFunctionSetMac(int arg_cnt, char **args);
void cmdFunctionGet(int arg_cnt, char **args);
void printBool(bool arg);
/*
void saveFlash(short n, char *str);
int loadFlash(short n, char *str, short l=MAXFLASHSTR);
@@ -264,6 +268,7 @@ int loadFlash(short n, char *str, short l=MAXFLASHSTR);
void saveFlash(short n, IPAddress& ip);
int ipLoadFromFlash(short n, IPAddress &ip);
*/
lan_status loadConfigFromHttp(int arg_cnt = 0, char **args = NULL);

View File

@@ -25,6 +25,7 @@ WiFiUDP udpClientB;
IPAddress targetIP;
uint16_t targetPort=5555;
int loglev=0;
short halfduplex=1;
bool udpdump=false;
@@ -32,6 +33,10 @@ uint32_t timerA = 0;
uint32_t timerB = 0;
uint16_t sizeA = 0;
uint16_t sizeB = 0;
char * PDUbondaries = NULL;
char bufA[MAX_PDU];
char bufB[MAX_PDU];
//extern aJsonObject *modbusObj;
//extern ModbusMaster node;
@@ -73,6 +78,9 @@ bool out_UARTbridge::getConfig()
aJsonObject * debugIPObj=aJson.getObjectItem(item->itemArg, "ip");
aJsonObject * debugPortObj=aJson.getObjectItem(item->itemArg, "port");
aJsonObject * hdObj=aJson.getObjectItem(item->itemArg, "hd");
aJsonObject * logObj=aJson.getObjectItem(item->itemArg, "log");
aJsonObject * bondObj=aJson.getObjectItem(item->itemArg, "bond");
if (debugIPObj)
{
@@ -86,6 +94,8 @@ bool out_UARTbridge::getConfig()
if (debugPortObj) targetPort = debugPortObj->valueint;
if (hdObj) halfduplex = hdObj->valueint;
if (logObj) loglev = logObj->valueint;
if (bondObj) PDUbondaries = bondObj->valuestring;
return true;
}
@@ -105,6 +115,8 @@ if (!store)
sizeB=0;
timerA=0;
timerB=0;
bufA[0]=0;
bufB[0]=0;
if (getConfig())
{
@@ -141,15 +153,88 @@ if (store)
return CST_UNKNOWN;
}
String _RR_A;
String _WR_A;
String _RR_B;
String _WR_B;
void strcat_c (char *str, char c, int len)
{
char strn[]="-";
strn[0]=c;
///str[1]=0;
strncat(str,strn,len);
/*
return;
int i;
for (i=0;*str && (i<len);i++) str++; // note the terminating semicolon here.
if (i+1<len)
{
*str++ = c;
*str++ = 0;
}
*/
}
void logA(char c)
{
strcat_c(bufA,c,sizeof(bufA)-1);
}
void logB(char c)
{
strcat_c(bufB,c,sizeof(bufB)-1);
}
void flushA()
{
if (sizeA)
{
if (lanStatus>=HAVE_IP_ADDRESS && udpdump) udpClientA.endPacket();
debugSerial<<endl;
//logA('\n');
}
timerA=0;
sizeA=0;
String curStr(bufA);
bufA[0]=0;
{
//if (curStr[0]<0x20) curStr.remove(0);
curStr.trim();
if (!curStr.length()) return;
//if(loglev) debugSerial<<curStr<<endl;
if (curStr.startsWith("@00RR"))
{
if (_RR_A == curStr) return;
else _RR_A = curStr;
}
else if (curStr.startsWith("@00WR"))
{
if (_WR_A == curStr) return;
else _WR_A = curStr;
}
if(!loglev) return;
debugSerial<<F("<");
short i=0;
while (curStr[i] && i<curStr.length())
{
switch (loglev)
{
case 1: debugSerial<<((curStr[i]<16)?"0":"")<<_HEX(curStr[i]);
break;
case 2: debugSerial<<curStr[i];
}
i++;
}
debugSerial<<endl;
}
}
void flushB()
@@ -157,10 +242,47 @@ void flushB()
if (sizeB)
{
if (lanStatus>=HAVE_IP_ADDRESS && udpdump) udpClientB.endPacket();
debugSerial<<endl;
//logB('\n');
}
timerB=0;
sizeB=0;
String curStr(bufB);
bufB[0]=0;
{
//if (curStr[0]<0x20) curStr.remove(0);
curStr.trim();
if (!curStr.length()) return;
//if(loglev) debugSerial<<curStr<<endl;
if (curStr.startsWith("@00RR"))
{
if (_RR_B == curStr) return;
else _RR_B = curStr;
}
else if (curStr.startsWith("@00WR"))
{
if (_WR_B == curStr) return;
else _WR_B = curStr;
}
if(!loglev) return;
debugSerial<<F(">");
short i=0;
while (curStr[i] && i<curStr.length())
{
switch (loglev)
{
case 1: debugSerial<<((curStr[i]<16)?"0":"")<<_HEX(curStr[i]);
break;
case 2: debugSerial<<curStr[i];
}
i++;
}
debugSerial<<endl;
}
}
int out_UARTbridge::Poll(short cause)
@@ -183,7 +305,7 @@ int out_UARTbridge::Poll(short cause)
////if (timerB) return 1;
chA=MODULE_UATRBRIDGE_UARTA.read();
MODULE_UATRBRIDGE_UARTB.write(chA);
debugSerial<<F("<")<<((chA<16)?"0":"")<<_HEX(chA);
logA(chA);
if (lanStatus>=HAVE_IP_ADDRESS && udpdump) {udpClientA.write(chA);}
sizeA++;
}
@@ -199,14 +321,14 @@ int out_UARTbridge::Poll(short cause)
////if (timerA) return 1;
chB=MODULE_UATRBRIDGE_UARTB.read();
MODULE_UATRBRIDGE_UARTA.write(chB);
debugSerial<<F(">")<<((chB<16)?"0":"")<<_HEX(chB);
logB(chB);
if (lanStatus>=HAVE_IP_ADDRESS && udpdump) {udpClientB.write(chB);};
sizeB++;
}
}
if ((timerA && (isTimeOver(timerA,millis(),PDELAY)) || sizeA>=MAX_PDU)) flushA();
if ((timerB && (isTimeOver(timerB,millis(),PDELAY)) || sizeB>=MAX_PDU)) flushB();
if ((timerA && (isTimeOver(timerA,millis(),PDELAY)) || (chA=='*') || (chA=='\r') || sizeA>=MAX_PDU)) flushA();
if ((timerB && (isTimeOver(timerB,millis(),PDELAY)) || (chB=='*') || (chB=='\r') || sizeB>=MAX_PDU)) flushB();
/*

View File

@@ -14,7 +14,7 @@
#define SOURCE_PORT_A 5551
#define SOURCE_PORT_B 5552
#define MAX_PDU 1024
#define MAX_PDU 64
#ifndef MODULE_UATRBRIDGE_UARTA
#define MODULE_UATRBRIDGE_UARTA Serial1
@@ -54,7 +54,7 @@ public:
//aJsonObject * parameters;
};
#define PDELAY 50
#define PDELAY 10
class out_UARTbridge : public abstractOut {
public:

View File

@@ -1,4 +1,19 @@
#include <Arduino.h>
#ifndef MAX_JSON_CONF_SIZE
#if defined(__SAM3X8E__)
#define MAX_JSON_CONF_SIZE 16000
#elif defined(ARDUINO_ARCH_AVR)
#define MAX_JSON_CONF_SIZE 4096
#elif defined(ARDUINO_ARCH_ESP32)
#define MAX_JSON_CONF_SIZE 65535
#else
#define MAX_JSON_CONF_SIZE 32000
#endif
#endif
// Configuration of drivers enabled
#define SYSLOG_LOCAL_SOCKET 514
@@ -39,7 +54,7 @@
#define TXEnablePin MODBUS_TX_PIN
#endif
#define ESP_EEPROM_SIZE 2048
//#define ESP_EEPROM_SIZE 2048
#ifndef AVR_DMXOUT_PIN
#define AVR_DMXOUT_PIN 18
@@ -63,6 +78,7 @@
#define MIN_VOLUME 25
#define INIT_VOLUME 40
/*
#define MAXFLASHSTR 32
#define PWDFLASHSTR 16
#define EEPROM_SIGNATURE "LHCF"
@@ -80,7 +96,7 @@
#define EEPROM_offsetJSON EEPROM_offset_NotAlligned + (4 -(EEPROM_offset_NotAlligned & 3))
//#define EEPROM_offsetJSON IFLASH_PAGE_SIZE
#define EEPROM_FIX_PART_LEN EEPROM_offsetJSON-OFFSET_MAC
*/
#ifndef INTERVAL_CHECK_INPUT
#define INTERVAL_CHECK_INPUT 15

View File

@@ -1,3 +1,4 @@
#pragma once
#include <Print.h>
#include <HardwareSerial.h>
#include <inttypes.h>

View File

@@ -116,9 +116,10 @@ const char HSV_P[] PROGMEM = "HSV";
const char RGB_P[] PROGMEM = "RGB";
const char VAL_P[] PROGMEM = "val";
const char DEL_P[] PROGMEM = "del";
const char _RAW_P[] PROGMEM = "raw";
/*
const char RPM_P[] PROGMEM = "rpm";
const char STATE_P[] PROGMEM = "state";
*/
const char EEPROM_signature[] = EEPROM_SIGNATURE;

View File

@@ -54,12 +54,12 @@ default_envs =
; STM32 board + ENC28j60 network
; stm32-enc2860
; NRF52 board + Wiznet 5500 network
; nrf52840-5500
; NRF52 board + Wiznet 5x00 network
; nrf52840
;build_dir = /tmp/pioenvs
;libdeps_dir = /tmp/piolibdeps
[env:nrf52840-5500]
[env:nrf52840]
platform = nordicnrf52
board = nrf52840_dk
monitor_baud = 115200
@@ -67,7 +67,7 @@ monitor_baud = 115200
;upload_protocol = mbed
;upload_port = /dev/cu.SLAB_USBtoUART
framework = arduino
build_flags = !python get_build_flags.py nrf52840-5500
build_flags = !python get_build_flags.py nrf52840
lib_ignore =
;DS2482_OneWire //UNCOMMENT for software 1-wire driver
ESP_EEPROM
@@ -78,7 +78,7 @@ lib_ignore =
SD
SdFat
WifiManager
Ethernet
Ethernet2
Ethernet3
Ethernet5100
httpClient
@@ -99,7 +99,7 @@ lib_ignore =
lib_deps =
https://github.com/anklimov/Arduino-Temperature-Control-Library.git
https://github.com/anklimov/DS2482_OneWire
https://github.com/anklimov/Ethernet2
https://github.com/anklimov/Ethernet
ArduinoHttpClient
https://github.com/anklimov/aJson
https://github.com/anklimov/CmdArduino
@@ -113,9 +113,11 @@ lib_deps =
https://github.com/anklimov/NRFFlashStorage
https://github.com/adafruit/Adafruit_NeoPixel.git
https://github.com/anklimov/ArduinoOTA
https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit MCP23017 Arduino Library
Adafruit BusIO
https://github.com/arcao/Syslog.git
br3ttb/PID@^1.2.1
;ArduinoMDNS
monitor_speed = 115200
[env:m5stack]
@@ -153,7 +155,7 @@ lib_deps =
https://github.com/knolleary/pubsubclient.git
Streaming
;ESP_EEPROM
https://github.com/anklimov/NRFFlashStorage
;https://github.com/anklimov/NRFFlashStorage
Adafruit Unified Sensor
DHT sensor library for ESPx
https://github.com/anklimov/Artnet.git
@@ -168,9 +170,11 @@ lib_deps =
M5Stack
Adafruit NeoPixel
https://github.com/anklimov/ArduinoOTA
https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit MCP23017 Arduino Library
Adafruit BusIO
https://github.com/arcao/Syslog.git
br3ttb/PID@^1.2.1
ArduinoMDNS
monitor_speed = 115200
[env:esp32-wifi]
@@ -182,8 +186,8 @@ board = esp32-evb
monitor_baud = 115200
;upload_speed = 115200
;upload_command = arduinoOTA -address 192.168.88.60 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE;sleep 5
upload_command = arduinoOTA -address 192.168.11.230 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE;sleep 5
upload_protocol = custom
;upload_command = arduinoOTA -address 192.168.11.230 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE;sleep 5
;upload_protocol = custom
build_flags = !python get_build_flags.py esp32-wifi
lib_ignore =
@@ -215,7 +219,7 @@ lib_deps =
https://github.com/knolleary/pubsubclient.git
Streaming
;ESP_EEPROM
https://github.com/anklimov/NRFFlashStorage
;https://github.com/anklimov/NRFFlashStorage
Adafruit Unified Sensor
DHT sensor library for ESPx
https://github.com/anklimov/Artnet.git
@@ -228,9 +232,11 @@ lib_deps =
SparkFun CCS811 Arduino Library@~1.0.7
Adafruit NeoPixel
https://github.com/anklimov/ArduinoOTA
https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit MCP23017 Arduino Library
Adafruit BusIO
https://github.com/arcao/Syslog.git
br3ttb/PID@^1.2.1
ArduinoMDNS
monitor_speed = 115200
@@ -280,8 +286,10 @@ lib_deps =
SparkFun CCS811 Arduino Library@~1.0.7
Adafruit NeoPixel
https://github.com/anklimov/ArduinoOTA
https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit MCP23017 Arduino Library
Adafruit BusIO
br3ttb/PID@^1.2.1
ArduinoMDNS
monitor_speed = 115200
[env:due]
@@ -338,8 +346,10 @@ lib_deps =
SparkFun CCS811 Arduino Library@~1.0.7
Adafruit NeoPixel
https://github.com/anklimov/ArduinoOTA
https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit MCP23017 Arduino Library
Adafruit BusIO
br3ttb/PID@^1.2.1
ArduinoMDNS
monitor_speed = 115200
[env:mega2560slim-5100]
@@ -384,8 +394,10 @@ lib_deps =
SparkFun CCS811 Arduino Library@~1.0.7
Adafruit NeoPixel
https://github.com/anklimov/ArduinoOTA
https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit MCP23017 Arduino Library
Adafruit BusIO
br3ttb/PID@^1.2.1
ArduinoMDNS
monitor_speed = 115200
@@ -403,7 +415,10 @@ board_build.f_cpu = 16000000L
; Comment out to enable LTO (this line unflags it)
;build_unflags = -flto - not working without LTO!
upload_protocol = arduino
;upload_protocol = arduino
upload_command = arduinoOTA -address 192.168.11.213 -port 65280 -username arduino -password password -b -upload /sketch -sketch $BUILD_DIR/${PROGNAME}.bin;sleep 5
upload_protocol = custom
board_upload.speed = ${env:fuses_bootloader.board_bootloader.speed}
framework = arduino
build_flags = !python get_build_flags.py mega2560slim2
@@ -444,8 +459,10 @@ lib_deps =
SparkFun CCS811 Arduino Library@~1.0.7
Adafruit NeoPixel
https://github.com/anklimov/ArduinoOTA
https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit MCP23017 Arduino Library
Adafruit BusIO
br3ttb/PID@^1.2.1
ArduinoMDNS
monitor_speed = 115200
@@ -491,8 +508,10 @@ lib_deps =
SparkFun CCS811 Arduino Library@~1.0.7
Adafruit NeoPixel
https://github.com/anklimov/ArduinoOTA
https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit MCP23017 Arduino Library
Adafruit BusIO
br3ttb/PID@^1.2.1
ArduinoMDNS
monitor_speed = 115200
[env:esp8266-wifi]
@@ -554,8 +573,10 @@ lib_deps =
SparkFun CCS811 Arduino Library@~1.0.7
Adafruit NeoPixel
https://github.com/anklimov/ArduinoOTA.git
https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit MCP23017 Arduino Library
Adafruit BusIO
br3ttb/PID@^1.2.1
;ArduinoMDNS
monitor_speed = 115200
[env:mega2560-5100]
@@ -602,8 +623,10 @@ lib_deps =
SparkFun CCS811 Arduino Library@~1.0.7
Adafruit NeoPixel
https://github.com/anklimov/ArduinoOTA
https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit MCP23017 Arduino Library
Adafruit BusIO
br3ttb/PID@^1.2.1
ArduinoMDNS
monitor_speed = 115200
[env:due-5500]
@@ -653,8 +676,10 @@ lib_deps =
SparkFun CCS811 Arduino Library@~1.0.7
Adafruit NeoPixel
https://github.com/anklimov/ArduinoOTA
https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit MCP23017 Arduino Library
Adafruit BusIO
br3ttb/PID@^1.2.1
ArduinoMDNS
monitor_speed = 115200
[env:lighthub21]
@@ -663,8 +688,8 @@ framework = arduino
board = due
monitor_baud = 115200
build_flags = !python get_build_flags.py lighthub21
;upload_command = arduinoOTA -address 192.168.11.172 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE ;sleep 4
;upload_command = arduinoOTA -address 192.168.88.45 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE;sleep 4
;upload_command = arduinoOTA -address 192.168.11.172 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE ;sleep 6
;upload_command = arduinoOTA -address 192.168.88.45 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE;sleep 6
;upload_protocol = custom
lib_ignore =
;DS2482_OneWire //UNCOMMENT for software 1-wire driver
@@ -708,8 +733,10 @@ lib_deps =
SparkFun CCS811 Arduino Library@~1.0.7
Adafruit NeoPixel
https://github.com/anklimov/ArduinoOTA
https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit MCP23017 Arduino Library
Adafruit BusIO
br3ttb/PID@^1.2.1
ArduinoMDNS
monitor_speed = 115200
[env:controllino]
@@ -754,9 +781,11 @@ lib_deps =
SparkFun CCS811 Arduino Library@~1.0.7
Adafruit NeoPixel
https://github.com/anklimov/ArduinoOTA
https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit MCP23017 Arduino Library
Adafruit BusIO
https://github.com/arcao/Syslog.git
br3ttb/PID@^1.2.1
ArduinoMDNS
monitor_speed = 115200
[env:stm32-enc2860]
@@ -807,9 +836,11 @@ lib_deps =
UIPEthernet
https://github.com/anklimov/NRFFlashStorage
Adafruit NeoPixel
https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit MCP23017 Arduino Library
Adafruit BusIO
SPI
br3ttb/PID@^1.2.1
ArduinoMDNS
monitor_speed = 115200
; Run the following command to upload with this environment