mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
32 broken
This commit is contained in:
7
include/Clock.h
Normal file
7
include/Clock.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Global.h"
|
||||||
|
|
||||||
|
void startTimeSync();
|
||||||
|
|
||||||
|
void reconfigTime();
|
||||||
22
include/CommonTypes.h
Normal file
22
include/CommonTypes.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
enum ErrorType_t {
|
||||||
|
ET_NONE,
|
||||||
|
ET_FUNCTION,
|
||||||
|
ET_MODULE,
|
||||||
|
ET_SYSTEM
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ErrorLevel_t {
|
||||||
|
EL_NONE,
|
||||||
|
EL_INFO,
|
||||||
|
EL_WARNING,
|
||||||
|
EL_ERROR
|
||||||
|
};
|
||||||
|
|
||||||
|
enum LedStatus_t {
|
||||||
|
LED_OFF,
|
||||||
|
LED_ON,
|
||||||
|
LED_SLOW,
|
||||||
|
LED_FAST
|
||||||
|
};
|
||||||
@@ -2,19 +2,7 @@
|
|||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
enum ErrorType_t {
|
#include "CommonTypes.h"
|
||||||
ET_NONE,
|
|
||||||
ET_FUNCTION,
|
|
||||||
ET_MODULE,
|
|
||||||
ET_SYSTEM
|
|
||||||
};
|
|
||||||
|
|
||||||
enum ErrorLevel_t {
|
|
||||||
EL_NONE,
|
|
||||||
EL_INFO,
|
|
||||||
EL_WARNING,
|
|
||||||
EL_ERROR
|
|
||||||
};
|
|
||||||
|
|
||||||
String getErrorLevelStr(ErrorLevel_t level);
|
String getErrorLevelStr(ErrorLevel_t level);
|
||||||
|
|
||||||
|
|||||||
@@ -10,14 +10,18 @@
|
|||||||
#include "ESP8266.h"
|
#include "ESP8266.h"
|
||||||
//
|
//
|
||||||
#include "Consts.h"
|
#include "Consts.h"
|
||||||
|
#include "CommonTypes.h"
|
||||||
#include "Errors.h"
|
#include "Errors.h"
|
||||||
#include "GyverFilters.h"
|
#include "GyverFilters.h"
|
||||||
#include "UptimeInterval.h"
|
#include "UptimeInterval.h"
|
||||||
|
#include "Clock.h"
|
||||||
#include "Utils\FileUtils.h"
|
#include "Utils\FileUtils.h"
|
||||||
#include "Utils\JsonUtils.h"
|
#include "Utils\JsonUtils.h"
|
||||||
#include "Utils\StringUtils.h"
|
#include "Utils\StringUtils.h"
|
||||||
#include "Utils\SysUtils.h"
|
#include "Utils\SysUtils.h"
|
||||||
#include "Utils\TimeUtils.h"
|
#include "Utils\TimeUtils.h"
|
||||||
|
#include "Utils\PrintMessage.h"
|
||||||
|
#include "Utils\WiFiUtils.h"
|
||||||
|
|
||||||
//=========ПОДКЛЮЧЕНИЕ ОБЩИХ БИБЛИОТЕК===============
|
//=========ПОДКЛЮЧЕНИЕ ОБЩИХ БИБЛИОТЕК===============
|
||||||
#include <Adafruit_BME280.h>
|
#include <Adafruit_BME280.h>
|
||||||
@@ -60,7 +64,7 @@ extern DallasTemperature sensors;
|
|||||||
* Global vars
|
* Global vars
|
||||||
*/
|
*/
|
||||||
|
|
||||||
enum { ROUTER_SEARCHING,
|
enum { WIFI_SCAN,
|
||||||
WIFI_MQTT_CONNECTION_CHECK,
|
WIFI_MQTT_CONNECTION_CHECK,
|
||||||
SENSORS,
|
SENSORS,
|
||||||
STEPPER1,
|
STEPPER1,
|
||||||
@@ -188,7 +192,7 @@ extern String getURL(const String &urls);
|
|||||||
|
|
||||||
extern void servo_();
|
extern void servo_();
|
||||||
|
|
||||||
extern void led_blink(String satus);
|
extern void setLedStatus(LedStatus_t);
|
||||||
|
|
||||||
// Mqtt
|
// Mqtt
|
||||||
extern void MQTT_init();
|
extern void MQTT_init();
|
||||||
@@ -200,12 +204,6 @@ extern void sendCONTROL(String id, String topik, String state);
|
|||||||
extern void do_mqtt_connection();
|
extern void do_mqtt_connection();
|
||||||
extern void handleMQTT();
|
extern void handleMQTT();
|
||||||
|
|
||||||
// WiFiUtils
|
|
||||||
extern void WIFI_init();
|
|
||||||
extern void All_init();
|
|
||||||
extern bool StartAPMode();
|
|
||||||
extern void ROUTER_Connecting();
|
|
||||||
|
|
||||||
//Scenario
|
//Scenario
|
||||||
extern void eventGen(String event_name, String number);
|
extern void eventGen(String event_name, String number);
|
||||||
extern String add_set(String param_name);
|
extern String add_set(String param_name);
|
||||||
@@ -259,7 +257,7 @@ extern void delTimer(String number);
|
|||||||
extern int readTimer(int number);
|
extern int readTimer(int number);
|
||||||
|
|
||||||
//Upgrade
|
//Upgrade
|
||||||
extern void initUpgrade();
|
extern void init_updater();
|
||||||
|
|
||||||
// widget
|
// widget
|
||||||
extern void createWidget(String widget_name, String page_name, String page_number, String file, String topic);
|
extern void createWidget(String widget_name, String page_name, String page_number, String file, String topic);
|
||||||
@@ -290,4 +288,6 @@ extern void do_upgrade();
|
|||||||
extern void uptime_init();
|
extern void uptime_init();
|
||||||
|
|
||||||
// Web
|
// Web
|
||||||
extern void web_init();
|
extern void web_init();
|
||||||
|
|
||||||
|
extern void telemetry_init();
|
||||||
72
include/Module/CharBuffer.h
Normal file
72
include/Module/CharBuffer.h
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
class CharBuffer : Print {
|
||||||
|
public:
|
||||||
|
CharBuffer(size_t size) : _capacity(size < 2 ? 2 : size), _write(0), _read(0) {
|
||||||
|
_pool = new char[_capacity + 1];
|
||||||
|
memset(_pool, 0, _capacity + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
CharBuffer(const CharBuffer &src) {
|
||||||
|
_capacity = src._capacity;
|
||||||
|
_write = src._write;
|
||||||
|
memcpy(_pool, src._pool, src._write);
|
||||||
|
}
|
||||||
|
|
||||||
|
CharBuffer(const char *str) : CharBuffer(strlen(str) + 1) {
|
||||||
|
write((const uint8_t *)str, strlen(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
~CharBuffer() {
|
||||||
|
delete _pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear() {
|
||||||
|
memset(_pool, 0, _capacity);
|
||||||
|
_write = 0;
|
||||||
|
_read = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t size() const { return _capacity; }
|
||||||
|
|
||||||
|
size_t free() const { return _capacity - _write - 2; }
|
||||||
|
|
||||||
|
size_t available() const { return _write; }
|
||||||
|
|
||||||
|
const char *c_str() {
|
||||||
|
if (_pool[_write] != '\x00')
|
||||||
|
_pool[_write] = '\x00';
|
||||||
|
return _pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t write(char ch) {
|
||||||
|
return write((uint8_t)ch);
|
||||||
|
};
|
||||||
|
|
||||||
|
size_t write(const uint8_t ch) {
|
||||||
|
size_t n = 0;
|
||||||
|
if (_write < (_capacity - 2)) {
|
||||||
|
_pool[_write++] = ch;
|
||||||
|
n = 1;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t write(const uint8_t *ptr, const size_t size) {
|
||||||
|
size_t n = 0;
|
||||||
|
while (n < size) {
|
||||||
|
uint8_t ch = ptr[n++];
|
||||||
|
if (!write(ch))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
char *_pool;
|
||||||
|
size_t _capacity;
|
||||||
|
size_t _write;
|
||||||
|
size_t _read;
|
||||||
|
};
|
||||||
86
include/Module/CircularBuffer.h
Normal file
86
include/Module/CircularBuffer.h
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
template <typename T, size_t BUFFER_SIZE>
|
||||||
|
class CircularBuffer {
|
||||||
|
public:
|
||||||
|
CircularBuffer() : _head{0}, _tail{0}, _full{false} {}
|
||||||
|
|
||||||
|
~CircularBuffer() {}
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
_head = _tail = _full = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool empty() const {
|
||||||
|
return _head == _tail && !_full;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool full() const {
|
||||||
|
return _full;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t size() const {
|
||||||
|
size_t res = 0;
|
||||||
|
if (!_full) {
|
||||||
|
if (_head < _tail)
|
||||||
|
res = BUFFER_SIZE + _head - _tail;
|
||||||
|
else
|
||||||
|
res = _head - _tail;
|
||||||
|
} else {
|
||||||
|
res = BUFFER_SIZE;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void push(const T &item) {
|
||||||
|
if (_full) {
|
||||||
|
_tail++;
|
||||||
|
if (_tail == BUFFER_SIZE)
|
||||||
|
_tail = 0;
|
||||||
|
}
|
||||||
|
_pool[_head++] = item;
|
||||||
|
if (_head == BUFFER_SIZE)
|
||||||
|
_head = 0;
|
||||||
|
if (_head == _tail)
|
||||||
|
_full = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool pop(T &item) {
|
||||||
|
bool res = false;
|
||||||
|
if (!empty()) {
|
||||||
|
item = _pool[_tail++];
|
||||||
|
if (_tail == BUFFER_SIZE) _tail = 0;
|
||||||
|
_full = false;
|
||||||
|
res = true;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool pop_back(T &item) {
|
||||||
|
bool res = false;
|
||||||
|
if (!empty()) {
|
||||||
|
item = _pool[--_head];
|
||||||
|
_full = false;
|
||||||
|
res = true;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool peek(T &item) const {
|
||||||
|
bool res = false;
|
||||||
|
if (!empty()) {
|
||||||
|
item = _pool[_tail];
|
||||||
|
|
||||||
|
res = true;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
T _pool[BUFFER_SIZE];
|
||||||
|
size_t _head;
|
||||||
|
size_t _tail;
|
||||||
|
bool _full;
|
||||||
|
};
|
||||||
45
include/Module/CommandShell.h
Normal file
45
include/Module/CommandShell.h
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#include "Module/Terminal.h"
|
||||||
|
#include "Module/CircularBuffer.h"
|
||||||
|
#include "Module/Runner.h"
|
||||||
|
|
||||||
|
class CommandShell {
|
||||||
|
public:
|
||||||
|
CommandShell(Runner *runner);
|
||||||
|
|
||||||
|
void setTerm(Terminal *term);
|
||||||
|
Terminal *term();
|
||||||
|
|
||||||
|
void showGreetings(bool = true);
|
||||||
|
void showFarewell(bool = true);
|
||||||
|
|
||||||
|
void clearHistory();
|
||||||
|
void addHistory(const char *);
|
||||||
|
bool getHistoryInput(String &);
|
||||||
|
void setEditLine(const String &);
|
||||||
|
bool active();
|
||||||
|
void loop();
|
||||||
|
|
||||||
|
private:
|
||||||
|
size_t printGreetings(Print *);
|
||||||
|
size_t printFarewell(Print *);
|
||||||
|
size_t printPrompt(Print *);
|
||||||
|
|
||||||
|
void onOpen(Print *out);
|
||||||
|
void onClose(Print *out);
|
||||||
|
void onData(const char *);
|
||||||
|
void onHistory(Print *out);
|
||||||
|
bool getLastInput(String &);
|
||||||
|
|
||||||
|
private:
|
||||||
|
CircularBuffer<String, 4> _history;
|
||||||
|
Terminal *_term;
|
||||||
|
Runner *_runner;
|
||||||
|
String _path;
|
||||||
|
bool _active;
|
||||||
|
bool _greetings;
|
||||||
|
bool _farewell;
|
||||||
|
};
|
||||||
68
include/Module/EditLine.h
Normal file
68
include/Module/EditLine.h
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Module/CharBuffer.h"
|
||||||
|
|
||||||
|
class EditLine : public CharBuffer {
|
||||||
|
public:
|
||||||
|
EditLine(size_t size) : CharBuffer(size){};
|
||||||
|
|
||||||
|
char &operator[](size_t i) { return _pool[i]; }
|
||||||
|
|
||||||
|
char operator[](size_t i) const { return _pool[i]; }
|
||||||
|
|
||||||
|
EditLine &operator=(const EditLine &src) {
|
||||||
|
delete[] _pool;
|
||||||
|
_pool = new char[src._capacity];
|
||||||
|
memcpy(_pool, src._pool, src._capacity);
|
||||||
|
_read = src._read;
|
||||||
|
_write = src._write;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void del() {
|
||||||
|
size_t i;
|
||||||
|
for (i = _write; i < _capacity; ++i)
|
||||||
|
_pool[i] = _pool[i + 1];
|
||||||
|
_pool[i] = '\x00';
|
||||||
|
}
|
||||||
|
|
||||||
|
bool backspace() {
|
||||||
|
bool res = false;
|
||||||
|
if (prev()) {
|
||||||
|
del();
|
||||||
|
res = true;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool next() {
|
||||||
|
bool res = false;
|
||||||
|
if (_write < _capacity - 1) {
|
||||||
|
_write++;
|
||||||
|
res = true;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool prev() {
|
||||||
|
bool res = false;
|
||||||
|
if (_write > 0) {
|
||||||
|
_write--;
|
||||||
|
res = true;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t home() {
|
||||||
|
size_t res = _write;
|
||||||
|
_write = 0;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t end() {
|
||||||
|
size_t n;
|
||||||
|
for (n = 0; n < _capacity - 1; ++n)
|
||||||
|
if (_pool[n] == '\x00') break;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
};
|
||||||
84
include/Module/Module.h
Normal file
84
include/Module/Module.h
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
enum ModuleState_t {
|
||||||
|
MOD_INIT,
|
||||||
|
MOD_INIT_FAILED,
|
||||||
|
MOD_INIT_COMPLETE,
|
||||||
|
MOD_START_FAILED,
|
||||||
|
MOD_ACTIVE
|
||||||
|
};
|
||||||
|
|
||||||
|
class Module {
|
||||||
|
protected:
|
||||||
|
virtual bool onInit() { return true; };
|
||||||
|
virtual void onEnd(){};
|
||||||
|
virtual bool onStart() { return true; }
|
||||||
|
virtual void onStop(){};
|
||||||
|
virtual void onLoop() = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Print *_out;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Module() : _state{MOD_INIT} {}
|
||||||
|
|
||||||
|
bool init(bool force = false) {
|
||||||
|
if (_state > MOD_INIT_COMPLETE) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (_state == MOD_INIT_FAILED && !force) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_state = onInit() ? MOD_INIT_COMPLETE : MOD_INIT_FAILED;
|
||||||
|
|
||||||
|
return _state == MOD_INIT_COMPLETE;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool start(bool force = false) {
|
||||||
|
if (_state == MOD_ACTIVE) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (_state == MOD_START_FAILED && !force) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (_state < MOD_INIT_COMPLETE) {
|
||||||
|
if (!init(force)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_state = onStart() ? MOD_ACTIVE : MOD_START_FAILED;
|
||||||
|
return _state == MOD_ACTIVE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void stop() {
|
||||||
|
if (_state < MOD_ACTIVE) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
onStop();
|
||||||
|
_state = MOD_INIT_COMPLETE;
|
||||||
|
};
|
||||||
|
|
||||||
|
void end() {
|
||||||
|
if (_state < MOD_INIT_FAILED) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
onEnd();
|
||||||
|
_state = MOD_INIT;
|
||||||
|
};
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
if (_state == MOD_ACTIVE || start()) onLoop();
|
||||||
|
};
|
||||||
|
|
||||||
|
void setOutput(Print *p) { _out = p; }
|
||||||
|
|
||||||
|
ModuleState_t getState() {
|
||||||
|
return _state;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
ModuleState_t _state;
|
||||||
|
};
|
||||||
8
include/Module/Runner.h
Normal file
8
include/Module/Runner.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Print.h>
|
||||||
|
|
||||||
|
class Runner {
|
||||||
|
public:
|
||||||
|
virtual void run(const char*, Print*);
|
||||||
|
};
|
||||||
50
include/Module/Telnet.h
Normal file
50
include/Module/Telnet.h
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Global.h"
|
||||||
|
|
||||||
|
#include "Module/Module.h"
|
||||||
|
#include "Module/Terminal.h"
|
||||||
|
#include "Module/CommandShell.h"
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
enum TelnetEvent_t {
|
||||||
|
TE_CONNECTED,
|
||||||
|
TE_DISCONNECTED
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef std::function<void(TelnetEvent_t, WiFiClient*)> TelnetEventHandler;
|
||||||
|
|
||||||
|
class Telnet : public Module {
|
||||||
|
public:
|
||||||
|
Telnet(uint16_t port) : _port{port}, _lastConnected{false} {};
|
||||||
|
|
||||||
|
public:
|
||||||
|
void setEventHandler(TelnetEventHandler);
|
||||||
|
void sendData(const String&);
|
||||||
|
bool hasClient();
|
||||||
|
bool isShellActive();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool onInit() override;
|
||||||
|
void onEnd() override;
|
||||||
|
bool onStart() override;
|
||||||
|
void onStop() override;
|
||||||
|
void onLoop() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void onConnect();
|
||||||
|
void onDisconnect();
|
||||||
|
void onData();
|
||||||
|
void onOpen();
|
||||||
|
void onClose();
|
||||||
|
|
||||||
|
private:
|
||||||
|
TelnetEventHandler _eventHandler;
|
||||||
|
uint16_t _port;
|
||||||
|
bool _lastConnected;
|
||||||
|
WiFiClient _client;
|
||||||
|
WiFiServer* _server;
|
||||||
|
Terminal* _term;
|
||||||
|
CommandShell* _shell;
|
||||||
|
};
|
||||||
183
include/Module/Terminal.h
Normal file
183
include/Module/Terminal.h
Normal file
@@ -0,0 +1,183 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Module/EditLine.h"
|
||||||
|
|
||||||
|
#define A_NORMAL 0x0000 // normal
|
||||||
|
#define A_UNDERLINE 0x0001 // underline
|
||||||
|
#define A_REVERSE 0x0002 // reverse
|
||||||
|
#define A_BLINK 0x0004 // blink
|
||||||
|
#define A_BOLD 0x0008 // bold
|
||||||
|
#define A_DIM 0x0010 // dim
|
||||||
|
#define A_STANDOUT A_BOLD // standout (same as bold)
|
||||||
|
|
||||||
|
#define F_BLACK 0x0100 // foreground black
|
||||||
|
#define F_RED 0x0200 // foreground red
|
||||||
|
#define F_GREEN 0x0300 // foreground green
|
||||||
|
#define F_BROWN 0x0400 // foreground brown
|
||||||
|
#define F_BLUE 0x0500 // foreground blue
|
||||||
|
#define F_MAGENTA 0x0600 // foreground magenta
|
||||||
|
#define F_CYAN 0x0700 // foreground cyan
|
||||||
|
#define F_WHITE 0x0800 // foreground white
|
||||||
|
#define F_YELLOW F_BROWN // some terminals show brown as yellow (with A_BOLD)
|
||||||
|
#define F_COLOR 0x0F00 // foreground mask
|
||||||
|
|
||||||
|
#define B_BLACK 0x1000 // background black
|
||||||
|
#define B_RED 0x2000 // background red
|
||||||
|
#define B_GREEN 0x3000 // background green
|
||||||
|
#define B_BROWN 0x4000 // background brown
|
||||||
|
#define B_BLUE 0x5000 // background blue
|
||||||
|
#define B_MAGENTA 0x6000 // background magenta
|
||||||
|
#define B_CYAN 0x7000 // background cyan
|
||||||
|
#define B_WHITE 0x8000 // background white
|
||||||
|
#define B_YELLOW B_BROWN // some terminals show brown as yellow (with A_BOLD)
|
||||||
|
#define B_COLOR 0xF000 // background mask
|
||||||
|
|
||||||
|
#define CHAR_NULL 0x00
|
||||||
|
#define CHAR_BEL 0x07
|
||||||
|
#define CHAR_BS 0x08
|
||||||
|
#define CHAR_SPACE 0x20
|
||||||
|
#define CHAR_TAB 0x09
|
||||||
|
#define CHAR_LF 0x0a
|
||||||
|
#define CHAR_CR 0x0d
|
||||||
|
#define CHR_ZERO 0x30
|
||||||
|
|
||||||
|
#define KEY_DEL 0x7f
|
||||||
|
#define KEY_DOWN 0x80
|
||||||
|
#define KEY_UP 0x81
|
||||||
|
#define KEY_LEFT 0x82
|
||||||
|
#define KEY_RIGHT 0x83
|
||||||
|
#define KEY_HOME 0x84
|
||||||
|
#define KEY_INS 0x86
|
||||||
|
#define KEY_PAGE_DOWN 0x87
|
||||||
|
#define KEY_PAGE_UP 0x88
|
||||||
|
#define KEY_END 0x89
|
||||||
|
#define CHAR_LT 0x8b
|
||||||
|
#define CHAR_CSI 0x9b
|
||||||
|
#define CHAR_ESC 0x1b
|
||||||
|
#define CHAR_BIN 0xFF
|
||||||
|
|
||||||
|
#define ESC_CURSOR_HOME "\x1b[H"
|
||||||
|
#define ESC_SAVE_CURSOR "\x1b[s"
|
||||||
|
#define ESC_UNSAVE_CURSOR "\x1b[u"
|
||||||
|
#define ESC_SAVE_CURSOR_AND_ATTRS "\x1b[7"
|
||||||
|
#define ESC_RESTORE_CURSOR_AND_ATTRS "\x1b[8"
|
||||||
|
|
||||||
|
#define ESC_CLEAR "\x1b[2J"
|
||||||
|
#define ESC_CLEAR_BOTTOM "\x1b[J"
|
||||||
|
#define ESC_CLEAR_EOL "\x1b[0K"
|
||||||
|
|
||||||
|
#define ESC_CURSOR_UP "\x1b[1A"
|
||||||
|
#define ESC_CURSOR_DOWN "\x1b[1B"
|
||||||
|
#define ESC_CURSOR_FORWARD "\x1b[1C"
|
||||||
|
#define ESC_CURSOR_BACKWARD "\x1b[1D"
|
||||||
|
|
||||||
|
#define SEQ_CSI PSTR("\033[") // code introducer
|
||||||
|
#define SEQ_LOAD_G1 PSTR("\033)0") // load G1 character set
|
||||||
|
#define SEQ_CLEAR PSTR("\033[2J") // clear screen
|
||||||
|
#define SEQ_ATTRSET PSTR("\033[0") // set attributes, e.g. "\033[0;7;1m"
|
||||||
|
|
||||||
|
#define SEQ_ATTRSET_BOLD PSTR(";1") // bold
|
||||||
|
#define SEQ_ATTRSET_DIM PSTR(";2") // dim
|
||||||
|
#define SEQ_ATTRSET_FCOLOR PSTR(";3") // forground color
|
||||||
|
#define SEQ_ATTRSET_UNDERLINE PSTR(";4") // underline
|
||||||
|
#define SEQ_ATTRSET_BCOLOR PSTR(";4") // background color
|
||||||
|
#define SEQ_ATTRSET_BLINK PSTR(";5") // blink
|
||||||
|
#define SEQ_ATTRSET_REVERSE PSTR(";7") // reverse
|
||||||
|
|
||||||
|
enum TerminalEventEnum {
|
||||||
|
EVENT_OPEN,
|
||||||
|
EVENT_CLOSE,
|
||||||
|
EVENT_TAB
|
||||||
|
};
|
||||||
|
|
||||||
|
enum SpecialKeyEnum { SPEC_KEY_UP,
|
||||||
|
SPEC_KEY_TAB,
|
||||||
|
SPEC_KEY_ENTER,
|
||||||
|
SPEC_KEY_ESC };
|
||||||
|
|
||||||
|
typedef std::function<bool(SpecialKeyEnum key)> SpecialKeyPressedEvent;
|
||||||
|
|
||||||
|
typedef std::function<void(TerminalEventEnum, Stream *)> TerminalEventHandler;
|
||||||
|
|
||||||
|
typedef std::function<void(const char *)> TerminalInputEventHandler;
|
||||||
|
|
||||||
|
enum EOLType_t { CRLF,
|
||||||
|
LFCR,
|
||||||
|
LF,
|
||||||
|
CR };
|
||||||
|
|
||||||
|
enum State { ST_INACTIVE,
|
||||||
|
ST_NORMAL,
|
||||||
|
ST_ESC_SEQ,
|
||||||
|
ST_CTRL_SEQ };
|
||||||
|
|
||||||
|
class Terminal : public Print {
|
||||||
|
public:
|
||||||
|
Terminal(Stream *stream = nullptr);
|
||||||
|
~Terminal();
|
||||||
|
void setStream(Stream *stream);
|
||||||
|
void setEOL(EOLType_t code);
|
||||||
|
void enableControlCodes(bool enabled = true);
|
||||||
|
void enableEcho(bool enabled = true);
|
||||||
|
void enableColors(bool enabled = true);
|
||||||
|
void setOnEvent(TerminalEventHandler);
|
||||||
|
void setOnSpecKeyPress(SpecialKeyPressedEvent);
|
||||||
|
void setOnReadLine(TerminalInputEventHandler);
|
||||||
|
|
||||||
|
bool setLine(const uint8_t *bytes, size_t size);
|
||||||
|
CharBuffer &getLine();
|
||||||
|
|
||||||
|
void backsp();
|
||||||
|
void clear();
|
||||||
|
void clear_line();
|
||||||
|
size_t println(const char *str);
|
||||||
|
size_t println(void);
|
||||||
|
size_t write_P(PGM_P str);
|
||||||
|
size_t write(uint8_t c);
|
||||||
|
size_t write(const uint8_t *buf, size_t size);
|
||||||
|
void writeByDigit(uint8_t i);
|
||||||
|
bool available();
|
||||||
|
void loop();
|
||||||
|
void start();
|
||||||
|
void quit();
|
||||||
|
void initscr();
|
||||||
|
void attrset(uint16_t attr);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void move(uint8_t y, uint8_t x);
|
||||||
|
TerminalEventHandler eventHandler_;
|
||||||
|
TerminalInputEventHandler inputHandler_;
|
||||||
|
|
||||||
|
uint8_t attr = 0xff;
|
||||||
|
uint8_t curY = 0xff;
|
||||||
|
uint8_t curX = 0xff;
|
||||||
|
|
||||||
|
unsigned long lastReceived = 0;
|
||||||
|
State state = ST_INACTIVE;
|
||||||
|
Stream *_stream;
|
||||||
|
EditLine _line;
|
||||||
|
char cc_buf[32] = {0};
|
||||||
|
size_t cc_pos = 0;
|
||||||
|
bool _color = false;
|
||||||
|
bool _controlCodes = false;
|
||||||
|
bool _echo = false;
|
||||||
|
EOLType_t _eol = CRLF;
|
||||||
|
|
||||||
|
struct ControlCode {
|
||||||
|
const char *cc;
|
||||||
|
const char ch;
|
||||||
|
};
|
||||||
|
|
||||||
|
ControlCode keyMap[10] = {
|
||||||
|
{"G", KEY_HOME}, // 71 Home key
|
||||||
|
{"H", KEY_UP}, // 72 Up arrow
|
||||||
|
{"I", KEY_PAGE_UP}, // 73 PageUp
|
||||||
|
{"K", KEY_LEFT}, // 75 Left arrow
|
||||||
|
{"M", KEY_RIGHT}, // 77 Right arrow
|
||||||
|
{"O", KEY_END}, // 79 End key
|
||||||
|
{"P", KEY_DOWN}, // 80 Down arrow
|
||||||
|
{"Q", KEY_PAGE_DOWN}, // 81 PageDown
|
||||||
|
{"R", KEY_INS}, // 82 Insert
|
||||||
|
{"S", KEY_DEL}, // 83 Delete
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
class UptimeInterval {
|
class UptimeInterval {
|
||||||
public:
|
public:
|
||||||
UptimeInterval(unsigned long interval, boolean postpone = true) : _interval{interval} {
|
UptimeInterval(unsigned long interval, boolean postpone = true) : _next{0}, _interval{interval} {
|
||||||
reset(postpone);
|
reset(postpone);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,5 +28,5 @@ class UptimeInterval {
|
|||||||
static unsigned long _uptime_seconds;
|
static unsigned long _uptime_seconds;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned long _interval, _next;
|
unsigned long _next, _interval;
|
||||||
};
|
};
|
||||||
|
|||||||
55
include/Utils/FileHelper.h
Normal file
55
include/Utils/FileHelper.h
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#include "FS.h"
|
||||||
|
|
||||||
|
#ifdef ESP32
|
||||||
|
#include "LITTLEFS.h"
|
||||||
|
#define LittleFS LITTLEFS
|
||||||
|
#endif
|
||||||
|
#ifdef ESP8266
|
||||||
|
#include <LittleFS.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class FileHelper {
|
||||||
|
public:
|
||||||
|
FileHelper(const String filename);
|
||||||
|
/*
|
||||||
|
* Проверить существование
|
||||||
|
*/
|
||||||
|
void exists();
|
||||||
|
/*
|
||||||
|
* Удалить файл
|
||||||
|
*/
|
||||||
|
void remove();
|
||||||
|
/*
|
||||||
|
* Открыть файл установить позицию @position
|
||||||
|
*/
|
||||||
|
File seek(size_t position = 0);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Чтение строки с содержащей @substr
|
||||||
|
*/
|
||||||
|
String readFileString(const String substr);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Добовление строки @str в файл
|
||||||
|
*/
|
||||||
|
String appendStr(const String str);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Запись строки
|
||||||
|
*/
|
||||||
|
String writeStr(const String);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Чтение в строку
|
||||||
|
*/
|
||||||
|
String readStr(size_t);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Размер в байтах
|
||||||
|
*/
|
||||||
|
size_t getSize();
|
||||||
|
};
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
#include <LittleFS.h>
|
#include <LittleFS.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Инициализация ФС
|
* Инициализация ФС
|
||||||
*/
|
*/
|
||||||
|
|||||||
32
include/Utils/PrintMessage.h
Normal file
32
include/Utils/PrintMessage.h
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Arduino.h"
|
||||||
|
#include "CommonTypes.h"
|
||||||
|
#include "Utils\StringUtils.h"
|
||||||
|
#include "Utils\TimeUtils.h"
|
||||||
|
#include "Errors.h"
|
||||||
|
|
||||||
|
#define pm PrintMessage(MODULE)
|
||||||
|
|
||||||
|
class PrintMessage {
|
||||||
|
public:
|
||||||
|
PrintMessage(const char* module) {
|
||||||
|
_module = module;
|
||||||
|
}
|
||||||
|
|
||||||
|
void error(const String str) {
|
||||||
|
print(EL_ERROR, str);
|
||||||
|
}
|
||||||
|
|
||||||
|
void info(const String str) {
|
||||||
|
print(EL_INFO, str);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void print(const ErrorLevel_t level, const String& str) {
|
||||||
|
Serial.printf("%s [%s] [%s] %s\n", prettyMillis().c_str(), getErrorLevelStr(level).c_str(), _module, str.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const char* _module;
|
||||||
|
};
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#include "CommonTypes.h"
|
||||||
|
|
||||||
uint8_t hexStringToUint8(String hex);
|
uint8_t hexStringToUint8(String hex);
|
||||||
|
|
||||||
uint16_t hexStringToUint16(String hex);
|
uint16_t hexStringToUint16(String hex);
|
||||||
|
|||||||
@@ -7,10 +7,6 @@
|
|||||||
|
|
||||||
void Time_Init();
|
void Time_Init();
|
||||||
|
|
||||||
void time_check();
|
|
||||||
|
|
||||||
void reconfigTime();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Получение текущего времени
|
* Получение текущего времени
|
||||||
*/
|
*/
|
||||||
@@ -22,7 +18,7 @@ String getTimeUnix();
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Параметр время
|
* Параметр время
|
||||||
* Результат выполнения
|
* @result результат
|
||||||
*/
|
*/
|
||||||
boolean getUnixTimeStr(String&);
|
boolean getUnixTimeStr(String&);
|
||||||
|
|
||||||
@@ -38,3 +34,9 @@ String getDateDigitalFormated();
|
|||||||
int timeToMin(String Time);
|
int timeToMin(String Time);
|
||||||
|
|
||||||
const String prettyMillis(unsigned long time_ms = millis());
|
const String prettyMillis(unsigned long time_ms = millis());
|
||||||
|
|
||||||
|
int timeZoneInSeconds(const byte timeZone);
|
||||||
|
|
||||||
|
bool hasTimeSynced();
|
||||||
|
|
||||||
|
int getBiasInSeconds();
|
||||||
10
include/Utils/WiFiUtils.h
Normal file
10
include/Utils/WiFiUtils.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Global.h"
|
||||||
|
|
||||||
|
boolean scanWiFi(String ssid);
|
||||||
|
|
||||||
|
void startSTAMode();
|
||||||
|
|
||||||
|
bool startAPMode();
|
||||||
|
|
||||||
54
src/Clock.cpp
Normal file
54
src/Clock.cpp
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#include "Clock.h"
|
||||||
|
|
||||||
|
#include "Utils/TimeUtils.h"
|
||||||
|
|
||||||
|
static const char* MODULE = "Clock";
|
||||||
|
|
||||||
|
void startTimeSync() {
|
||||||
|
if (!hasTimeSynced()) {
|
||||||
|
pm.info("Start syncing");
|
||||||
|
reconfigTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setupSntp() {
|
||||||
|
int tzs = getBiasInSeconds();
|
||||||
|
int tzh = tzs / 3600;
|
||||||
|
tzs -= tzh * 3600;
|
||||||
|
int tzm = tzs / 60;
|
||||||
|
tzs -= tzm * 60;
|
||||||
|
|
||||||
|
String ntp = jsonReadStr(configSetupJson, "ntp");
|
||||||
|
pm.info("Setup ntp: " + ntp);
|
||||||
|
char tzstr[64];
|
||||||
|
snprintf(tzstr, sizeof tzstr, "ESPUSER<%+d:%02d:%02d>", tzh, tzm, tzs);
|
||||||
|
return configTime(tzstr, ntp.c_str(), "pool.ntp.org", "time.nist.gov");
|
||||||
|
}
|
||||||
|
|
||||||
|
void reconfigTime() {
|
||||||
|
#ifdef ESP32
|
||||||
|
uint8_t i = 0;
|
||||||
|
struct tm timeinfo;
|
||||||
|
while (!getLocalTime(&timeinfo) && i <= 4) {
|
||||||
|
Serial.print(".");
|
||||||
|
i++;
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef ESP8266
|
||||||
|
setupSntp();
|
||||||
|
uint8_t i = 0;
|
||||||
|
while (!hasTimeSynced() && i < 4) {
|
||||||
|
Serial.print(".");
|
||||||
|
i++;
|
||||||
|
delay(300);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (getTimeUnix() != "failed") {
|
||||||
|
pm.info("Time synced " + getDateDigitalFormated() + " " + getTime());
|
||||||
|
} else {
|
||||||
|
pm.error("Failed to obtain");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -549,6 +549,7 @@ void firmwareVersion() {
|
|||||||
choose_widget_and_create(widget_name, page_name, page_number, "any-data", "firmver");
|
choose_widget_and_create(widget_name, page_name, page_number, "any-data", "firmver");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//==============================================================================================================================
|
//==============================================================================================================================
|
||||||
//============================выполнение команд (в лупе) по очереди из строки order=============================================
|
//============================выполнение команд (в лупе) по очереди из строки order=============================================
|
||||||
void handleCMD_loop() {
|
void handleCMD_loop() {
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
#include "Errors.h"
|
|
||||||
|
|
||||||
static const char *str_info = "Info";
|
|
||||||
static const char *str_warn = "Warn";
|
|
||||||
static const char *str_error = "Error";
|
|
||||||
static const char *str_unknown = "Unknown";
|
|
||||||
|
|
||||||
String getErrorLevelStr(ErrorLevel_t level) {
|
|
||||||
const char *ptr;
|
|
||||||
switch (level) {
|
|
||||||
case EL_INFO:
|
|
||||||
ptr = str_info;
|
|
||||||
break;
|
|
||||||
case EL_WARNING:
|
|
||||||
ptr = str_warn;
|
|
||||||
break;
|
|
||||||
case EL_ERROR:
|
|
||||||
ptr = str_error;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
ptr = str_unknown;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return String(ptr);
|
|
||||||
}
|
|
||||||
@@ -9,6 +9,7 @@ AsyncWebSocket ws;
|
|||||||
//AsyncEventSource events;
|
//AsyncEventSource events;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
TickerScheduler ts(TEST + 1);
|
TickerScheduler ts(TEST + 1);
|
||||||
|
|
||||||
WiFiClient espClient;
|
WiFiClient espClient;
|
||||||
|
|||||||
12
src/Init.cpp
12
src/Init.cpp
@@ -1,14 +1,10 @@
|
|||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
|
|
||||||
unsigned long UptimeInterval::_uptime_seconds;
|
|
||||||
|
|
||||||
UptimeInterval myUptime(10);
|
|
||||||
|
|
||||||
void handle_uptime();
|
void handle_uptime();
|
||||||
void handle_statistics();
|
void handle_statistics();
|
||||||
|
void telemetry_init();
|
||||||
|
|
||||||
void loadConfig() {
|
void loadConfig() {
|
||||||
|
|
||||||
configSetupJson = readFile("config.json", 4096);
|
configSetupJson = readFile("config.json", 4096);
|
||||||
configSetupJson.replace(" ", "");
|
configSetupJson.replace(" ", "");
|
||||||
configSetupJson.replace("\r\n", "");
|
configSetupJson.replace("\r\n", "");
|
||||||
@@ -81,7 +77,9 @@ void uptime_init() {
|
|||||||
handle_uptime();
|
handle_uptime();
|
||||||
},
|
},
|
||||||
nullptr, true);
|
nullptr, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void telemetry_init() {
|
||||||
if (TELEMETRY_UPDATE_INTERVAL) {
|
if (TELEMETRY_UPDATE_INTERVAL) {
|
||||||
ts.add(
|
ts.add(
|
||||||
STATISTICS, TELEMETRY_UPDATE_INTERVAL, [&](void*) {
|
STATISTICS, TELEMETRY_UPDATE_INTERVAL, [&](void*) {
|
||||||
@@ -92,9 +90,7 @@ void uptime_init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void handle_uptime() {
|
void handle_uptime() {
|
||||||
if (myUptime.check()) {
|
jsonWriteStr(configSetupJson, "uptime", prettyMillis());
|
||||||
jsonWriteStr(configSetupJson, "uptime", prettyMillis());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_statistics() {
|
void handle_statistics() {
|
||||||
|
|||||||
84
src/Module/Telnet.cpp
Normal file
84
src/Module/Telnet.cpp
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
#include "Module/Telnet.h"
|
||||||
|
|
||||||
|
bool Telnet::onInit() {
|
||||||
|
_server = new WiFiServer(_port);
|
||||||
|
_term = new Terminal();
|
||||||
|
_term->enableControlCodes();
|
||||||
|
_term->enableEcho(false);
|
||||||
|
_term->setStream(&_client);
|
||||||
|
|
||||||
|
// _shell = new CommandShell(Cli::get());
|
||||||
|
_shell->setTerm(_term);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Telnet::onEnd() {
|
||||||
|
delete _server;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Telnet::onStart() {
|
||||||
|
_server->begin();
|
||||||
|
_server->setNoDelay(true);
|
||||||
|
return _server->status() != CLOSED;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Telnet::onStop() {
|
||||||
|
if (hasClient()) {
|
||||||
|
_client.stop();
|
||||||
|
}
|
||||||
|
_server->stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Telnet::hasClient() { return _client.connected(); }
|
||||||
|
|
||||||
|
void Telnet::sendData(const String& data) {
|
||||||
|
if (hasClient()) {
|
||||||
|
_client.write(data.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Telnet::setEventHandler(TelnetEventHandler h) { _eventHandler = h; }
|
||||||
|
|
||||||
|
void Telnet::onLoop() {
|
||||||
|
if (_server->hasClient()) {
|
||||||
|
if (!_client) {
|
||||||
|
_client = _server->available();
|
||||||
|
} else {
|
||||||
|
if (!_client.connected()) {
|
||||||
|
_server->stop();
|
||||||
|
_client = _server->available();
|
||||||
|
} else {
|
||||||
|
WiFiClient rejected;
|
||||||
|
rejected = _server->available();
|
||||||
|
rejected.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_lastConnected != hasClient()) {
|
||||||
|
_lastConnected = hasClient();
|
||||||
|
if (_lastConnected) {
|
||||||
|
onConnect();
|
||||||
|
} else {
|
||||||
|
onDisconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasClient() && _shell != nullptr) _shell->loop();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Telnet::isShellActive() {
|
||||||
|
return _shell->active();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Telnet::onConnect() {
|
||||||
|
if (_eventHandler) {
|
||||||
|
_eventHandler(TE_CONNECTED, &_client);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Telnet::onDisconnect() {
|
||||||
|
if (_eventHandler) {
|
||||||
|
_eventHandler(TE_DISCONNECTED, nullptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
10
src/Mqtt.cpp
10
src/Mqtt.cpp
@@ -22,7 +22,7 @@ void MQTT_init() {
|
|||||||
Serial.println("[VV] WiFi-ok");
|
Serial.println("[VV] WiFi-ok");
|
||||||
if (client_mqtt.connected()) {
|
if (client_mqtt.connected()) {
|
||||||
Serial.println("[VV] MQTT-ok");
|
Serial.println("[VV] MQTT-ok");
|
||||||
led_blink("off");
|
setLedStatus(LED_OFF);
|
||||||
} else {
|
} else {
|
||||||
MQTT_Connecting();
|
MQTT_Connecting();
|
||||||
if (!just_load) mqtt_lost_error++;
|
if (!just_load) mqtt_lost_error++;
|
||||||
@@ -31,7 +31,7 @@ void MQTT_init() {
|
|||||||
Serial.println("[E] Lost WiFi connection");
|
Serial.println("[E] Lost WiFi connection");
|
||||||
wifi_lost_error++;
|
wifi_lost_error++;
|
||||||
ts.remove(WIFI_MQTT_CONNECTION_CHECK);
|
ts.remove(WIFI_MQTT_CONNECTION_CHECK);
|
||||||
StartAPMode();
|
startAPMode();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
nullptr, true);
|
nullptr, true);
|
||||||
@@ -58,14 +58,14 @@ boolean MQTT_Connecting() {
|
|||||||
String mqtt_server = jsonReadStr(configSetupJson, "mqttServer");
|
String mqtt_server = jsonReadStr(configSetupJson, "mqttServer");
|
||||||
if ((mqtt_server != "")) {
|
if ((mqtt_server != "")) {
|
||||||
Serial.println("[E] Lost MQTT connection, start reconnecting");
|
Serial.println("[E] Lost MQTT connection, start reconnecting");
|
||||||
led_blink("fast");
|
setLedStatus(LED_FAST);
|
||||||
client_mqtt.setServer(mqtt_server.c_str(), jsonReadInt(configSetupJson, "mqttPort"));
|
client_mqtt.setServer(mqtt_server.c_str(), jsonReadInt(configSetupJson, "mqttPort"));
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
if (!client_mqtt.connected()) {
|
if (!client_mqtt.connected()) {
|
||||||
Serial.println("[V] Connecting to MQTT server commenced");
|
Serial.println("[V] Connecting to MQTT server commenced");
|
||||||
if (client_mqtt.connect(chipId.c_str(), jsonReadStr(configSetupJson, "mqttUser").c_str(), jsonReadStr(configSetupJson, "mqttPass").c_str())) {
|
if (client_mqtt.connect(chipId.c_str(), jsonReadStr(configSetupJson, "mqttUser").c_str(), jsonReadStr(configSetupJson, "mqttPass").c_str())) {
|
||||||
Serial.println("[VV] MQTT connected");
|
Serial.println("[VV] MQTT connected");
|
||||||
led_blink("off");
|
setLedStatus(LED_OFF);
|
||||||
client_mqtt.setCallback(callback);
|
client_mqtt.setCallback(callback);
|
||||||
client_mqtt.subscribe(jsonReadStr(configSetupJson, "mqttPrefix").c_str()); // Для приема получения HELLOW и подтверждения связи
|
client_mqtt.subscribe(jsonReadStr(configSetupJson, "mqttPrefix").c_str()); // Для приема получения HELLOW и подтверждения связи
|
||||||
client_mqtt.subscribe((jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipId + "/+/control").c_str()); // Подписываемся на топики control
|
client_mqtt.subscribe((jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipId + "/+/control").c_str()); // Подписываемся на топики control
|
||||||
@@ -77,7 +77,7 @@ boolean MQTT_Connecting() {
|
|||||||
res = true;
|
res = true;
|
||||||
} else {
|
} else {
|
||||||
Serial.println("[E] try again in " + String(MQTT_RECONNECT_INTERVAL / 1000) + " sec");
|
Serial.println("[E] try again in " + String(MQTT_RECONNECT_INTERVAL / 1000) + " sec");
|
||||||
led_blink("fast");
|
setLedStatus(LED_FAST);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
void initUpgrade() {
|
void init_updater() {
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
if (WiFi.status() == WL_CONNECTED) last_version = getURL("http://91.204.228.124:1100/update/esp8266/version.txt");
|
if (WiFi.status() == WL_CONNECTED) last_version = getURL("http://91.204.228.124:1100/update/esp8266/version.txt");
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,18 +1,15 @@
|
|||||||
#include "Utils/FileUtils.h"
|
#include "Utils/FileUtils.h"
|
||||||
|
#include "Utils/PrintMessage.h"
|
||||||
|
|
||||||
static const char* MODULE = "FS";
|
static const char* MODULE = "FS";
|
||||||
|
|
||||||
void printError(const String str) {
|
|
||||||
Serial.printf("[E] [%s] %s\n", MODULE, str.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
const String filepath(const String& filename) {
|
const String filepath(const String& filename) {
|
||||||
return filename.startsWith("/") ? filename : "/" + filename;
|
return filename.startsWith("/") ? filename : "/" + filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool fileSystemInit() {
|
bool fileSystemInit() {
|
||||||
if (!LittleFS.begin()) {
|
if (!LittleFS.begin()) {
|
||||||
printError("init");
|
pm.error("init");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -20,14 +17,14 @@ bool fileSystemInit() {
|
|||||||
|
|
||||||
void removeFile(const String filename) {
|
void removeFile(const String filename) {
|
||||||
if (!LittleFS.remove(filepath(filename))) {
|
if (!LittleFS.remove(filepath(filename))) {
|
||||||
printError("remove " + filename);
|
pm.error("remove " + filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
File seekFile(const String filename, size_t position) {
|
File seekFile(const String filename, size_t position) {
|
||||||
auto file = LittleFS.open(filepath(filename), "r");
|
auto file = LittleFS.open(filepath(filename), "r");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
printError("open " + filename);
|
pm.error("open " + filename);
|
||||||
}
|
}
|
||||||
// поставим курсор в начало файла
|
// поставим курсор в начало файла
|
||||||
file.seek(position, SeekSet);
|
file.seek(position, SeekSet);
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ uint16_t hexStringToUint16(String hex) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t itemsCount(String str, const String& separator) {
|
size_t itemsCount(String str, const String &separator) {
|
||||||
// если строки поиск нет сразу выход
|
// если строки поиск нет сразу выход
|
||||||
if (str.indexOf(separator) == -1) {
|
if (str.indexOf(separator) == -1) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -77,7 +77,7 @@ size_t itemsCount(String str, const String& separator) {
|
|||||||
return cnt;
|
return cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isDigitStr(const String& str) {
|
boolean isDigitStr(const String &str) {
|
||||||
for (size_t i = 0; i < str.length(); i++) {
|
for (size_t i = 0; i < str.length(); i++) {
|
||||||
if (!isDigit(str.charAt(i))) {
|
if (!isDigit(str.charAt(i))) {
|
||||||
return false;
|
return false;
|
||||||
@@ -96,3 +96,27 @@ String prettyBytes(size_t size) {
|
|||||||
else
|
else
|
||||||
return String(size / 1024.0 / 1024.0 / 1024.0) + "GB";
|
return String(size / 1024.0 / 1024.0 / 1024.0) + "GB";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char *str_info = "I";
|
||||||
|
static const char *str_warn = "W";
|
||||||
|
static const char *str_error = "E";
|
||||||
|
static const char *str_unknown = "?";
|
||||||
|
|
||||||
|
String getErrorLevelStr(ErrorLevel_t level) {
|
||||||
|
const char *ptr;
|
||||||
|
switch (level) {
|
||||||
|
case EL_INFO:
|
||||||
|
ptr = str_info;
|
||||||
|
break;
|
||||||
|
case EL_WARNING:
|
||||||
|
ptr = str_warn;
|
||||||
|
break;
|
||||||
|
case EL_ERROR:
|
||||||
|
ptr = str_error;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ptr = str_unknown;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return String(ptr);
|
||||||
|
}
|
||||||
@@ -3,54 +3,33 @@
|
|||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
#include "Utils\StringUtils.h"
|
#include "Utils\StringUtils.h"
|
||||||
|
|
||||||
void Time_Init() {
|
#define ONE_MINUTE_s 60
|
||||||
ts.add(
|
#define ONE_HOUR_s 60 * ONE_MINUTE_s
|
||||||
TIME_SYNC, 30000, [&](void*) {
|
|
||||||
time_check();
|
int getBiasInSeconds() {
|
||||||
},
|
return 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
|
||||||
nullptr, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void time_check() {
|
int getBiasInMinutes() {
|
||||||
if (getTimeUnix() == "failed") {
|
return getBiasInSeconds() / 60;
|
||||||
Serial.println("[I] Trying to obtain time");
|
|
||||||
reconfigTime();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void reconfigTime() {
|
const timezone getTimeZone() {
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
return timezone{getBiasInMinutes(), 0};
|
||||||
String ntp = jsonReadStr(configSetupJson, "ntp");
|
}
|
||||||
configTime(0, 0, ntp.c_str());
|
|
||||||
Serial.println("[I] Time sync");
|
time_t getSystemTime() {
|
||||||
#ifdef ESP32
|
timeval tv{0, 0};
|
||||||
uint8_t i = 0;
|
timezone tz = getTimeZone();
|
||||||
struct tm timeinfo;
|
time_t epoch = 0;
|
||||||
while (!getLocalTime(&timeinfo) && i <= 4) {
|
if (gettimeofday(&tv, &tz) != -1)
|
||||||
Serial.print(".");
|
epoch = tv.tv_sec;
|
||||||
i++;
|
return epoch;
|
||||||
delay(1000);
|
}
|
||||||
}
|
|
||||||
#endif
|
bool hasTimeSynced() {
|
||||||
#ifdef ESP8266
|
time_t now = time(nullptr);
|
||||||
//uint8_t i = 0;
|
return now > millis();
|
||||||
//while (!time(nullptr) && i < 4) {
|
|
||||||
// Serial.print(".");
|
|
||||||
// i++;
|
|
||||||
delay(2000);
|
|
||||||
//}
|
|
||||||
#endif
|
|
||||||
if (getTimeUnix() != "failed") {
|
|
||||||
Serial.print("[V] Got time = ");
|
|
||||||
Serial.print(getDateDigitalFormated());
|
|
||||||
Serial.print(' ');
|
|
||||||
Serial.println(getTime());
|
|
||||||
} else {
|
|
||||||
Serial.println("[E] Failed to obtain time, retry in 30 sec");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Serial.println("[E] Unable to obtain time");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String getTimeUnix() {
|
String getTimeUnix() {
|
||||||
@@ -69,7 +48,7 @@ boolean getUnixTimeStr(String& res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String getTime() {
|
String getTime() {
|
||||||
time_t now = time(nullptr); // получаем время с помощью библиотеки time.h
|
time_t now = time(nullptr);
|
||||||
int zone = 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
|
int zone = 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
|
||||||
now = now + zone;
|
now = now + zone;
|
||||||
String Time = ""; // Строка для результатов времени
|
String Time = ""; // Строка для результатов времени
|
||||||
@@ -80,7 +59,7 @@ String getTime() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String getTimeWOsec() {
|
String getTimeWOsec() {
|
||||||
time_t now = time(nullptr); // получаем время с помощью библиотеки time.h
|
time_t now = time(nullptr);
|
||||||
int zone = 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
|
int zone = 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
|
||||||
now = now + zone;
|
now = now + zone;
|
||||||
String Time = ""; // Строка для результатов времени
|
String Time = ""; // Строка для результатов времени
|
||||||
@@ -91,7 +70,7 @@ String getTimeWOsec() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String getDate() {
|
String getDate() {
|
||||||
time_t now = time(nullptr); // получаем время с помощью библиотеки time.h
|
time_t now = time(nullptr);
|
||||||
int zone = 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
|
int zone = 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
|
||||||
now = now + zone;
|
now = now + zone;
|
||||||
String Data = ""; // Строка для результатов времени
|
String Data = ""; // Строка для результатов времени
|
||||||
@@ -142,11 +121,11 @@ int timeToMin(String Time) {
|
|||||||
return min;
|
return min;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char* TIME_FORMAT PROGMEM = "%2d:%2d:%2d";
|
static const char* TIME_FORMAT PROGMEM = "%02d:%02d:%02d";
|
||||||
static const char* TIME_FORMAT_WITH_DAYS PROGMEM = "%dd %2d:%2d";
|
static const char* TIME_FORMAT_WITH_DAYS PROGMEM = "%dd %02d:%02d";
|
||||||
|
|
||||||
const String prettyMillis(unsigned long time_ms) {
|
const String prettyMillis(unsigned long time_ms) {
|
||||||
unsigned long tmp = time_ms;
|
unsigned long tmp = time_ms / 1000;
|
||||||
unsigned long seconds;
|
unsigned long seconds;
|
||||||
unsigned long minutes;
|
unsigned long minutes;
|
||||||
unsigned long hours;
|
unsigned long hours;
|
||||||
@@ -156,15 +135,137 @@ const String prettyMillis(unsigned long time_ms) {
|
|||||||
|
|
||||||
minutes = tmp % 60;
|
minutes = tmp % 60;
|
||||||
tmp = tmp / 60;
|
tmp = tmp / 60;
|
||||||
|
|
||||||
hours = tmp % 24;
|
hours = tmp % 24;
|
||||||
days = tmp / 24;
|
days = tmp / 24;
|
||||||
|
|
||||||
char buf[16];
|
char buf[32];
|
||||||
|
|
||||||
if (days) {
|
if (days) {
|
||||||
sprintf_P(buf, TIME_FORMAT, hours, minutes, seconds);
|
sprintf_P(buf, TIME_FORMAT_WITH_DAYS, days, hours, minutes, seconds);
|
||||||
} else {
|
} else {
|
||||||
sprintf_P(buf, TIME_FORMAT_WITH_DAYS, days, hours, minutes);
|
sprintf_P(buf, TIME_FORMAT, hours, minutes, seconds);
|
||||||
}
|
}
|
||||||
return String(buf);
|
return String(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int timeZoneInSeconds(const byte timeZone) {
|
||||||
|
int res = 0;
|
||||||
|
switch (constrain(timeZone, 1, 38)) {
|
||||||
|
case 1:
|
||||||
|
res = -12 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
res = -11 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
res = -10 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
res = -9 * ONE_HOUR_s - 30 * ONE_MINUTE_s;
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
res = -9 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
res = -8 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
res = -7 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
res = -6 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 9:
|
||||||
|
res = -5 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 10:
|
||||||
|
res = -4 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 11:
|
||||||
|
res = -3 * ONE_HOUR_s - 30 * ONE_MINUTE_s;
|
||||||
|
break;
|
||||||
|
case 12:
|
||||||
|
res = -3 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 13:
|
||||||
|
res = -2 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 14:
|
||||||
|
res = -1 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 15:
|
||||||
|
res = 0;
|
||||||
|
break;
|
||||||
|
case 16:
|
||||||
|
res = 1 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 17:
|
||||||
|
res = 2 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 18:
|
||||||
|
res = 3 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 19:
|
||||||
|
res = 3 * ONE_HOUR_s + 30 * ONE_MINUTE_s;
|
||||||
|
break;
|
||||||
|
case 20:
|
||||||
|
res = 4 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 21:
|
||||||
|
res = 4 * ONE_HOUR_s + 30 * ONE_MINUTE_s;
|
||||||
|
break;
|
||||||
|
case 22:
|
||||||
|
res = 5 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 23:
|
||||||
|
res = 5 * ONE_HOUR_s + 30 * ONE_MINUTE_s;
|
||||||
|
break;
|
||||||
|
case 24:
|
||||||
|
res = 5 * ONE_HOUR_s + 45 * ONE_MINUTE_s;
|
||||||
|
break;
|
||||||
|
case 25:
|
||||||
|
res = 6 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 26:
|
||||||
|
res = 6 * ONE_HOUR_s + 30 * ONE_MINUTE_s;
|
||||||
|
break;
|
||||||
|
case 27:
|
||||||
|
res = 7 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 28:
|
||||||
|
res = 8 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 29:
|
||||||
|
res = 8 * ONE_HOUR_s + 45 * ONE_MINUTE_s;
|
||||||
|
break;
|
||||||
|
case 30:
|
||||||
|
res = 9 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 31:
|
||||||
|
res = 9 * ONE_HOUR_s + 30 * ONE_MINUTE_s;
|
||||||
|
break;
|
||||||
|
case 32:
|
||||||
|
res = 10 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 33:
|
||||||
|
res = 10 * ONE_HOUR_s + 30 * ONE_MINUTE_s;
|
||||||
|
break;
|
||||||
|
case 34:
|
||||||
|
res = 11 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 35:
|
||||||
|
res = 12 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 36:
|
||||||
|
res = 12 * ONE_HOUR_s + 45 * ONE_MINUTE_s;
|
||||||
|
break;
|
||||||
|
case 37:
|
||||||
|
res = 13 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
case 38:
|
||||||
|
res = 14 * ONE_HOUR_s;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
108
src/Utils/WiFiUtils.cpp
Normal file
108
src/Utils/WiFiUtils.cpp
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
#include "Utils/WiFiUtils.h"
|
||||||
|
|
||||||
|
static const char* MODULE = "WiFi";
|
||||||
|
|
||||||
|
void startSTAMode() {
|
||||||
|
setLedStatus(LED_SLOW);
|
||||||
|
pm.info("STA Mode");
|
||||||
|
|
||||||
|
String ssid = jsonReadStr(configSetupJson, "routerssid");
|
||||||
|
String passwd = jsonReadStr(configSetupJson, "routerpass");
|
||||||
|
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
if (ssid == "" && passwd == "") {
|
||||||
|
WiFi.begin();
|
||||||
|
} else {
|
||||||
|
if (WiFi.begin(ssid.c_str(), passwd.c_str()) == WL_CONNECT_FAILED) {
|
||||||
|
pm.error("failed on start");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool keepConnecting = true;
|
||||||
|
uint8_t tries = 20;
|
||||||
|
do {
|
||||||
|
sint8_t connRes = WiFi.waitForConnectResult(1000);
|
||||||
|
switch (connRes) {
|
||||||
|
case WL_NO_SSID_AVAIL: {
|
||||||
|
pm.error("No ssid available");
|
||||||
|
keepConnecting = false;
|
||||||
|
} break;
|
||||||
|
case WL_CONNECTED: {
|
||||||
|
String hostIpStr = WiFi.localIP().toString();
|
||||||
|
pm.info("http://" + hostIpStr);
|
||||||
|
jsonWriteStr(configSetupJson, "ip", hostIpStr);
|
||||||
|
keepConnecting = false;
|
||||||
|
} break;
|
||||||
|
case WL_CONNECT_FAILED: {
|
||||||
|
pm.error("Check credentials");
|
||||||
|
jsonWriteInt(configOptionJson, "pass_status", 1);
|
||||||
|
keepConnecting = false;
|
||||||
|
} break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (keepConnecting && tries--);
|
||||||
|
|
||||||
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
|
MQTT_init();
|
||||||
|
setLedStatus(LED_OFF);
|
||||||
|
} else {
|
||||||
|
startAPMode();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool startAPMode() {
|
||||||
|
setLedStatus(LED_ON);
|
||||||
|
pm.info("AP Mode");
|
||||||
|
|
||||||
|
String ssid = jsonReadStr(configSetupJson, "apssid");
|
||||||
|
String passwd = jsonReadStr(configSetupJson, "appass");
|
||||||
|
|
||||||
|
WiFi.mode(WIFI_AP);
|
||||||
|
|
||||||
|
WiFi.softAP(ssid.c_str(), passwd.c_str());
|
||||||
|
String hostIpStr = WiFi.softAPIP().toString();
|
||||||
|
pm.info("Host IP: " + hostIpStr);
|
||||||
|
jsonWriteStr(configSetupJson, "ip", hostIpStr);
|
||||||
|
|
||||||
|
ts.add(
|
||||||
|
WIFI_SCAN, 10 * 1000,
|
||||||
|
[&](void*) {
|
||||||
|
String sta_ssid = jsonReadStr(configSetupJson, "routerssid");
|
||||||
|
pm.info("scanning for " + sta_ssid);
|
||||||
|
if (scanWiFi(sta_ssid)) {
|
||||||
|
ts.remove(WIFI_SCAN);
|
||||||
|
startSTAMode();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
nullptr, true);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean scanWiFi(String ssid) {
|
||||||
|
bool res = false;
|
||||||
|
int8_t n = WiFi.scanComplete();
|
||||||
|
pm.info("scan result: " + String(n, DEC));
|
||||||
|
if (n == -2) {
|
||||||
|
// не было запущено, запускаем
|
||||||
|
pm.info("start scanning");
|
||||||
|
// async, show_hidden
|
||||||
|
WiFi.scanNetworks(true, false);
|
||||||
|
} else if (n == -1) {
|
||||||
|
// все еще выполняется
|
||||||
|
pm.info("scanning in progress");
|
||||||
|
} else if (n == 0) {
|
||||||
|
// не найдена ни одна сеть
|
||||||
|
pm.info("no networks found");
|
||||||
|
WiFi.scanNetworks(true, false);
|
||||||
|
} else if (n > 0) {
|
||||||
|
for (int8_t i = 0; i < n; i++) {
|
||||||
|
if (WiFi.SSID(i) == ssid) {
|
||||||
|
res = true;
|
||||||
|
}
|
||||||
|
pm.info((res ? "*" : "") + String(i, DEC) + ") " + WiFi.SSID(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
@@ -1,113 +0,0 @@
|
|||||||
#include "Global.h"
|
|
||||||
|
|
||||||
boolean RouterFind(String ssid);
|
|
||||||
|
|
||||||
void ROUTER_Connecting() {
|
|
||||||
led_blink("slow");
|
|
||||||
|
|
||||||
WiFi.mode(WIFI_STA);
|
|
||||||
|
|
||||||
byte tries = 20;
|
|
||||||
String _ssid = jsonReadStr(configSetupJson, "routerssid");
|
|
||||||
String _password = jsonReadStr(configSetupJson, "routerpass");
|
|
||||||
//WiFi.persistent(false);
|
|
||||||
|
|
||||||
if (_ssid == "" && _password == "") {
|
|
||||||
WiFi.begin();
|
|
||||||
} else {
|
|
||||||
WiFi.begin(_ssid.c_str(), _password.c_str());
|
|
||||||
Serial.print("ssid: ");
|
|
||||||
Serial.println(_ssid);
|
|
||||||
}
|
|
||||||
// Делаем проверку подключения до тех пор пока счетчик tries
|
|
||||||
// не станет равен нулю или не получим подключение
|
|
||||||
while (--tries && WiFi.status() != WL_CONNECTED) {
|
|
||||||
if (WiFi.status() == WL_CONNECT_FAILED) {
|
|
||||||
Serial.println("[E] password is not correct");
|
|
||||||
tries = 1;
|
|
||||||
jsonWriteInt(configOptionJson, "pass_status", 1);
|
|
||||||
}
|
|
||||||
Serial.print(".");
|
|
||||||
delay(1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
|
||||||
// Если не удалось подключиться запускаем в режиме AP
|
|
||||||
Serial.println("");
|
|
||||||
// WiFi.disconnect(true);
|
|
||||||
StartAPMode();
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// Иначе удалось подключиться отправляем сообщение
|
|
||||||
// о подключении и выводим адрес IP
|
|
||||||
Serial.println("");
|
|
||||||
Serial.println("[V] WiFi connected");
|
|
||||||
Serial.print("[V] IP address: http://");
|
|
||||||
Serial.print(WiFi.localIP());
|
|
||||||
Serial.println("");
|
|
||||||
jsonWriteStr(configSetupJson, "ip", WiFi.localIP().toString());
|
|
||||||
led_blink("off");
|
|
||||||
//add_dev_in_list("dev.txt", chipID, WiFi.localIP().toString());
|
|
||||||
MQTT_init();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool StartAPMode() {
|
|
||||||
Serial.println("[I] WiFi AP");
|
|
||||||
WiFi.disconnect();
|
|
||||||
|
|
||||||
WiFi.mode(WIFI_AP);
|
|
||||||
|
|
||||||
String _ssidAP = jsonReadStr(configSetupJson, "apssid");
|
|
||||||
String _passwordAP = jsonReadStr(configSetupJson, "appass");
|
|
||||||
WiFi.softAP(_ssidAP.c_str(), _passwordAP.c_str());
|
|
||||||
IPAddress myIP = WiFi.softAPIP();
|
|
||||||
led_blink("on");
|
|
||||||
Serial.printf("[I] AP IP: %s\n", myIP.toString().c_str());
|
|
||||||
jsonWriteStr(configSetupJson, "ip", myIP.toString());
|
|
||||||
|
|
||||||
//if (jsonReadInt(configOptionJson, "pass_status") != 1) {
|
|
||||||
ts.add(
|
|
||||||
ROUTER_SEARCHING, 10 * 1000, [&](void*) {
|
|
||||||
Serial.println("[I] searching ssid");
|
|
||||||
if (RouterFind(jsonReadStr(configSetupJson, "routerssid"))) {
|
|
||||||
ts.remove(ROUTER_SEARCHING);
|
|
||||||
WiFi.scanDelete();
|
|
||||||
ROUTER_Connecting();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
nullptr, true);
|
|
||||||
//}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean RouterFind(String ssid) {
|
|
||||||
bool res = false;
|
|
||||||
|
|
||||||
int8_t n = WiFi.scanComplete();
|
|
||||||
Serial.printf("[I][WIFI] scan result = %d\n", n);
|
|
||||||
if (n == -2) {
|
|
||||||
// не было запущено, запускаем
|
|
||||||
Serial.println("[I][WIFI] start scanning");
|
|
||||||
// async, show_hidden
|
|
||||||
WiFi.scanNetworks(true, false);
|
|
||||||
} else if (n == -1) {
|
|
||||||
// все еще выполняется
|
|
||||||
Serial.println("[I][WIFI] scanning in progress");
|
|
||||||
} else if (n == 0) {
|
|
||||||
// не найдена ни одна сеть
|
|
||||||
Serial.println("[I][WIFI] no wifi stations, start scanning");
|
|
||||||
WiFi.scanNetworks(true, false);
|
|
||||||
} else if (n > 0) {
|
|
||||||
for (int8_t i = 0; i < n; i++) {
|
|
||||||
if (WiFi.SSID(i) == ssid) {
|
|
||||||
WiFi.scanDelete();
|
|
||||||
res = true;
|
|
||||||
} else {
|
|
||||||
Serial.printf("%d) %s;\n", i, WiFi.SSID(i).c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
WiFi.scanDelete();
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
#include "Global.h"
|
|
||||||
|
|
||||||
void not_async_actions();
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(115200);
|
|
||||||
Serial.println();
|
|
||||||
Serial.println("--------------started----------------");
|
|
||||||
|
|
||||||
setChipId();
|
|
||||||
|
|
||||||
fileSystemInit();
|
|
||||||
|
|
||||||
loadConfig();
|
|
||||||
|
|
||||||
CMD_init();
|
|
||||||
Serial.println("[V] Commands");
|
|
||||||
|
|
||||||
sensors_init();
|
|
||||||
Serial.println("[V] Sensors");
|
|
||||||
|
|
||||||
All_init();
|
|
||||||
Serial.println("[V] All");
|
|
||||||
|
|
||||||
ROUTER_Connecting();
|
|
||||||
Serial.println("[V] ROUTER_Connecting");
|
|
||||||
|
|
||||||
uptime_init();
|
|
||||||
Serial.println("[V] statistics_init");
|
|
||||||
|
|
||||||
initUpgrade();
|
|
||||||
Serial.println("[V] initUpgrade");
|
|
||||||
|
|
||||||
Web_server_init();
|
|
||||||
Serial.println("[V] Web_server_init");
|
|
||||||
|
|
||||||
web_init();
|
|
||||||
Serial.println("[V] web_init");
|
|
||||||
|
|
||||||
Time_Init();
|
|
||||||
Serial.println("[V] Time_Init");
|
|
||||||
|
|
||||||
#ifdef UDP_ENABLED
|
|
||||||
UDP_init();
|
|
||||||
Serial.println("[V] UDP_init");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ts.add(
|
|
||||||
TEST, 10000, [&](void*) {
|
|
||||||
printMemoryStatus("[I] sysinfo ");
|
|
||||||
},
|
|
||||||
nullptr, true);
|
|
||||||
|
|
||||||
just_load = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
#ifdef OTA_UPDATES_ENABLED
|
|
||||||
ArduinoOTA.handle();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef WS_enable
|
|
||||||
ws.cleanupClients();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
not_async_actions();
|
|
||||||
|
|
||||||
handleMQTT();
|
|
||||||
handleCMD_loop();
|
|
||||||
handleButton();
|
|
||||||
handleScenario();
|
|
||||||
#ifdef UDP_ENABLED
|
|
||||||
handleUdp();
|
|
||||||
#endif
|
|
||||||
ts.update();
|
|
||||||
}
|
|
||||||
|
|
||||||
void not_async_actions() {
|
|
||||||
do_mqtt_connection();
|
|
||||||
do_upgrade_url();
|
|
||||||
do_upgrade();
|
|
||||||
#ifdef UDP_ENABLED
|
|
||||||
do_udp_data_parse();
|
|
||||||
do_mqtt_send_settings_to_udp();
|
|
||||||
#endif
|
|
||||||
do_i2c_scanning();
|
|
||||||
}
|
|
||||||
131
src/main.cpp
131
src/main.cpp
@@ -1,9 +1,111 @@
|
|||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
|
|
||||||
|
void not_async_actions();
|
||||||
|
|
||||||
|
static const char* MODULE = "Main";
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
WiFi.setAutoConnect(false);
|
||||||
|
WiFi.persistent(false);
|
||||||
|
|
||||||
|
Serial.begin(115200);
|
||||||
|
Serial.flush();
|
||||||
|
Serial.println();
|
||||||
|
Serial.println("--------------started----------------");
|
||||||
|
|
||||||
|
setChipId();
|
||||||
|
|
||||||
|
pm.info("FS");
|
||||||
|
fileSystemInit();
|
||||||
|
|
||||||
|
pm.info("Config");
|
||||||
|
loadConfig();
|
||||||
|
|
||||||
|
pm.info("Commands");
|
||||||
|
CMD_init();
|
||||||
|
|
||||||
|
pm.info("Sensors");
|
||||||
|
sensors_init();
|
||||||
|
|
||||||
|
pm.info("Init");
|
||||||
|
All_init();
|
||||||
|
|
||||||
|
pm.info("Network");
|
||||||
|
startSTAMode();
|
||||||
|
|
||||||
|
pm.info("Uptime");
|
||||||
|
uptime_init();
|
||||||
|
|
||||||
|
pm.info("Telemery: " + !TELEMETRY_UPDATE_INTERVAL ? "Disabled" : "Enabled");
|
||||||
|
telemetry_init();
|
||||||
|
|
||||||
|
pm.info("Updater");
|
||||||
|
init_updater();
|
||||||
|
|
||||||
|
pm.info("WebServer");
|
||||||
|
Web_server_init();
|
||||||
|
|
||||||
|
pm.info("WebAdmin");
|
||||||
|
web_init();
|
||||||
|
|
||||||
|
pm.info("TimeSync");
|
||||||
|
ts.add(
|
||||||
|
TIME_SYNC, 30000, [&](void*) {
|
||||||
|
startTimeSync();
|
||||||
|
},
|
||||||
|
nullptr, true);
|
||||||
|
|
||||||
|
#ifdef UDP_ENABLED
|
||||||
|
UDP_init();
|
||||||
|
pm.info("Broadcast");
|
||||||
|
#endif
|
||||||
|
ts.add(
|
||||||
|
TEST, 10000, [&](void*) {
|
||||||
|
printMemoryStatus();
|
||||||
|
},
|
||||||
|
nullptr, true);
|
||||||
|
|
||||||
|
just_load = false;
|
||||||
|
}
|
||||||
|
|
||||||
void saveConfig() {
|
void saveConfig() {
|
||||||
writeFile("config.json", configSetupJson);
|
writeFile("config.json", configSetupJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
#ifdef OTA_UPDATES_ENABLED
|
||||||
|
ArduinoOTA.handle();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef WS_enable
|
||||||
|
ws.cleanupClients();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
not_async_actions();
|
||||||
|
|
||||||
|
handleMQTT();
|
||||||
|
handleCMD_loop();
|
||||||
|
handleButton();
|
||||||
|
handleScenario();
|
||||||
|
#ifdef UDP_ENABLED
|
||||||
|
handleUdp();
|
||||||
|
#endif
|
||||||
|
ts.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void not_async_actions() {
|
||||||
|
do_mqtt_connection();
|
||||||
|
do_upgrade_url();
|
||||||
|
do_upgrade();
|
||||||
|
|
||||||
|
#ifdef UDP_ENABLED
|
||||||
|
do_udp_data_parse();
|
||||||
|
do_mqtt_send_settings_to_udp();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
do_i2c_scanning();
|
||||||
|
}
|
||||||
|
|
||||||
String getURL(const String& urls) {
|
String getURL(const String& urls) {
|
||||||
String res = "";
|
String res = "";
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
@@ -54,18 +156,27 @@ void setChipId() {
|
|||||||
|
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
#ifdef LED_PIN
|
#ifdef LED_PIN
|
||||||
void led_blink(String satus) {
|
|
||||||
|
void setLedStatus(LedStatus_t status) {
|
||||||
pinMode(LED_PIN, OUTPUT);
|
pinMode(LED_PIN, OUTPUT);
|
||||||
if (satus == "off") {
|
switch (status) {
|
||||||
noTone(LED_PIN);
|
case LED_OFF:
|
||||||
digitalWrite(LED_PIN, HIGH);
|
noTone(LED_PIN);
|
||||||
|
digitalWrite(LED_PIN, HIGH);
|
||||||
|
break;
|
||||||
|
case LED_ON:
|
||||||
|
noTone(LED_PIN);
|
||||||
|
digitalWrite(LED_PIN, LOW);
|
||||||
|
break;
|
||||||
|
case LED_SLOW:
|
||||||
|
tone(LED_PIN, 1);
|
||||||
|
break;
|
||||||
|
case LED_FAST:
|
||||||
|
tone(LED_PIN, 20);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (satus == "on") {
|
|
||||||
noTone(LED_PIN);
|
|
||||||
digitalWrite(LED_PIN, LOW);
|
|
||||||
}
|
|
||||||
if (satus == "slow") tone(LED_PIN, 1);
|
|
||||||
if (satus == "fast") tone(LED_PIN, 20);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user