/* * Controlling 28BYJ-48 Stepper Motor over WiFi using ESP8266, * NodeMCU and D1 Mini * using 2 push buttons: CW and CCW over WiFi and mobile phone. * * Watch Video instrution for this code:https://youtu.be/CzvGR0WTllI * * Full explanation of this code and wiring diagram is available at * my Arduino Course at Udemy.com here: http://robojax.com/L/?id=62 * Written by Ahmad Shamshiri on April 20, 2020 at 12:27 * in Ajax, Ontario, Canada. www.robojax.com * * Get this code and other Arduino codes from Robojax.com Learn Arduino step by step in structured course with all material, wiring diagram and library all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62 If you found this tutorial helpful, please support me so I can continue creating content like this. You can support me on Patreon http://robojax.com/L/?id=63 or make donation using PayPal http://robojax.com/L/?id=64 * * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* * This code has been download from Robojax.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Copyright (c) 2015, Majenko Technologies All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * * Neither the name of Majenko Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ ///watch video for details of lines below //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; //static const uint8_t D5 = 14; //static const uint8_t D6 = 12; //static const uint8_t D7 = 13; //static const uint8_t D8 = 15; //static const uint8_t D9 = 3; //static const uint8_t D10 = 1; 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"};//Watch Video for details (on Udemy) String buttonTitle2[] ={"CCW", "CW"}; String argId[] ={"ccw", "cw"}; #include #include #include #include #ifndef STASSID #define STASSID "WiFi SSID" #define STAPSK "password" #endif const char *ssid = STASSID; const char *password = STAPSK; ESP8266WebServer server(80); void handleRoot() { //Robojax.com ESP32 Relay Motor Control String HTML ="\ \ \ \t\nRobojax 28BYJ-48 Motor Control\ \t\n\ \n\n\n\n\n\n

Robojax 28BYJ-48 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) { //Robojax.com 28BYJ-48 Steper Motor Control 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("Robojax 28BYJ-48 Stepper Motor Control"); //Robojax.com 28BYJ-48 Steper 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()); //multicast DNS //Robojax.com 28BYJ-48 Steper Motor Control if (MDNS.begin("robojaxESP8266")) { Serial.println("MDNS responder started"); Serial.println("access via http://robojaxESP8266"); } 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(); //Watch details at my Arduino Course at Udemy.com 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); //Robojax.com 28BYJ-48 Steper Motor Control } /* * motorControl() * updates the value of "dirStatus" varible to 1, 2 or 3 * returns nothing * written by Ahmad Shamshiri * on Wednesday April 19, 2020 at 18:03 in Ajax, Ontario, Canada * www.robojax.com */ 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 /* * @brief sends signal to the motor * @param "c" is integer representing the pol of motor * @return does not return anything * * www.Robojax.com code June 2019 */ 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