adding backend script + test program for microcontroller
This commit is contained in:
129
arduino_code/Test/src/main.cpp
Normal file
129
arduino_code/Test/src/main.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
// arduino_code/Test/src/main.cpp
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
// WiFi credentials
|
||||
const char* ssid = "Home";
|
||||
const char* password = "88888888qwE";
|
||||
|
||||
// MQTT Server details
|
||||
const char* mqtt_server = "192.168.31.225";
|
||||
const char* mqtt_user = "test";
|
||||
const char* mqtt_password = "1234";
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
// Пины для управления TMC2209
|
||||
const int DIR_PIN = 18; // Направление
|
||||
const int STEP_PIN = 19; // Шаг
|
||||
const int EN_PIN = 21; // Включение (опционально)
|
||||
|
||||
// Переменные для управления
|
||||
bool motor_running = false;
|
||||
int step_delay = 1000; // Задержка между шагами (в микросекундах)
|
||||
int steps_per_second = 1000; // Количество шагов в секунду
|
||||
|
||||
void setup_wifi() {
|
||||
delay(10);
|
||||
// Подключение к WiFi
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
}
|
||||
}
|
||||
|
||||
void startMotor() {
|
||||
motor_running = true;
|
||||
Serial.println("Motor started");
|
||||
}
|
||||
|
||||
void stopMotor() {
|
||||
motor_running = false;
|
||||
Serial.println("Motor stopped");
|
||||
}
|
||||
|
||||
void updateStepDelay() {
|
||||
// Рассчитываем задержку между шагами
|
||||
step_delay = 1000000 / steps_per_second;
|
||||
}
|
||||
|
||||
void setSpeed(int speed) {
|
||||
// Установка скорости в шагах в секунду
|
||||
if (speed > 0) {
|
||||
steps_per_second = speed;
|
||||
updateStepDelay();
|
||||
Serial.print("Speed set to: ");
|
||||
Serial.print(speed);
|
||||
Serial.println(" steps/sec");
|
||||
}
|
||||
}
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
// Обработка команд через MQTT
|
||||
String message;
|
||||
for (int i = 0; i < length; i++) {
|
||||
message += (char)payload[i];
|
||||
}
|
||||
|
||||
if (message == "start") {
|
||||
startMotor();
|
||||
} else if (message == "stop") {
|
||||
stopMotor();
|
||||
} else if (message.startsWith("speed:")) {
|
||||
int speed = message.substring(6).toInt();
|
||||
setSpeed(speed);
|
||||
} else if (message.startsWith("turnoff")) {
|
||||
digitalWrite(EN_PIN, HIGH);
|
||||
} else if (message.startsWith("turnon")) {
|
||||
digitalWrite(EN_PIN, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
// Повторное подключение к MQTT
|
||||
while (!client.connected()) {
|
||||
if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
|
||||
client.subscribe("motor/control");
|
||||
} else {
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
setup_wifi();
|
||||
client.setServer(mqtt_server, 1883);
|
||||
client.setCallback(callback);
|
||||
|
||||
// Настройка пинов
|
||||
pinMode(DIR_PIN, OUTPUT);
|
||||
pinMode(STEP_PIN, OUTPUT);
|
||||
pinMode(EN_PIN, OUTPUT);
|
||||
|
||||
// Включаем двигатель
|
||||
digitalWrite(EN_PIN, LOW);
|
||||
|
||||
// Начальное направление (вперед)
|
||||
digitalWrite(DIR_PIN, HIGH);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
// Обновляем задержку для правильной скорости
|
||||
updateStepDelay();
|
||||
// Управление двигателем
|
||||
if (motor_running) {
|
||||
// Выполняем шаг
|
||||
digitalWrite(STEP_PIN, HIGH);
|
||||
delayMicroseconds(step_delay);
|
||||
digitalWrite(STEP_PIN, LOW);
|
||||
delayMicroseconds(step_delay);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user