Last active
January 17, 2024 18:10
-
-
Save SimedruF/2d547e43c351b9b6fe0df3d82c9c149b to your computer and use it in GitHub Desktop.
ESP32_Stepper_control.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Florin Simedru | |
Complete project details at https://blog.automatic-house.ro | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files. | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
*/ | |
/* | |
; PlatformIO Project Configuration File | |
; | |
; Build options: build flags, source filter | |
; Upload options: custom upload port, speed and extra flags | |
; Library options: dependencies, extra library storages | |
; Advanced options: extra scripting | |
; | |
; Please visit documentation for the other options and examples | |
; https://docs.platformio.org/page/projectconf.html | |
[env:esp32doit-devkit-v1] | |
platform = espressif32 | |
board = esp32doit-devkit-v1 | |
framework = arduino | |
upload_port = COM9 | |
monitor_port = COM9 | |
monitor_speed = 115200 | |
lib_deps = | |
ottowinter/ESPAsyncWebServer-esphome@^3.1.0 | |
*/ | |
#include <ESPAsyncWebServer.h> | |
void Setup_and_Start_Server(); | |
void notFound(AsyncWebServerRequest *request); | |
// Define stepper motor connections | |
const int DIR = 12; | |
const int STEP = 14; | |
const int steps_per_rev = 200; | |
int rotations = 0; | |
bool clockwise = false; | |
bool counter_clockwise = false; | |
bool selected_rotations = false; | |
String directionParam = ""; | |
const char *ssid = "ssid"; | |
const char *password = "password"; | |
AsyncWebServer server(80); | |
void notFound(AsyncWebServerRequest *request) | |
{ | |
request->send(404, "text/plain", "Not found"); | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
pinMode(STEP, OUTPUT); | |
pinMode(DIR, OUTPUT); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
if (WiFi.waitForConnectResult() != WL_CONNECTED) | |
{ | |
Serial.printf("WiFi Failed!\n"); | |
return; | |
} | |
Serial.print("IP Address: "); | |
Serial.println(WiFi.localIP()); | |
Setup_and_Start_Server(); | |
} | |
void loop() | |
{ | |
if(clockwise==true) | |
{ | |
digitalWrite(DIR, HIGH); | |
Serial.println("Spinning Clockwise..."); | |
for (int i = 0; i < steps_per_rev; i++) | |
{ | |
digitalWrite(STEP, HIGH); | |
delayMicroseconds(1000); | |
digitalWrite(STEP, LOW); | |
delayMicroseconds(1000); | |
} | |
} | |
if(counter_clockwise==true) | |
{ | |
digitalWrite(DIR, LOW); // Select the direction backward | |
Serial.println("Spinning Counter clockwise..."); | |
for (int i = 0; i < steps_per_rev; i++) | |
{ | |
digitalWrite(STEP, HIGH); | |
delayMicroseconds(1000); | |
digitalWrite(STEP, LOW); | |
delayMicroseconds(1000); | |
} | |
} | |
if(selected_rotations == true) | |
{ | |
int directionPin; | |
if (directionParam == "forward") | |
{ | |
directionPin = HIGH; // Select the direction forward | |
} | |
else | |
{ | |
directionPin = LOW; // Select the direction backward | |
} | |
digitalWrite(DIR, directionPin); | |
for (int i = 0; i < rotations; i++) | |
{ | |
digitalWrite(STEP, HIGH); | |
delayMicroseconds(1000); | |
digitalWrite(STEP, LOW); | |
delayMicroseconds(1000); | |
} | |
selected_rotations = false; | |
} | |
} | |
void Setup_and_Start_Server() | |
{ | |
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) | |
{ | |
String html = "<html><head>"; | |
html += "<style>"; | |
html += "body { font-family: Arial, sans-serif; text-align: center; }"; | |
html += "button { padding: 10px 20px; font-size: 16px; margin: 10px; }"; | |
html += "</style>"; | |
html += "</head><body>"; | |
html += "<h1>Stepper motor control</h1>"; | |
html += "<label for='rotationsInput'>Number of rotations:</label>"; | |
html += "<input type='number' id='rotationsInput' placeholder='Input number of rotations'>"; | |
html += "<label for='directionSelect'>Select direction:</label>"; | |
html += "<select id='directionSelect'>"; | |
html += " <option value='forward'>Forward</option>"; | |
html += " <option value='backward'>Backward</option>"; | |
html += "</select>"; | |
html += "<button id='startRotationButton'>Start rotation</button>"; | |
html += "<br><button id='forwardButton'>Forward</button>"; | |
html += "<button id='stopButton'>Stop</button>"; | |
html += "<button id='backwardButton'>Backward</button>"; | |
html += "<script>"; | |
html += "document.getElementById('startRotationButton').onclick = function() {"; | |
html += " var rotations = document.getElementById('rotationsInput').value;"; | |
html += " var direction = document.getElementById('directionSelect').value;"; | |
html += " sendCommand('/startRotation?rotations=' + rotations + '&direction=' + direction);"; | |
html += "};"; | |
html += "document.getElementById('forwardButton').onclick = function() {"; | |
html += " sendCommand('/forward');"; | |
html += "};"; | |
html += "document.getElementById('stopButton').onclick = function() {"; | |
html += " sendCommand('/stop');"; | |
html += "};"; | |
html += "document.getElementById('backwardButton').onclick = function() {"; | |
html += " sendCommand('/backward');"; | |
html += "};"; | |
html += "function sendCommand(command) {"; | |
html += " var xhr = new XMLHttpRequest();"; | |
html += " xhr.open('GET', command, true);"; | |
html += " xhr.send();"; | |
html += "}"; | |
html += "</script>"; | |
html += "</body></html>"; | |
request->send(200, "text/html", html); }); | |
server.on("/forward", HTTP_GET, [](AsyncWebServerRequest *request) | |
{ | |
digitalWrite(DIR, HIGH); // Select the direction forward | |
clockwise = true; | |
counter_clockwise = false; | |
selected_rotations = false; | |
request->send(200, "text/plain", "forward"); }); | |
server.on("/backward", HTTP_GET, [](AsyncWebServerRequest *request) | |
{ | |
digitalWrite(DIR, LOW); // Select the direction backward | |
clockwise = false; | |
counter_clockwise = true; | |
selected_rotations = false; | |
request->send(200, "text/plain", "backward"); }); | |
server.on("/stop", HTTP_GET, [](AsyncWebServerRequest *request) | |
{ | |
digitalWrite(DIR, LOW); // Setează direcția înapoi | |
clockwise = false; | |
counter_clockwise = false; | |
selected_rotations = false; | |
Serial.println("Stop spinning !"); | |
request->send(200, "text/plain", "stop"); }); | |
server.on("/startRotation", HTTP_GET, [](AsyncWebServerRequest *request) | |
{ | |
String rotationsParam = request->getParam("rotations")->value(); | |
directionParam = request->getParam("direction")->value(); | |
rotations = rotationsParam.toInt(); | |
clockwise = false; | |
counter_clockwise = false; | |
selected_rotations = true; | |
request->send(200, "text/plain", "Free Rotation"); }); | |
server.onNotFound(notFound); | |
server.begin(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment