Finish socket operations
This commit is contained in:
@@ -62,10 +62,8 @@ void SesameController::configure(ServoConfiguration servoConfig, ServerConfigura
|
||||
|
||||
void SesameController::loop(uint32_t millis) {
|
||||
currentTime = millis;
|
||||
server.loop();
|
||||
server.loop(millis);
|
||||
servo.loop(millis);
|
||||
|
||||
ensureWebSocketConnection();
|
||||
}
|
||||
|
||||
// MARK: Local
|
||||
@@ -199,20 +197,6 @@ void SesameController::prepareResponseBuffer(MessageResult result, Message* mess
|
||||
}
|
||||
}
|
||||
|
||||
void SesameController::ensureWebSocketConnection() {
|
||||
/*
|
||||
if (isReconnecting && WiFi.status() == WL_CONNECTED) {
|
||||
isReconnecting = false;
|
||||
Serial.print("WiFi IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
server.connect();
|
||||
timeCheck.startNTP();
|
||||
timeCheck.printLocalTime();
|
||||
localWebServer.begin();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
// MARK: Helper
|
||||
|
||||
/**
|
||||
|
15
src/main.cpp
15
src/main.cpp
@@ -4,6 +4,15 @@
|
||||
*
|
||||
* The code for a simple door unlock mechanism where a servo pushes on an existing
|
||||
* physical button.
|
||||
*
|
||||
* On compile error:
|
||||
*
|
||||
* In <Server.h>
|
||||
*
|
||||
* change:
|
||||
* virtual void begin(uint16_t port=0) =0;
|
||||
* to:
|
||||
* virtual void begin() =0;
|
||||
*/
|
||||
#include <Arduino.h>
|
||||
|
||||
@@ -35,6 +44,9 @@ void setup() {
|
||||
.path = serverPath,
|
||||
.key = serverAccessKey,
|
||||
.reconnectTime = 5000,
|
||||
.socketHeartbeatIntervalMs = socketHeartbeatIntervalMs,
|
||||
.socketHeartbeatTimeoutMs = socketHeartbeatTimeoutMs,
|
||||
.socketHeartbeatFailureReconnectCount = socketHeartbeatFailureReconnectCount,
|
||||
};
|
||||
|
||||
EthernetConfiguration ethernetConfig {
|
||||
@@ -47,9 +59,6 @@ void setup() {
|
||||
.dhcpLeaseResponseTimeoutMs = dhcpLeaseResponseTimeoutMs,
|
||||
.manualIp = manualIpAddress,
|
||||
.manualDnsAddress = manualDnsServerAddress,
|
||||
.socketHeartbeatIntervalMs = socketHeartbeatIntervalMs,
|
||||
.socketHeartbeatTimeoutMs = socketHeartbeatTimeoutMs,
|
||||
.socketHeartbeatFailureReconnectCount = socketHeartbeatFailureReconnectCount,
|
||||
};
|
||||
|
||||
KeyConfiguration keyConfig {
|
||||
|
@@ -20,7 +20,8 @@ void ServerConnection::connect() {
|
||||
return;
|
||||
}
|
||||
|
||||
webSocket.beginSSL(configuration.url, configuration.port, configuration.path);
|
||||
webSocket.begin(configuration.url, configuration.port, configuration.path);
|
||||
webSocket.setAuthorization(configuration.key);
|
||||
|
||||
std::function<void(WStype_t, uint8_t *, size_t)> f = [this](WStype_t type, uint8_t *payload, size_t length) {
|
||||
this->webSocketEventHandler(type, payload, length);
|
||||
@@ -29,23 +30,41 @@ void ServerConnection::connect() {
|
||||
webSocket.setReconnectInterval(configuration.reconnectTime);
|
||||
}
|
||||
|
||||
void ServerConnection::didDisconnect() {
|
||||
if (shouldReconnect) {
|
||||
return; // Disconnect already registered.
|
||||
}
|
||||
Serial.println("[INFO] Socket disconnected");
|
||||
nextReconnectAttemptMs = currentTime + configuration.socketHeartbeatIntervalMs;
|
||||
shouldReconnect = true;
|
||||
}
|
||||
|
||||
void ServerConnection::didConnect() {
|
||||
Serial.println("[INFO] Socket connected");
|
||||
webSocket.sendTXT(configuration.key);
|
||||
webSocket.enableHeartbeat(configuration.socketHeartbeatIntervalMs, configuration.socketHeartbeatTimeoutMs, configuration.socketHeartbeatFailureReconnectCount);
|
||||
}
|
||||
|
||||
void ServerConnection::disconnect() {
|
||||
webSocket.disconnect();
|
||||
}
|
||||
|
||||
void ServerConnection::loop() {
|
||||
void ServerConnection::loop(uint32_t millis) {
|
||||
currentTime = millis;
|
||||
webSocket.loop();
|
||||
if (shouldReconnect) {
|
||||
shouldReconnect = false;
|
||||
connect();
|
||||
}
|
||||
}
|
||||
|
||||
void ServerConnection::webSocketEventHandler(WStype_t type, uint8_t * payload, size_t length) {
|
||||
switch(type) {
|
||||
case WStype_DISCONNECTED:
|
||||
Serial.println("[INFO] Socket disconnected.");
|
||||
didDisconnect();
|
||||
break;
|
||||
case WStype_CONNECTED:
|
||||
webSocket.sendTXT(configuration.key);
|
||||
Serial.printf("[INFO] Socket connected to url: %s\n", payload);
|
||||
webSocket.enableHeartbeat(pingInterval, pongTimeout, disconnectTimeoutCount);
|
||||
didConnect();
|
||||
break;
|
||||
case WStype_TEXT:
|
||||
controller->sendServerError(MessageResult::TextReceived);
|
||||
@@ -67,5 +86,7 @@ switch(type) {
|
||||
}
|
||||
|
||||
void ServerConnection::sendResponse(uint8_t* buffer, uint16_t length) {
|
||||
webSocket.sendBIN(buffer, length);
|
||||
if (socketIsConnected()) {
|
||||
webSocket.sendBIN(buffer, length);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user