Ich mir ein Steuerung für meine Alarmanlage gebaut. Ich verwende dazu ein 1.8" TFT Display und einen PN532 an einem NodeMCU.
Auf dem Display wird die Uhrzeit, der Status der Alarmanlage und die das Ergebnis des RFID Auslesens angezeigt.
Funktioniert alles soweit jedoch wird die Uhrzeit und der Status nur alle 20 sec. aktualisiert. Da die Uhrzeit mit Sekunden angezeigt werden soll ist es natürlich nicht so toll. auch wenn der RFID als richtig erkannt wird, wird die Alarmanlage sofort deaktiviert, doch der Status ändert sich erst nach den 20 sec. Leider sind meine Kenntnisse noch nicht so weit, dass ich das Problem beheben kann. Ich hoffe ihr könnt mir helfen.
Hier mein Sketch:
#include <SPI.h>
#include <TFT_eSPI.h> // Hardware-specific library
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Adafruit_GFX.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);
const char* SSID = "XXXXXX";
const char* PSK = "XXXXXX";
const char* MQTT_BROKER = "XXXXX";
const long utcOffsetInSeconds = 3600;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "de.pool.ntp.org", utcOffsetInSeconds);
TFT_eSPI tft = TFT_eSPI();
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
setup_wifi();
timeClient.begin();
client.setServer(MQTT_BROKER, 1886);
client.setCallback(callback);
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
nfc.setPassiveActivationRetries(0xFF);
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A card");
tft.init();
tft.fillScreen(TFT_BLACK);
tft.setRotation(1);
tft.setCursor(65, 0, 2);
tft.print("SmartHome");
tft.drawLine(0, 17, 160, 17, TFT_WHITE);
tft.setCursor(8, 25, 4);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.println("Alarmanlage");
tft.setCursor(20, 60, 4);
tft.setTextColor(TFT_RED, TFT_BLACK);
//tft.println(msg);
}
void setup_wifi() {
WiFi.begin(SSID, PSK);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println(WiFi.localIP());
}
void loop() {
if (!client.connected()) {
while (!client.connected()) {
client.connect("ESPTFT");
client.subscribe("/Alarmanlage/OLED/ausgabe");
delay(100);
}
}
client.loop();
timeClient.update();
Serial.print(timeClient.getFormattedTime());
tft.setCursor(0, 0, 2);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.print(timeClient.getFormattedTime());
delay(1000);
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
uint8_t compare[] = {0xCA, 0x0B, 0x20, 0x58};
uint8_t compare1[] = {0x43, 0xE0, 0x57, 0x05};
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (success) {
Serial.println("Found a card!");
Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i=0; i < uidLength; i++)
{
Serial.print(" 0x");Serial.print(uid[i], HEX);
}
Serial.println("");
Serial.print("Card Status :");
//erste RFID-Chip
if (memcmp(uid, compare, 4) == 0)
{
Serial.println("Card Accepted");
tft.setCursor(0, 95, 4);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.println("Key Accepted");
client.publish("/Alarmanlage/OLED/rfid", "correct");
delay(5000);
tft.setCursor(0, 95, 4);
tft.setTextColor(TFT_BLACK, TFT_BLACK);
tft.println("Key Accepted");
}
else
{
//zweite RFID-Chip
if (memcmp(uid, compare1, 4) == 0)
{
Serial.println("Card Accepted");
tft.setCursor(0, 95, 4);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.println("Key Accepted");
client.publish("/Alarmanlage/OLED/rfid", "correct");
delay(5000);
tft.setCursor(0, 95, 4);
tft.setTextColor(TFT_BLACK, TFT_BLACK);
tft.println("Key Accepted");
}
else
{
Serial.println("Access denied");
tft.setCursor(0, 95, 4);
tft.setTextColor(TFT_BLACK, TFT_BLACK);
tft.println("Key Accepted");
tft.setCursor(15, 95, 4);
tft.setTextColor(TFT_RED, TFT_BLACK);
tft.println("Wrong Key");
client.publish("/Alarmanlage/OLED/rfid", "wrong");
delay(5000);
tft.setCursor(15, 95, 4);
tft.setTextColor(TFT_BLACK, TFT_BLACK);
tft.println("Wrong Key");
}
}
// Wait 1 second before continuing
delay(1000);
}
else
{
// PN532 probably timed out waiting for a card
Serial.println("Wait for RFID");
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String msg;
for (byte i = 0; i < length; i++) {
char tmp = char(payload[i]);
msg += tmp;
}
Serial.println(msg);
tft.setCursor(20, 60, 4);
tft.setTextColor(TFT_RED, TFT_BLACK);
tft.println(msg);
}
Wie gesagt es funktioniert eigentlich alles, außer das es der Status und die Uhrzeit nicht jede Sekunde erneuert wird.
Vielen Dank für Eure Hilfe