Compare commits

..

5 Commits

Author SHA1 Message Date
jean 1bcbd3b8e3 Notes.md, Readme.md und 2 weitere dateien aktualisiert... 2022-04-10 19:00:43 +02:00
jean c6ad3f2259 RelayClass 2022-04-10 15:13:23 +02:00
jean 156e095d4d CORS fix 2022-03-23 19:20:26 +01:00
jean 8caffd50b2 settings api completed 2022-03-20 16:46:21 +01:00
jean dc995d6e7e corrected admin file 2022-03-20 16:15:38 +01:00
18 changed files with 126 additions and 44 deletions
+11
View File
@@ -36,3 +36,14 @@
+ Mode 0 - station; 1 - client 4C uint8
+ AuthError Timeout (secs) 4D
+ OpenLock hold (secs) 4E
### 20.03.2022
+ settings API implemented and tested
+ TODO: factory reset
### 10.04.2022
+ Toni:
+ Reed Contact -> Door closed?
+ Tracking in->out
+ Stats
+ Sensors
+7 -2
View File
@@ -5,9 +5,14 @@ Adress 0x21
### 1.1 Wiring
* Red: 3.3V
* Black GND
* Green SDA -> D3
* Grey SCL -> D4
* Green SDA -> D2
* Grey SCL -> D1
## 2.0 LCD
The LCD display is driven on the same i2c bus as the keypad.
nterface Definition
BYTE BIT
7 (MSB) 6 5 4 3 2 1 0 (LSB)
I2C slave address L H L L A2 A1 A0 R/W
I/O data bus P7 P6 P5 P4 P3 P2 P1 P0
BIN
View File
Binary file not shown.
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1 +1 @@
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>doorlock_pwa</title><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-capable" content="yes"><link rel="apple-touch-icon" href="/assets/icons/apple-touch-icon.png"><link rel="manifest" href="/manifest.json"><meta name="theme-color" content="#673ab8"><style>*{box-sizing:border-box}html{font-family:Helvetica,sans-serif;font-size:16px}body,html{height:100%}body{background-color:#fff;margin:0;padding:0;width:100%}</style><link href="/bundle.45d14.css" rel="stylesheet" media="only x" onload="this.media='all'"><noscript><link rel="stylesheet" href="/bundle.45d14.css"></noscript></head><body><script defer="defer" src="/bundle.357e7.js"></script><script nomodule="" src="/polyfills.058fb.js"></script></body></html>
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>doorlock_pwa</title><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-capable" content="yes"><link rel="apple-touch-icon" href="/assets/icons/apple-touch-icon.png"><link rel="manifest" href="/manifest.json"><meta name="theme-color" content="#673ab8"><style>*{box-sizing:border-box}html{font-family:Helvetica,sans-serif;font-size:16px}body,html{height:100%}body{background-color:#fff;margin:0;padding:0;width:100%}</style><link href="/bundle.3bf6c.css" rel="stylesheet" media="only x" onload="this.media='all'"><noscript><link rel="stylesheet" href="/bundle.3bf6c.css"></noscript></head><body><script defer="defer" src="/bundle.4bdf3.js"></script><script nomodule="" src="/polyfills.914a6.js"></script></body></html>
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+7 -5
View File
@@ -1,6 +1,6 @@
#include "Config.h"
Config::Config()
Config::Config(const char* filepath): _filepath(filepath)
{
if (!LittleFS.begin())
{
@@ -12,10 +12,13 @@ Config::~Config()
{
free(buffer);
}
bool Config::setFilePath(const char* filepath){
this->_filepath = filepath;
return loadBin();
}
bool Config::loadBin()
{
File config_file = LittleFS.open("/settings", "r");
File config_file = LittleFS.open(_filepath, "r");
//if (buffer == nullptr) // Allocate only once
// buffer = (uint8_t *)malloc(CONFIG_SIZE);
if (config_file.available())
@@ -23,7 +26,6 @@ bool Config::loadBin()
config_file.read(buffer, CONFIG_SIZE);
buffer[OFFSET_SSID + 0x1F] = 0x00; // ensure ssid and password are terminated with a null character
buffer[OFFSET_PASS + 0x1F] = 0x00;
}
config_file.close();
return true;
@@ -31,7 +33,7 @@ bool Config::loadBin()
bool Config::saveBin()
{
File config_file = LittleFS.open("/settings", "w");
File config_file = LittleFS.open(_filepath, "w");
config_file.write(buffer, CONFIG_SIZE);
delay(100);
config_file.close();
+4 -3
View File
@@ -1,7 +1,6 @@
#pragma once
#include "LittleFS.h"
#include "ArduinoJson.h"
#define CONFIG_SIZE 0x4E
#define OFFSET_SSID 0x00
#define OFFSET_PASS 0x20
#define OFFSET_IP 0x40
@@ -10,13 +9,14 @@
#define OFFSET_MODE 0x4C
#define OFFSET_FAIL_TIMEOUT 0x4D
#define OFFSET_HOLD_TIME 0x4E
#define CONFIG_SIZE 0x4F
class Config
{
private:
uint8_t *buffer = (uint8_t *)malloc(CONFIG_SIZE);
public:
Config();
Config(const char* filepath);
~Config();
const char *SSID = (char *)(buffer + OFFSET_SSID);
const char *PASS = (char *)(buffer + OFFSET_PASS);
@@ -26,7 +26,8 @@ public:
uint8_t &mode = *(buffer + OFFSET_MODE);
uint8_t &fail_timeout = *(buffer+OFFSET_FAIL_TIMEOUT);
uint8_t &hold_time = *(buffer+OFFSET_HOLD_TIME);
const char* _filepath;
bool setFilePath(const char* filepath);
bool loadConfig();
bool loadBin();
bool saveBin();
+2 -4
View File
@@ -1,8 +1,6 @@
#include "Keyboard.h"
//#define DEBUG
#define PIN_WIRE_SDA D3
#define PIN_WIRE_SCL D4
Keyboard::Keyboard(uint8_t _debounce)
{
this->keybind.insert({
@@ -23,7 +21,7 @@ Keyboard::Keyboard(uint8_t _debounce)
}
void Keyboard::begin(TwoWire *databus)
{
pcf8574 = new PCF8574(databus, 0x21, PIN_WIRE_SDA, PIN_WIRE_SCL);
pcf8574 = new PCF8574(databus, 0x21);
pcf8574->pinMode(0, OUTPUT);
for (int i = 1; i < 8; i++)
{
+26
View File
@@ -0,0 +1,26 @@
#include "Relais.h"
Relais::Relais(uint8_t pin)
{
pinMode(pin, OUTPUT);
digitalWrite(_pin, 1);
_pin = pin;
}
void Relais::cylce()
{
if (!_state)
return;
if (millis() > _call_time)
{
digitalWrite(_pin, 1);
_state = false;
Serial.println("Relay released.");
}
}
void Relais::activate(uint8_t seconds)
{
Serial.println("Relay activated for "+String(seconds)+" Seconds.");
digitalWrite(_pin, 0);
_state = true;
_call_time = millis() + seconds * 1000;
}
+16
View File
@@ -0,0 +1,16 @@
#pragma once
#include <Arduino.h>
#include <Wire.h>
class Relais
{
public:
Relais(uint8_t pin);
void cylce();
void activate(uint8_t seconds);
private:
unsigned long _call_time = 0;
bool _state = false;
uint8_t _pin;
};
+4 -3
View File
@@ -1,8 +1,8 @@
#include "Rfid.h"
#define SS_PIN D8
#define RST_PIN D1
#define RST_PIN D0
#define RFID_TIMEOUT 3000
Rfid::Rfid(/* args */) : _mfrc522(SS_PIN)
Rfid::Rfid(/* args */) : _mfrc522(SS_PIN, RST_PIN)
{
}
@@ -33,11 +33,12 @@ void Rfid::scan()
{
_status = 1;
#ifdef DEBUG
Serial.print(this->_rfid);
_mfrc522.PICC_DumpToSerial(&(_mfrc522.uid));
#endif
this->_lastRfid = this->_rfid;
this->_lastRfidScan = millis();
}
}
}
+34 -14
View File
@@ -22,7 +22,7 @@ bool WebConsole::init(Config *config, userdb::UserDb *userdb)
Serial.println(WiFi.softAPConfig(_config->ip, _config->gw, _config->subnet) ? "Ready" : "Failed!");
Serial.print("\t2. DNS config... ");
Serial.println(_dnsServer->start(53, "*", _config->ip) ? "Ready" : "Failed!");
Serial.print("\t3 AP setup <SSID:\"" + String(_config->SSID) + "\"...");
Serial.print("\t3 AP setup SSID:\"" + String(_config->SSID) + "\"... ");
if (strlen(_config->PASS) > 0)
Serial.println(WiFi.softAP(_config->SSID, _config->PASS) ? "Ready" : "Failed!");
else
@@ -45,7 +45,7 @@ bool WebConsole::init(Config *config, userdb::UserDb *userdb)
_server->on(UriBraces("/api/user/{}"), HTTPMethod::HTTP_GET, std::bind(&WebConsole::_getUser, this));
_server->on(UriBraces("/api/user/{}"), HTTPMethod::HTTP_PUT, std::bind(&WebConsole::_createUser, this));
_server->on(UriBraces("/api/user/{}"), HTTPMethod::HTTP_POST, std::bind(&WebConsole::_updateUser, this));
_server->on(UriBraces("/api/config/{}"), std::bind(&WebConsole::_deleteUser, this));
_server->on(UriBraces("/api/config"), HTTPMethod::HTTP_POST, std::bind(&WebConsole::_settings, this));
_server->serveStatic("/", LittleFS, "/s/");
_server->onNotFound(std::bind(&WebConsole::_handleUnknown, this));
return true;
@@ -83,6 +83,11 @@ uint8_t WebConsole::isInterceptingRfid()
}
bool WebConsole::_isAuth()
{
if (_server->method() == HTTPMethod::HTTP_OPTIONS)
{
_server->send(204);
return false;
}
if (!_server->hasHeader("Authentification"))
{
_server->send(401, "text/plain", "Error 401: Unauthorized (missing auth token)");
@@ -131,6 +136,8 @@ void WebConsole::_auth()
void WebConsole::_settings()
{
_sendCORS();
if (!_isAuth())
return;
String action = _server->arg("action");
if (action.equals("update"))
{
@@ -155,10 +162,10 @@ void WebConsole::_settings()
sucess |= 0b00000010;
}
if (_server->hasArg("IP"))
if (_server->hasArg("ip"))
{
IPAddress temp;
if (temp.fromString(_server->arg("IP")))
if (temp.fromString(_server->arg("ip")))
{
_config->ip = temp.v4();
sucess |= 0b00000100;
@@ -166,10 +173,10 @@ void WebConsole::_settings()
else
error |= 0b00000100;
}
if (_server->hasArg("SUBNET"))
if (_server->hasArg("subnet"))
{
IPAddress temp;
if (temp.fromString(_server->arg("SUBNET")))
if (temp.fromString(_server->arg("subnet")))
{
_config->subnet = temp.v4();
sucess |= 0b00001000;
@@ -177,10 +184,10 @@ void WebConsole::_settings()
else
error |= 0b00001000;
}
if (_server->hasArg("GW"))
if (_server->hasArg("gw"))
{
IPAddress temp;
if (temp.fromString(_server->arg("GW")))
if (temp.fromString(_server->arg("gw")))
{
_config->gw = temp.v4();
sucess |= 0b00010000;
@@ -188,25 +195,25 @@ void WebConsole::_settings()
else
error |= 0b00010000;
}
if (_server->hasArg("MODE"))
if (_server->hasArg("mode"))
{
_config->mode = _server->arg("MODE").toInt();
_config->mode = _server->arg("mode").toInt();
if (_config->mode >= 0 && _config->mode <= 1)
sucess |= 0b00100000;
else
error |= 0b00100000;
}
if (_server->hasArg("FAIL_TIMEOUT"))
if (_server->hasArg("fail_timeout"))
{
_config->fail_timeout = _server->arg("FAIL_TIMEOUT").toInt();
_config->fail_timeout = _server->arg("fail_timeout").toInt();
if (_config->fail_timeout >= 0 && _config->fail_timeout <= 250)
sucess |= 0b01000000;
else
error |= 0b01000000;
}
if (_server->hasArg("HOLD_TIME"))
if (_server->hasArg("hold_time"))
{
_config->hold_time = _server->arg("HOLD_TIME").toInt();
_config->hold_time = _server->arg("hold_time").toInt();
if (_config->hold_time > 0 && _config->hold_time <= 250)
sucess |= 0b10000000;
else
@@ -222,7 +229,20 @@ void WebConsole::_settings()
_config->loadBin();
_server->send(200, "text/json", "{\"status\":\"failed\", \"sucess\":\"" + String(sucess) + "\", \"error\":\"" + String(error) + "\"}");
}
else
_server->send(200, "text/json", "{\"status\":\"noting_todo\"}");
}
else if (action.equals("get"))
{
_server->send(200, "text/json", "{\"SSID\":\"" + String(_config->SSID) + "\", \"PASS\":\"" + String(_config->PASS) + "\", \"ip\":\"" + IPAddress(_config->ip).toString() + "\", \"subnet\":\"" + IPAddress(_config->subnet).toString() + "\", \"gw\":\"" + IPAddress(_config->gw).toString() + "\", \"mode\":\"" + String(_config->mode) + "\", \"fail_timeout\":\"" + String(_config->fail_timeout) + "\", \"hold_time\":\"" + String(_config->hold_time) + "\"}");
}
else if (action.equals("apply"))
{
_server->send(200, "text/json", "{\"status\":\"restarting\"}");
ESP.restart();
}
else
_server->send(404, "text/plain", "unknown action");
}
void WebConsole::_sendCORS()
{
+8 -6
View File
@@ -8,17 +8,18 @@
#include "WebConsole.h"
#include "UserDb.h"
#include "Config.h"
#include "Relais.h"
// File config
Config config;
userdb::UserDb userdatabase("userdb.csv");
Config config("/settings");
userdb::UserDb userdatabase("/userdb.csv");
webconsole::WebConsole web;
// Rfid
Rfid rfid;
// i2C Bus
#define PIN_WIRE_SDA D3
#define PIN_WIRE_SCL D4
#define PIN_WIRE_SDA D1
#define PIN_WIRE_SCL D2
Relais relay(D4);
Keyboard keyboard(200);
Interface iface;
void setup()
@@ -34,11 +35,11 @@ void setup()
rfid.begin();
web.attachRfid(&rfid);
iface.begin(&keyboard);
config.print();
}
void loop()
{
relay.cylce();
rfid.scan();
web.serve();
keyboard.scanAsync();
@@ -75,6 +76,7 @@ void loop()
{
iface.greetUser(login_user.first_name + " " + login_user.last_name);
Serial.println("Logon from User " + login_user.toString());
relay.activate(config.hold_time);
}
iface.render();
}