Compare commits
4 Commits
6256b6ef33
...
master
Author | SHA1 | Date | |
---|---|---|---|
da22f8a37c | |||
3afe8b16a9 | |||
1504ce6b0c | |||
4a88d1a380 |
@ -28,4 +28,6 @@ struct EthernetConfiguration {
|
||||
// The IP address of the DNS server, if DHCP fails
|
||||
uint8_t manualDnsAddress[4];
|
||||
|
||||
// The port for the incoming UDP connection
|
||||
uint16_t udpPort;
|
||||
};
|
@ -3,27 +3,59 @@
|
||||
#include "server.h"
|
||||
#include "servo.h"
|
||||
#include "message.h"
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include "configurations/EthernetConfiguration.h"
|
||||
#include "configurations/KeyConfiguration.h"
|
||||
|
||||
enum class SesameDeviceStatus {
|
||||
|
||||
/**
|
||||
* @brief The initial state of the device after boot
|
||||
*/
|
||||
initial,
|
||||
|
||||
/**
|
||||
* @brief The device has configured the individual parts,
|
||||
* but has no the ethernet hardware detected.
|
||||
*/
|
||||
configuredButNoEthernetHardware,
|
||||
|
||||
/**
|
||||
* @brief The device has ethernet hardware, but no ethernet link
|
||||
*/
|
||||
ethernetHardwareButNoLink,
|
||||
|
||||
/**
|
||||
* @brief The device has an ethernet link, but no IP address.
|
||||
*/
|
||||
ethernetLinkButNoIP,
|
||||
|
||||
/**
|
||||
* @brief The device has an IP address, but no socket connection
|
||||
*/
|
||||
ipAddressButNoSocketConnection,
|
||||
};
|
||||
|
||||
class SesameController: public ServerConnectionCallbacks {
|
||||
|
||||
public:
|
||||
SesameController(uint16_t localWebServerPort);
|
||||
|
||||
void configure(ServoConfiguration servoConfig, ServerConfiguration serverConfig, EthernetConfiguration ethernetConfig, KeyConfiguration keyConfig);
|
||||
SesameController(ServoConfiguration servoConfig, ServerConfiguration serverConfig, EthernetConfiguration ethernetConfig, KeyConfiguration keyConfig);
|
||||
|
||||
void loop(uint32_t millis);
|
||||
|
||||
private:
|
||||
|
||||
SesameDeviceStatus status = SesameDeviceStatus::initial;
|
||||
|
||||
uint32_t currentTime = 0;
|
||||
|
||||
ServerConnection server;
|
||||
ServoController servo;
|
||||
AsyncWebServer localWebServer;
|
||||
|
||||
// UDP
|
||||
|
||||
// An EthernetUDP instance to send and receive packets over UDP
|
||||
EthernetUDP udp;
|
||||
EthernetHardwareStatus ethernetStatus;
|
||||
EthernetConfiguration ethernetConfig;
|
||||
bool ethernetIsConfigured = false;
|
||||
|
||||
@ -40,6 +72,31 @@ private:
|
||||
|
||||
SignedMessage outgoingMessage;
|
||||
|
||||
// MARK: Ethernet
|
||||
|
||||
void initializeSpiBusForEthernetModule();
|
||||
|
||||
/**
|
||||
* @brief Checks to ensure that Ethernet hardware is available
|
||||
*
|
||||
* @return true The hardware is available
|
||||
* @return false The hardware is missing
|
||||
*/
|
||||
bool hasAvailableEthernetHardware();
|
||||
|
||||
/**
|
||||
* @brief Check that an active ethernet link is available
|
||||
*
|
||||
* @return true Link is available
|
||||
* @return false Link is absent
|
||||
*/
|
||||
bool hasEthernetLink();
|
||||
|
||||
void configureEthernet();
|
||||
|
||||
void startUDP();
|
||||
void stopUDP();
|
||||
|
||||
bool hasCurrentChallenge() {
|
||||
return currentChallengeExpiry > currentTime;
|
||||
}
|
||||
@ -52,7 +109,7 @@ private:
|
||||
|
||||
// MARK: Local client callbacks
|
||||
|
||||
void handleLocalMessage(AsyncWebServerRequest *request);
|
||||
void checkLocalMessage();
|
||||
|
||||
// MARK: Socket Callbacks
|
||||
|
||||
@ -76,7 +133,7 @@ private:
|
||||
*
|
||||
* Note: Prepares the response in the outgoing message buffer.
|
||||
*/
|
||||
void processMessage(SignedMessage* message);
|
||||
void processMessage(SignedMessage* message, bool shouldPerformUnlock);
|
||||
|
||||
/**
|
||||
* @brief Checks that the message is valid and prepares a challenge.
|
||||
@ -105,7 +162,7 @@ private:
|
||||
*
|
||||
* Note: Prepares the response in the outgoing message buffer.
|
||||
*/
|
||||
void completeUnlockRequest(Message* message);
|
||||
void completeUnlockRequest(Message* message, bool shouldPerformUnlock);
|
||||
|
||||
// MARK: Responses
|
||||
|
||||
@ -117,12 +174,18 @@ private:
|
||||
*/
|
||||
void prepareResponseBuffer(MessageResult event, Message* message = NULL);
|
||||
|
||||
/**
|
||||
* @brief Read a message from the UDP port
|
||||
*
|
||||
*/
|
||||
bool readLocalMessage();
|
||||
|
||||
/**
|
||||
* @brief Send the prepared outgoing message to a locally connected client
|
||||
*
|
||||
* @param request The original request of the client
|
||||
*/
|
||||
void sendPreparedLocalResponse(AsyncWebServerRequest *request);
|
||||
void sendPreparedLocalResponse();
|
||||
|
||||
/**
|
||||
* @brief Send the prepared outgoing message to the server
|
||||
|
@ -46,14 +46,16 @@ class ServerConnection {
|
||||
|
||||
public:
|
||||
|
||||
ServerConnection();
|
||||
ServerConnection(ServerConfiguration configuration);
|
||||
|
||||
/**
|
||||
* @brief Set the configuration and the callback handler
|
||||
*
|
||||
* @param callback The handler to handle messages and errors
|
||||
*/
|
||||
void configure(ServerConfiguration configuration, ServerConnectionCallbacks* callbacks);
|
||||
void setCallbacks(ServerConnectionCallbacks* callbacks);
|
||||
|
||||
void shouldConnect(bool connect);
|
||||
|
||||
/**
|
||||
* @brief Call this function regularly to handle socket operations.
|
||||
@ -73,7 +75,8 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
uint32_t currentTime;
|
||||
uint32_t currentTime = 0;
|
||||
bool shouldBeConnected = false;
|
||||
|
||||
bool socketIsConnected() {
|
||||
return webSocket.isConnected();
|
||||
@ -81,15 +84,12 @@ private:
|
||||
|
||||
void connect();
|
||||
|
||||
void disconnect();
|
||||
|
||||
bool shouldReconnect = true;
|
||||
bool isConnecting = false;
|
||||
bool isDisconnecting = false;
|
||||
uint32_t connectionTimeout = 0;
|
||||
uint32_t nextReconnectAttemptMs = 0;
|
||||
|
||||
void didDisconnect();
|
||||
void didConnect();
|
||||
void didChangeConnectionState(bool isConnected);
|
||||
|
||||
|
||||
ServerConfiguration configuration;
|
||||
|
@ -52,20 +52,15 @@ public:
|
||||
|
||||
/**
|
||||
* @brief Construct a new servo controller
|
||||
*/
|
||||
ServoController();
|
||||
|
||||
/**
|
||||
* @brief Configure the servo
|
||||
*
|
||||
* @param The configuration for the servo
|
||||
*/
|
||||
void configure(ServoConfiguration configuration);
|
||||
ServoController(ServoConfiguration configuration);
|
||||
|
||||
/**
|
||||
* @brief Update the servo state periodically
|
||||
*
|
||||
* This function should be periodically called to update the servo state,
|
||||
* This function will be periodically called to update the servo state,
|
||||
* specifically to release the button after the opening time has elapsed.
|
||||
*
|
||||
* There is no required interval to call this function, but the accuracy of
|
||||
@ -78,16 +73,14 @@ public:
|
||||
*/
|
||||
void pressButton();
|
||||
|
||||
/**
|
||||
* Release the door opener button by moving the servo arm.
|
||||
*/
|
||||
void releaseButton();
|
||||
|
||||
private:
|
||||
|
||||
// Indicator that the door button is pushed
|
||||
bool buttonIsPressed = false;
|
||||
|
||||
// Indicate that the button should be pressed
|
||||
bool shouldPressButton = false;
|
||||
|
||||
uint32_t openDuration = 0;
|
||||
|
||||
int pressedValue = 0;
|
||||
@ -103,4 +96,12 @@ private:
|
||||
// PWM Module needed for the servo
|
||||
ESP32PWM pwm;
|
||||
|
||||
// The task on core 1 that resets the servo
|
||||
TaskHandle_t servoResetTask;
|
||||
|
||||
/**
|
||||
* Release the door opener button by moving the servo arm.
|
||||
*/
|
||||
void releaseButton();
|
||||
|
||||
};
|
@ -13,9 +13,7 @@ platform = espressif32
|
||||
board = az-delivery-devkit-v4
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
; links2004/WebSockets@^2.4.0
|
||||
madhephaestus/ESP32Servo@^1.1.0
|
||||
ottowinter/ESPAsyncWebServer-esphome@^3.0.0
|
||||
arduino-libraries/Ethernet@^2.0.2
|
||||
https://github.com/christophhagen/arduinoWebSockets#master
|
||||
rweather/Crypto@^0.4.0
|
||||
|
@ -5,88 +5,172 @@
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
|
||||
SesameController::SesameController(uint16_t localWebServerPort) : localWebServer(localWebServerPort) {
|
||||
SesameController::SesameController(ServoConfiguration servoConfig, ServerConfiguration serverConfig, EthernetConfiguration ethernetConfig, KeyConfiguration keyConfig)
|
||||
: ethernetConfig(ethernetConfig), keyConfig(keyConfig), servo(servoConfig), server(serverConfig) {
|
||||
|
||||
}
|
||||
|
||||
void SesameController::configure(ServoConfiguration servoConfig, ServerConfiguration serverConfig, EthernetConfiguration ethernetConfig, KeyConfiguration keyConfig) {
|
||||
this->ethernetConfig = ethernetConfig;
|
||||
this->keyConfig = keyConfig;
|
||||
|
||||
// Ensure source of random numbers without WiFi and Bluetooth
|
||||
enableCrypto();
|
||||
|
||||
// Initialize SPI interface to Ethernet module
|
||||
void SesameController::initializeSpiBusForEthernetModule() {
|
||||
SPI.begin(ethernetConfig.spiPinSclk, ethernetConfig.spiPinMiso, ethernetConfig.spiPinMosi, ethernetConfig.spiPinSS); //SCLK, MISO, MOSI, SS
|
||||
pinMode(ethernetConfig.spiPinSS, OUTPUT);
|
||||
|
||||
Ethernet.init(ethernetConfig.spiPinSS);
|
||||
|
||||
if (Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.dhcpLeaseTimeoutMs, ethernetConfig.dhcpLeaseResponseTimeoutMs) == 1) {
|
||||
Serial.print("[INFO] DHCP assigned IP ");
|
||||
Serial.println(Ethernet.localIP());
|
||||
ethernetIsConfigured = true;
|
||||
} else {
|
||||
// Check for Ethernet hardware present
|
||||
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
||||
Serial.println("[ERROR] Ethernet shield not found.");
|
||||
} else if (Ethernet.linkStatus() == LinkOFF) {
|
||||
Serial.println("[ERROR] Ethernet cable is not connected.");
|
||||
} else if (Ethernet.linkStatus() == Unknown) {
|
||||
Serial.println("[ERROR] Ethernet cable status unknown.");
|
||||
} else if (Ethernet.linkStatus() == LinkON) {
|
||||
Serial.println("[INFO] Ethernet cable is connected.");
|
||||
|
||||
// Try to configure using IP address instead of DHCP
|
||||
Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.manualIp, ethernetConfig.manualDnsAddress);
|
||||
Serial.print("[WARNING] DHCP failed, using self-assigned IP ");
|
||||
Serial.println(Ethernet.localIP());
|
||||
ethernetIsConfigured = true;
|
||||
}
|
||||
}
|
||||
|
||||
servo.configure(servoConfig);
|
||||
Serial.println("[INFO] Servo configured");
|
||||
|
||||
// Direct messages and errors over the websocket to the controller
|
||||
server.configure(serverConfig, this);
|
||||
Serial.println("[INFO] Server connection configured");
|
||||
|
||||
// Direct messages from the local web server to the controller
|
||||
localWebServer.on("/message", HTTP_POST, [this] (AsyncWebServerRequest *request) {
|
||||
this->handleLocalMessage(request);
|
||||
this->sendPreparedLocalResponse(request);
|
||||
});
|
||||
|
||||
Serial.println("[INFO] Local web server configured");
|
||||
}
|
||||
|
||||
void SesameController::loop(uint32_t millis) {
|
||||
currentTime = millis;
|
||||
|
||||
switch (status) {
|
||||
case SesameDeviceStatus::initial:
|
||||
// In initial state, first configure SPI and
|
||||
enableCrypto(); // Ensure source of random numbers without WiFi and Bluetooth
|
||||
initializeSpiBusForEthernetModule();
|
||||
// Direct messages and errors over the websocket to the controller
|
||||
server.setCallbacks(this);
|
||||
configureEthernet();
|
||||
status = SesameDeviceStatus::configuredButNoEthernetHardware;
|
||||
Serial.println("[INFO] State: initial -> noHardware");
|
||||
// Directly check for ethernet hardware
|
||||
// break;
|
||||
case SesameDeviceStatus::configuredButNoEthernetHardware:
|
||||
if (!hasAvailableEthernetHardware()) {
|
||||
// No ethernet hardware found, wait
|
||||
// TODO: Try rebooting after some time as a potential fix?
|
||||
break;
|
||||
}
|
||||
status = SesameDeviceStatus::ethernetHardwareButNoLink;
|
||||
Serial.println("[INFO] State: noHardware -> noLink");
|
||||
// Directly check for Ethernet link
|
||||
// break;
|
||||
case SesameDeviceStatus::ethernetHardwareButNoLink:
|
||||
if (!hasEthernetLink()) {
|
||||
if (!hasAvailableEthernetHardware()) {
|
||||
status = SesameDeviceStatus::configuredButNoEthernetHardware;
|
||||
Serial.println("[INFO] State: noLink -> noHardware");
|
||||
break;
|
||||
}
|
||||
// Wait for ethernet link
|
||||
// TODO: Try rebooting after some time as a potential fix?
|
||||
break;
|
||||
}
|
||||
status = SesameDeviceStatus::ethernetLinkButNoIP;
|
||||
Serial.println("[INFO] State: noLink -> noIP");
|
||||
// Directly check for socket connection
|
||||
// break;
|
||||
|
||||
case SesameDeviceStatus::ethernetLinkButNoIP:
|
||||
if (!hasEthernetLink()) {
|
||||
if (hasAvailableEthernetHardware()) {
|
||||
status = SesameDeviceStatus::ethernetHardwareButNoLink;
|
||||
Serial.println("[INFO] State: noIP -> noLink");
|
||||
} else {
|
||||
status = SesameDeviceStatus::configuredButNoEthernetHardware;
|
||||
Serial.println("[INFO] State: noIP -> noHardware");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
startUDP();
|
||||
status = SesameDeviceStatus::ipAddressButNoSocketConnection;
|
||||
Serial.println("[INFO] State: noIP -> noSocket");
|
||||
// Directly check for socket connection
|
||||
// break;
|
||||
|
||||
case SesameDeviceStatus::ipAddressButNoSocketConnection:
|
||||
if (!hasEthernetLink()) {
|
||||
server.shouldConnect(false);
|
||||
stopUDP();
|
||||
if (!hasAvailableEthernetHardware()) {
|
||||
status = SesameDeviceStatus::configuredButNoEthernetHardware;
|
||||
Serial.println("[INFO] State: noSocket -> noHardware");
|
||||
} else {
|
||||
status = SesameDeviceStatus::ethernetHardwareButNoLink;
|
||||
Serial.println("[INFO] State: noSocket -> noLink");
|
||||
}
|
||||
break;
|
||||
}
|
||||
server.shouldConnect(true);
|
||||
server.loop(millis);
|
||||
servo.loop(millis);
|
||||
checkLocalMessage();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool SesameController::hasAvailableEthernetHardware() {
|
||||
EthernetHardwareStatus ethernetStatus = Ethernet.hardwareStatus();
|
||||
|
||||
static bool didNotify = false;
|
||||
|
||||
if (ethernetStatus != EthernetW5500) {
|
||||
if (!didNotify) {
|
||||
Serial.print("[ERROR] No Ethernet hardware found: ");
|
||||
Serial.println(ethernetStatus);
|
||||
didNotify = true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SesameController::hasEthernetLink() {
|
||||
return Ethernet.linkStatus() == EthernetLinkStatus::LinkON;
|
||||
}
|
||||
|
||||
void SesameController::configureEthernet() {
|
||||
if (Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.dhcpLeaseTimeoutMs, ethernetConfig.dhcpLeaseResponseTimeoutMs) == 1) {
|
||||
Serial.print("[INFO] DHCP assigned IP ");
|
||||
Serial.println(Ethernet.localIP());
|
||||
return;
|
||||
}
|
||||
Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.manualIp, ethernetConfig.manualDnsAddress);
|
||||
Serial.print("[WARNING] DHCP failed, using self-assigned IP ");
|
||||
Serial.println(Ethernet.localIP());
|
||||
}
|
||||
|
||||
void SesameController::startUDP() {
|
||||
udp.begin(ethernetConfig.udpPort);
|
||||
}
|
||||
|
||||
void SesameController::stopUDP() {
|
||||
udp.stop();
|
||||
}
|
||||
|
||||
// MARK: Local
|
||||
|
||||
void SesameController::handleLocalMessage(AsyncWebServerRequest *request) {
|
||||
if (!request->hasParam(messageUrlParameter)) {
|
||||
Serial.println("Missing url parameter");
|
||||
prepareResponseBuffer(MessageResult::InvalidUrlParameter);
|
||||
return;
|
||||
void SesameController::checkLocalMessage() {
|
||||
if (readLocalMessage()) {
|
||||
sendPreparedLocalResponse();
|
||||
}
|
||||
String encoded = request->getParam(messageUrlParameter)->value();
|
||||
if (!convertHexMessageToBinary(encoded.c_str())) {
|
||||
Serial.println("Invalid hex encoding");
|
||||
prepareResponseBuffer(MessageResult::InvalidMessageSizeFromRemote);
|
||||
return;
|
||||
}
|
||||
processMessage(&receivedLocalMessage);
|
||||
}
|
||||
|
||||
void SesameController::sendPreparedLocalResponse(AsyncWebServerRequest *request) {
|
||||
request->send_P(200, "application/octet-stream", (uint8_t*) &outgoingMessage, SIGNED_MESSAGE_SIZE);
|
||||
Serial.printf("[INFO] Local response %u,%u\n", outgoingMessage.message.messageType, outgoingMessage.message.result);
|
||||
bool SesameController::readLocalMessage() {
|
||||
// if there's data available, read a packet
|
||||
int packetSize = udp.parsePacket();
|
||||
if (packetSize == 0) {
|
||||
return false;
|
||||
}
|
||||
if (packetSize != SIGNED_MESSAGE_SIZE) {
|
||||
Serial.print("[WARN] Received UDP packet of invalid size ");
|
||||
Serial.println(packetSize);
|
||||
prepareResponseBuffer(MessageResult::InvalidMessageSizeFromRemote);
|
||||
return true;
|
||||
}
|
||||
int bytesRead = udp.read((uint8_t*) &receivedLocalMessage, SIGNED_MESSAGE_SIZE);
|
||||
if (bytesRead != SIGNED_MESSAGE_SIZE) {
|
||||
Serial.println("[WARN] Failed to read full local message");
|
||||
prepareResponseBuffer(MessageResult::InvalidMessageSizeFromRemote);
|
||||
return true;
|
||||
}
|
||||
Serial.println("[INFO] Received local message");
|
||||
processMessage(&receivedLocalMessage, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
void SesameController::sendPreparedLocalResponse() {
|
||||
// send a reply to the IP address and port that sent us the packet we received
|
||||
udp.beginPacket(udp.remoteIP(), udp.remotePort());
|
||||
udp.write((uint8_t*) &outgoingMessage, SIGNED_MESSAGE_SIZE);
|
||||
udp.endPacket();
|
||||
Serial.println("[INFO] Sent local response");
|
||||
}
|
||||
|
||||
// MARK: Server
|
||||
@ -102,7 +186,7 @@ void SesameController::handleServerMessage(uint8_t* payload, size_t length) {
|
||||
sendServerError(MessageResult::InvalidMessageSizeFromRemote);
|
||||
return;
|
||||
}
|
||||
processMessage((SignedMessage*) payload);
|
||||
processMessage((SignedMessage*) payload, true);
|
||||
sendPreparedResponseToServer();
|
||||
}
|
||||
|
||||
@ -113,7 +197,7 @@ void SesameController::sendPreparedResponseToServer() {
|
||||
|
||||
// MARK: Message handling
|
||||
|
||||
void SesameController::processMessage(SignedMessage* message) {
|
||||
void SesameController::processMessage(SignedMessage* message, bool shouldPerformUnlock) {
|
||||
// Result must be empty
|
||||
if (message->message.result != MessageResult::MessageAccepted) {
|
||||
prepareResponseBuffer(MessageResult::InvalidMessageResultFromRemote);
|
||||
@ -128,7 +212,7 @@ void SesameController::processMessage(SignedMessage* message) {
|
||||
checkAndPrepareChallenge(&message->message);
|
||||
return;
|
||||
case MessageType::request:
|
||||
completeUnlockRequest(&message->message);
|
||||
completeUnlockRequest(&message->message, shouldPerformUnlock);
|
||||
return;
|
||||
default:
|
||||
prepareResponseBuffer(MessageResult::InvalidMessageTypeFromRemote);
|
||||
@ -159,7 +243,7 @@ void SesameController::prepareChallenge(Message* message) {
|
||||
prepareResponseBuffer(MessageResult::MessageAccepted, message);
|
||||
}
|
||||
|
||||
void SesameController::completeUnlockRequest(Message* message) {
|
||||
void SesameController::completeUnlockRequest(Message* message, bool shouldPerformUnlock) {
|
||||
// Client and server challenge must match
|
||||
if (message->clientChallenge != currentClientChallenge) {
|
||||
prepareResponseBuffer(MessageResult::InvalidClientChallengeFromRemote, message);
|
||||
@ -181,7 +265,9 @@ void SesameController::completeUnlockRequest(Message* message) {
|
||||
clearCurrentChallenge();
|
||||
|
||||
// Move servo
|
||||
if (shouldPerformUnlock) {
|
||||
servo.pressButton();
|
||||
}
|
||||
prepareResponseBuffer(MessageResult::MessageAccepted, message);
|
||||
Serial.println("[INFO] Accepted message");
|
||||
}
|
||||
|
31
src/main.cpp
31
src/main.cpp
@ -22,23 +22,16 @@
|
||||
#include "controller.h"
|
||||
#include "config.h"
|
||||
|
||||
SesameController controller(localPort);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(serialBaudRate);
|
||||
Serial.setDebugOutput(true);
|
||||
Serial.println("[INFO] Device started");
|
||||
|
||||
ServoConfiguration servoConfig {
|
||||
ServoConfiguration servoConfig {
|
||||
.pwmTimer = pwmTimer,
|
||||
.pwmFrequency = servoFrequency,
|
||||
.pin = servoPin,
|
||||
.openDuration = lockOpeningDuration,
|
||||
.pressedValue = servoPressedState,
|
||||
.releasedValue = servoReleasedState,
|
||||
};
|
||||
};
|
||||
|
||||
ServerConfiguration serverConfig {
|
||||
ServerConfiguration serverConfig {
|
||||
.url = serverUrl,
|
||||
.port = serverPort,
|
||||
.path = serverPath,
|
||||
@ -47,9 +40,9 @@ void setup() {
|
||||
.socketHeartbeatIntervalMs = socketHeartbeatIntervalMs,
|
||||
.socketHeartbeatTimeoutMs = socketHeartbeatTimeoutMs,
|
||||
.socketHeartbeatFailureReconnectCount = socketHeartbeatFailureReconnectCount,
|
||||
};
|
||||
};
|
||||
|
||||
EthernetConfiguration ethernetConfig {
|
||||
EthernetConfiguration ethernetConfig {
|
||||
.macAddress = ethernetMacAddress,
|
||||
.spiPinMiso = spiPinMiso,
|
||||
.spiPinMosi = spiPinMosi,
|
||||
@ -59,15 +52,21 @@ void setup() {
|
||||
.dhcpLeaseResponseTimeoutMs = dhcpLeaseResponseTimeoutMs,
|
||||
.manualIp = manualIpAddress,
|
||||
.manualDnsAddress = manualDnsServerAddress,
|
||||
};
|
||||
.udpPort = localUdpPort,
|
||||
};
|
||||
|
||||
KeyConfiguration keyConfig {
|
||||
KeyConfiguration keyConfig {
|
||||
.remoteKey = remoteKey,
|
||||
.localKey = localKey,
|
||||
.challengeExpiryMs = challengeExpiryMs,
|
||||
};
|
||||
};
|
||||
|
||||
controller.configure(servoConfig, serverConfig, ethernetConfig, keyConfig);
|
||||
SesameController controller(servoConfig, serverConfig, ethernetConfig, keyConfig);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(serialBaudRate);
|
||||
Serial.setDebugOutput(true);
|
||||
Serial.println("[INFO] Device started");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
@ -1,26 +1,25 @@
|
||||
#include "server.h"
|
||||
|
||||
constexpr int32_t pingInterval = 10000;
|
||||
constexpr uint32_t pongTimeout = 5000;
|
||||
uint8_t disconnectTimeoutCount = 3;
|
||||
ServerConnection::ServerConnection(ServerConfiguration configuration) : configuration(configuration) { }
|
||||
|
||||
ServerConnection::ServerConnection() { }
|
||||
|
||||
void ServerConnection::configure(ServerConfiguration configuration, ServerConnectionCallbacks *callbacks) {
|
||||
void ServerConnection::setCallbacks(ServerConnectionCallbacks *callbacks) {
|
||||
controller = callbacks;
|
||||
this->configuration = configuration;
|
||||
}
|
||||
|
||||
void ServerConnection::connect() {
|
||||
if (webSocket.isConnected()) {
|
||||
void ServerConnection::shouldConnect(bool connect) {
|
||||
if (connect == shouldBeConnected) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (controller == NULL) {
|
||||
Serial.println("[ERROR] No callbacks set for server");
|
||||
return;
|
||||
}
|
||||
shouldBeConnected = connect;
|
||||
nextReconnectAttemptMs = currentTime;
|
||||
}
|
||||
|
||||
isConnecting = true;
|
||||
void ServerConnection::connect() {
|
||||
Serial.printf("[INFO] Connecting to %s:%d%s\n", configuration.url, configuration.port, configuration.path);
|
||||
connectionTimeout = currentTime + configuration.socketHeartbeatIntervalMs;
|
||||
webSocket.begin(configuration.url, configuration.port, configuration.path);
|
||||
@ -33,47 +32,76 @@ void ServerConnection::connect() {
|
||||
webSocket.setReconnectInterval(configuration.reconnectTime);
|
||||
}
|
||||
|
||||
void ServerConnection::didDisconnect() {
|
||||
if (shouldReconnect || isConnecting) {
|
||||
return; // Disconnect already registered.
|
||||
}
|
||||
Serial.println("[INFO] Socket disconnected");
|
||||
nextReconnectAttemptMs = currentTime + configuration.socketHeartbeatIntervalMs;
|
||||
shouldReconnect = true;
|
||||
}
|
||||
|
||||
void ServerConnection::didConnect() {
|
||||
void ServerConnection::didChangeConnectionState(bool isConnected) {
|
||||
static bool wasConnected = false;
|
||||
if (isConnected) {
|
||||
Serial.println("[INFO] Socket connected, enabling heartbeat");
|
||||
isConnecting = false;
|
||||
Serial.println("[INFO] Socket connected");
|
||||
webSocket.enableHeartbeat(configuration.socketHeartbeatIntervalMs, configuration.socketHeartbeatTimeoutMs, configuration.socketHeartbeatFailureReconnectCount);
|
||||
}
|
||||
|
||||
void ServerConnection::disconnect() {
|
||||
webSocket.disconnect();
|
||||
} else {
|
||||
isDisconnecting = false;
|
||||
if (!wasConnected && shouldBeConnected && nextReconnectAttemptMs < currentTime) {
|
||||
nextReconnectAttemptMs = currentTime + configuration.socketHeartbeatIntervalMs;
|
||||
Serial.println("[INFO] Socket disconnected, setting reconnect time");
|
||||
} else if (wasConnected) {
|
||||
Serial.println("[INFO] Socket disconnected");
|
||||
}
|
||||
}
|
||||
wasConnected = isConnected;
|
||||
}
|
||||
|
||||
void ServerConnection::loop(uint32_t millis) {
|
||||
currentTime = millis;
|
||||
webSocket.loop();
|
||||
if (shouldReconnect && !isConnecting) {
|
||||
shouldReconnect = false;
|
||||
connect();
|
||||
|
||||
if (shouldBeConnected) {
|
||||
if (isDisconnecting) {
|
||||
return; // Wait for disconnect to finish, then it will be reconnected
|
||||
}
|
||||
if (isConnecting && millis > connectionTimeout) {
|
||||
Serial.println("[INFO] Failed to connect");
|
||||
disconnect();
|
||||
shouldReconnect = true;
|
||||
if (isConnecting) {
|
||||
if (millis > connectionTimeout) {
|
||||
// Cancel connection attempt
|
||||
Serial.println("[INFO] Canceling socket connection attempt");
|
||||
isDisconnecting = true;
|
||||
isConnecting = false;
|
||||
webSocket.disconnect();
|
||||
}
|
||||
return; // Wait for connect to finish
|
||||
}
|
||||
if (webSocket.isConnected()) {
|
||||
return; // Already connected
|
||||
}
|
||||
if (controller == NULL) {
|
||||
return;
|
||||
}
|
||||
if (millis < nextReconnectAttemptMs) {
|
||||
return; // Wait for next reconnect
|
||||
}
|
||||
isConnecting = true;
|
||||
connect();
|
||||
} else {
|
||||
if (isDisconnecting) {
|
||||
return; // Wait for disconnect
|
||||
}
|
||||
if (isConnecting) {
|
||||
return; // Wait until connection is established, then it will be disconnected
|
||||
}
|
||||
if (!webSocket.isConnected()) {
|
||||
return;
|
||||
}
|
||||
isDisconnecting = true;
|
||||
Serial.println("[INFO] Disconnecting socket");
|
||||
webSocket.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
void ServerConnection::webSocketEventHandler(WStype_t type, uint8_t * payload, size_t length) {
|
||||
switch(type) {
|
||||
case WStype_DISCONNECTED:
|
||||
didDisconnect();
|
||||
didChangeConnectionState(false);
|
||||
break;
|
||||
case WStype_CONNECTED:
|
||||
didConnect();
|
||||
didChangeConnectionState(true);
|
||||
break;
|
||||
case WStype_TEXT:
|
||||
controller->sendServerError(MessageResult::TextReceivedOverSocket);
|
||||
|
@ -2,9 +2,28 @@
|
||||
|
||||
#include <esp32-hal.h> // For `millis()`
|
||||
|
||||
ServoController::ServoController() { }
|
||||
void performReset(void * pvParameters) {
|
||||
ServoController* servo = (ServoController *) pvParameters;
|
||||
for(;;){
|
||||
servo->loop(millis());
|
||||
delay(50);
|
||||
}
|
||||
}
|
||||
|
||||
ServoController::ServoController(ServoConfiguration configuration) {
|
||||
|
||||
// Create a task that runs on a different core,
|
||||
// So that it's always executed
|
||||
xTaskCreatePinnedToCore(
|
||||
performReset, /* Task function. */
|
||||
"Servo", /* name of task. */
|
||||
1000, /* Stack size of task */
|
||||
this, /* parameter of the task */
|
||||
1, /* priority of the task */
|
||||
&servoResetTask,
|
||||
1); /* pin task to core 1 */
|
||||
|
||||
|
||||
void ServoController::configure(ServoConfiguration configuration) {
|
||||
openDuration = configuration.openDuration;
|
||||
pressedValue = configuration.pressedValue;
|
||||
releasedValue = configuration.releasedValue;
|
||||
@ -15,9 +34,7 @@ void ServoController::configure(ServoConfiguration configuration) {
|
||||
}
|
||||
|
||||
void ServoController::pressButton() {
|
||||
servo.write(pressedValue);
|
||||
buttonIsPressed = true;
|
||||
openingEndTime = millis() + openDuration;
|
||||
shouldPressButton = true;
|
||||
}
|
||||
|
||||
void ServoController::releaseButton() {
|
||||
@ -26,7 +43,12 @@ void ServoController::releaseButton() {
|
||||
}
|
||||
|
||||
void ServoController::loop(uint32_t millis) {
|
||||
if (buttonIsPressed && millis > openingEndTime) {
|
||||
if (shouldPressButton) {
|
||||
servo.write(pressedValue);
|
||||
openingEndTime = millis + openDuration;
|
||||
buttonIsPressed = true;
|
||||
shouldPressButton = false;
|
||||
} else if (buttonIsPressed && millis > openingEndTime) {
|
||||
releaseButton();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user