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
139
240#include < Servo.h>
341#include < Wire.h>
442#include < Firmata.h>
543
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
846#include < PipedStream.h> // https://github.com/paulo-raca/ArduinoBufferedStreams
947
1048#include < utility/ExampleStandardFirmataCommon.h>
1149
12- #define LOG_ENABLED 1 // 0 or 1
13- #define DEBUG_ENABLED 1 // 0 or 1
14-
1550#if LOG_ENABLED
1651#define LOG (x... ) do { x; } while (0 )
1752#define IS_IGNORE_PIN (i ) ((i) == 1 ) // 1 == Serial TX
2560#define DEBUG (x... ) do { (void )0 ; } while (0 )
2661#endif
2762
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
2867PipedStreamPair pipe; // streamify data coming from websocket
2968PipedStream& firmataInternalStream = pipe.first;
3069PipedStream& firmataStream = pipe.second;
31- WebSocketsServer webSocket (3031 );
70+
71+ WebSocketsServer webSocket (WS_PORT);
3272
3373/*
3474 * StandardFirmataWiFi communicates with WiFi shields over SPI. Therefore all
@@ -49,16 +89,16 @@ void ignorePins()
4989
5090void initTransport ()
5191{
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 ()));
62102}
63103
64104int clients = 0 ;
@@ -81,15 +121,15 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length
81121 clients++;
82122 }
83123 break ;
84-
124+
85125 case WStype_TEXT:
86126 DEBUG (Serial.printf (" Informative Text from WebSocket: '%s'\n " , (const char *)payload));
87127 break ;
88128
89129 case WStype_BIN:
90130 DEBUG (Serial.printf (" binary from WebSocket\n " ));
91131#if 1
92- // !! there _is_StreamFirmataInternal a Print::write(ptr,size_t) !?
132+ // !! there _is_ a Print::write(ptr,size_t) !?
93133 for (size_t i = 0 ; i < length; i++)
94134 {
95135 if (firmataStream.write (payload[i]) != 1 ) {
@@ -119,20 +159,18 @@ void initFirmata()
119159{
120160 initFirmataCommonBegin ();
121161 ignorePins ();
122-
123162 Firmata.begin (firmataInternalStream);
124-
125163 initFirmataCommonEnd ();
126164}
127165
128166void setup ()
129167{
130- LOG (Serial.begin (115200 ));
168+ LOG (Serial.begin (SERIAL_BAUD ));
131169
132170 initTransport ();
133171 initWebSocket ();
134172 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
136174}
137175
138176/* ==============================================================================
@@ -144,34 +182,34 @@ void loop()
144182 webSocket.loop ();
145183 MDNS.update ();
146184
147- size_t firmataStreamLen = firmataStream.available ();
185+ size_t firmata2WSLen = firmataStream.available ();
148186
149- #if 0 && DEBUG_ENABLED
187+ #if DEBUG_ENABLED
150188 static unsigned long last = 0 ;
151189 if (millis () - last > 5000 )
152190 {
153191 last += 1000 ;
154192 Serial.printf (" Firmata to WS: available=%zd --- WS to Firmata: available=%zd\n " ,
155- firmataStreamLen,
193+ firmata2WSLen,
156194 firmataInternalStream.available ());
157195 }
158196#endif
159197
160- if (clients && firmataStreamLen )
198+ if (clients && firmata2WSLen )
161199 {
162- DEBUG (Serial.printf (" Firmata -> WS: %zd bytes\n " , firmataStreamLen ));
200+ DEBUG (Serial.printf (" Firmata -> WS: %zd bytes\n " , firmata2WSLen ));
163201
164202 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 ))
169207 {
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 ));
171209 }
172210 else
173211 {
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 ));
175213 }
176214 }
177215}
0 commit comments