Hallo ,
ich stoße bei meinem RFID-Projekt an meine Grenzen, da mir die Erfahrung und das wissen fehlt. Ich möchte per MQTT eine "1", "2" oder "3" an den D1 mini zurückgeben um damit den Buzzer zu steuern (Registrierte Karte = kurzes Signal, ungültige Karte = langes Signal).
Der RFID Code wird einwandfrei an den Mosquitto-Broker übermittelt. Allerdings empfängt der D1 mini keine Werte, die ich über den MQTT-Explorer an das Topic
( client.subscribe("SmartHome/RFID/Test")) publish.
Wenn ich den MQTT-Teil aus dem Programm isoliere und diesen in einem separatem Programm mit einem Buzzer betreibe funktioniert alles einwandfrei.
Was genau muss ich verändern, damit mein Code funktioniert.
Vielen Dank
#include <Arduino.h>
#include <SPI.h>
#include <MFRC522.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#define SS_PIN D8
#define RST_PIN D1
#define BUZZER_PIN D2
MFRC522 mfrc522(SS_PIN, RST_PIN);
unsigned long cardId = 0;
WiFiClient net;
PubSubClient client(net);
const char* mqtt_server = "192.168.178.214";
const char* ssid = "*****";
const char* password = "******";
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
pinMode(BUZZER_PIN, OUTPUT);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
digitalWrite(BUZZER_PIN, HIGH);
delay(100);
digitalWrite(BUZZER_PIN, LOW);
}
void reconnect() {
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
WiFi.begin(ssid, password);
}
while (!client.connected()) {
String clientId = "NodeMCUClient-";
clientId += String(random(0xffff), HEX);
client.subscribe("SmartHome/RFID/Test");
if (!client.connect(clientId.c_str(), "****", "****")) {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void loop() {
reconnect();
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
cardId = getCardId();
Serial.print("New ");
Serial.println(cardId);
client.publish("SmartHome/RFID/Stop", "1");
char buffer[10];
sprintf(buffer, "%lu", cardId);
client.publish("SmartHome/RFID/Code", buffer);
digitalWrite(BUZZER_PIN, HIGH);
delay(100);
digitalWrite(BUZZER_PIN, LOW);
uint8_t control = 0x00;
do {
control = 0;
for (int i = 0; i < 3; i++) {
if (!mfrc522.PICC_IsNewCardPresent()) {
if (mfrc522.PICC_ReadCardSerial()) {
control |= 0x16;
}
if (mfrc522.PICC_ReadCardSerial()) {
control |= 0x16;
}
control += 0x1;
}
control += 0x4;
}
delay(1);
} while (control == 13 || control == 14);
reconnect();
Serial.println("Removed");
digitalWrite(BUZZER_PIN, HIGH);
delay(300);
digitalWrite(BUZZER_PIN, LOW);
client.publish("SmartHome/RFID/Stop", "0");
delay(500);
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
unsigned long getCardId() {
byte readCard[4];
for (int i = 0; i < 4; i++) {
readCard[i] = mfrc522.uid.uidByte[i];
}
return (unsigned long)readCard[0] << 24
| (unsigned long)readCard[1] << 16
| (unsigned long)readCard[2] << 8
| (unsigned long)readCard[3];
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if ((char)payload[0] == '1') {
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(BUZZER_PIN, LOW);
} if ((char)payload[0] == '2') {
digitalWrite(BUZZER_PIN, HIGH);
delay(1000);
digitalWrite(BUZZER_PIN, LOW);
} if ((char)payload[0] == '3') {
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(BUZZER_PIN, LOW);
delay(200);
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(BUZZER_PIN, LOW);
}
}