Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New features: watchdog, status led, web server auth #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

.pio/

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
Expand Down Expand Up @@ -106,3 +108,5 @@ cmake-build-debug
# End of https://www.gitignore.io/api/clion+all,platformio,visualstudiocode
#
secrets.h
.vscode/launch.json
.pio/build/project.checksum
2 changes: 1 addition & 1 deletion .pio/build/project.checksum
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d03f253803ab29b9105b4976c1a1c7e882ed3b02
444c4573a3053783784b69c1be55a30f7b76e4dd
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
ESP32 port of the keyble library
working, with some additions!

new features/changes from corgan: watchdog, status led, webserver auth

- watchdog set WDT_TIMEOUT (sec) after this time the esp will reset if something hangs up
- status LED on GPIO32: constant light=wifi conected, slow blink: mqtt conected and waiting, fast blink=conected to keyble
- webserver auth, set WEBSERVER_USER and WEBSERVER_PW. Default admin:admin


Thanks go to <a href="https://github.com/RoP09">RoP09</a>, <a href="https://github.com/tc-maxx">tc-maxx</a>, <a href="https://github.com/henfri">henfri</a>, <a href="https://github.com/MariusSchiffer">MariusSchiffer</a> and of course <a href="https://github.com/oyooyo">oyooyo</a> for their brillant work!


Expand Down
8 changes: 6 additions & 2 deletions lib/eQ3/eQ3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,14 @@ bool eQ3::onTick() {
//xSemaphoreGive(mutex); // function will take the semaphore again
queueFunc->second();
}
} else {
} else if (state.connectionState != DISCONNECTED) {
sendNextFragment();
lastActivity = time(NULL);
}
}
// } else {
// sendNextFragment();
// lastActivity = time(NULL);
// }
// TODO disconnect if no answer for long time?
/* if (state.connectionState >= CONNECTED && difftime(lastActivity, time(NULL)) > LOCK_TIMEOUT && sendQueue.empty()) {
Serial.println("# Lock timeout");
Expand Down
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ board = esp32dev
framework = arduino
monitor_speed = 115200
lib_ldf_mode = deep
upload_port = COM5
lib_deps =
ESP32 BLE Arduino @ 1.0.1
PubSubClient
Expand Down
95 changes: 86 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <FS.h>
#include <SPIFFS.h>
#include "AutoConnect.h"
#include <esp_task_wdt.h>

#define PARAM_FILE "/keyble.json"
#define AUX_SETTING_URI "/keyble_setting"
Expand All @@ -33,8 +34,13 @@

#define CARD_KEY "M001AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

#define WDT_TIMEOUT 20 // WDT Timeout in
#define LED_GPIO 32 // LED GPIO

#define WEBSERVER_USER "admin"
#define WEBSERVER_PW "admin"

// ---[Variables]---------------------------------------------------------------
#pragma region
WebServer Server;
AutoConnect Portal(Server);
AutoConnectConfig config;
Expand Down Expand Up @@ -76,12 +82,11 @@ String mqtt_pub2 = "";
String mqtt_pub3 = "";
String mqtt_pub4 = "";


WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
#pragma endregion

// ---[Add Menue Items]---------------------------------------------------------
#pragma region

static const char AUX_keyble_setting[] PROGMEM = R"raw(
[
{
Expand Down Expand Up @@ -180,7 +185,23 @@ static const char AUX_keyble_setting[] PROGMEM = R"raw(
]
)raw";

#pragma endregion
static const char PAGE_AUTH[] PROGMEM = R"(
{
"uri": "/auth",
"title": "Auth",
"menu": true,
"element": [
{
"name": "text",
"type": "ACText",
"value": "AutoConnect has authorized",
"style": "font-family:Arial;font-size:18px;font-weight:400;color:#191970"
}
]
}
)";


// ---[getParams]---------------------------------------------------------------
void getParams(AutoConnectAux& aux) {

Expand All @@ -201,6 +222,8 @@ void getParams(AutoConnectAux& aux) {
KeyBleUserId = aux["KeyBleUserId"].value;
KeyBleUserId.trim();
}


// ---[loadParams]--------------------------------------------------------------
String loadParams(AutoConnectAux& aux, PageArgument& args) {
(void)(args);
Expand All @@ -222,6 +245,8 @@ String loadParams(AutoConnectAux& aux, PageArgument& args) {
Serial.println("# " PARAM_FILE " open failed");
return String("");
}


// ---[saveParams]--------------------------------------------------------------
String saveParams(AutoConnectAux& aux, PageArgument& args) {
AutoConnectAux& keyble_setting = *Portal.aux(Portal.where());
Expand All @@ -245,6 +270,8 @@ String saveParams(AutoConnectAux& aux, PageArgument& args) {

return String("");
}


// ---[MqttCallback]------------------------------------------------------------
void MqttCallback(char* topic, byte* payload, unsigned int length) {
Serial.print("# Message received: ");
Expand Down Expand Up @@ -294,6 +321,8 @@ void MqttCallback(char* topic, byte* payload, unsigned int length) {
Serial.println(mqtt_sub);
}
}


// ---[MQTTpublish]-------------------------------------------------------------
void MqttPublish()
{
Expand Down Expand Up @@ -371,6 +400,8 @@ void MqttPublish()

Serial.println("# waiting for command...");
}


// ---[MQTT-Setup]--------------------------------------------------------------
void SetupMqtt() {
while (!mqttClient.connected()) { // Loop until we're reconnected to the MQTT server
Expand All @@ -387,6 +418,8 @@ void SetupMqtt() {
}
}
}


// ---[RootPage]----------------------------------------------------------------
void rootPage()
{
Expand Down Expand Up @@ -432,7 +465,7 @@ void rootPage()
"<h2 align=\"center\" style=\"color:green;margin:20px;\">MQTT User Name: " + MqttUserName + "</h2>"
"<h2 align=\"center\" style=\"color:green;margin:20px;\">MQTT Topic: " + MqttTopic + "</h2>"
"<h2 align=\"center\" style=\"color:green;margin:20px;\">KeyBLE MAC Address: " + KeyBleMac + "</h2>"
"<h2 align=\"center\" style=\"color:green;margin:20px;\">KeyBLE User Key: " + KeyBleUserKey + "</h2>"

"<h2 align=\"center\" style=\"color:green;margin:20px;\">KeyBLE User ID: " + KeyBleUserId + "</h2>"
"<h2 align=\"center\" style=\"color:green;margin:20px;\">KeyBLE last battery state: " + mqtt_pub3 + "</h2>"
"<h2 align=\"center\" style=\"color:green;margin:20px;\">KeyBLE last command received: " + mqtt_sub + "</h2>"
Expand All @@ -441,13 +474,17 @@ void rootPage()
"<h2 align=\"center\" style=\"color:green;margin:20px;\">KeyBLE last task: " + mqtt_pub2 + "</h2>"
"<br>"
"<h2 align=\"center\" style=\"color:blue;margin:20px;\">page refresh every 30 seconds</h2>";

//"<h2 align=\"center\" style=\"color:green;margin:20px;\">KeyBLE User Key: " + KeyBleUserKey + "</h2>"
}

content +=
"</body>"
"</html>";
Server.send(200, "text/html", content);
}


// ---[Wifi Signalquality]-----------------------------------------------------
int GetWifiSignalQuality() {
float signal = 2 * (WiFi.RSSI() + 100);
Expand All @@ -456,6 +493,8 @@ if (signal > 100)
else
return signal;
}


// ---[SetWifi]-----------------------------------------------------------------
void SetWifi(bool active) {
wifiActive = active;
Expand All @@ -466,16 +505,22 @@ void SetWifi(bool active) {
else {
WiFi.mode(WIFI_OFF);
Serial.println("# WiFi disabled");
digitalWrite(LED_GPIO,LOW);
}
}


// ---[SetupWiFi]---------------------------------------------------------------
void SetupWifi()
{
digitalWrite(LED_GPIO,LOW);

if (Portal.begin())
{
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("# WIFI: connected to SSiD: " + WiFi.SSID());
digitalWrite(LED_GPIO,HIGH);
}
int maxWait=100;
while (WiFi.status() != WL_CONNECTED) {
Expand All @@ -489,8 +534,11 @@ void SetupWifi()
Serial.println("# WIFI: connected!");
Serial.println("# WIFI: signalquality: " + String(GetWifiSignalQuality()) + "%");
Serial.println("# WiFi connected to IP: " + WiFi.localIP().toString());
digitalWrite(LED_GPIO,HIGH);
}
}


// ---[Setup]-------------------------------------------------------------------
void setup() {
delay(1000);
Expand All @@ -511,8 +559,19 @@ void setup() {
config.title = AP_TITLE;
config.gateway = IPAddress(AP_IP);
config.ota = AC_OTA_BUILTIN;

//basic auth
config.auth = AC_AUTH_DIGEST;
config.authScope = AC_AUTHSCOPE_AUX;
config.username = WEBSERVER_USER;
config.password = WEBSERVER_PW;

Server.on("/", rootPage);
Portal.config(config);
Portal.load(FPSTR(PAGE_AUTH));

//status led
pinMode(LED_GPIO,OUTPUT);

if (Portal.load(FPSTR(AUX_keyble_setting))) {
AutoConnectAux& keyble_setting = *Portal.aux(AUX_SETTING_URI);
Expand Down Expand Up @@ -546,10 +605,17 @@ void setup() {
Serial.println("# Please fill in MQTT and KeyBLE credentials first!");

}

//watchdog
esp_task_wdt_init(WDT_TIMEOUT, true); // Initialize ESP32 Task WDT
esp_task_wdt_add(NULL); // Subscribe to the Task WDT

}


// ---[loop]--------------------------------------------------------------------
void loop() {

Portal.handleClient();

// This statement will declare pin 0 as digital input
Expand All @@ -568,6 +634,7 @@ if (Push_button_state == LOW && WiFi.status() == WL_CONNECTED)
// Wifi reconnect
if (wifiActive)
{
digitalWrite(LED_GPIO,HIGH);
if (WiFi.status() != WL_CONNECTED)
{
Serial.println("# WiFi disconnected, reconnect...");
Expand Down Expand Up @@ -598,6 +665,9 @@ if (wifiActive)
else if(mqttClient.connected())
{
mqttClient.loop();
digitalWrite(LED_GPIO,LOW);
delay(250);
digitalWrite(LED_GPIO,HIGH);
}
}
}
Expand Down Expand Up @@ -702,6 +772,10 @@ if (do_open || do_lock || do_unlock || do_status || do_toggle || do_pair)
bool timeout=(millis() - starttime > LOCK_TIMEOUT *2000 +1000);
bool finished=false;

digitalWrite(LED_GPIO,LOW);
delay(50);
digitalWrite(LED_GPIO,HIGH);

if ((keyble->_LockStatus != -1) || timeout)
{
if(keyble->_LockStatus == 1)
Expand All @@ -710,7 +784,7 @@ if (do_open || do_lock || do_unlock || do_status || do_toggle || do_pair)
if(timeout)
{
finished=true;
Serial.println("!!! Lockstatus 1 - timeout !!!");
Serial.println("!!! Lockstatus 1 - timeout1 !!!");
}
}
else if(keyble->_LockStatus == -1)
Expand All @@ -720,7 +794,7 @@ if (do_open || do_lock || do_unlock || do_status || do_toggle || do_pair)
{
keyble->_LockStatus = 9; //timeout
finished=true;
Serial.println("!!! Lockstatus -1 - timeout !!!");
Serial.println("!!! Lockstatus -1 - timeout2 !!!");
}
}
else if(keyble->_LockStatus != 1)
Expand Down Expand Up @@ -749,4 +823,7 @@ if (do_open || do_lock || do_unlock || do_status || do_toggle || do_pair)
}
}
}

esp_task_wdt_reset();

}