Hallo
ich möchte gern meinen Motor nach eine bestimmten Zeit abschalten falls der Endtaster nicht funktioniert.Ich hatte nämlich schon mal diese Problem und dadurch wurde der Motor extrem heiß.
Ich verwende folgende Code und habe das Problem dass der Motor nicht immer 10 sek. läuft und dann ausschaltet. Teilweise läuft er auch nur eine Sekunde obwohl der Endschalter noch nicht aktiv ist. Hat jemand eine Idee was falsch gemacht habe?
#include <Bounce2.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <DHT.h>
#define DHTPIN D13
DHT dht (DHTPIN, DHT22);
#ifndef STASSID
#define STASSID "XXXXX"
#define STAPSK "XXXX"
#endif
uint32_t previousMillisauf = 0;
uint32_t previousMilliszu = 0;
unsigned long currentMillis = millis();
const char* ssid = STASSID;
const char* password = STAPSK;
const char* MQTT_BROKER = "10.0.0.170";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
int pinmotorhoch = D2; //const byte pinmotorhoch = 6;
int pinmotorrunter = D3; //const byte pinmotorrunter = 7
int pinlicht = D4;
int tasterAuf = D5;
int tasterZu = D6;
int tasterLicht = D7;
const byte pinendtasteroben = D8;
const byte pinendtasterunten = D9;
int Status = 0;
const bool _pushed = LOW; //Pegel bei gedrückten Taster
const bool _AN = HIGH; //Pegel für aktives Relais
const bool _AUS = LOW;
bool automatisch = true;
bool auf = false;
bool zu = false;
bool mqttauf = false;
bool mqttzu = false;
bool mqttauto = false;
Bounce myButtonauf = Bounce();
Bounce myButtonzu = Bounce();
Bounce myButtonauto = Bounce();
void setup()
{
Serial.begin(115200);
dht.begin();
setup_wifi();
client.setServer(MQTT_BROKER, 1883);
client.setCallback(callback);
pinMode(pinmotorhoch, OUTPUT);
pinMode(pinmotorrunter, OUTPUT);
pinMode(pinlicht, OUTPUT);
pinMode(tasterAuf, INPUT_PULLUP);
pinMode(tasterZu, INPUT_PULLUP);
pinMode(pinendtasteroben, INPUT_PULLUP);
pinMode(pinendtasterunten, INPUT_PULLUP);
myButtonauf.attach(tasterAuf);
myButtonauf.interval(5); // 5ms zum Entprellen
myButtonzu.attach(tasterZu);
myButtonzu.interval(5); // 5ms zum Entprellen
//pinmotorhoch = _AUS;
//pinmotorrunter = _AUS;
if (!_pushed) { //Taster schalten gegen GND
pinMode(pinendtasteroben, INPUT_PULLUP);
pinMode(pinendtasterunten, INPUT_PULLUP);
}
ArduinoOTA.setHostname("Hühnerstall"); //Hostnamezuordnung
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else {
type = "filesystem";
}
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void setup_wifi() {
delay(500);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
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("Received message [");
Serial.print(topic);
Serial.print("] ");
char msg[length + 1];
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
msg[i] = (char)payload[i];
}
Serial.println();
msg[length] = '\0';
Serial.println(msg);
if (strcmp(msg, "auf") == 0 && zu != true)
{
auf = true; // sollte er aus sein, setzen wir auf auf true
zu = false;
automatisch = false;
}
else if (strcmp(msg, "zu") == 0 && auf != true)
{
auf = false; // sollte er aus sein, setzen wir auf auf true
zu = true;
automatisch = false;
}
else if (strcmp(msg, "ein") == 0)
{
digitalWrite(pinlicht, HIGH);
client.publish("/Hühnerstall/Licht/status", "1");
}
else if (strcmp(msg, "aus") == 0) {
digitalWrite(pinlicht, LOW);
client.publish("/Hühnerstall/Licht/status", "0");
}
}
void reconnect() {
while (!client.connected())
{
Serial.println("Reconnecting MQTT...");
if (!client.connect("Huehnerstall")) {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" retrying in 5 seconds");
delay(5000);
}
}
client.subscribe("/Hühnerstall/");
Serial.println("MQTT Connected...");
}
void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
delay(150);
ArduinoOTA.handle();
long now = millis();
currentMillis = millis();
if (now - lastMsg > 60000) {
lastMsg = now;
//int chk = dht.read();
String msg = "";
char MsgTemp[25];
char MsgFeutigkeit[25];
char buffStr[5]; //Buffer mit maximaler Zeichenanzahl: 5
itoa(pinendtasteroben,buffStr,10);
msg = dht.readTemperature();
msg.toCharArray(MsgTemp, 25);
msg = dht.readHumidity();
msg = msg + "%";
msg.toCharArray(MsgFeutigkeit, 25);
client.publish("/Hühnerstall/Temp/status", MsgTemp);
client.publish("/Hühnerstall/Feuchtigkeit/status", MsgFeutigkeit);
client.publish("/Hühnerstall/Klappenstatus/status",buffStr);
}
//-------- Taster AUF --------
myButtonauf.update();
if (myButtonauf.fallingEdge())
{
if (auf == false) // Abfrage ob der unser logischer Schalter aus ist
{
auf = true; // sollte er aus sein, setzen wir auf auf true
zu = false;
digitalWrite(pinmotorrunter, _AUS);
}
}
//-------- Taster ZU --------
myButtonzu.update();
if (myButtonzu.fallingEdge())
{
if (zu == false) { // Abfrage ob der unser logischer Schalter aus ist
zu = true; // sollte er aus sein, setzen wir auf auf true
auf = false;
digitalWrite(pinmotorhoch, _AUS);
}
}
//----------Steuerung durch Tasten------------
if (auf == true || mqttauf == true) // Taster auf betätigen; Motor fährt hoch bis Endschalter betätigt
{
digitalWrite(pinmotorhoch, HIGH);
previousMillisauf = millis(); //neu hinzu 26.02.2021
}
if ((digitalRead(pinmotorhoch) == _AN && digitalRead(pinendtasteroben) == _pushed) || (currentMillis - previousMillisauf > 10 * 1000UL && digitalRead(pinmotorhoch) == _AN)) //neu hinzu 26.02.2021
{
digitalWrite(pinmotorhoch, _AUS);
client.publish("/Hühnerstall/Klappe/status", "0"); // 1= offen
auf = false;
mqttauf = false;
automatisch = false;
}
//----------Steuerung mit Taster zu---------------------
if (zu == true || mqttzu == true) // Taster zu betätigen; Motor fährt runter bis Endschalter betätigt
{
digitalWrite(pinmotorrunter, HIGH);
previousMilliszu = millis(); //neu hinzu 26.02.2021
}
if ((digitalRead(pinmotorrunter) == _AN && digitalRead(pinendtasterunten) == _pushed) || (currentMillis - previousMilliszu > 10 * 1000UL && digitalRead(pinmotorrunter) == _AN)) //neu hinzu 26.02.2021
{
digitalWrite(pinmotorrunter, _AUS);
client.publish("/Hühnerstall/Klappe/status", "1"); // 0= geschlossen
zu = false;
mqttzu = false;
automatisch = false;
}
}
//-------------------------------------------------------------------------------------------------------------------------------