-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ESP32-3DPrinter-Bridge.ino
318 lines (259 loc) · 8.48 KB
/
ESP32-3DPrinter-Bridge.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
ESP32-3DPrinter-Bridge - A network <=> ESP32 <=> USB (FTDI) <=> 3D printer bridge
Source: https://github.com/tobozo/ESP32-3DPrinter-Bridge
MIT License
Copyright (c) 2018 tobozo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-----------------------------------------------------------------------------
Credits:
- https://github.com/tomorrow56/M5Stack_USB_Host_FTDI_Serial_Monitor
- https://github.com/AlphaLima/ESP32-Serial-Bridge
- http://www.circuitsathome.com
- https://reprap.org/forum/read.php?1,236755
Software Library Requirements:
- https://github.com/felis/USB_Host_Shield_2.0
Hardware Requirements:
- Mini USB Host Shield
- ESP32
Wiring:
USBHOST | ESP32
------------------
INT | 17
GND | GND
RST | EN
SS | 05
MOSI | 23
MISO | 19
SCK | 18
3V3 | 3V3
GND | GND
Usage:
- Plug the 3D Printer to the Mini USB Host Shield
- Power the ESP32
- From any machine on the network: telnet [IP Address of the ESP32] 1337 to access the bridge
*/
//#define DEBUG_USB_HOST
//#define BRIDGE_DEBUG
#include "cdcftdimod.h" // https://github.com/tomorrow56/M5Stack_USB_Host_FTDI_Serial_Monitor
#include <usbhub.h>
#include "pgmstrings.h"
#include "gcode.h" // https://reprap.org/forum/read.php?1,236755
#include "descriptor.h" // USB FTDIMod instance / tools for this sketch
#include <WiFi.h>
#include <SPI.h>
#include <ArduinoOTA.h>
WiFiServer TCPServer( 1337 ); // port number to speak to the printer
WiFiClient TCPClient;
// For the byte we read from the serial port
byte data = 0;
bool detected = false;
bool async = false;
String SerialInput = "";
void setup() {
WiFi.mode(WIFI_STA);
//WiFi.begin("my-WiFi-SSID", "My-WiFi-Password");
WiFi.begin();
uint8_t ticks_count = 0;
while (WiFi.status() != WL_CONNECTED) {
ticks_count++;
delay(500);
Serial.print(".");
if(ticks_count>10) {
ESP.restart();
}
}
// OTA Handler
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
// if DNSServer is started with "*" for domain name, it will reply with
// provided IP to all DNS request
ArduinoOTA.begin();
// Start TCP Port
TCPServer.begin(); // start TCP server
TCPServer.setNoDelay(true);
//keepalive = millis();
Serial.begin( 115200 );
Serial.println("Booted");
Serial.println("\nWiFi connected");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
Serial.print("Mac: ");
Serial.println(WiFi.macAddress());
Serial.print("Subnet: ");
Serial.println(WiFi.subnetMask());
Serial.print("GW IP: ");
Serial.println(WiFi.gatewayIP());
Serial.print("DNS: ");
Serial.println(WiFi.dnsIP());
if (Usb.Init() == -1){
Serial.println("OSC did not start.");
} else {
Serial.println("Started USB :-)");
}
}
void broadcastPrintln(String str) {
Serial.println(str);
if(TCPClient) {
TCPClient.println(str);
}
}
void broadcastPrint(char chr) {
Serial.print(chr);
if(TCPClient) {
TCPClient.write(chr);
}
}
void loop() {
ArduinoOTA.handle();
Usb.Task();
if(TCPServer.hasClient()) { // handle TCP client connect / disconnect
if (!TCPClient || !TCPClient.connected()){
if(TCPClient) TCPClient.stop();
TCPClient = TCPServer.available();
broadcastPrintln("New TCP client for COM");
//continue;
}
//no free/disconnected spot so reject
WiFiClient TmpserverClient = TCPServer.available();
TmpserverClient.stop();
}
if(TCPClient.available()) { // read TCP input
SerialInput += TCPClient.readStringUntil('\r\n');
SerialInput.trim();
#ifdef BRIDGE_DEBUG
if(SerialInput!="") {
broadcastPrintln("TCP: "+ SerialInput);
}
#endif
}
if(Serial.available()) { // read Serial input (use it for debug)
SerialInput += Serial.readStringUntil('\r\n');
SerialInput.trim();
#ifdef BRIDGE_DEBUG
if(SerialInput!="") {
broadcastPrintln("SERIAL: "+ SerialInput);
}
#endif
}
if(SerialInput=="async") { // handle external commands
async = !async;
if(async) {
broadcastPrintln("now async (will write to SD)");
} else {
broadcastPrintln("now sync (will commit realtime)");
}
}
if(SerialInput=="restart") { // handle external commands
ESP.restart();
}
if(SerialInput=="imperialmarch") { // handle external commands
if(async) {
broadcastPrintln("Will write on the SD, please be patient");
SerialInput = "M28 blah.g";
nextnote = millis() + 1000;
}
playing = true;
}
if(SerialInput=="status") { // handle external commands
SerialInput = "";
if( Usb.getUsbTaskState() == USB_STATE_RUNNING ) {
broadcastPrintln("USB Running");
Usb.ForEachUsbDevice(&PrintAllDescriptors);
Usb.ForEachUsbDevice(&PrintAllAddresses);
detected = true;
} else {
broadcastPrintln("USB NOT Running");
}
}
if( Usb.getUsbTaskState() == USB_STATE_RUNNING ) { // handle usb tasks
uint8_t rcode;
uint8_t buf[64];
uint16_t rcvd = 64;
if(!detected) { // show device information on detection and play a beep
Usb.ForEachUsbDevice(&PrintAllDescriptors);
Usb.ForEachUsbDevice(&PrintAllAddresses);
detected = true;
// char strbuf[] = "M31\r\n";
char strbuf[] = "M300 S100 P200\r\nM300 S200 P300\r\nM300 S300 P436\r\n";
rcode = Ftdi.SndData(strlen(strbuf), (uint8_t*)strbuf);
if (rcode){
ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
} else {
broadcastPrintln("Handshake OK");
}
delay(50);
}
if(playing) {
if(nextnote<millis()) {
SerialInput = imperialMarchAsString(async);
return;
}
}
if(SerialInput!="") { // forward collected input to USB
SerialInput += "\r\n";
rcode = Ftdi.SndData(strlen(SerialInput.c_str()), (uint8_t*)SerialInput.c_str());
if (rcode){
ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
} else {
#ifdef BRIDGE_DEBUG
broadcastPrintln("SENT: "+ SerialInput);
#endif
}
SerialInput = "";
}
// collect data received from USB, if any
for (uint8_t i=0; i<64; i++){
buf[i] = 0;
}
rcvd = 64;
rcode = Ftdi.RcvData(&rcvd, buf);
if (rcode && rcode != hrNAK){
ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
}
if( rcvd > 2 ) { //more than 2 bytes received
for(uint16_t i = 2; i < rcvd; i++ ) {
broadcastPrint((char)buf[i]);
data = buf[i];
if (data > 31 && data < 128) { /* printable char */ }
if (data == '\t') { /* tab */ }
if (data == '\r') { /* end of line */ }
}
}
}
}