Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grbl tcp #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions GrblStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once
#include <Arduino.h>
#include <WiFi.h>

class GrblStream
{
public:
virtual int available() = 0;
virtual char read() = 0;
virtual bool canSend(size_t n) = 0;
virtual void waitSent() = 0;
virtual size_t print(char c) = 0;
virtual size_t print(const char* str) = 0;
size_t print(float n) { return print(String(n).c_str()); }
size_t print(uint32_t n) { return print(String(n).c_str()); }
size_t println(const char* str) { size_t n = print(str); return n + print('\n'); } ;

// You need a virtual destructor for 'delete' to work correctly:
virtual ~GrblStream() {}
};

class SerialGrblStream : public GrblStream
{
HardwareSerial _serial;
public:
SerialGrblStream(HardwareSerial serial) : _serial(serial) {}
int available() override { return _serial.available(); }
bool canSend(size_t n) override { return _serial.availableForWrite() > n; }
void waitSent() override { while (_serial.availableForWrite() != 0x7F) ; }
char read() override { return _serial.read(); }
size_t print(char c) override { return _serial.print(c); }
size_t print(const char* str) override { return _serial.print(str); }
};

class TCPGrblStream : public GrblStream
{
WiFiClient _netconn;
public:
TCPGrblStream(const char *host, uint16_t port, int timeout) {
if (_netconn.connect(host, port, timeout) == 0) {
throw -2;
}
_netconn.setNoDelay(true);
}
~TCPGrblStream() {
_netconn.stop();
};
int available() override { return _netconn.available(); }
char read() override { return _netconn.read(); }
bool canSend(size_t n) override { return true; }
void waitSent() override { }
size_t print(char c) override { return _netconn.write((uint8_t)c); }
size_t print(const char* str) override { return _netconn.write((uint8_t *)str, strlen(str)); }
};

extern GrblStream* grblStream;
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# grbl_controller_esp32

*This fork of mstrens/grbl_controller_esp32.git has been modified so
grbl_controller_esp32 can connect to a GRBL GCode controller using a
TCP connection instead of a hardwired serial port. It can thus be
used as a handheld wireless pendant for Grbl_Esp32. To configure for
TCP operation, define GRBL_IP and GRBL_PORT in config.h so they refer
to the Grbl_Esp32 system. You will also need to configure the WiFi
settings as described below.*

*In addition, if THICK_OUTLINE is defined, the highlight for pressed
buttons is thicker and easier to see.*

This Grbl controller runs on a ESP32

This project allows to control a CNC running GRBL without having to use a pc.<br>
Expand Down Expand Up @@ -228,4 +239,4 @@ Notes:
- A button being added will be displayed only after the next reset of ESP32.
- "Printing" a file having the same Cmd digit as an existing Cmd button will replace as well the name as the Gcode of the button
- to delete a Cmd button, "Print" a file having a name equal to "delete" So, e.g. "Cmd2_delete.xxx" will delete the second button.


2 changes: 1 addition & 1 deletion TFT_eSPI_ms/TFT_Drivers/ILI9341_Init.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@
pinMode(TFT_BL, OUTPUT);
#endif

}
}
54 changes: 28 additions & 26 deletions actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "cmd.h"
#include "com.h"
#include "log.h"
#include "GrblStream.h"


// create for touchscreeen
extern TFT_eSPI tft ;
Expand Down Expand Up @@ -98,7 +100,7 @@ void fGoBack(uint8_t param) {
void fHome(uint8_t param) {
if( machineStatus[0] == 'I' || machineStatus[0] == 'A' ) {
#define HOME_CMD "$H"
Serial2.println(HOME_CMD) ;
grblStream->println(HOME_CMD) ;
} else {
fillMsg(__INVALID_BTN_HOME ) ;
}
Expand All @@ -107,15 +109,15 @@ void fHome(uint8_t param) {

void fUnlock(uint8_t param) {
if( machineStatus[0] == 'A') { // if grbl is in alarm
Serial2.println("$X") ; // send command to unlock
grblStream->println("$X") ; // send command to unlock
//Serial.println("$X has been sent");
}
// Stay on current page
waitReleased = true ; // discard "pressed" until a release
}

void fReset(uint8_t param) {
Serial2.print( (char) SOFT_RESET) ;
grblStream->print( (char) SOFT_RESET) ;
waitReleased = true ; // discard "pressed" until a release
fillMsg( " " );

Expand All @@ -125,10 +127,10 @@ void fCancel(uint8_t param) {
if( statusPrinting == PRINTING_FROM_SD || statusPrinting == PRINTING_PAUSED ) {
statusPrinting = PRINTING_STOPPED ;
closeFileToRead() ;
Serial2.print( (char) SOFT_RESET) ;
grblStream->print( (char) SOFT_RESET) ;
} else if ( statusPrinting == PRINTING_STRING ) {
statusPrinting = PRINTING_STOPPED ;
Serial2.print( (char) SOFT_RESET) ;
grblStream->print( (char) SOFT_RESET) ;
}
currentPage = _P_INFO ; // go to page Info
updateFullPage = true ; // force a redraw even if current page does not change
Expand All @@ -138,7 +140,7 @@ void fCancel(uint8_t param) {
void fPause(uint8_t param) {
if( statusPrinting == PRINTING_FROM_SD && ( machineStatus[0] == 'R' || machineStatus[0] == 'J' ) ) { // test on J added mainly for test purpose
#define PAUSE_CMD "!"
Serial2.print(PAUSE_CMD) ;
grblStream->print(PAUSE_CMD) ;
statusPrinting = PRINTING_PAUSED ;
updateFullPage = true ; //
}
Expand All @@ -148,7 +150,7 @@ void fPause(uint8_t param) {
void fResume(uint8_t param) {
if( statusPrinting == PRINTING_PAUSED && machineStatus[0] == 'H') {
#define RESUME_CMD "~"
Serial2.print(RESUME_CMD) ;
grblStream->print(RESUME_CMD) ;
resetWaitOkWhenSdMillis() ; // we reset the time we sent the last cmd otherwise, we can get a wrong warning saying that we are missing an OK (because it seems that GRBL suspends OK while in pause)
statusPrinting = PRINTING_FROM_SD ;
updateFullPage = true ; // we have to redraw the buttons because Resume should become Pause
Expand Down Expand Up @@ -187,29 +189,29 @@ void fMove( uint8_t param ) {
distance = 10 ;
break ;
}
Serial2.println("") ; Serial2.print("$J=G91 G21 ") ;
grblStream->println("") ; grblStream->print("$J=G91 G21 ") ;

//switch ( justPressedBtn ) { // we convert the position of the button into the type of button
// case 7 : Serial2.print("X") ; break ;
// case 5 : Serial2.print("X-") ; break ;
// case 2 : Serial2.print("Y") ; break ;
// case 10 : Serial2.print("Y-") ; break ;
// case 4 : Serial2.print("Z") ; break ;
// case 12 : Serial2.print("Z-") ; break ;
// case 7 : grblStream->print("X") ; break ;
// case 5 : grblStream->print("X-") ; break ;
// case 2 : grblStream->print("Y") ; break ;
// case 10 : grblStream->print("Y-") ; break ;
// case 4 : grblStream->print("Z") ; break ;
// case 12 : grblStream->print("Z-") ; break ;
// }
uint8_t typeOfMove ;
typeOfMove = convertBtnPosToBtnIdx( currentPage , justPressedBtn ) ;
switch ( typeOfMove ) { // we convert the position of the button into the type of button
case _XP : Serial2.print("X") ; break ;
case _XM : Serial2.print("X-") ; break ;
case _YP : Serial2.print("Y") ; break ;
case _YM : Serial2.print("Y-") ; break ;
case _ZP : Serial2.print("Z") ; break ;
case _ZM : Serial2.print("Z-") ; break ;
case _AP : Serial2.print("A") ; break ;
case _AM : Serial2.print("A-") ; break ;
case _XP : grblStream->print("X") ; break ;
case _XM : grblStream->print("X-") ; break ;
case _YP : grblStream->print("Y") ; break ;
case _YM : grblStream->print("Y-") ; break ;
case _ZP : grblStream->print("Z") ; break ;
case _ZM : grblStream->print("Z-") ; break ;
case _AP : grblStream->print("A") ; break ;
case _AM : grblStream->print("A-") ; break ;
}
Serial2.print(distance) ; Serial2.println (" F100") ;
grblStream->print(distance) ; grblStream->println (" F100") ;
//Serial.print("move for button") ; Serial.print(justPressedBtn) ;Serial.print(" ") ; Serial.print(distance) ; Serial.println (" F100") ;

updatePartPage = true ; // force a redraw of data
Expand Down Expand Up @@ -305,9 +307,9 @@ void fSdFilePrint(uint8_t param ){ // lance l'impression d'un fichier; param c
return ;
} else { // file can be printed
waitOk = false ;
Serial2.print(PAUSE_CMD) ;
grblStream->print(PAUSE_CMD) ;
delay(10);
Serial2.print("?") ;
grblStream->print("?") ;
//waitOk = false ; // do not wait for OK before sending char.
statusPrinting = PRINTING_PAUSED ; // initially it was PRINTING_FROM_SD ; // change the status, so char will be read and sent in main loop
prevPage = currentPage ; // go to INFO page
Expand Down Expand Up @@ -506,7 +508,7 @@ void fOverModify (uint8_t BtnParam) {
grblOverwriteCode += 0x99 ; // 0x99 is the GRBL code for 100% RPM
// Serial.println("We change RPM"); Serial.println( (uint8_t) grblOverwriteCode, HEX); // to debug
}
Serial2.print( (char) grblOverwriteCode ) ;
grblStream->print( (char) grblOverwriteCode ) ;
updatePartPage = true ; // force a redraw of data
waitReleased = true ; // discard "pressed" until a release
}
Expand Down
12 changes: 6 additions & 6 deletions browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ boolean checkWifiOnSD(void){
bool wifiTypeOk = false;
bool wifiPasswordOk = false;
bool wifiSsidOk = false;
bool local_IPOk = false ;
bool gatewayOk = false ;
bool subnetOk = false ;
// bool local_IPOk = false ;
// bool gatewayOk = false ;
// bool subnetOk = false ;
uint8_t n; // number of bytes in a line
char * pBeginValue ;
char * pEndValue ;
Expand Down Expand Up @@ -217,15 +217,15 @@ boolean checkWifiOnSD(void){
} else if ( memcmp ( "LOCAL_IP=", line, sizeof("LOCAL_IP=")-1) == 0){
memcpy(local_IPChar , pBeginValue+1 , sizeValue) ;
local_IPStr = local_IPChar ;
local_IPOk = true ;
// local_IPOk = true ;
} else if ( memcmp ( "GATEWAY=", line, sizeof("GATEWAY=")-1) == 0){
memcpy(gatewayChar , pBeginValue+1 , sizeValue) ;
gatewayStr = gatewayChar ;
gatewayOk = true ;
// gatewayOk = true ;
} else if ( memcmp ( "SUBNET=", line, sizeof("SUBNET=")-1) == 0){
memcpy(subnetChar , pBeginValue+1 , sizeValue) ;
subnetStr = subnetChar ;
subnetOk = true ;
// subnetOk = true ;
}
}
}
Expand Down
Loading