#include <AccelStepper.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* serverAddress = "http://pm.is2b.ru/robot/command.php";
// Time interval in milliseconds between command processing
const unsigned long COMMAND_INTERVAL = 5000;
// Last time a command was processed
unsigned long lastCommandTime = 0;
#define ZDIR 17
#define ZPUL 18
#define XDIR 16
#define XPUL 15
int stepsPerRevolution = 200;
AccelStepper stepperZ(AccelStepper::DRIVER, ZPUL, ZDIR);
AccelStepper stepperX(AccelStepper::DRIVER, XPUL, XDIR);
#define motorInerfaceType 1
void setup() {
// set the speed at 60 rpm:
// myStepper.setSpeed(2);
// initialize the serial port:
// Serial.begin(9600);
pinMode(ZDIR, OUTPUT);
pinMode(ZPUL, OUTPUT);
pinMode(XDIR, OUTPUT);
pinMode(XPUL, OUTPUT);
stepperZ.setMaxSpeed(1000);
stepperZ.setAcceleration(100);
stepperZ.moveTo(0);
stepperX.setMaxSpeed(1000);
stepperX.setAcceleration(100);
stepperX.moveTo(0);
Serial.begin(9600);
}
bool runStepper(AccelStepper* stepper){
if (stepper->distanceToGo() != 0) stepper->run();
}
void loop() {
bool movement = false;
if (stepperX.distanceToGo() != 0) {
stepperX.run();
movement = true;
}
if (stepperZ.distanceToGo() != 0) {
stepperZ.run();
movement = true;
}
if (!movement && millis() - lastCommandTime > COMMAND_INTERVAL){
lastCommandTime = millis();
if (WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Wifi connected");
}
HTTPClient http;
http.begin(String(serverAddress));
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
String input = http.getString();
Serial.println(input);
int command = 0;
int x = 0.0;
int y = 0.0;
int z = 0.0;
if (input.length() > 5){
int i = 0;
while (i < input.length()) {
char c = input.charAt(i);
i++;
if (c == 'G') {
String value;
while (i < input.length() && (isdigit(input.charAt(i)) || input.charAt(i) == '.')) {
value += input.charAt(i);
i++;
}
command = value.toInt();
} else if (c == 'X') {
String value;
while (i < input.length() && (isdigit(input.charAt(i)) || input.charAt(i) == '.')) {
value += input.charAt(i);
i++;
}
x = value.toInt();
if (command == 0) stepperX.moveTo(x);
if (command == 92) stepperX.setCurrentPosition(x);
} else if (c == 'Y') {
String value;
while (i < input.length() && (isdigit(input.charAt(i)) || input.charAt(i) == '.')) {
value += input.charAt(i);
i++;
}
y = value.toInt();
} else if (c == 'Z') {
String value;
while (i < input.length() && (isdigit(input.charAt(i)) || input.charAt(i) == '.')) {
value += input.charAt(i);
i++;
}
z = value.toInt();
if (command == 0) stepperZ.moveTo(z);
if (command == 92) stepperZ.setCurrentPosition(z);
} else if (isspace(c)) {
// Ignore whitespace between tokens
continue;
} else {
// Invalid character, ignore the rest of the line
break;
}
}
// Print out the results
Serial.print("Command: ");
Serial.println(command);
Serial.print("X: ");
Serial.println(x);
Serial.print("Y: ");
Serial.println(y);
Serial.print("Z: ");
Serial.println(z);
}
} else {
Serial.println("Error getting commands from server, HTTP code: " + httpCode);
}
http.end();
}
}
Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1