-
Notifications
You must be signed in to change notification settings - Fork 26
/
karyacontroller.ino
1067 lines (995 loc) · 27.7 KB
/
karyacontroller.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//#define timing
//#define timingG
//#define echoserial
//#define VERSION "Firmware 1.0.0" // first to have firmware update
#define VERSION "Firmware 1.0.1" // new remote, able to control more on remote
#define VERSION "Firmware 1.0.2" // fix lots issues with remote, LCD
#define VERSION "Firmware 1.0.3" // fix lots issues with remote, LCD, PLASMA THC,Better Loop
#define VERSION "Firmware 1.0.4" // fix pause, stop, smooth stop on pause/stop, reduce use of waitbufferempty
#include "common.h"
#include "gcode.h"
#include "temp.h"
#include "timer.h"
#include "eprom.h"
#include "gcodesave.h"
#include "ir_remote.h"
#include "ir_oled.h"
#include <stdint.h>
#ifndef WIFISERVER
// provide empty implementation
inline void wifi_loop() {}
inline void setupwifi(int num) {}
#else
#ifdef USEOTA
#include <ArduinoOTA.h>
#endif
#include <FS.h> // Include the SPIFFS library
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266mDNS.h>
ESP8266WebServer server(80);
#define WIFI_AUTH_OPEN ENC_TYPE_NONE
// ===============================
#elif ESP32
#include <WiFi.h>
#include <WiFiAP.h>
#include <HTTPClient.h>
#include <WebServer.h>
#include "SPIFFS.h"
WebServer server(80);
#endif
// ==============================
#ifdef TCPSERVER
#include <WiFiClient.h>
#endif
#ifdef WEBSOCKSERVER
#include <WebSocketsServer.h>
#endif
uint8_t wfhead = 0;
uint8_t wftail = 0;
uint8_t wfbuf[BUFSIZE];
char wfb[300];
int wfl = 0;
int bpwf = 0;
#ifdef TCPSERVER
WiFiClient client;
WiFiServer servertcp(82);
#endif
#ifdef WEBSOCKSERVER
WebSocketsServer webSocket = WebSocketsServer(81); // create a websocket server on port 81
#endif
IPAddress ip;
// Server
#define IOT_IP_ADDRESS "172.245.97.171"
long lm = 0;
int ISWIFIOK = 0;
void touchserver(int v, String info)
{
#ifndef TOUCHSERVER
return;
#else
if (uncompress)
return;
if (WiFi.status() != WL_CONNECTED)
return;
// put your main code here, to run repeatedly:
long m = millis();
if (v || (m - lm > 5000)) {
char lc, c;
int cp = 0;
char cc[20];
HTTPClient http;
zprintf(PSTR("Touch server:\n"));
String url = "http://172.245.97.171/connect?info=" + info + "&ipaddress=" + String((ip[0])) + "." + String((ip[1])) + "." + String((ip[2])) + "." + String((ip[3]));
http.begin(url);
int httpCode = http.GET();
String cmd, par, par2;
cmd = http.getString();
//pn("Command:"+cmd);
if (cmd.indexOf(" ") > 0) {
par = cmd.substring(cmd.indexOf(" ") + 1);
cmd = cmd.substring(0, cmd.indexOf(" "));
}
http.end();
if (cmd == "print") {
par2 = par.substring(par.indexOf(",") + 1);
par = par.substring(0, par.indexOf(","));
reload_eeprom();
zprintf(PSTR("Print:%d Temp:%d\n"), fi(par.toInt()), fi(par2.toInt()));
if (wifi_gcode == par.toInt()) {
// no need to redownload
}
else {
zprintf(PSTR("Downloading..\n"));
String durl = "http://172.245.97.171/download?act=Download&gid=" + par;
File f = SPIFFS.open("/gcode.gcode", "w");
if (f) {
overridetemp = par2.toInt();
http.begin(durl);
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
//Serial.println(durl);
//Serial.println(http.getSize());
http.writeToStream(&f);
}
}
else {
zprintf(PSTR("Error download")); //[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
f.close();
eepromwrite(EE_gcode, par.toInt());
}
}
extern int8_t RUNNING;
RUNNING = 1;
beginuncompress("/gcode.gcode");
}
//p("\n");
lm = m;
}
#endif
}
File fsUploadFile;
//#define NOINTS noInterrupts();
//#define INTS interrupts();
#define NOINTS timerPause();
#define INTS timerResume();
//#define NOINTS
//#define INTS
String getContentType(String filename)
{ // convert the file extension to the MIME type
if (filename.endsWith(".html"))
return "text/html";
else if (filename.endsWith(".css"))
return "text/css";
else if (filename.endsWith(".js"))
return "application/javascript";
else if (filename.endsWith(".ico"))
return "image/x-icon";
else if (filename.endsWith(".gz"))
return "application/x-gzip";
return "text/plain";
}
bool handleFileRead(String path)
{ // send the right file to the client (if it exists)
//NOINTS
if (SPIFFS.exists(path + ".html"))
path += ".html";
else if (path.endsWith("/config"))
path = "/karyaconfig.html"; // If a folder is requested, send the index file
else if (path.endsWith("/cnc"))
path = "/index.html"; // If a folder is requested, send the index file
else if (path.endsWith("/")) {
// if wifi not connected redirect to configuration
if (ISWIFIOK) {
path = INDEX; // If a folder is requested, send the index file
if (!SPIFFS.exists(path))
path = "/3d.html"; // If a folder is requested, send the index file
if (!SPIFFS.exists(path))
path = "/index.html";
if (!SPIFFS.exists(path))
path = "/jobs.html";
}
else
path = "/karyaconfig.html"; // If a folder is requested, send the index file
}
xprintf(PSTR("handleFileRead: %s\n"), path.c_str());
String contentType = getContentType(path); // Get the MIME type
String pathWithGz = path + ".gz";
bool gz = SPIFFS.exists(pathWithGz);
if (gz || SPIFFS.exists(path)) { // If the file exists, either as a compressed archive, or normal
File file = SPIFFS.open(gz ? pathWithGz : path, "r"); // Open the file
size_t sent = server.streamFile(file, contentType); // Send it to the client
file.close(); // Close the file again
xprintf(PSTR("\tSent file: %s\n"), path.c_str());
}
else
xprintf(PSTR("\tFile Not Found: %s\n"), path.c_str()); // If the file doesn't exist, return false
//INTS
return false;
}
void updateFirmware()
{
File file = SPIFFS.open("/firmware.bin", "r");
if (!file) {
d_clear();
d_text(0, 0, "/firmware.bin");
d_text(0, 1, "Not found");
d_show();
delay(2000);
return;
}
size_t fileSize = file.size();
if (!Update.begin(fileSize)) {
d_clear();
d_text(0, 0, "Update failed !");
d_text(0, 1, "Not enough space");
d_show();
delay(2000);
return;
}
d_clear();
d_text(0, 0, "Update Firmware");
d_text(0, 1, "Please wait");
d_show();
Update.writeStream(file);
if (Update.end()) {
file.close();
SPIFFS.remove("/firmware.bin");
d_clear();
d_text(0, 0, "Update Firmware");
d_text(0, 1, "Success !");
d_show();
delay(3000);
ESP.restart();
}
else {
d_clear();
d_text(0, 0, "Failed.");
d_text(0, 1, "Try again...");
d_show();
file.close();
delay(3000);
}
}
void handleFileUpload()
{ // upload a new file to the SPIFFS
NOINTS
if (uncompress) {
server.send(500, "text/plain", "Still PRINTING");
}
else {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
String filename = upload.filename;
if (filename == "")
filename = "/gcode.gcode";
if (!filename.startsWith("/"))
filename = "/" + filename;
xprintf(PSTR("handleFileUpload Name: %s\n"), filename.c_str());
fsUploadFile = SPIFFS.open(filename, "w"); // Open the file for writing in SPIFFS (create if it doesn't exist)
filename = String();
}
else if (upload.status == UPLOAD_FILE_WRITE) {
// do temploop to avoid overheating
temp_loop(micros());
if (fsUploadFile)
fsUploadFile.write(upload.buf, upload.currentSize); // Write the received bytes to the file
}
else if (upload.status == UPLOAD_FILE_END) {
if (fsUploadFile) { // If the file was successfully created
fsUploadFile.close(); // Close the file again
xprintf(PSTR("handleFileUpload Size: %d\n"), upload.totalSize);
server.sendHeader("Location", "/upload"); // Redirect the client to the success page
server.send(303);
}
else {
server.send(500, "text/plain", "500: couldn't create file");
}
}
}
INTS
}
#ifdef WEBSOCKSERVER
void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t lenght)
{ // When a WebSocket message is received
switch (type) {
case WStype_DISCONNECTED: // if the websocket is disconnected
xprintf(PSTR("[%d] Disconnected!\n"), fi(num));
break;
case WStype_CONNECTED: { // if a new websocket connection is established
IPAddress ip = webSocket.remoteIP(num);
xprintf(PSTR("[%d] Connected from %d.%d.%d.%d url: %s\n"), fi(num), fi(ip[0]), fi(ip[1]), fi(ip[2]), fi(ip[3]), payload);
// on connect, send the scale
zprintf(PSTR("EPR:3 185 %f Lscale\n"), ff(Lscale));
} break;
case WStype_TEXT: // if new text data is received
//xprintf(PSTR("%s"),payload);
//webSocket.sendTXT(num, payload);
// if runnning job, ignore command from all channel
if (!uncompress) {
for (int i = 0; i < lenght; i++) {
buf_push(wf, payload[i]);
}
}
//webSocket.broadcastTXT(payload);
break;
}
}
#endif
void wifiwr(uint8_t s)
{
wfb[wfl] = s;
wfl++;
if (s == '\n') {
wfb[wfl] = 0;
#ifdef TCPSERVER
if (client && client.connected()) {
client.write(wfb, wfl);
}
#endif
#ifdef WEBSOCKSERVER
if (!uncompress)webSocket.broadcastTXT(wfb);
#endif
wfl = 0;
}
}
void wifi_push(char c)
{
buf_push(wf, c);
}
void connectWifi(int ret = 1)
{
if (ret) {
WiFi.reconnect();
return;
}
xprintf(PSTR("Try connect wifi AP:%s \n"), wifi_ap);
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ap, wifi_pwd);
restartLCD();
int cntr = 30;
#ifdef IR_OLED_MENU
d_clear();
d_text(0, 0, VERSION);
d_text(0, 1, "connecting ...");
d_text(0, 2, wifi_ap);
d_show();
#endif
while (WiFi.status() != WL_CONNECTED) {
delay(200);
cntr--;
if (!cntr)
break;
//xprintf(PSTR("."));
//Serial.print(".");
}
//WiFi.setAutoReconnect(true);
//WiFi.persistent(true);
}
bool isSTA;
void setupwifi(int num)
{
NOINTS
while (1) {
xprintf(PSTR("Wifi Initialization\n"));
if (num) {
//xprintf(PSTR("Connected to:%s Ip:%d.%d.%d.%d\n"), wifi_ap, fi(ip[0]), fi(ip[1]), fi(ip[2]), fi(ip[3]) );
//xprintf(PSTR("Disconnect\n"));
WiFi.disconnect();
server.close();
//servertcp.close();
#ifdef WEBSOCKSERVER
webSocket.close();
#endif
}
char c = wifi_ap[0];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
}
else {
String("cnc").toCharArray(wifi_dns, 29);
String("Tenda_AD26C0").toCharArray(wifi_ap, 49);
String("45712319").toCharArray(wifi_pwd, 19);
eepromwritestring(470, wifi_dns);
eepromwritestring(400, wifi_ap);
eepromwritestring(450, wifi_pwd);
// reset factory
reset_eeprom();
c = 'a';
}
if (c != 0) connectWifi(0);
String ipa = "";
if (WiFi.status() == WL_CONNECTED) {
ip = WiFi.localIP();
ISWIFIOK = 1;
xprintf(PSTR("Connected to:%s Ip:%d.%d.%d.%d\n"), wifi_ap, fi(ip[0]), fi(ip[1]), fi(ip[2]), fi(ip[3]));
xprintf(PSTR("HTTP server started\n"));
ipa = "Wifi : " + String(ip[2]);
ipa += ".";
ipa += String(ip[3]);
}
else {
xprintf(PSTR("Access Point Mode\n"));
/*IPAddress local_IP(192, 168, 4, 1);
IPAddress gateway(192, 168, 4, 9);
IPAddress subnet(255, 255, 255, 0);
WiFi.softAPConfig(local_IP, gateway, subnet);
*/
WiFi.mode(WIFI_AP);
ISWIFIOK = 0;
const char* ssid = "cnc1-9";
const char* password = "123456789";
WiFi.softAP(wifi_dns);
//ip = WiFi.softAPIP();
//xprintf(PSTR("AP:%s Ip:%d.%d.%d.%d\n"), ((' '<wifi_dns[0]<'Z')?wifi_dns:ssid), fi(ip[0]), fi(ip[1]), fi(ip[2]), fi(ip[3]));
ipa = String((' ' < wifi_dns[0] < 'Z') ? wifi_dns : ssid) + " : 4.1";
}
#ifdef IR_OLED_MENU
extern String IPA;
IPA = ipa;
Serial.end();
hasSerial = 0;
#endif
server.on("/setconfig", HTTP_GET, []() { // if the client requests the upload page
NOINTS
server.arg("name").toCharArray(wifi_dns, 29);
eepromwritestring(470, wifi_dns);
server.arg("ap").toCharArray(wifi_ap, 49);
eepromwritestring(400, wifi_ap);
server.arg("pw").toCharArray(wifi_pwd, 19);
eepromwritestring(450, wifi_pwd);
server.send(200, "text/html", "OK");
reload_eeprom();
delay(1000);
ESP.restart();
INTS
});
server.on("/resetlcd", HTTP_GET, []() { // if the client requests the upload page
IR_end();
REINIT;
IR_setup();
server.send(200, "text/html", "OK");
});
server.on("/gcode", HTTP_GET, []() { // if the client requests the upload page
if (!uncompress) {
char gc[100];
server.arg("t").toCharArray(gc, 99);
for (int i = 0; i < strlen(gc); i++) {
buf_push(wf, gc[i]);
}
buf_push(wf, '\n');
server.send(200, "text/html", "OK");
}
else {
server.send(200, "text/plain", "ERR");
}
});
server.on("/speed", HTTP_GET, []() { // if the client requests the upload page
extern uint8_t head, tail, tailok;
char gc[10];
server.arg("t").toCharArray(gc, 9);
extern float f_multiplier;
f_multiplier = atoi(gc) * 0.01;
tailok = tail + 5; // need to replanning all moves
server.send(200, "text/html", "OK");
});
server.on("/updatefirmware", HTTP_GET, []() { // if the client requests the upload page
server.send(200, "text/html", "OK");
updateFirmware();
});
server.on("/getconfig", HTTP_GET, []() { // if the client requests the upload page
String st = "disconnected";
if (WiFi.status() == WL_CONNECTED)
st = "connected";
server.send(200, "text/html", "['" + String(wifi_ap) + "','" + String(wifi_pwd) + "','" + String(wifi_dns) + "','" + st + "']");
});
server.on("/scanwifi", HTTP_GET, []() {
int n = WiFi.scanNetworks();
String res = "[";
if (n == 0) {
}
else {
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
if (i)
res += ",";
res += "['" + WiFi.SSID(i) + "','" + WiFi.RSSI(i) + "',";
if (WiFi.encryptionType(i) == WIFI_AUTH_OPEN)
res += "''";
else
res += "'*'";
res += "]";
}
}
res += "]";
server.send(200, "text/html", res);
});
#ifdef ANALOG_THC
server.on("/thc", HTTP_GET, []() {
extern String formatthc();
String res = formatthc();
server.send(200, "text/html", res);
});
#endif
server.on("/velo", HTTP_GET, []() {
extern String formatvelo();
String res = formatvelo();
server.send(200, "text/html", res);
});
server.on("/pauseprint", HTTP_GET, []() { // if the client requests the upload page
//NOINTS
if (uncompress) {
ispause = ispause == 0 ? 1 : 0;
extern int8_t PAUSE;
PAUSE = ispause;
server.send(200, "text/html", ispause == 1 ? "PAUSED" : "RESUMED");
}
else {
server.send(200, "text/plain", "ERR");
}
//INTS
});
server.on("/probe", HTTP_GET, []() {
//NOINTS
if (!uncompress) {
docheckendstop(1);
extern int16_t endstopstatus;
server.send(200, "text/html", endstopstatus < 0 ? "HIGH" : "LOW");
}
else {
server.send(200, "text/plain", "ERR");
}
//INTS
});
server.on("/resumeprint", HTTP_GET, []() { // if the client requests the upload page
//NOINTS
if (uncompress) {
ispause = 0;
extern int8_t PAUSE;
PAUSE = ispause;
server.send(200, "text/html", "OK");
}
else {
server.send(200, "text/plain", "ERR");
}
//INTS
});
server.on("/startprint", HTTP_GET, []() { // if the client requests the upload page
NOINTS
if (uncompress) {
server.send(200, "text/html", "FAIL, STILL PRINTING");
}
else {
server.send(200, "text/html", "OK");
beginuncompress("/gcode.gcode");
}
INTS
});
server.on("/startjob", HTTP_GET, []() { // if the client requests the upload page
NOINTS
if (uncompress) {
server.send(200, "text/html", "FAIL, STILL PRINTING");
}
else {
beginuncompress(server.arg("jobname"));
server.send(200, "text/html", "Start Ok");
}
INTS
});
server.on("/removejob", HTTP_GET, []() { // if the client requests the upload page
NOINTS
if (uncompress) {
server.send(200, "text/html", "FAIL, STILL PRINTING");
}
else {
deletejob(server.arg("jobname"));
server.send(200, "text/html", "Delete Ok");
}
INTS
});
server.on("/renamejob", HTTP_GET, []() { // if the client requests the upload page
NOINTS
if (uncompress) {
server.send(200, "text/html", "FAIL, STILL PRINTING");
}
else {
SPIFFS.rename(server.arg("jobname"), server.arg("newname"));
SPIFFS.rename(server.arg("jobname") + ".jpg", server.arg("newname") + ".jpg");
server.send(200, "text/html", "Rename Ok");
}
INTS
});
server.on("/getjobs", HTTP_GET, []() {
NOINTS
if (!uncompress) {
String str = "[";
#ifdef ESP32
File dir = SPIFFS.open("/");
File file = dir.openNextFile();
while (file) {
String s = file.name();
if (s.endsWith(".gcode")) {
if (str.length() > 2)
str += ",";
str += "['";
str += file.name();
str += "',";
str += file.size();
str += "]";
}
file = dir.openNextFile();
}
#elif ESP8266
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
String s = dir.fileName();
if (s.endsWith(".gcode")) {
if (str.length() > 2)str += ",";
str += "['";
str += dir.fileName();
str += "',";
str += dir.fileSize();
str += "]";
}
}
#endif
str += "]";
server.send(200, "text/plain", str);
}
else {
server.send(200, "text/plain", "ERR");
}
INTS
});
server.on("/stopprint", HTTP_GET, []() { // if the client requests the upload page
if (!uncompress) {
server.send(200, "text/html", "FAIL, NOT PRINTING");
}
else {
server.send(200, "text/html", "OK");
extern void stopmachine();
stopmachine();
}
});
server.on("/home", HTTP_GET, []() { // if the client requests the upload page
addmove(100, 0, 0, 10, 0, 1, 1);
addmove(100, 0, 0, 10, 0, 1, 0);
addmove(100, 0, 0, 0, 0, 1, 0);
server.send(200, "text/html", "OK");
//homing();
});
server.on("/jogmove", HTTP_GET, []() { // if the client requests the upload page
server.send(200, "text/html", "OK");
if (!uncompress) {
float x, y, z;
x = server.arg("x").toFloat();
y = server.arg("y").toFloat();
z = server.arg("z").toFloat();
addmove(100, x, y, z, 0, 1, 1);
}
});
server.on("/heating", HTTP_GET, []() { // if the client requests the upload page
int temp = 0;
if (server.arg("t0") == "")
temp = 180;
else
temp = server.arg("t0").toInt();
server.send(200, "text/html", String(Input));
set_temp(180);
});
server.on("/cooling", HTTP_GET, []() { // if the client requests the upload page
server.send(200, "text/html", "Cooling");
set_temp(0);
});
server.on("/upload", HTTP_GET, []() { // if the client requests the upload page
//xprintf(PSTR("Handle UPLOAD \n"));
server.send(200, "text/html", "<form method=\"post\" enctype=\"multipart/form-data\"><input type=\"file\" name=\"name\"> <input class=\"button\" type=\"submit\" value=\"Upload\"></form>");
});
server.on("/delete", HTTP_GET, []() { // if the client requests the upload page
//xprintf(PSTR("Handle UPLOAD \n"));
SPIFFS.remove(server.arg("fn"));
server.send(200, "text/html", "Delete " + server.arg("fn"));
});
server.on("/rename", HTTP_GET, []() { // if the client requests the upload page
//xprintf(PSTR("Handle UPLOAD \n"));
SPIFFS.rename(server.arg("fn"), server.arg("fnew"));
server.send(200, "text/html", "Rename to " + server.arg("fnew"));
});
server.on("/upload", HTTP_POST, // if the client posts to the upload page
[]() {
server.sendHeader("Access-Control-Expose-Headers", "Access-Control-*");
server.sendHeader("Access-Control-Allow-Headers", "Access-Control-*, Origin, X-Requested-With, Content-Type, Accept");
server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200);
}, // Send status 200 (OK) to tell the client we are ready to receive
handleFileUpload // Receive and save the file
);
server.onNotFound([]() { // If the client requests any URI
if (!handleFileRead(server.uri())) // send it if it exists
server.send(404, "text/plain", "404: Not Found ?"); // otherwise, respond with a 404 (Not Found) error
});
server.begin();
if (MDNS.begin(wifi_ap)) {
//Serial.println("MDNS responder started");
}
#ifdef TCPSERVER
servertcp.begin();
#endif
#ifdef WEBSOCKSERVER
webSocket.begin(); // start the websocket server
webSocket.onEvent(webSocketEvent); // if there's an incomming websocket message, go to function 'webSocketEvent'
#endif
if (num)
return;
#ifdef ESP32
SPIFFS.begin(true);
#else
SPIFFS.begin();
#endif
loadmeshleveling();
touchserver(1, String(wifi_dns));
#ifdef DISABLESERIAL
//Serial.end();
#endif
#ifdef USEOTA
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);
});
ArduinoOTA.onEnd([]() {
//Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
//Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.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");
*/
});
ArduinoOTA.begin();
#endif
break;
}
// return;
INTS
}
extern int sendwait;
boolean alreadyConnected = false;
uint32_t wmc = 0;
extern uint32_t cm;
uint32_t i_l_c=0;
void important_loop(){
if (cm - wmc < (uncompress?1000000:200000)) {
return;
}
IR_loop(0);
//server.handleClient();
/*
motionloop();
motionloop();
#ifdef WEBSOCKSERVER
if (!uncompress) webSocket.loop();
motionloop();
#endif //webserver
*/
wmc = cm;
}
void wifi_loop()
{
if (cm - wmc < (uncompress?1000000:200000)) {
return;
}
server.handleClient();
motionloop();
MDNS.update();
motionloop();
#ifdef WEBSOCKSERVER
if (!uncompress)webSocket.loop();
motionloop();
#endif //webserver
#ifdef USEOTA
if (!uncompress) ArduinoOTA.handle();
motionloop();
#endif
#ifdef TOUCHSERVER
if (sendwait == 1)
touchserver(0, String(wifi_dns));
#endif // touch
wmc = cm;
if (ISWIFIOK) {
if (WiFi.status() != WL_CONNECTED) {
//connectWifi();
//return;
}
}
#ifdef TCPSERVER
// wait for a new client:
if (servertcp.hasClient()) {
if (client)
client.stop();
client = servertcp.available();
// when the client sends the first byte, say hello:
}
if (client) {
if (client.connected()) {
while (client.available()) {
// read the bytes incoming from the client:
char thisChar = client.read();
//serialwr(thisChar);
//xprintf(PSTR("%s"),payload);
buf_push(wf, thisChar);
// echo the bytes back to the client:
}
}
motionloop();
else
client.stop();
}
#endif // tcp server
}
#endif
int line_done, ack_waiting = 0;
int ct = 0;
uint32_t gt = 0;
int n = 0;
/*
*/
/*
=========================================================================================================================================================
*/
int tmax, tmin;
char gcode_loop()
{
#ifndef ISPC
char c = 0;
if (waitexecute) {
if (tryexecute()) {
ack_waiting = 1;
if (ack_waiting) {
//zprintf(PSTR("ok\n"));
ack_waiting = 0;
}
#ifdef timingG
zprintf(PSTR("Gcode:%dus\n"), fi(micros() - gt));
#endif
}
} else
{
if (ack_waiting) {
//zprintf(PSTR("ok\n"));
ack_waiting = 0;
n = 1;
}
// wifi are first class
#ifdef WIFISERVER
if (buf_canread(wf)) {
buf_pop(wf, c);
}
else
#endif
{
if (serialav()) {
if (n) {
gt = micros();
n = 0;
}
serialrd(c);
}
}
#ifdef USE_SDCARD
if (sdcardok == 2) {
// read from the file until there's nothing else in it:
if (myFile.available()) {
c = myFile.read();
//myFile.write(c);
}
else {
// close the file:
#ifdef POWERFAILURE
eepromwrite(EE_lastline, fi(0));
#endif
myFile.close();
sdcardok = 0;
zprintf(PSTR("Done\n"));
c = 0;
}
}
#endif
if (c) {
if (c == '\n') {
lineprocess++;
}
//#define echoserial
#ifdef echoserial
serialwr(c);
#endif
gcode_parse_char(c);
}
//motionloop();
}
#else
#endif
return c;
}
int setupok = 0;
void setupother()
{
#ifdef USE_SDCARD
demoSD();
#endif
#ifdef output_enable
zprintf(PSTR("Init motion\n"));
initmotion();
zprintf(PSTR("Init timer\n"));
timer_init();
zprintf(PSTR("Init Gcode\n"));
init_gcode();
zprintf(PSTR("Init Temp\n"));
init_temp();
zprintf(PSTR("Init eeprom\n"));
reload_eeprom();
#else
initmotion();
init_gcode();
init_temp();
reload_eeprom();
timer_init();
#endif
#ifdef spindle_pin