Skip to content

Commit b80d505

Browse files
committed
wip
1 parent fef369e commit b80d505

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

examples/esp8266-websocket/esp8266-websocket.ino

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@
99

1010
#include <utility/ExampleStandardFirmataCommon.h>
1111

12-
#define DEBUG_ENABLED 0 // 0 or 1
12+
#define LOG_ENABLED 1 // 0 or 1
13+
#define DEBUG_ENABLED 1 // 0 or 1
1314

14-
#if DEBUG_ENABLED
15-
#define DEBUG(x...) do { x; } while (0)
15+
#if LOG_ENABLED
16+
#define LOG(x...) do { x; } while (0)
1617
#define IS_IGNORE_PIN(i) ((i) == 1) // 1 == Serial TX
1718
#else
19+
#define LOG(x...) do { (void)0; } while (0)
20+
#endif
21+
22+
#if LOG_ENABLED && DEBUG_ENABLED
23+
#define DEBUG(x...) do { x; } while (0)
24+
#else
1825
#define DEBUG(x...) do { (void)0; } while (0)
1926
#endif
2027

2128
PipedStreamPair pipe; // streamify data coming from websocket
22-
PipedStream& StreamFirmataInternal = pipe.first;
23-
PipedStream& StreamAsFirmata = pipe.second;
29+
PipedStream& firmataInternalStream = pipe.first;
30+
PipedStream& firmataStream = pipe.second;
2431
WebSocketsServer webSocket(3031);
2532

2633
/*
@@ -42,22 +49,23 @@ void ignorePins()
4249

4350
void initTransport()
4451
{
52+
WiFi.persistent(false);
4553
WiFi.mode(WIFI_STA);
4654
WiFi.begin(STASSID, STAPSK);
47-
DEBUG(Serial.printf("Connecting to SSID '%s'...\n", STASSID));
55+
LOG(Serial.printf("Connecting to SSID '%s'...\n", STASSID));
4856
while (WiFi.status() != WL_CONNECTED)
4957
{
5058
delay(1000);
51-
DEBUG(Serial.print('.'));
59+
LOG(Serial.print('.'));
5260
}
53-
DEBUG(Serial.printf("Connected, IP address: %s\n", WiFi.localIP().toString().c_str()));
61+
LOG(Serial.printf("Connected, IP address: %s\n", WiFi.localIP().toString().c_str()));
5462
}
5563

5664
int clients = 0;
5765

5866
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length)
5967
{
60-
DEBUG(Serial.printf("[ws:%u,t:%d,l:%z]: ", num, (int)type, length));
68+
DEBUG(Serial.printf("[ws:%u,t:%d,l:%zd]: ", num, (int)type, length));
6169

6270
switch(type) {
6371

@@ -81,22 +89,16 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length
8189
case WStype_BIN:
8290
DEBUG(Serial.printf("binary from WebSocket\n"));
8391
#if 1
84-
// !! there is a Print::write(ptr,size_t) !?
85-
//DEBUG(Serial.print("before: ");
86-
//DEBUG(Serial.println(StreamFirmataInternal.available());
92+
// !! there _is_StreamFirmataInternal a Print::write(ptr,size_t) !?
8793
for (size_t i = 0; i < length; i++)
8894
{
89-
//Serial.printf("0x%02x ", (int)payload[i]);
90-
if (StreamAsFirmata.write(payload[i]) != 1) {
91-
DEBUG(Serial.println("stream error writing to StreamAsFirmata"));
95+
if (firmataStream.write(payload[i]) != 1) {
96+
LOG(Serial.println("stream error writing to firmataStream"));
9297
}
9398
}
94-
//DEBUG(Serial.println();
95-
//DEBUG(Serial.print("after: ");
96-
//DEBUG(Serial.println(StreamFirmataInternal.available());
9799
#else
98-
if (StreamAsFirmata.write(payload, length) != length) {
99-
DEBUG(Serial.println("stream error writing to StreamAsFirmata"));
100+
if (firmataStream.write(payload, length) != length) {
101+
LOG(Serial.println("stream error writing to firmataStream"));
100102
}
101103
#endif
102104
break;
@@ -110,25 +112,22 @@ void initWebSocket()
110112
{
111113
webSocket.begin();
112114
webSocket.onEvent(webSocketEvent);
113-
DEBUG(Serial.println("WS: initialized"));
115+
LOG(Serial.println("WS: initialized"));
114116
}
115117

116118
void initFirmata()
117119
{
118120
initFirmataCommonBegin();
119121
ignorePins();
120122

121-
// Using the pipes stream
122-
// it is written to by the websocket event function
123-
// it is read from by the main loop
124-
Firmata.begin(StreamFirmataInternal);
123+
Firmata.begin(firmataInternalStream);
125124

126125
initFirmataCommonEnd();
127126
}
128127

129128
void setup()
130129
{
131-
DEBUG(Serial.begin(115200));
130+
LOG(Serial.begin(115200));
132131

133132
initTransport();
134133
initWebSocket();
@@ -145,34 +144,34 @@ void loop()
145144
webSocket.loop();
146145
MDNS.update();
147146

148-
size_t StreamAsFirmataLen = StreamAsFirmata.available();
147+
size_t firmataStreamLen = firmataStream.available();
149148

150-
#if DEBUG_ENABLED
149+
#if 0 && DEBUG_ENABLED
151150
static unsigned long last = 0;
152-
if (millis() - last > 1000)
151+
if (millis() - last > 5000)
153152
{
154153
last += 1000;
155-
DEBUG(Serial.printf("Firmata to WS: available=%zd --- WS to Firmata: available=%zd\n",
156-
StreamAsFirmataLen,
157-
StreamFirmataInternal.available()));
154+
Serial.printf("Firmata to WS: available=%zd --- WS to Firmata: available=%zd\n",
155+
firmataStreamLen,
156+
firmataInternalStream.available());
158157
}
159158
#endif
160159

161-
if (clients && StreamAsFirmataLen)
160+
if (clients && firmataStreamLen)
162161
{
163-
DEBUG(Serial.printf("Firmata -> WS: %zd bytes", StreamAsFirmataLen));
162+
DEBUG(Serial.printf("Firmata -> WS: %zd bytes\n", firmataStreamLen));
164163

165164
static byte tempBuffer [MAX_DATA_BYTES];
166-
if (StreamAsFirmataLen > sizeof(tempBuffer))
167-
StreamAsFirmataLen = sizeof(tempBuffer);
168-
if ( StreamAsFirmata.readBytes(tempBuffer, StreamAsFirmataLen) == StreamAsFirmataLen
169-
&& webSocket.broadcastBIN(tempBuffer, StreamAsFirmataLen))
165+
if (firmataStreamLen > sizeof(tempBuffer))
166+
firmataStreamLen = sizeof(tempBuffer);
167+
if ( firmataStream.readBytes(tempBuffer, firmataStreamLen) == firmataStreamLen
168+
&& webSocket.broadcastBIN(tempBuffer, firmataStreamLen))
170169
{
171-
DEBUG(Serial.printf("Successfully broadcasted-to-websocket a binary message of %zd bytes\n", StreamAsFirmataLen));
170+
DEBUG(Serial.printf("Successfully broadcasted-to-websocket a binary message of %zd bytes\n", firmataStreamLen));
172171
}
173172
else
174173
{
175-
DEBUG(Serial.printf("Error broadcasting-to-websocket a binary message of %zd bytes", StreamAsFirmataLen));
174+
LOG(Serial.printf("Error broadcasting-to-websocket a binary message of %zd bytes\n", firmataStreamLen));
176175
}
177176
}
178177
}

0 commit comments

Comments
 (0)