Updates to the refacotred code

Files changed (2) hide show
  1. WSHeartbeatRefactored/WSHeartbeatRefactored.ino +19 -8
  2. WSHeartbeatRefactored/{wsheartbeater.h → wscommunicator.h} +68 -39
WSHeartbeatRefactored/WSHeartbeatRefactored.ino CHANGED
@@ -1,24 +1,35 @@
1
  #include "ledblinker.h"
2
- #include "wsheartbeater.h"
 
 
 
 
3
 
4
  // Network configuration
5
  const char* SSID = "Pomona";
6
- // const uint16_t PORT = 8181;
7
- // const unsigned long HEARTBEAT_INTERVAL = 1000;
 
8
 
9
  // LED Blinking configuration
10
  const unsigned long BLINK_INTERVAL = 250;
11
-
12
- // WSHeartbeater wsHeartbeater(PORT, HEARTBEAT_INTERVAL);
13
  LedBlinker ledBlinker(BLINK_INTERVAL);
14
 
 
 
 
 
15
  void setup() {
16
  Serial.begin(115200);
17
- wsHeartbeater.setup(SSID);
18
  ledBlinker.setup();
19
  }
20
 
 
 
 
 
21
  void loop() {
22
- wsHeartbeater.loopStep();
23
- ledBlinker.loopStep(wsHeartbeater.wsState == WS_ENABLED);
24
  }
 
1
  #include "ledblinker.h"
2
+ #include "wscommunicator.h"
3
+
4
+ //
5
+ // Global state
6
+ //
7
 
8
  // Network configuration
9
  const char* SSID = "Pomona";
10
+ const uint16_t PORT = 8181;
11
+ const unsigned long HEARTBEAT_INTERVAL = 1000;
12
+ WSCommunicator wsCommunicator(SSID, PORT, HEARTBEAT_INTERVAL);
13
 
14
  // LED Blinking configuration
15
  const unsigned long BLINK_INTERVAL = 250;
 
 
16
  LedBlinker ledBlinker(BLINK_INTERVAL);
17
 
18
+ //
19
+ // Setup
20
+ //
21
+
22
  void setup() {
23
  Serial.begin(115200);
24
+ wsCommunicator.setup();
25
  ledBlinker.setup();
26
  }
27
 
28
+ //
29
+ // Loop
30
+ //
31
+
32
  void loop() {
33
+ wsCommunicator.loopStep();
34
+ ledBlinker.loopStep(wsCommunicator.isEnabled());
35
  }
WSHeartbeatRefactored/{wsheartbeater.h → wscommunicator.h} RENAMED
@@ -1,31 +1,51 @@
1
  #include <WebSocketsServer.h>
2
  #include <WiFi.h>
3
 
4
- typedef enum {
5
- WS_ENABLED,
6
- WS_DISABLED
7
- } WsState;
8
 
9
- void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length);
10
 
11
- class WSHeartbeater {
12
- public:
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  unsigned long heartbeatInterval;
14
  unsigned long heartbeatLastTime;
 
 
15
  uint16_t port;
16
  WebSocketsServer webSocket;
17
- WsState wsState;
18
 
19
- WSHeartbeater(uint16_t port, unsigned long interval) : heartbeatInterval(interval),
20
- heartbeatLastTime(0),
21
- port(port),
22
- webSocket(port),
23
- wsState(WS_DISABLED) {}
 
 
 
24
 
25
- void setup(const char* ssid) {
 
26
  // Setup WiFi
 
 
27
  WiFi.begin(ssid);
28
- Serial.printf("\n[SETUP] Connecting to '%s'... ", ssid);
29
  while (WiFi.status() != WL_CONNECTED) {
30
  delay(500);
31
  Serial.print(".");
@@ -33,12 +53,19 @@ class WSHeartbeater {
33
  Serial.print("done\n");
34
 
35
  IPAddress ip = WiFi.localIP();
36
- Serial.printf("[SETUP] IP Address: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
37
 
 
38
  // Start WebSockets server
 
 
 
 
 
 
39
  webSocket.begin();
40
- webSocket.onEvent(webSocketEvent);
41
- Serial.printf("[SETUP] WebSocketsServer started at ws://%d.%d.%d.%d:%d\n", ip[0], ip[1], ip[2], ip[3], port);
42
  }
43
 
44
  void loopStep() {
@@ -48,54 +75,56 @@ class WSHeartbeater {
48
  // Update for handling heartbeat
49
  unsigned long now = millis();
50
  if (now - heartbeatLastTime > heartbeatInterval) {
51
- if (wsState == WS_ENABLED) {
52
- wsState = WS_DISABLED;
53
- Serial.println("[HEARTBEAT] Heartbeat timeout");
54
  }
55
  }
56
  }
 
 
57
  };
58
 
59
- const uint16_t PORT = 8181;
60
- const unsigned long HEARTBEAT_INTERVAL = 1000;
61
- WSHeartbeater wsHeartbeater(PORT, HEARTBEAT_INTERVAL);
62
 
63
- void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
64
  IPAddress ip;
65
 
66
  switch (type) {
67
  case WStype_DISCONNECTED:
68
- wsHeartbeater.wsState = WS_DISABLED;
69
- Serial.printf("[%u] Disconnected!\n", num);
70
  break;
71
 
72
  case WStype_CONNECTED:
73
- ip = wsHeartbeater.webSocket.remoteIP(num);
74
- Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
75
- wsHeartbeater.webSocket.sendTXT(num, "Connected");
76
  break;
77
 
78
  case WStype_TEXT:
79
- if (strcmp((char*)payload, "heartbeat") == 0) {
80
- wsHeartbeater.wsState = WS_ENABLED;
81
- wsHeartbeater.heartbeatLastTime = millis();
82
  } else {
83
- Serial.printf("[%u] get Text: %s\n", num, payload);
84
  }
85
  break;
86
 
87
  case WStype_BIN:
88
- Serial.printf("[%u] get binary length: %u\n", num, length);
89
- // hexdump(payload, length);
90
- // webSocket.sendBIN(num, payload, length);
91
  break;
92
 
93
  case WStype_ERROR:
 
94
  case WStype_FRAGMENT_TEXT_START:
95
  case WStype_FRAGMENT_BIN_START:
96
- case WStype_FRAGMENT:
97
  case WStype_FRAGMENT_FIN:
98
- Serial.printf("[%u] received unhandled WS event type: %d\n", num, type);
 
 
99
  break;
100
  }
101
  }
 
1
  #include <WebSocketsServer.h>
2
  #include <WiFi.h>
3
 
4
+ //
5
+ // State of WebSocket heartbeats
6
+ //
 
7
 
8
+ typedef enum { HEARTBEAT_ENABLED, HEARTBEAT_DISABLED } WsHeartbeatState;
9
 
10
+ //
11
+ // Forward declaration of the WebSocket event callback
12
+ //
13
+
14
+ class WSCommunicator;
15
+ void wsEventCB(WSCommunicator& wsComm, uint8_t num, WStype_t type, uint8_t* payload, size_t length);
16
+
17
+ //
18
+ // WebSocket server configuration, communication, and state
19
+ //
20
+
21
+ class WSCommunicator {
22
+ friend void wsEventCB(WSCommunicator& wsComm, uint8_t num, WStype_t type, uint8_t* payload, size_t length);
23
+
24
+ private:
25
  unsigned long heartbeatInterval;
26
  unsigned long heartbeatLastTime;
27
+
28
+ const char* ssid;
29
  uint16_t port;
30
  WebSocketsServer webSocket;
31
+ WsHeartbeatState hbState;
32
 
33
+ public:
34
+ WSCommunicator(const char* ssid, uint16_t port, unsigned long interval)
35
+ : heartbeatInterval(interval)
36
+ , heartbeatLastTime(0)
37
+ , ssid(ssid)
38
+ , port(port)
39
+ , webSocket(port)
40
+ , hbState(HEARTBEAT_DISABLED) {}
41
 
42
+ void setup() {
43
+ //
44
  // Setup WiFi
45
+ //
46
+
47
  WiFi.begin(ssid);
48
+ Serial.printf("\n[COMMUNICATOR::SETUP] Connecting to '%s'...", ssid);
49
  while (WiFi.status() != WL_CONNECTED) {
50
  delay(500);
51
  Serial.print(".");
 
53
  Serial.print("done\n");
54
 
55
  IPAddress ip = WiFi.localIP();
56
+ Serial.printf("[COMMUNICATOR::SETUP] IP address: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
57
 
58
+ //
59
  // Start WebSockets server
60
+ //
61
+
62
+ auto wrappedCB = [this](uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
63
+ wsEventCB(*this, num, type, payload, length);
64
+ };
65
+
66
  webSocket.begin();
67
+ webSocket.onEvent(wrappedCB);
68
+ Serial.printf("[COMMUNICATOR::SETUP] WebSocket server at ws://%d.%d.%d.%d:%d\n", ip[0], ip[1], ip[2], ip[3], port);
69
  }
70
 
71
  void loopStep() {
 
75
  // Update for handling heartbeat
76
  unsigned long now = millis();
77
  if (now - heartbeatLastTime > heartbeatInterval) {
78
+ if (hbState == HEARTBEAT_ENABLED) {
79
+ hbState = HEARTBEAT_DISABLED;
80
+ Serial.println("[COMMUNICATOR::HEARTBEAT] Heartbeat timeout");
81
  }
82
  }
83
  }
84
+
85
+ bool isEnabled() { return hbState == HEARTBEAT_ENABLED; }
86
  };
87
 
88
+ //
89
+ // WebSocket event callback
90
+ //
91
 
92
+ void wsEventCB(WSCommunicator& wsComm, uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
93
  IPAddress ip;
94
 
95
  switch (type) {
96
  case WStype_DISCONNECTED:
97
+ wsComm.hbState = HEARTBEAT_DISABLED;
98
+ Serial.printf("[COMMUNICATOR::%u] Disconnected!\n", num);
99
  break;
100
 
101
  case WStype_CONNECTED:
102
+ ip = wsComm.webSocket.remoteIP(num);
103
+ Serial.printf("[COMMUNICATOR::%u] Client connected: %d.%d.%d.%d\n", num, ip[0], ip[1], ip[2], ip[3]);
104
+ wsComm.webSocket.sendTXT(num, "Connected");
105
  break;
106
 
107
  case WStype_TEXT:
108
+ if (strncmp((char*)payload, "heartbeat", length) == 0) {
109
+ wsComm.hbState = HEARTBEAT_ENABLED;
110
+ wsComm.heartbeatLastTime = millis();
111
  } else {
112
+ Serial.printf("[COMMUNICATOR::%u] Received: %s\n", num, payload);
113
  }
114
  break;
115
 
116
  case WStype_BIN:
117
+ Serial.printf("[COMMUNICATOR::%u] Received %u bytes of data\n", num, length);
 
 
118
  break;
119
 
120
  case WStype_ERROR:
121
+ case WStype_FRAGMENT:
122
  case WStype_FRAGMENT_TEXT_START:
123
  case WStype_FRAGMENT_BIN_START:
 
124
  case WStype_FRAGMENT_FIN:
125
+ case WStype_PING:
126
+ case WStype_PONG:
127
+ Serial.printf("[COMMUNICATOR::%u] Received unhandled event: %d\n", num, type);
128
  break;
129
  }
130
  }