/********* HTMAA 2021 - Final Project - CoreXY Plotting Machine Created by: Joanne Leong *********/ // Load Wi-Fi library #include //Define GPIO Pins controlling the motor driver 1 #define DIR_A 17 #define STEP_A 18 #define M0_A 19 #define NSLEEP_A 21 //Define GPIO Pins controlling motor driver 2 #define DIR_B 32 #define STEP_B 33 #define M0_B 25 #define NSLEEP_B 27 // Replace with your network credentials const char* ssid = "MIT"; const char* password = ""; // Set web server port number to 80 WiFiServer server(80); // Variable to store the HTTP request String header; // Auxiliar variables to store the current output state String gpio15State = "off"; // Assign output variables to GPIO pins const int gpio15 = 15; // Current time unsigned long currentTime = millis(); // Previous time unsigned long previousTime = 0; // Define timeout time in milliseconds (example: 2000ms = 2s) const long timeoutTime = 2000; //motor variables String gantryState = "stop"; int delayus = 800; //1000; //delay in microseconds int unitStepDist = 2000; //demo variables bool isDemoing = false; String lego_heart; int demoCount = 0; /// custom functions void setupDemo(){ isDemoing = true; lego_heart = "ururdrdldldldlulululururdrdrs"; demoCount = 0; } void drawDemo(){ if(lego_heart.length() > demoCount){ setGantryDirection(lego_heart.charAt(demoCount)); stepGantry(); // Serial.println("Step this way:" + lego_heart.charAt(demoCount)); demoCount = demoCount + 1; }else{ isDemoing = false; } } //todo: decouple for loop... void stepGantry(){ if(gantryState != "stop"){ if(gantryState == "up" || gantryState == "down" ||gantryState == "left" || gantryState == "right"){ for (int i = 0; i < unitStepDist; ++i){ // manual pwm, first high, then low digitalWrite(STEP_A, HIGH); digitalWrite(STEP_B, HIGH); delayMicroseconds(delayus); digitalWrite(STEP_A, LOW); digitalWrite(STEP_B, LOW); } }else if(gantryState == "upright" || gantryState == "bottomleft"){ for (int i = 0; i < unitStepDist; ++i){ // manual pwm, first high, then low digitalWrite(STEP_A, HIGH); delayMicroseconds(delayus); digitalWrite(STEP_A, LOW); } }else if(gantryState == "upleft" || gantryState == "bottomright"){ for (int i = 0; i < unitStepDist; ++i){ // manual pwm, first high, then low digitalWrite(STEP_B, HIGH); delayMicroseconds(delayus); digitalWrite(STEP_B, LOW); } } } //if not stopped } // stepGantry void setGantryDirection(char dir){ switch(dir){ case 'u': Serial.println("corexy up"); gantryState = "up"; digitalWrite(DIR_A, HIGH); digitalWrite(DIR_B, LOW); break; case 'r': Serial.println("corexy right"); gantryState = "right"; digitalWrite(DIR_A, LOW); digitalWrite(DIR_B, LOW); break; case 'd': Serial.println("corexy down"); gantryState = "down"; digitalWrite(DIR_A, LOW); digitalWrite(DIR_B, HIGH); break; case 'l': Serial.println("corexy left"); gantryState = "left"; digitalWrite(DIR_A, HIGH); digitalWrite(DIR_B, HIGH); break; case 'y': Serial.println("corexy up-right"); gantryState = "upright"; digitalWrite(DIR_A, HIGH); break; case 't': Serial.println("corexy up-left"); gantryState = "upleft"; digitalWrite(DIR_B, HIGH); break; case 'g': Serial.println("corexy bottom-left"); gantryState = "bottomleft"; digitalWrite(DIR_A, LOW); break; case 'h': Serial.println("corexy bottom-right"); gantryState = "bottomright"; digitalWrite(DIR_B, LOW); break; case 's': Serial.println("corexy STOP"); gantryState = "stop"; break; } } void motorSetup(){ //define as output pins pinMode(DIR_A, OUTPUT); pinMode(STEP_A, OUTPUT); pinMode(M0_A, OUTPUT); pinMode(NSLEEP_A, OUTPUT); pinMode(DIR_B, OUTPUT); pinMode(STEP_B, OUTPUT); pinMode(M0_B, OUTPUT); pinMode(NSLEEP_B, OUTPUT); //settings for A digitalWrite(DIR_A, LOW); digitalWrite(M0_A, LOW); // 1/8 step mode digitalWrite(NSLEEP_A, HIGH); // awake //settings for B digitalWrite(DIR_B, LOW); digitalWrite(M0_B, LOW); // 1/8 step mode digitalWrite(NSLEEP_B, HIGH); //awake } void setup() { Serial.begin(115200); // Initialize the output variables as outputs pinMode(gpio15, OUTPUT); ; // Set outputs to LOW digitalWrite(gpio15, LOW); motorSetup(); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); } void loop(){ WiFiClient client = server.available(); // Listen for incoming clients if (client) { // If a new client connects, currentTime = millis(); previousTime = currentTime; 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() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected currentTime = millis(); 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(); // turns the GPIOs on and off if (header.indexOf("GET /15/on") >= 0) { Serial.println("GPIO 15 on"); gpio15State = "on"; digitalWrite(gpio15, HIGH); } else if (header.indexOf("GET /15/off") >= 0) { Serial.println("GPIO 15 off"); gpio15State = "off"; digitalWrite(gpio15, LOW); } else if (header.indexOf("GET /corexy/up") >= 0) { setGantryDirection('u'); stepGantry(); } else if (header.indexOf("GET /corexy/down") >= 0) { setGantryDirection('d'); stepGantry(); } else if (header.indexOf("GET /corexy/left") >= 0) { setGantryDirection('l'); stepGantry(); } else if (header.indexOf("GET /corexy/right") >= 0) { setGantryDirection('r'); stepGantry(); } else if (header.indexOf("GET /corexy/demo") >= 0) { Serial.println("start demo"); setupDemo(); } // // Display the HTML web page client.println(""); client.println(""); client.println(""); // CSS to style the on/off buttons // Feel free to change the background-color and font-size attributes to fit your preferences client.println(""); // Web Page Heading client.println("

ESP32 Web Server - CoreXY

"); // Display current state, and ON/OFF buttons for GPIO 15 client.println("

LED State (GPIO 15): " + gpio15State + "

"); // If the gpio15State is off, it displays the ON button if (gpio15State=="off") { client.println("

"); } else { client.println("

"); //else turn the light off } // Display a button to move the corexy client.println("

COREXY - Move " + gantryState + "

"); client.println("

"); client.println("

"); client.println("

"); client.println("

"); //demo drawing client.println("

"); //else turn the light off client.println(""); // 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(""); } //demo if(isDemoing){ drawDemo(); } }