Mr.Dee67
Published © MIT

CNC Router from Hardwarestore

A WWAH type CNC router build (What was at hand), using unconventional materials and methods, using mostly locally available, cheap parts.

IntermediateWork in progressOver 2 days118
CNC Router from Hardwarestore

Things used in this project

Hardware components

ESP32, generic
×1
Steppermotor hybrid / 4-phase
×3
Driver board
×1
M8 threaded bolts
×3

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

powerdrill
Set of standard handtools

Story

Read more

Schematics

Steppermotor driverboard 3.3V to 12V, 12 channel

As described in my earlier article

Code

Simple CNC routing

C/C++
Rudimentary way of running stepper motors.
// control 3 stepper motors (4-phase). Commands sent via TCP, example: "GET http://192.168.4.1 /moveZ-240/ HTTP/1.1" would move Z-axis minus 240 steps

// Load Wi-Fi library
#include <WiFi.h>

// Replace with your network credentials
const char* ssid     = "myWWAHCNCserver";
const char* password = ".............";

// localhost IP will be 192.168.4.1

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;


// my CNC globals...
int myMotorPin[13];

int myPowerSwitchPin=17;
int myStopSwitchPin=16;


void setup() {
myMotorPin[ 0]=32; // z
myMotorPin[ 1]=33;
myMotorPin[ 2]=25;
myMotorPin[ 3]=26;
myMotorPin[ 4]=27; // y
myMotorPin[ 5]=14;
myMotorPin[ 6]=12;
myMotorPin[ 7]=13;
myMotorPin[ 8]=21; // x
myMotorPin[ 9]=19;
myMotorPin[10]=18;
myMotorPin[11]= 5;

// first set motor pins to sink!
  for(int i=0;i<12;i++)  
   {
    pinMode(myMotorPin[i], OUTPUT);
    digitalWrite(myMotorPin[i], LOW);
   }
// only then turn on main switch:
    pinMode(myPowerSwitchPin, OUTPUT);
    digitalWrite(myPowerSwitchPin, HIGH);
   
  
  delay(60);
  Serial.begin(115200);

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Setting AP (Access Point)…");
  // Remove the password parameter, if you want the AP (Access Point) to be open
  WiFi.softAP(ssid, password);

  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(IP);
  
  server.begin();
}

// more of my globals
String myStatus="";
int motspeed=20;
int mot0stp[3];
int mot0lastDir[3];
int thenext[3];

            

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    myStatus="";
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
// my stuff            MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMmy
            int i=0;
            int stepsToMove=0;
            int motChoice=0; // can be 0, 4, 8
            int motBit=0; // is motChoice/4, for indexing direction, laststep etc
            if (header.indexOf("/setSpeed") >= 0) 
            {
              motspeed=header.substring(header.indexOf("/setSpeed")+9).toInt();              
              if((motspeed<1) || (motspeed>1000)){motspeed=20;}
              myStatus+="Speed set to "+((String)motspeed) +" ms. ";
            }
            if (  (header.indexOf("/moveX") >= 0) ||    (header.indexOf("/moveY") >= 0) ||    (header.indexOf("/moveZ") >= 0)  ) 
            {
              myStatus="moved axis ";
              if (header.indexOf("/moveX") >= 0) {motChoice=8; myStatus+="X ";}
              if (header.indexOf("/moveY") >= 0) {motChoice=4; myStatus+="Y ";}
              if (header.indexOf("/moveZ") >= 0) {motChoice=0; myStatus+="Z ";}
              motBit=motChoice/4;
              stepsToMove=header.substring(header.indexOf("/move")+6).toInt();
              myStatus+=((String)stepsToMove) +" steps. ";
              if(stepsToMove>0)
              {
                if(mot0lastDir[motBit]==-1){mot0stp[motBit]+=2; if(mot0stp[motBit]>3){mot0stp[motBit]=mot0stp[motBit]-4;}}
                thenext[motBit]=(mot0stp[motBit]+1) & 3; // here I tried to use 2 phases instead of just one to increase torque, but there seem to be some problem with the hardware (?)
                //if(thenext>3){thenext=0;}
                for(i=0;i<stepsToMove;i++)
                {
                 digitalWrite(myMotorPin[motChoice+mot0stp[motBit]], HIGH);
//                digitalWrite(myMotorPin[motChoice+thenext[motBit]], HIGH);
                 delay(motspeed);
//                digitalWrite(myMotorPin[motChoice+thenext[motBit]], LOW);
                 digitalWrite(myMotorPin[motChoice+mot0stp[motBit]], LOW);
                 mot0stp[motBit]++;
                 if(mot0stp[motBit]>3){mot0stp[motBit]=0;}
                 mot0lastDir[motBit]=1;
               }
                
              } // endif whether moveXYZ>0 -------------------
              if(stepsToMove<0)
              {
               if(mot0lastDir[motBit]==1){mot0stp[motBit]-=2; if(mot0stp[motBit]<0){mot0stp[motBit]=4+mot0stp[motBit];}}                
               thenext[motBit]=mot0stp[motBit]-1;
               if(thenext[motBit]<0){thenext[motBit]=3;}
               for(i=0;i<(0-stepsToMove);i++)
               {
                digitalWrite(myMotorPin[motChoice+mot0stp[motBit]], HIGH);
//                digitalWrite(myMotorPin[motChoice+thenext[motBit]], HIGH);
                delay(motspeed);
//                digitalWrite(myMotorPin[motChoice+thenext[motBit]], LOW);
                digitalWrite(myMotorPin[motChoice+mot0stp[motBit]], LOW);
                mot0stp[motBit]--;
                if(mot0stp[motBit]<0){mot0stp[motBit]=3;}
                mot0lastDir[motBit]=-1;
               }
              } // endif whether moveX<0
            } // endif whether header contains moveX
// eo my stuff            MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMy


            
// ----------------------------------------------------------------------------------------            
            
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<title>CNC test</title>");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons  // note, some leftovers from the button demo, feel free to remove
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #555555;}</style></head>");
            
            // Web Page Heading
            client.println("<body><h1>ESP32 CNC controller via WiFi AP/Webserver</h1>");
            
            client.println("<br>Status: "+myStatus+". ");

            client.println("</body></html>");
            
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
  delay(10);
}

Credits

Mr.Dee67
3 projects • 1 follower

Comments