From 8c957321643c9524b4954ff1e26be1591936d0e9 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Sun, 31 May 2020 18:14:10 +0300 Subject: [PATCH] sysLog fixed, added logging library (for serial & syslog) --- build-flags/build_flags_due | 7 ++- build-flags/build_flags_lighthub21 | 3 +- lighthub/inputs.cpp | 1 + lighthub/main.cpp | 37 +++++++++------ lighthub/main.h | 5 +- lighthub/modules/in_ccs811_hdc1080.cpp | 1 + lighthub/modules/out_ac.cpp | 1 + lighthub/modules/out_modbus.cpp | 1 + lighthub/modules/out_motor.cpp | 1 + lighthub/modules/out_spiled.cpp | 1 + lighthub/options.h | 12 +++-- lighthub/owTerm.cpp | 1 + lighthub/streamlog.cpp | 63 ++++++++++++++++++++++++++ lighthub/streamlog.h | 41 +++++++++++++++++ lighthub/utils.cpp | 1 + platformio.ini | 11 ++++- 16 files changed, 161 insertions(+), 26 deletions(-) create mode 100644 lighthub/streamlog.cpp create mode 100644 lighthub/streamlog.h diff --git a/build-flags/build_flags_due b/build-flags/build_flags_due index 5670e22..ee3d1d3 100644 --- a/build-flags/build_flags_due +++ b/build-flags/build_flags_due @@ -1,7 +1,6 @@ --DW5500_CS_PIN=53 +#-DW5500_CS_PIN=53 -DDMX_SMOOTH -#-DSYSLOG_ENABLE +-DSYSLOG_ENABLE -DMODBUS_DIMMER_PARAM=SERIAL_8E1 -#-DARTNET_ENABLE +-DARTNET_ENABLE -DOTA -#-DWiz5500 diff --git a/build-flags/build_flags_lighthub21 b/build-flags/build_flags_lighthub21 index f644898..d28e605 100644 --- a/build-flags/build_flags_lighthub21 +++ b/build-flags/build_flags_lighthub21 @@ -1,6 +1,7 @@ --DWiz5500 -DW5500_CS_PIN=53 -DARTNET_ENABLE -DDMX_SMOOTH -DMODBUS_DIMMER_PARAM=SERIAL_8N1 -DMODBUS_SERIAL_BAUD=9600 +-DOTA +-DSYSLOG_ENABLE diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index e87d5ee..07b2fb6 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -22,6 +22,7 @@ e-mail anklimov@gmail.com #include "item.h" #include "utils.h" #include +#include "main.h" #ifndef DHT_DISABLE #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) diff --git a/lighthub/main.cpp b/lighthub/main.cpp index 41d4a29..f6b40e6 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -66,6 +66,7 @@ PWM Out */ #include "main.h" + #include #if defined(OTA) #include @@ -109,13 +110,27 @@ NRFFlashStorage EEPROM; EthernetClient ethClient; #endif + + + #ifdef SYSLOG_ENABLE #include EthernetUDP udpSyslogClient; -Syslog udpSyslog(udpSyslogClient, SYSLOG_PROTO_IETF); +Syslog udpSyslog(udpSyslogClient, SYSLOG_PROTO_BSD); unsigned long nextSyslogPingTime; + +Streamlog debugSerial(&debugSerialPort,LOG_DEBUG,&udpSyslog); +Streamlog errorSerial(&debugSerialPort,LOG_ERROR,&udpSyslog); +Streamlog infoSerial (&debugSerialPort,LOG_INFO,&udpSyslog); +#else +Streamlog debugSerial(&debugSerialPort,LOG_DEBUG); +Streamlog errorSerial(&debugSerialPort,LOG_ERROR); +Streamlog infoSerial (&debugSerialPort,LOG_INFO); #endif + + + lan_status lanStatus = INITIAL_STATE; @@ -598,8 +613,9 @@ void ip_ready_config_loaded_connecting_to_broker() { debugSerial<1) syslogPort = aJson.getArrayItem(udpSyslogArr, 1)->valueint; @@ -620,7 +636,7 @@ void ip_ready_config_loaded_connecting_to_broker() { #endif if (!mqttClient.connected() && mqttArr && ((n = aJson.getArraySize(mqttArr)) > 1)) { - // char *client_id = aJson.getArrayItem(mqttArr, 0)->valuestring; + // char *client_id = aJson.getArrayItemartnet(mqttArr, 0)->valuestring; char *servername = getStringFromConfig(mqttArr, 1); if (n >= 3) port = aJson.getArrayItem(mqttArr, 2)->valueint; if (n >= 4) user = getStringFromConfig(mqttArr, 3); @@ -1231,7 +1247,7 @@ void cmdFunctionClearEEPROM(int arg_cnt, char **args){ for (int i = OFFSET_MAC; i < OFFSET_MAC+EEPROM_FIX_PART_LEN; i++) EEPROM.write(i, 0); - for (int i = 0; i < sizeof(EEPROM_signature); i++) + for (int i = 0; i < EEPROM_SIGNATURE_LENGTH; i++) EEPROM.write(i+OFFSET_SIGNATURE,EEPROM_signature[i]); debugSerial< #endif +#include "streamlog.h" +extern Streamlog debugSerial; + #if defined(__SAM3X8E__) #define wdt_res() watchdogReset() #define wdt_en() @@ -186,7 +189,7 @@ typedef union { uint8_t UID_Byte[20]; } UID; -bool isNotRetainingStatus(); +bool isNotRetainingStatus(); //void watchdogSetup(void); void mqttCallback(char *topic, byte *payload, unsigned int length); diff --git a/lighthub/modules/in_ccs811_hdc1080.cpp b/lighthub/modules/in_ccs811_hdc1080.cpp index 57f81db..8a36e22 100644 --- a/lighthub/modules/in_ccs811_hdc1080.cpp +++ b/lighthub/modules/in_ccs811_hdc1080.cpp @@ -3,6 +3,7 @@ #include "options.h" #include "Streaming.h" #include "item.h" +#include "main.h" #if defined(M5STACK) #include diff --git a/lighthub/modules/out_ac.cpp b/lighthub/modules/out_ac.cpp index fc2456f..4e4f161 100644 --- a/lighthub/modules/out_ac.cpp +++ b/lighthub/modules/out_ac.cpp @@ -6,6 +6,7 @@ #include "Streaming.h" #include "item.h" #include "textconst.h" +#include "main.h" #ifndef AC_Serial #define AC_Serial Serial3 diff --git a/lighthub/modules/out_modbus.cpp b/lighthub/modules/out_modbus.cpp index a78fa34..b84bc55 100644 --- a/lighthub/modules/out_modbus.cpp +++ b/lighthub/modules/out_modbus.cpp @@ -8,6 +8,7 @@ #include "item.h" #include +#include "main.h" extern aJsonObject *modbusObj; extern ModbusMaster node; diff --git a/lighthub/modules/out_motor.cpp b/lighthub/modules/out_motor.cpp index bf6f5c0..6394994 100644 --- a/lighthub/modules/out_motor.cpp +++ b/lighthub/modules/out_motor.cpp @@ -6,6 +6,7 @@ #include "Streaming.h" #include "item.h" +#include "main.h" static int driverStatus = CST_UNKNOWN; diff --git a/lighthub/modules/out_spiled.cpp b/lighthub/modules/out_spiled.cpp index df6e9df..3dba380 100644 --- a/lighthub/modules/out_spiled.cpp +++ b/lighthub/modules/out_spiled.cpp @@ -6,6 +6,7 @@ #include "Streaming.h" #include "item.h" +#include "main.h" #ifdef ADAFRUIT_LED diff --git a/lighthub/options.h b/lighthub/options.h index 5c15756..17225b8 100644 --- a/lighthub/options.h +++ b/lighthub/options.h @@ -1,4 +1,5 @@ // Configuration of drivers enabled +#define SYSLOG_LOCAL_SOCKET 514 #ifndef FASTLED #define ADAFRUIT_LED @@ -39,6 +40,8 @@ #define MAXFLASHSTR 32 #define PWDFLASHSTR 16 +#define EEPROM_SIGNATURE "LHCF" +#define EEPROM_SIGNATURE_LENGTH 4 #define OFFSET_MAC 0 #define OFFSET_IP OFFSET_MAC+6 @@ -48,10 +51,10 @@ #define OFFSET_CONFIGSERVER OFFSET_MASK+4 #define OFFSET_MQTT_PWD OFFSET_CONFIGSERVER+MAXFLASHSTR #define OFFSET_SIGNATURE OFFSET_MQTT_PWD+PWDFLASHSTR -#define EEPROM_offset_NotAlligned OFFSET_SIGNATURE+4 +#define EEPROM_offset_NotAlligned OFFSET_SIGNATURE+EEPROM_SIGNATURE_LENGTH #define EEPROM_offsetJSON EEPROM_offset_NotAlligned + (4 -(EEPROM_offset_NotAlligned & 3)) #define EEPROM_FIX_PART_LEN EEPROM_offsetJSON-OFFSET_MAC -#define EEPROM_SIGNATURE "LHCF" + #ifndef INTERVAL_CHECK_INPUT #define INTERVAL_CHECK_INPUT 15 @@ -218,10 +221,11 @@ //#define debugSerial M5.Lcd //#endif -#ifndef debugSerial -#define debugSerial Serial +#ifndef debugSerialPort +#define debugSerialPort Serial #endif + #ifndef Wiz5500 #define W5100_ETHERNET_SHIELD #else diff --git a/lighthub/owTerm.cpp b/lighthub/owTerm.cpp index 1499993..e47f235 100644 --- a/lighthub/owTerm.cpp +++ b/lighthub/owTerm.cpp @@ -24,6 +24,7 @@ e-mail anklimov@gmail.com #include #include "utils.h" #include "options.h" +#include "main.h" OneWire *oneWire = NULL; diff --git a/lighthub/streamlog.cpp b/lighthub/streamlog.cpp new file mode 100644 index 0000000..6a21f4e --- /dev/null +++ b/lighthub/streamlog.cpp @@ -0,0 +1,63 @@ +#include "streamlog.h" +#include + +Streamlog::Streamlog (UARTClass * _serialPort, int _severity , Syslog * _syslog ) +{ + serialPort=_serialPort; + severity=_severity; + syslog=_syslog; +} + +void Streamlog::begin(unsigned long speed) +{ + if (serialPort) serialPort->begin(speed); +}; + +void Streamlog::end() +{ + if (serialPort) serialPort->end(); +}; + +int Streamlog::available(void) +{ + if (serialPort) return serialPort->available(); + return 0; +}; + +int Streamlog::peek(void) +{ + if (serialPort) return serialPort->peek(); + return 0; +}; + +int Streamlog::read(void) +{ + if (serialPort) return serialPort->read(); + return 0; +}; + + +void Streamlog::flush(void) +{ + if (serialPort) serialPort->flush(); + +}; + +size_t Streamlog::write(uint8_t ch) +{ +#ifdef SYSLOG_ENABLE + if (ch=='\n') + { + logBuffer[logBufferPos]=0; + if (syslog) syslog->log(severity,(char *)logBuffer); + logBufferPos=0; + } + else + { + if (logBufferPoswrite(ch); + + return 1; +}; diff --git a/lighthub/streamlog.h b/lighthub/streamlog.h new file mode 100644 index 0000000..f9080f9 --- /dev/null +++ b/lighthub/streamlog.h @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +#define LOG_DEBUG 7 +#define LOG_INFO 6 +#define LOG_ERROR 3 + +#ifndef LOGBUFFER_SIZE +#define LOGBUFFER_SIZE 80 +#endif + +static uint8_t serialDebugLevel = 7; +static uint8_t udpDebugLevel =7; + +#ifdef SYSLOG_ENABLE +static char logBuffer[LOGBUFFER_SIZE]; +static int logBufferPos=0; +#endif + +class Streamlog : public Print +{ + public: + Streamlog (UARTClass * _serialPort, int _severity = LOG_DEBUG, Syslog * _syslog = NULL); + // {serialPort=_serialPort;severity=_severity; syslog=_syslog; } + void begin(unsigned long speed); + void end() ; + + int available(void); + int peek(void); + int read(void); + void flush(void); + size_t write(uint8_t ch); + using Print::write; // pull in write(str) and write(buf, size) from Print + operator bool() {return true;}; + private: + uint16_t severity; + UARTClass *serialPort; + Syslog * syslog; +}; diff --git a/lighthub/utils.cpp b/lighthub/utils.cpp index fb1d2c3..713d5be 100644 --- a/lighthub/utils.cpp +++ b/lighthub/utils.cpp @@ -22,6 +22,7 @@ e-mail anklimov@gmail.com #include "options.h" #include "stdarg.h" #include +#include "main.h" #include "item.h" #include diff --git a/platformio.ini b/platformio.ini index bc006ab..0d9e6ca 100644 --- a/platformio.ini +++ b/platformio.ini @@ -264,6 +264,11 @@ platform = atmelsam framework = arduino board = due build_flags = !python get_build_flags.py due +; Need to place arduinoOTA utility from Arduino IDE distribution to folder in your PATH +;fix address and password +;upload_flags = +upload_command = arduinoOTA -address 192.168.88.21 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE +upload_protocol = custom lib_ignore = ;DS2482_OneWire //UNCOMMENT for software 1-wire driver DHT sensor library for ESPx @@ -546,6 +551,8 @@ platform = atmelsam framework = arduino board = due build_flags = !python get_build_flags.py lighthub21 +;upload_command = arduinoOTA -address 192.168.88.34 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE +;upload_protocol = custom lib_ignore = ;DS2482_OneWire //UNCOMMENT for software 1-wire driver DHT sensor library for ESPx @@ -555,7 +562,7 @@ lib_ignore = WifiManager DmxSimple httpClient - Ethernet + Ethernet2 Ethernet3 NRFFlashStorage WebServer @@ -573,7 +580,7 @@ lib_deps = https://github.com/anklimov/aJson https://github.com/anklimov/CmdArduino https://github.com/anklimov/ModbusMaster - https://github.com/anklimov/Ethernet2 + https://github.com/anklimov/Ethernet https://github.com/knolleary/pubsubclient.git https://github.com/anklimov/Artnet.git FastLED@3.3.2