static const uint8_t D0 = 16; static const uint8_t D1 = 5; static const uint8_t D2 = 4; static const uint8_t D3 = 0; static const uint8_t D4 = 2; int Pin1 = D1;//IN1 is connected int Pin2 = D2;//IN2 is connected int Pin3 = D3;//IN3 is connected int Pin4 = D4;//IN4 is connected int pole1[] ={0,0,0,0, 0,1,1,1, 0};//pole1, 8 step values int pole2[] ={0,0,0,1, 1,1,0,0, 0};//pole2, 8 step values int pole3[] ={0,1,1,1, 0,0,0,0, 0};//pole3, 8 step values int pole4[] ={1,1,0,0, 0,0,0,1, 0};//pole4, 8 step values int poleStep = 0; int dirStatus = 3;// stores direction status 3= stop (do not change) String buttonTitle1[] ={"CCW", "CW"}; String buttonTitle2[] ={"CCW", "CW"}; String argId[] ={"ccw", "cw"}; #include #include #include #include #ifndef STASSID #define STASSID "HUAWEI_H112_8FBD" #define STAPSK "45TR1LYB86F" #endif const char *ssid = STASSID; const char *password = STAPSK; ESP8266WebServer server(80); void handleRoot() { String HTML ="\ \ \ \t\n Motor Control\ \t\n\ \n\n\n\n\n\n

Stepper Motor Control

\n"; if(dirStatus ==2){ HTML +="\n\t

Motor Running in CW

\n"; }else if(dirStatus ==1){ HTML +="\n\t

Motor Running in CCW

\n"; }else{ HTML +="\n\t

Motor OFF

\n"; } if(dirStatus ==1){ HTML +="\t
\n\t\t"; HTML +=buttonTitle1[0]; //motor ON title }else{ HTML +="\t\n\n"; //Watch details at my Arduino Course at Udemy.com if(dirStatus ==2){ HTML +="\t
\n\t\t"; HTML +=buttonTitle1[1]; //motor ON title }else{ HTML +="\t\n\n"; HTML +="\t\n\n\n"; server.send(200, "text/html", HTML); } void handleNotFound() { String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET) ? "GET" : "POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i = 0; i < server.args(); i++) { message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); } void setup(void) { pinMode(Pin1, OUTPUT);//define pin for ULN2003 in1 pinMode(Pin2, OUTPUT);//define pin for ULN2003 in2 pinMode(Pin3, OUTPUT);//define pin for ULN2003 in3 pinMode(Pin4, OUTPUT);//define pin for ULN2003 in4 Serial.begin(115200);//initialize the serial monitor Serial.println("Stepper Motor Control"); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: http://"); Serial.println(WiFi.localIP()); if (MDNS.begin("robojaxESP8266")) { Serial.println("MDNS responder started"); } server.on("/", handleRoot); server.on("/motor", HTTP_GET, motorControl); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); } void loop(void) { server.handleClient(); MDNS.update(); if(dirStatus ==1){ poleStep++; driveStepper(poleStep); }else if(dirStatus ==2){ poleStep--; driveStepper(poleStep); }else{ driveStepper(8); } if(poleStep>7){ poleStep=0; } if(poleStep<0){ poleStep=7; } delay(1); } void motorControl() { //Watch details at my Arduino Course at Udemy.com if(server.arg(argId[0]) == "on") { dirStatus = 1;// CCW }else if(server.arg(argId[0]) == "off"){ dirStatus = 3; // motor OFF }else if(server.arg(argId[1]) == "on"){ dirStatus = 2; // CW }else if(server.arg(argId[1]) == "off"){ dirStatus = 3; // motor OFF } handleRoot(); }//motorControl end void driveStepper(int c) { //Watch details at my Arduino Course at Udemy.com digitalWrite(Pin1, pole1[c]); digitalWrite(Pin2, pole2[c]); digitalWrite(Pin3, pole3[c]); digitalWrite(Pin4, pole4[c]); }//driveStepper end here