diff --git a/include/server.h b/include/server.h index 35b2223..f8e18a2 100644 --- a/include/server.h +++ b/include/server.h @@ -84,6 +84,8 @@ private: void disconnect(); bool shouldReconnect = true; + bool isConnecting = false; + uint32_t connectionTimeout = 0; uint32_t nextReconnectAttemptMs = 0; void didDisconnect(); diff --git a/src/controller.cpp b/src/controller.cpp index 4c92609..f82c182 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -22,20 +22,21 @@ void SesameController::configure(ServoConfiguration servoConfig, ServerConfigura Ethernet.init(ethernetConfig.spiPinSS); - // 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."); - if (Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.dhcpLeaseTimeoutMs, ethernetConfig.dhcpLeaseResponseTimeoutMs) == 1) { - Serial.print("[INFO] DHCP assigned IP "); - Serial.println(Ethernet.localIP()); - ethernetIsConfigured = true; - } else { + 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 "); diff --git a/src/server.cpp b/src/server.cpp index 499d823..7c1b17b 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -20,6 +20,9 @@ void ServerConnection::connect() { return; } + isConnecting = true; + 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); webSocket.setAuthorization(configuration.key); @@ -31,7 +34,7 @@ void ServerConnection::connect() { } void ServerConnection::didDisconnect() { - if (shouldReconnect) { + if (shouldReconnect || isConnecting) { return; // Disconnect already registered. } Serial.println("[INFO] Socket disconnected"); @@ -40,6 +43,7 @@ void ServerConnection::didDisconnect() { } void ServerConnection::didConnect() { + isConnecting = false; Serial.println("[INFO] Socket connected"); webSocket.sendTXT(configuration.key); webSocket.enableHeartbeat(configuration.socketHeartbeatIntervalMs, configuration.socketHeartbeatTimeoutMs, configuration.socketHeartbeatFailureReconnectCount); @@ -52,10 +56,16 @@ void ServerConnection::disconnect() { void ServerConnection::loop(uint32_t millis) { currentTime = millis; webSocket.loop(); - if (shouldReconnect) { + if (shouldReconnect && !isConnecting) { shouldReconnect = false; connect(); } + if (isConnecting && millis > connectionTimeout) { + Serial.println("[INFO] Failed to connect"); + disconnect(); + shouldReconnect = true; + isConnecting = false; + } } void ServerConnection::webSocketEventHandler(WStype_t type, uint8_t * payload, size_t length) { @@ -75,11 +85,13 @@ switch(type) { case WStype_PONG: break; case WStype_PING: + break; case WStype_ERROR: case WStype_FRAGMENT_TEXT_START: case WStype_FRAGMENT_BIN_START: case WStype_FRAGMENT: case WStype_FRAGMENT_FIN: + Serial.printf("[WARN] Unexpected socket event %d\n", type); controller->sendServerError(MessageResult::UnexpectedSocketEvent); break; }