1
+ /*
2
+ Firmata is a generic protocol for communicating with microcontrollers
3
+ from software on a host computer. It is intended to work with
4
+ any host computer software package.
5
+
6
+ To download a host software package, please click on the following link
7
+ to open the list of Firmata client libraries in your default browser.
8
+
9
+ https://github.com/firmata/arduino#firmata-client-libraries
10
+
11
+ Copyright (C) 2006-2008 Hans-Christoph Steiner. All rights reserved.
12
+ Copyright (C) 2010-2011 Paul Stoffregen. All rights reserved.
13
+ Copyright (C) 2009 Shigeru Kobayashi. All rights reserved.
14
+ Copyright (C) 2009-2016 Jeff Hoefs. All rights reserved.
15
+ Copyright (C) 2015-2016 Jesse Frush. All rights reserved.
16
+ Copyright (C) 2016 Jens B. All rights reserved.
17
+ Copyright (C) 2020 david gauchard. All rights reserved (esp8266/websocket)
18
+
19
+ This library is free software; you can redistribute it and/or
20
+ modify it under the terms of the GNU Lesser General Public
21
+ License as published by the Free Software Foundation; either
22
+ version 2.1 of the License, or (at your option) any later version.
23
+
24
+ See file LICENSE.txt for further informations on licensing terms.
25
+
26
+ Last updated June 1st, 2020
27
+ */
28
+
29
+ #ifndef STASSID
30
+ // WiFi credentials
31
+ #define STASSID " your_network_name"
32
+ #define STAPSK " your_wpa_passphrase"
33
+ #endif
34
+
35
+ #define LOG_ENABLED 1 // 0 or 1
36
+ #define DEBUG_ENABLED 0 // 0 or 1
37
+ #define WS_PORT 3031 // websocket port
38
+ #define SERIAL_BAUD 9600
1
39
2
40
#include < Servo.h>
3
41
#include < Wire.h>
4
42
#include < Firmata.h>
5
43
6
- #include < ESP8266mDNS.h> // esp8266 Arduino standard library
7
- #include < WebSocketsServer.h> // https://github.com/Links2004/arduinoWebSockets
44
+ #include < ESP8266mDNS.h> // mDNS from esp8266 Arduino standard library
45
+ #include < WebSocketsServer.h> // https://github.com/Links2004/arduinoWebSockets
8
46
#include < PipedStream.h> // https://github.com/paulo-raca/ArduinoBufferedStreams
9
47
10
48
#include < utility/ExampleStandardFirmataCommon.h>
11
49
12
- #define LOG_ENABLED 1 // 0 or 1
13
- #define DEBUG_ENABLED 1 // 0 or 1
14
-
15
50
#if LOG_ENABLED
16
51
#define LOG (x... ) do { x; } while (0 )
17
52
#define IS_IGNORE_PIN (i ) ((i) == 1 ) // 1 == Serial TX
25
60
#define DEBUG (x... ) do { (void )0 ; } while (0 )
26
61
#endif
27
62
63
+ // everything written to firmataStream will be readable from firmataInternalStream
64
+ // everything written to firmataInternalStream will be readable from firmataStream
65
+ // firmataInternalStream is the stream given to Firmata
66
+ // websocket functions will read and write on firmataStream
28
67
PipedStreamPair pipe; // streamify data coming from websocket
29
68
PipedStream& firmataInternalStream = pipe.first;
30
69
PipedStream& firmataStream = pipe.second;
31
- WebSocketsServer webSocket (3031 );
70
+
71
+ WebSocketsServer webSocket (WS_PORT);
32
72
33
73
/*
34
74
* StandardFirmataWiFi communicates with WiFi shields over SPI. Therefore all
@@ -49,16 +89,16 @@ void ignorePins()
49
89
50
90
void initTransport ()
51
91
{
52
- WiFi.persistent (false );
53
- WiFi.mode (WIFI_STA);
54
- WiFi.begin (STASSID, STAPSK);
55
- LOG (Serial.printf (" Connecting to SSID '%s'...\n " , STASSID));
56
- while (WiFi.status () != WL_CONNECTED)
57
- {
58
- delay (1000 );
59
- LOG (Serial.print (' .' ));
60
- }
61
- LOG (Serial.printf (" Connected, IP address: %s\n " , WiFi.localIP ().toString ().c_str ()));
92
+ WiFi.persistent (false );
93
+ WiFi.mode (WIFI_STA);
94
+ WiFi.begin (STASSID, STAPSK);
95
+ LOG (Serial.printf (" Connecting to SSID '%s'...\n " , STASSID));
96
+ while (WiFi.status () != WL_CONNECTED)
97
+ {
98
+ delay (1000 );
99
+ LOG (Serial.print (' .' ));
100
+ }
101
+ LOG (Serial.printf (" Connected, IP address: %s\n " , WiFi.localIP ().toString ().c_str ()));
62
102
}
63
103
64
104
int clients = 0 ;
@@ -81,15 +121,15 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length
81
121
clients++;
82
122
}
83
123
break ;
84
-
124
+
85
125
case WStype_TEXT:
86
126
DEBUG (Serial.printf (" Informative Text from WebSocket: '%s'\n " , (const char *)payload));
87
127
break ;
88
128
89
129
case WStype_BIN:
90
130
DEBUG (Serial.printf (" binary from WebSocket\n " ));
91
131
#if 1
92
- // !! there _is_StreamFirmataInternal a Print::write(ptr,size_t) !?
132
+ // !! there _is_ a Print::write(ptr,size_t) !?
93
133
for (size_t i = 0 ; i < length; i++)
94
134
{
95
135
if (firmataStream.write (payload[i]) != 1 ) {
@@ -119,20 +159,18 @@ void initFirmata()
119
159
{
120
160
initFirmataCommonBegin ();
121
161
ignorePins ();
122
-
123
162
Firmata.begin (firmataInternalStream);
124
-
125
163
initFirmataCommonEnd ();
126
164
}
127
165
128
166
void setup ()
129
167
{
130
- LOG (Serial.begin (115200 ));
168
+ LOG (Serial.begin (SERIAL_BAUD ));
131
169
132
170
initTransport ();
133
171
initWebSocket ();
134
172
initFirmata ();
135
- MDNS.begin (" firmata" ); // "firmata.local" will be the dns name in LAN
173
+ MDNS.begin (" firmata" ); // "firmata.local" will be the mDNS name in LAN
136
174
}
137
175
138
176
/* ==============================================================================
@@ -144,34 +182,34 @@ void loop()
144
182
webSocket.loop ();
145
183
MDNS.update ();
146
184
147
- size_t firmataStreamLen = firmataStream.available ();
185
+ size_t firmata2WSLen = firmataStream.available ();
148
186
149
- #if 0 && DEBUG_ENABLED
187
+ #if DEBUG_ENABLED
150
188
static unsigned long last = 0 ;
151
189
if (millis () - last > 5000 )
152
190
{
153
191
last += 1000 ;
154
192
Serial.printf (" Firmata to WS: available=%zd --- WS to Firmata: available=%zd\n " ,
155
- firmataStreamLen,
193
+ firmata2WSLen,
156
194
firmataInternalStream.available ());
157
195
}
158
196
#endif
159
197
160
- if (clients && firmataStreamLen )
198
+ if (clients && firmata2WSLen )
161
199
{
162
- DEBUG (Serial.printf (" Firmata -> WS: %zd bytes\n " , firmataStreamLen ));
200
+ DEBUG (Serial.printf (" Firmata -> WS: %zd bytes\n " , firmata2WSLen ));
163
201
164
202
static byte tempBuffer [MAX_DATA_BYTES];
165
- if (firmataStreamLen > sizeof (tempBuffer))
166
- firmataStreamLen = sizeof (tempBuffer);
167
- if ( firmataStream.readBytes (tempBuffer, firmataStreamLen ) == firmataStreamLen
168
- && webSocket.broadcastBIN (tempBuffer, firmataStreamLen ))
203
+ if (firmata2WSLen > sizeof (tempBuffer))
204
+ firmata2WSLen = sizeof (tempBuffer);
205
+ if ( firmataStream.readBytes (tempBuffer, firmata2WSLen ) == firmata2WSLen
206
+ && webSocket.broadcastBIN (tempBuffer, firmata2WSLen ))
169
207
{
170
- DEBUG (Serial.printf (" Successfully broadcasted-to-websocket a binary message of %zd bytes\n " , firmataStreamLen ));
208
+ DEBUG (Serial.printf (" Successfully broadcasted-to-websocket a binary message of %zd bytes\n " , firmata2WSLen ));
171
209
}
172
210
else
173
211
{
174
- LOG (Serial.printf (" Error broadcasting-to-websocket a binary message of %zd bytes\n " , firmataStreamLen ));
212
+ LOG (Serial.printf (" Error broadcasting-to-websocket a binary message of %zd bytes\n " , firmata2WSLen ));
175
213
}
176
214
}
177
215
}
0 commit comments