mirror of
https://github.com/xoseperez/espurna.git
synced 2026-03-06 16:27:12 +01:00
Merge branch 'ssl' into dev
Conflicts: code/espurna/data/index.html.gz code/espurna/static/index.html.gz.h
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
#include "version.h"
|
||||
#include "arduino.h"
|
||||
#include "prototypes.h"
|
||||
#include "hardware.h"
|
||||
#include "general.h"
|
||||
#include "sensors.h"
|
||||
#include "prototypes.h"
|
||||
|
||||
/*
|
||||
If you want to modify the stock configuration but you don't want to touch
|
||||
|
||||
@@ -56,6 +56,7 @@
|
||||
//#define INFLUXDB_SUPPORT 0
|
||||
//#define MDNS_SUPPORT 0
|
||||
//#define NOFUSS_SUPPORT 1
|
||||
//#define NTP_SUPPORT 0
|
||||
//#define RF_SUPPORT 1
|
||||
//#define SPIFFS_SUPPORT 1
|
||||
//#define TERMINAL_SUPPORT 0
|
||||
|
||||
@@ -285,7 +285,9 @@ PROGMEM const char* const custom_reset_string[] = {
|
||||
// MDNS
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#ifndef MDNS_SUPPORT
|
||||
#define MDNS_SUPPORT 1 // Enable MDNS by default
|
||||
#endif
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// SPIFFS
|
||||
@@ -314,7 +316,7 @@ PROGMEM const char* const custom_reset_string[] = {
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#ifndef MQTT_USE_ASYNC
|
||||
#define MQTT_USE_ASYNC 1 // Use AysncMQTTClient (1) or PubSubClient (0)
|
||||
#define MQTT_USE_ASYNC 0 // Use AysncMQTTClient (1) or PubSubClient (0)
|
||||
#endif
|
||||
|
||||
// MQTT OVER SSL
|
||||
@@ -455,6 +457,10 @@ PROGMEM const char* const custom_reset_string[] = {
|
||||
// NTP
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#ifndef NTP_SUPPORT
|
||||
#define NTP_SUPPORT 1 // Build with NTP support by default
|
||||
#endif
|
||||
|
||||
#define NTP_SERVER "pool.ntp.org" // Default NTP server
|
||||
#define NTP_TIME_OFFSET 1 // Default timezone offset (GMT+1)
|
||||
#define NTP_DAY_LIGHT true // Enable daylight time saving by default
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include <Arduino.h>
|
||||
#include <functional>
|
||||
#include <NtpClientLib.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <AsyncMqttClient.h>
|
||||
|
||||
Binary file not shown.
@@ -80,7 +80,10 @@ void heartbeat() {
|
||||
unsigned long uptime_seconds = getUptime();
|
||||
unsigned int free_heap = ESP.getFreeHeap();
|
||||
|
||||
DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
|
||||
#if NTP_SUPPORT
|
||||
DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
|
||||
#endif
|
||||
|
||||
if (!mqttConnected()) {
|
||||
DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds);
|
||||
DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap);
|
||||
@@ -161,9 +164,9 @@ void hardwareSetup() {
|
||||
|
||||
#if DEBUG_SERIAL_SUPPORT
|
||||
DEBUG_PORT.begin(SERIAL_BAUDRATE);
|
||||
if (customReset() == CUSTOM_RESET_HARDWARE) {
|
||||
#if DEBUG_ESP_WIFI
|
||||
DEBUG_PORT.setDebugOutput(true);
|
||||
}
|
||||
#endif
|
||||
#elif defined(SERIAL_BAUDRATE)
|
||||
Serial.begin(SERIAL_BAUDRATE);
|
||||
#endif
|
||||
@@ -280,12 +283,14 @@ void setup() {
|
||||
wifiSetup();
|
||||
otaSetup();
|
||||
mqttSetup();
|
||||
ntpSetup();
|
||||
|
||||
#ifdef ITEAD_SONOFF_RFBRIDGE
|
||||
rfbSetup();
|
||||
#endif
|
||||
|
||||
#if NTP_SUPPORT
|
||||
ntpSetup();
|
||||
#endif
|
||||
#if I2C_SUPPORT
|
||||
i2cSetup();
|
||||
#endif
|
||||
@@ -334,12 +339,14 @@ void loop() {
|
||||
wifiLoop();
|
||||
otaLoop();
|
||||
mqttLoop();
|
||||
ntpLoop();
|
||||
|
||||
#ifdef ITEAD_SONOFF_RFBRIDGE
|
||||
rfbLoop();
|
||||
#endif
|
||||
|
||||
#if NTP_SUPPORT
|
||||
ntpLoop();
|
||||
#endif
|
||||
#if TERMINAL_SUPPORT
|
||||
settingsLoop();
|
||||
#endif
|
||||
|
||||
@@ -14,14 +14,22 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
|
||||
const char *mqtt_user = 0;
|
||||
const char *mqtt_pass = 0;
|
||||
|
||||
#if MQTT_USE_ASYNC
|
||||
#if MQTT_USE_ASYNC // Using AsyncMqttClient
|
||||
|
||||
#include <AsyncMqttClient.h>
|
||||
AsyncMqttClient mqtt;
|
||||
#else
|
||||
|
||||
#else // Using PubSubClient
|
||||
|
||||
#include <PubSubClient.h>
|
||||
WiFiClient mqttWiFiClient;
|
||||
PubSubClient mqtt(mqttWiFiClient);
|
||||
PubSubClient mqtt;
|
||||
bool _mqttConnected = false;
|
||||
|
||||
WiFiClient _mqttClient;
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
WiFiClientSecure _mqttClientSecure;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
String _mqttTopic;
|
||||
@@ -53,7 +61,7 @@ bool mqttConnected() {
|
||||
}
|
||||
|
||||
void mqttDisconnect() {
|
||||
mqtt.disconnect();
|
||||
if (mqtt.connected()) mqtt.disconnect();
|
||||
}
|
||||
|
||||
bool mqttForward() {
|
||||
@@ -91,7 +99,9 @@ void _mqttFlush() {
|
||||
mqtt_message_t element = _mqtt_queue[i];
|
||||
root[element.topic] = element.message;
|
||||
}
|
||||
if (ntpConnected()) root[MQTT_TOPIC_TIME] = ntpDateTime();
|
||||
#if NTP_SUPPORT
|
||||
if (ntpConnected()) root[MQTT_TOPIC_TIME] = ntpDateTime();
|
||||
#endif
|
||||
root[MQTT_TOPIC_HOSTNAME] = getSetting("hostname");
|
||||
root[MQTT_TOPIC_IP] = getIP();
|
||||
|
||||
@@ -242,9 +252,11 @@ void _mqttOnMessage(char* topic, char* payload, unsigned int len) {
|
||||
|
||||
}
|
||||
|
||||
bool fp2array(const char * fingerprint, unsigned char * bytearray) {
|
||||
#if MQTT_USE_ASYNC
|
||||
|
||||
// check length (20 2-character digits ':'-separated => 20*2+19 = 59)
|
||||
bool mqttFormatFP(const char * fingerprint, unsigned char * bytearray) {
|
||||
|
||||
// check length (20 2-character digits ':' or ' ' separated => 20*2+19 = 59)
|
||||
if (strlen(fingerprint) != 59) return false;
|
||||
|
||||
DEBUG_MSG_P(PSTR("[MQTT] Fingerprint %s\n"), fingerprint);
|
||||
@@ -258,6 +270,30 @@ bool fp2array(const char * fingerprint, unsigned char * bytearray) {
|
||||
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
bool mqttFormatFP(const char * fingerprint, char * destination) {
|
||||
|
||||
// check length (20 2-character digits ':' or ' ' separated => 20*2+19 = 59)
|
||||
if (strlen(fingerprint) != 59) return false;
|
||||
|
||||
DEBUG_MSG_P(PSTR("[MQTT] Fingerprint %s\n"), fingerprint);
|
||||
|
||||
// copy it
|
||||
strncpy(destination, fingerprint, 59);
|
||||
|
||||
// walk the fingerprint replacing ':' for ' '
|
||||
for (unsigned char i = 0; i<59; i++) {
|
||||
if (destination[i] == ':') destination[i] = ' ';
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
void mqttConnect() {
|
||||
|
||||
if (!mqtt.connected()) {
|
||||
@@ -281,8 +317,6 @@ void mqttConnect() {
|
||||
last_try = millis();
|
||||
#endif
|
||||
|
||||
mqtt.disconnect();
|
||||
|
||||
if (_mqttUser) free(_mqttUser);
|
||||
if (_mqttPass) free(_mqttPass);
|
||||
|
||||
@@ -293,43 +327,84 @@ void mqttConnect() {
|
||||
if (_mqttWill) free(_mqttWill);
|
||||
_mqttWill = strdup((_mqttTopic + MQTT_TOPIC_STATUS).c_str());
|
||||
|
||||
DEBUG_MSG_P(PSTR("[MQTT] Connecting to broker at %s:%d"), host, port);
|
||||
mqtt.setServer(host, port);
|
||||
DEBUG_MSG_P(PSTR("[MQTT] Connecting to broker at %s:%d\n"), host, port);
|
||||
|
||||
#if MQTT_USE_ASYNC
|
||||
|
||||
mqtt.setServer(host, port);
|
||||
mqtt.setKeepAlive(MQTT_KEEPALIVE).setCleanSession(false);
|
||||
mqtt.setWill(_mqttWill, MQTT_QOS, MQTT_RETAIN, "0");
|
||||
if ((strlen(_mqttUser) > 0) && (strlen(_mqttPass) > 0)) {
|
||||
DEBUG_MSG_P(PSTR(" as user '%s'."), _mqttUser);
|
||||
DEBUG_MSG_P(PSTR("[MQTT] Connecting as user %s\n"), _mqttUser);
|
||||
mqtt.setCredentials(_mqttUser, _mqttPass);
|
||||
}
|
||||
DEBUG_MSG_P(PSTR("\n"));
|
||||
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
|
||||
bool secure = getSetting("mqttUseSSL", MQTT_SSL_ENABLED).toInt() == 1;
|
||||
mqtt.setSecure(secure);
|
||||
if (secure) {
|
||||
DEBUG_MSG_P(PSTR("[MQTT] Using SSL\n"));
|
||||
unsigned char fp[20];
|
||||
if (fp2array(getSetting("mqttFP", MQTT_SSL_FINGERPRINT).c_str(), fp)) {
|
||||
unsigned char fp[20] = {0};
|
||||
if (mqttFormatFP(getSetting("mqttFP", MQTT_SSL_FINGERPRINT).c_str(), fp)) {
|
||||
mqtt.addServerFingerprint(fp);
|
||||
} else {
|
||||
DEBUG_MSG_P(PSTR("[MQTT] Wrong fingerprint\n"));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // ASYNC_TCP_SSL_ENABLED
|
||||
|
||||
mqtt.connect();
|
||||
|
||||
#else
|
||||
#else // not MQTT_USE_ASYNC
|
||||
|
||||
bool response;
|
||||
bool response = true;
|
||||
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
|
||||
bool secure = getSetting("mqttUseSSL", MQTT_SSL_ENABLED).toInt() == 1;
|
||||
if (secure) {
|
||||
DEBUG_MSG_P(PSTR("[MQTT] Using SSL\n"));
|
||||
if (_mqttClientSecure.connect(host, port)) {
|
||||
char fp[60] = {0};
|
||||
if (mqttFormatFP(getSetting("mqttFP", MQTT_SSL_FINGERPRINT).c_str(), fp)) {
|
||||
if (_mqttClientSecure.verify(fp, host)) {
|
||||
mqtt.setClient(_mqttClientSecure);
|
||||
} else {
|
||||
DEBUG_MSG_P(PSTR("[MQTT] Invalid fingerprint\n"));
|
||||
response = false;
|
||||
}
|
||||
} else {
|
||||
DEBUG_MSG_P(PSTR("[MQTT] Wrong fingerprint\n"));
|
||||
response = false;
|
||||
}
|
||||
} else {
|
||||
DEBUG_MSG_P(PSTR("[MQTT] Client connection failed\n"));
|
||||
response = false;
|
||||
}
|
||||
|
||||
} else {
|
||||
mqtt.setClient(_mqttClient);
|
||||
}
|
||||
|
||||
#else // not ASYNC_TCP_SSL_ENABLED
|
||||
|
||||
mqtt.setClient(_mqttClient);
|
||||
|
||||
#endif // ASYNC_TCP_SSL_ENABLED
|
||||
|
||||
if (response) {
|
||||
|
||||
mqtt.setServer(host, port);
|
||||
|
||||
if ((strlen(_mqttUser) > 0) && (strlen(_mqttPass) > 0)) {
|
||||
DEBUG_MSG_P(PSTR("[MQTT] Connecting as user %s\n"), _mqttUser);
|
||||
response = mqtt.connect(getIdentifier().c_str(), _mqttUser, _mqttPass, _mqttWill, MQTT_QOS, MQTT_RETAIN, "0");
|
||||
} else {
|
||||
response = mqtt.connect(getIdentifier().c_str(), _mqttWill, MQTT_QOS, MQTT_RETAIN, "0");
|
||||
}
|
||||
|
||||
if ((strlen(_mqttUser) > 0) && (strlen(_mqttPass) > 0)) {
|
||||
DEBUG_MSG_P(PSTR(" as user '%s'\n"), _mqttUser);
|
||||
response = mqtt.connect(getIdentifier().c_str(), _mqttUser, _mqttPass, _mqttWill, MQTT_QOS, MQTT_RETAIN, "0");
|
||||
} else {
|
||||
DEBUG_MSG_P(PSTR("\n"));
|
||||
response = mqtt.connect(getIdentifier().c_str(), _mqttWill, MQTT_QOS, MQTT_RETAIN, "0");
|
||||
}
|
||||
|
||||
if (response) {
|
||||
@@ -339,7 +414,7 @@ void mqttConnect() {
|
||||
DEBUG_MSG_P(PSTR("[MQTT] Connection failed\n"));
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif // MQTT_USE_ASYNC
|
||||
|
||||
free(host);
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
|
||||
|
||||
*/
|
||||
|
||||
#if NTP_SUPPORT
|
||||
|
||||
#include <TimeLib.h>
|
||||
#include <NtpClientLib.h>
|
||||
#include <WiFiClient.h>
|
||||
@@ -66,3 +68,5 @@ void ntpSetup() {
|
||||
void ntpLoop() {
|
||||
now();
|
||||
}
|
||||
|
||||
#endif // NTP_SUPPORT
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -18,12 +18,12 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
|
||||
|
||||
#if WEB_EMBEDDED
|
||||
#include "static/index.html.gz.h"
|
||||
#endif
|
||||
#endif // WEB_EMBEDDED
|
||||
|
||||
#if ASYNC_TCP_SSL_ENABLED & WEB_SSL_ENABLED
|
||||
#include "static/server.cer.h"
|
||||
#include "static/server.key.h"
|
||||
#endif
|
||||
#endif // ASYNC_TCP_SSL_ENABLED & WEB_SSL_ENABLED
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@@ -302,7 +302,9 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
|
||||
setSetting(key, value);
|
||||
save = changed = true;
|
||||
if (key.startsWith("mqtt")) changedMQTT = true;
|
||||
if (key.startsWith("ntp")) changedNTP = true;
|
||||
#if NTP_SUPPORT
|
||||
if (key.startsWith("ntp")) changedNTP = true;
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
@@ -369,9 +371,9 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
|
||||
}
|
||||
|
||||
// Check if we should reconfigure NTP connection
|
||||
if (changedNTP) {
|
||||
ntpConnect();
|
||||
}
|
||||
#if NTP_SUPPORT
|
||||
if (changedNTP) ntpConnect();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
@@ -424,12 +426,15 @@ void _wsStart(uint32_t client_id) {
|
||||
root["sketch_size"] = ESP.getSketchSize();
|
||||
root["free_size"] = ESP.getFreeSketchSpace();
|
||||
|
||||
root["ntpStatus"] = ntpConnected();
|
||||
root["ntpServer1"] = getSetting("ntpServer1", NTP_SERVER);
|
||||
root["ntpServer2"] = getSetting("ntpServer2");
|
||||
root["ntpServer3"] = getSetting("ntpServer3");
|
||||
root["ntpOffset"] = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
|
||||
root["ntpDST"] = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1;
|
||||
#if NTP_SUPPORT
|
||||
root["ntpVisible"] = 1;
|
||||
root["ntpStatus"] = ntpConnected();
|
||||
root["ntpServer1"] = getSetting("ntpServer1", NTP_SERVER);
|
||||
root["ntpServer2"] = getSetting("ntpServer2");
|
||||
root["ntpServer3"] = getSetting("ntpServer3");
|
||||
root["ntpOffset"] = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
|
||||
root["ntpDST"] = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1;
|
||||
#endif
|
||||
|
||||
root["mqttStatus"] = mqttConnected();
|
||||
root["mqttServer"] = getSetting("mqttServer", MQTT_SERVER);
|
||||
|
||||
@@ -236,9 +236,11 @@ void wifiSetup() {
|
||||
#endif
|
||||
|
||||
// NTP connection reset
|
||||
if (code == MESSAGE_CONNECTED) {
|
||||
ntpConnect();
|
||||
}
|
||||
#if NTP_SUPPORT
|
||||
if (code == MESSAGE_CONNECTED) {
|
||||
ntpConnect();
|
||||
}
|
||||
#endif
|
||||
|
||||
// Manage POW
|
||||
#if HLW8012_SUPPORT
|
||||
|
||||
@@ -99,7 +99,7 @@
|
||||
<a href="#" class="pure-menu-link" data="panel-mqtt">MQTT</a>
|
||||
</li>
|
||||
|
||||
<li class="pure-menu-item">
|
||||
<li class="pure-menu-item module module-ntp">
|
||||
<a href="#" class="pure-menu-link" data="panel-ntp">NTP</a>
|
||||
</li>
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ data_dir = espurna/data
|
||||
|
||||
[common]
|
||||
build_flags = -g -DMQTT_MAX_PACKET_SIZE=400 ${env.FLAGS}
|
||||
debug_flags = -DDEBUG_ESP_CORE -DDEBUG_ESP_SSL -DDEBUG_ESP_WIFI -DDEBUG_ESP_HTTP_CLIENT -DDEBUG_ESP_HTTP_UPDATE -DDEBUG_ESP_HTTP_SERVER -DDEBUG_ESP_UPDATER -DDEBUG_ESP_OTA -DDEBUG_TLS_MEM
|
||||
build_flags_512k = ${common.build_flags} -Wl,-Tesp8266.flash.512k0.ld
|
||||
build_flags_1m = ${common.build_flags} -Wl,-Tesp8266.flash.1m0.ld
|
||||
lib_deps =
|
||||
@@ -80,7 +81,7 @@ framework = arduino
|
||||
board = nodemcuv2
|
||||
lib_deps = ${common.lib_deps}
|
||||
lib_ignore = ${common.lib_ignore}
|
||||
build_flags = ${common.build_flags} -DNODEMCU_LOLIN -DDEBUG_FAUXMO=Serial -DNOWSAUTH -DASYNC_TCP_SSL_ENABLED=1
|
||||
build_flags = ${common.build_flags} -DNODEMCU_LOLIN -DDEBUG_FAUXMO=Serial -DNOWSAUTH -DASYNC_TCP_SSL_ENABLED=1 ${common.debug_flags}
|
||||
upload_speed = 460800
|
||||
monitor_baud = 115200
|
||||
|
||||
|
||||
Reference in New Issue
Block a user