Hallo liebe Community,
ich möchte eine Hühnerklappe steuern und dafür einen kleinen Steppermotor (28BYJ) nutzen.
Das ganze soll dann über mqtt getriggert werden.
Hier mein bisheriger Ansatz:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <AccelStepper.h>
#define HALFSTEP 4
// ########### WIFI ############################################
// Update these with values suitable for your network.
const char* ssid = "__";//put your wifi ssid here
const char* password = "xxxx";//put your wifi password here
const char* mqtt_server = "192......";
const char* clientId = "chickenDoor";
const char* userName = "xxx";
const char* passWord = "yyy";
// ################################################################
// Motor pin definitions
#define motorPin1 D1 // IN1 on the ULN2003 driver 1
#define motorPin2 D2 // IN2 on the ULN2003 driver 1
#define motorPin3 D5 // IN3 on the ULN2003 driver 1
#define motorPin4 D6 // IN4 on the ULN2003 driver 1
// ################################################################
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
// ################################################################
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(100);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
// ################################################################
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Command from MQTT broker is: [");
Serial.print(topic);
int p =(char)payload[0]-'0';
int stepperLoop = 0;
// ################################################################
if(p==1)
{
//myStepper.step(stepsPerRevolution);
Serial.print(" clockwise]" );
}
// ################################################################
else if(p==2)
{
//myStepper.step(-stepsPerRevolution);
Serial.print(" counterclockwise]" );
}
// ################################################################
else if(p==3)
{
Serial.print(" clockwise loop: ");
stepper1.moveTo(4000);
stepper1.run();
}
// ################################################################
else if(p==4)
{
Serial.print(" counterclockwise loop: " );
stepper1.moveTo(4000);
stepper1.run();
}
else if(p==0)
{
digitalWrite (D1,LOW);
digitalWrite (D2,LOW);
digitalWrite (D5,LOW);
digitalWrite (D6,LOW);
Serial.print(" off]" );
}
Serial.println();
}
// ################### RECONNECT ################################
void reconnect() {
// Loop until we're reconnected
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(clientId,userName,passWord))
{
Serial.println("connected");
//once connected to MQTT broker, subscribe command if any
client.subscribe("chickenDoor");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 6 seconds before retrying
delay(6000);
}
}
}
// ################################################################
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
// ################## Speed ####################################
stepper1.setMaxSpeed(100.0);
stepper1.setAcceleration(100.0);
stepper1.setSpeed(100);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
Ich würde ihn nun gerne wenn ich ihm eine 3 übergebe, dass er sich 4000 Inkremente bewegt.
Leider tut er gar nichts. der Befehl kommt aber an.
was kann ich tun?