Tommy,
hab den code mal überarbeitet und das was überflüssig war entfernt.
Ich poste den hier mal, vielleicht kannst Du doch mal drüber schauen, ich weiß echt nicht mehr weiter.
Wenn Dich wunderst, dass ich den ESP erneut starte liegt daran, dass das Loop nur einmal funktioniort, eine weitere Karte will er nicht einlesen.
Code:
#include <ESP8266WiFi.h> //Include Esp library
#include <WiFiClient.h>
#include <SPI.h>
#include <MFRC522.h> //include RFID library
#include <LiquidCrystal_I2C.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <NTPtimeESP.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
//UDP
IPAddress remoteIP(192,168,9,101);
unsigned int remotePort = 4210; // remote port to listen on
byte befehl[5];
WiFiUDP Udp;
//UDP
//TIME
const long utcOffsetInSeconds = 3600;
char daysOfTheWeek[7][12] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
//ENDE TIME
//#define DEBUG_ON
#define SS_PIN D8 //RX slave select
#define RST_PIN D3
NTPtime NTPch("de.pool.ntp.org"); // Choose server pool as required
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
LiquidCrystal_I2C lcd(0x27, 20, 4);
/* Set these to your desired credentials. */
const char *ssid = "SSID"; //ENTER YOUR WIFI SETTINGS
const char *password = "PWD";
//Web/Server address to read/write from
const char *host = "192.168.9.214"; //IP address of server
String CardID="0";
String Uname="1";
String UBerechtigung="";
String UDatumv="";
String Ergebnis="";
String Udatumv="";
String UDatumB="";
int UHET=0;
int UDGT=0;
int UEGT=0;
int Ur1=0;
int Ur2=0;
int Ur3=0;
int Ur4=0;
int Ur5=0;
int Ur6=0;
WiFiClient client;
MySQL_Connection conn((Client *)&client);
char SELECT[] = "SELECT * FROM RFID.User WHERE `UID` = '%d'";
char SELECT_VON[] = "SELECT * FROM RFID.User WHERE `UID` = '%d'";
char INSERT_SQL[] = "INSERT RFID.log_anfrage(UID, Door, Datum) VALUES ('%d', 'HET', now())";
char INSERT_SQL1[] = "INSERT RFID.LOG(UID, Door, Name, Ergebnis, wann) VALUES ('%d', 'HET', '%s', '%s', now())";
char query[128];
char query1[128];
char query2[128];
IPAddress server_addr(192,168,9,214); // MySQL server IP
char user[] = "rfid"; // MySQL user
char password1[] = "rfid"; // MySQL password
void setup() {
delay(1000);
Serial.begin(115200);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");
Serial.print("Connecting to ");
Serial.print(ssid);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Connecting to: ");
lcd.setCursor(0,1);
lcd.print(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
lcd.print(".");
}
SPI.begin();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected");
lcd.setCursor(0,2);
lcd.print("Connected to:");
lcd.setCursor(0,3);
lcd.print("IP: ");
lcd.print(WiFi.localIP());
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
Serial.println("Connecting to database");
while (conn.connect(server_addr, 3306, user, password1) != true) {
delay(200);
Serial.print ( "." );
lcd.setCursor(0,0);
lcd.print("Connecting SQL: ");
timeClient.begin();
}
Serial.println("");
Serial.println("Connected to SQL Server!");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("mit SQL-Verbunden");
}
void loop() {
timeClient.update();
row_values *row = NULL;
long head_count = 0;
if(WiFi.status() != WL_CONNECTED){
WiFi.disconnect();
WiFi.mode(WIFI_STA);
Serial.print("Reconnecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
}
if (CardID = "0") {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" WELCOME/HALLO ");
lcd.setCursor(0, 1);
lcd.print(daysOfTheWeek[timeClient.getDay()]);
lcd.setCursor(12, 1);
// lcd.print("-");
lcd.print(timeClient.getFormattedTime());
lcd.setCursor(0,2);
lcd.print(" Haustuere");
delay(1000);
}
//look for new card
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;//got to start of loop if there is no card present
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;//if read card serial(0) returns 1, the uid struct contians the ID of the read card.
}
long code = 0;
long code1 = 0;
long code2 = 0;
long code3 = 0;
long code4 = 0;
long code5 = 0;
long code6 = 0;
code = ((code + (mfrc522.uid.uidByte[0]*10000)));
code1 = ((code1 + (mfrc522.uid.uidByte[1])*10));
code2 = ((code + code1)*1000);
code3 = ((code3 + (mfrc522.uid.uidByte[2])));
code4 = ((code2 + code3));
code5 = ((code5 + (mfrc522.uid.uidByte[3])));
code6 = ((code4 + code5));
sprintf(query, SELECT, code6);
if (code6 >= 1) {
lcd.clear();
Serial.print("Karte: ");
Serial.println(code6);
lcd.setCursor(0,0);
lcd.print("Karte: ");
lcd.print(code6);
lcd.setCursor(0,1);
lcd.print("Bitte warten");
lcd.setCursor(0,2);
lcd.print("Datenpruefung!!!");
sprintf(query1, INSERT_SQL, code6);
int soil_hum = 1024;
delay(1000);
Serial.println("Recording data.");
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
cur_mem->execute(query1);
cur_mem->execute(query);
column_names *columns = cur_mem->get_columns();
do {
Serial.println("START SCHLEIFE!-");
row = cur_mem->get_next_row();
if (row != NULL) {
Serial.println("IN DER SCHLEIFE");
Serial.print("lfd_nr: ");
Serial.println(row->values[0]); // <--- der Name als Zeichenkette
Serial.print("UID: ");
Serial.println(row->values[1]); // <--- der Name als Zeichenkette
Serial.print("Name: ");
Serial.println(row->values[2]); // <--- der Name als Zeichenkette
Uname=(row->values[2]);
UBerechtigung=(row->values[3]);
// UDatumv=strstr(row->values[4]);
// UDatumb=(row->values[5]);
UHET=atof((row->values[6]));
UDGT=atof((row->values[7]));
UEGT=atof((row->values[8]));
Ur1=atof((row->values[9]));
Ur2=atof((row->values[10]));
Ur3=atof((row->values[11]));
Ur4=atof((row->values[12]));
Ur5=atof((row->values[13]));
// Ur6=atof((row->values14]));
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Karte gefunden! ");
lcd.setCursor(0,1);
lcd.print("Hallo ");
lcd.print(Uname);
lcd.setCursor(0,2);
lcd.print("Bitte warten.......");
lcd.setCursor(0,3);
lcd.print("Datenpruefung!!!");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Zutritt ab ");
lcd.print((row->values[4]));
lcd.setCursor(0,1);
lcd.print("bis ");
lcd.print((row->values[5]));
lcd.setCursor(0,2);
lcd.print("Haustuere: ");
{
if (UHET = 0)
lcd.print("NEIN");
else
lcd.print("JA");
}
lcd.setCursor(0,3);
lcd.print("OG: ");
if (UDGT >= 1)
{
lcd.print("JA ");
}
else {
lcd.print("NEIN ");
}
lcd.print("EG: ");
if (UEGT >= 1)
{
lcd.print("JA ");
}
else {
lcd.print("NEIN ");
}
delay(2000);
if (UHET =1)
{
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
Ergebnis="Karte gefunden - EGT geoeffnet";
sprintf(query2, INSERT_SQL1, code6, Uname.c_str(), Ergebnis.c_str());
// Serial.println(query2);
//UDP
byte befehl=1;
Serial.print("Inhalt befehl: "); Serial.println(befehl);
Udp.beginPacket(remoteIP, remotePort);
Udp.write(befehl);
Udp.endPacket();
//UDP
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Tuere wird geoeffnet!");
lcd.setCursor(0,2);
lcd.print("Bitte eintreten!");
cur_mem->execute(query2);
column_names *columns = cur_mem->get_columns();
cur_mem->close();
delete cur_mem;
delay(500);
CardID="0";
code6=0;
ESP.restart();
}
else
{
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
Ergebnis="Karte gefunden - Zutritt verweigert!";
sprintf(query2, INSERT_SQL1, code6, Uname.c_str(), Ergebnis.c_str());
// Serial.println(query2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Zutritt verweigert");
lcd.setCursor(0,2);
lcd.print("nicht autorisiert!");
cur_mem->execute(query2);
column_names *columns = cur_mem->get_columns();
cur_mem->close();
delete cur_mem;
delay(2000);
CardID="0";
code6=0;
return;
}
}
}
while (row != NULL);
if (code6 < 0) {
lcd.setCursor(0,1);
lcd.print("Karte nicht gefunden");
lcd.setCursor(0,2);
lcd.print("Zutritt verweigert!");
delay(3000);
}
CardID="0";
code6=0;
return;
}
}
Hier ist der Code vom D1, der gesteuert werden sollte:
Code:
// NodeMCU mit Relay an D1
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define relay1 D1 //Haustüre
#define relay2 D5 //Wohnung1
#define relay3 D6 //Wohnung2
WiFiUDP Udp;
unsigned int localUdpPort = 4210; // local port to listen on
byte befehl[5]; // buffer for incoming packets
int r1Status[5];
// <= 31 Zeichen
char *ssid = "ABK14-24";
// >= 8 oder <= 63 Zeichen oder NULL
char *password = "abernauer271269#%";
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
Serial.println("Start");
pinMode(relay1, OUTPUT);
digitalWrite(relay1, LOW);
pinMode(relay2, OUTPUT);
digitalWrite(relay2, LOW);
pinMode(relay3, OUTPUT);
digitalWrite(relay3, LOW);
Serial.print("Connecting to "); Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Udp.begin(localUdpPort);
Serial.print("\n connected UDP-Server bereit an IP: ");
Serial.print(WiFi.localIP());
Serial.print("UDP-Server bereit.");
}
void loop() {
int packetSize, len;
// UDP
packetSize = Udp.parsePacket();
// Packet ist da!
if (packetSize) {
Serial.print("Packet-Size: "); Serial.println(packetSize);
Serial.print("Empfangen "); Serial.print(packetSize);
Serial.print(" von IP "); Serial.print(Udp.remoteIP());
Serial.print(" Port "); Serial.println(Udp.remotePort());
len = Udp.read(befehl,sizeof(befehl));
Serial.print("LEN: "); Serial.println(len);
// r1Status = befehl[0];
Serial.print("Befehl-Status: "); Serial.println(befehl[0]);
// Serial.print("r1-Status: "); Serial.println(r1Status);
if (befehl[0] == 1){
digitalWrite(relay1, HIGH);
Serial.println("Relay (Haustüre) wird geschaltet");
delay(2500);
digitalWrite(relay1, LOW);
}
if (befehl[0] == 2){
digitalWrite(relay2, HIGH);
Serial.println("Relay (Wohnungstüre) wird geschaltet");
delay(2500);
digitalWrite(relay2, LOW);
}
if (befehl[0] == 3){
digitalWrite(relay3, HIGH);
Serial.println("Relay (Ferienwohnung) wird geschaltet");
delay(2500);
digitalWrite(relay3, LOW);
}
}
}
Hoffe nicht, dass der Notarzt kommen muss!
Gruß aus dem Schwarzwald!
Andy