Hi everyone, I am new to this Forum and have a question:
I am developing a log-in System based on a RPi Server (ubuntu, Apache) and several satellites (NodeMCU, RFID Readers). usually i can resolve problems on my own, but in this case i cant hence the question. I have two issues:
- Whenever I scan the RFID-Card the variable card_ID gets concatenated for some reason and I cant find it.
Here is the code for the Readers:
//**************************************************************************//
#include <Arduino.h>
#include <MFRC522.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <NTPClient.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>
#include <SPI.h>
#define CS_RFID D8
#define RST_RFID D1
MFRC522 rfid(CS_RFID, RST_RFID);
const char* ssid_wo = "XXX";
const char* ssid_ho = "XXX";
const char* password_wo = "XXX";
const char* password_ho = "XXX";
const char* post_data_wo = "XXX";
const char* post_data_ho = "XXX";
String apiKey = "tPmAT5Ab3j7F9";
String newHostname = "PROD_SAT";
String card_ID="";
String A = "4424779204";
String B = "3418723199";
String C = "502517468";
String D = "2102082462";
String E = "1621322442";
int NumbCard[6];
int j=0;
int statu[5];
int s=0;
int const RedLed=7;
int const GreenLed=6;
int const Buzzer=8;
String Log;
String Name;
long Number;
int n ;
int ID=1;
const long utcOffsetInSeconds = 3600;
char daysOfTheWeek[7][12] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", utcOffsetInSeconds);
String timeStamp;
String INOUT=" ";
WiFiClient client;
//**************************************************************************//
void setup() {
Serial.begin(74880);
WiFi.hostname(newHostname.c_str());
WiFi.mode(WIFI_STA);
WiFi.begin(ssid_ho, password_ho);
SPI.begin();
rfid.PCD_Init(CS_RFID, RST_RFID);
//**************************************************************************//
delay(1000);
Serial.print(F("Verbinde...."));
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println(F(" "));
Serial.print(F("Verbunden mit Netzwerk: "));
Serial.println(ssid_ho);
Serial.print(F("Verbunden mit IP: "));
Serial.println(WiFi.localIP());
Serial.println(F("Netzwerkstatus [OK]"));
Serial.println(F(" "));
Serial.println(F("Hole Netzwerkzeit...."));
delay(2000);
timeClient.begin();
timeClient.update();
Serial.print(daysOfTheWeek[timeClient.getDay()]);
Serial.print(F(", "));
Serial.print(timeClient.getFormattedTime());
Serial.println(F("Status Netzwerkzeit [OK]"));
//**************************************************************************//
Serial.println(F(" "));
rfid.PCD_DumpVersionToSerial();
bool result = rfid.PCD_PerformSelfTest();
if (result) {
Serial.println(F("RFID-Status [OK]"));}
else {
Serial.println(F("RFID-Status [nOK]"));
}
}
//**************************************************************************//
void loop() {
if (! rfid.PICC_IsNewCardPresent()) {
return;
}
if (! rfid.PICC_ReadCardSerial()) {
return;
}
for (byte i = 0; i < rfid.uid.size; i++) {
card_ID += rfid.uid.uidByte[i];
}
if(card_ID==A){
Name="a";
Number = 11111;
j=0;
s=0;
}
else if(card_ID==B){
Name="B";
Number= 11112;
j=1;
s=1;
}
else if(card_ID==C){
Name="C";
Number=11113;
j=2;
s=2;
}
else if(card_ID==D){
Name="D";
Number=11115;
j=4;
s=4;
}
else if(card_ID==E){
Name="E";
Number=11116;
j=5;
s=5;
}
if(INOUT == " ") {
INOUT = "IN";
}
else if(INOUT == "IN") {
INOUT = "OUT";
}
else if(INOUT == "OUT") {
INOUT = "IN";
}
//**************************************************************************//
if(NumbCard[j] == 1 && statu[s] == 0){
statu[s]=1;
NumbCard[j] = 0;
Serial.print(ID);
Serial.print(F(","));
Serial.print(Name);
Serial.print(F(","));
Serial.print(Number);
Serial.print(F(","));
Serial.print(card_ID);
Serial.print(F(","));
Serial.print(newHostname);
Serial.print(F(","));
Serial.print(INOUT);
timeClient.update();
Serial.print(F(","));
Serial.print(timeStamp);
ID=ID+1;
}
else if(NumbCard[j] == 0){
NumbCard[j] = 1;
statu[s] = 0;
n++;
Serial.print(ID);
Serial.print(F(","));
Serial.print(Name);
Serial.print(F(","));
Serial.print(Number);
Serial.print(F(","));
Serial.print(card_ID);
Serial.print(F(","));
Serial.print(newHostname);
Serial.print(F(","));
Serial.print(INOUT);
timeClient.update();
Serial.print(F(","));
Serial.print(timeStamp);
ID=ID+1;
}
else if(statu[s] == 1){}
delay(1000);
//**************************************************************************//
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
http.begin(client, post_data_ho);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "api_key=" + apiKey + "&ID_USER=" + ID
+ "&Name=" + Name + "&card_ID=" + String(card_ID)
+ "&newHostname=" + newHostname + "&INOUT=" + INOUT + "timeStamp" + String(timeStamp);
Serial.println("httpRequestData: ");
Serial.println(httpRequestData);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Status HTML: ");
Serial.println(httpResponseCode);
}
http.end();
}
else {
Serial.println("WiFi nicht verbunden!");
}
}
Here is the Serial output from the reader:
Verbinde.........
Verbunden mit Netzwerk: XXX
Verbunden mit IP: XXX
Netzwerkstatus [OK]
Hole Netzwerkzeit....
Dienstag, 10:04:51Status Netzwerkzeit [OK]
Firmware Version: 0x92 = v2.0
RFID-Status [OK]
1,XXX,11113,**502517468**,PROD_SAT,IN,httpRequestData:
api_key=tPmAT5Ab3j7F9&ID_USER=2&Name=XXX&card_ID=**502517468**&newHostname=PROD_SAT&INOUT=INtimeStamp
HTTP Response code: 200
2,XXX,11113,**502517468502517468**,PROD_SAT,OUT,httpRequestData:
api_key=tPmAT5Ab3j7F9&ID_USER=3&Name=XXX&card_ID=**502517468502517468**&newHostname=PROD_SAT&INOUT=OUTtimeStamp
HTTP Response code: 200
I have changed all sensitive data to "XXX" obviously, they are Server IPs and Names. The code compiles, uploads and works on the reader except this issue. Wiring is also okay.
- When I swipe the card the Serial Monitor gives me the http request 200 but the PHP script doesnt post to the database.
Here is the PHP script:
<?php
$servername = "localhost";
$username = "produser";
$password = 'XXX';
$dbname = "db_prodbox";
$dbtable = "tbl_prodbox_users";
$apiKey = "tPmAT5Ab3j7F9";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
//if ($conn->connect_error) {
// die("Connection failed: " . $conn->connect_error);
//}
// Check connection
if ($conn->connect_error) {
die("MySQL Status [nOK]: " . $conn->connect_error);
}
if ($_POST["api_key"]){
echo "<h3>Neue Daten</h3>";
echo 'ID: ' .htmlspecialchars($_POST["api_key"]).' Name: '.htmlspecialchars($_POST["Name"]).'</br>';
$sql = "INSERT INTO `".$dbtable."` (ID_USER,Name,Number,card_ID,newHostname,INOUT,timeStamp) VALUES ('".$_POST["ID"]."','".$_POST["Name"]."','".$_POST["Number"]."','".$_POST["card_ID"]."','".$_POST["newHostname"]."','".$_POST["INOUT"]."','".$_POST["timeStamp"]."')";
if ($conn->query($sql) === TRUE) {
echo "Neue Daten erfolgreich eingetragen!";
} else {
echo "MySQL Status [nOK]: " . $sql . "<br>" . $conn->error;
}
echo "0 Ergebnisse";
}
Here is the apache error log:
[Tue Mar 22 08:39:40.338270 2022] [core:notice] [pid 3033] AH00094: Command line: '/usr/sbin/apache2'
[Tue Mar 22 08:39:43.255149 2022] [php7:notice] [pid 3034] [client 192.168.178.34:51241] PHP Notice: Undefined index: api_key in /var/www/html/post-card-data.php on line 25
[Tue Mar 22 08:39:51.155865 2022] [php7:notice] [pid 3035] [client 192.168.178.45:63907] PHP Notice: Undefined index: ID in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 08:39:51.156113 2022] [php7:notice] [pid 3035] [client 192.168.178.45:63907] PHP Notice: Undefined index: Number in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 08:39:51.156184 2022] [php7:notice] [pid 3035] [client 192.168.178.45:63907] PHP Notice: Undefined index: timeStamp in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 08:41:02.053956 2022] [php7:notice] [pid 3037] [client 192.168.178.34:51255] PHP Notice: Undefined index: api_key in /var/www/html/post-card-data.php on line 25
[Tue Mar 22 09:04:58.707651 2022] [php7:notice] [pid 3038] [client 192.168.178.45:63165] PHP Notice: Undefined index: ID in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:04:58.707874 2022] [php7:notice] [pid 3038] [client 192.168.178.45:63165] PHP Notice: Undefined index: Number in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:04:58.707954 2022] [php7:notice] [pid 3038] [client 192.168.178.45:63165] PHP Notice: Undefined index: timeStamp in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:05:04.542488 2022] [php7:notice] [pid 3039] [client 192.168.178.45:50266] PHP Notice: Undefined index: ID in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:05:04.542714 2022] [php7:notice] [pid 3039] [client 192.168.178.45:50266] PHP Notice: Undefined index: Number in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:05:04.542771 2022] [php7:notice] [pid 3039] [client 192.168.178.45:50266] PHP Notice: Undefined index: timeStamp in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:24:04.628990 2022] [php7:notice] [pid 3034] [client 192.168.178.34:65403] PHP Notice: Undefined index: api_key in /var/www/html/post-card-data.php on line 25
[Tue Mar 22 09:34:15.734121 2022] [php7:notice] [pid 3037] [client 192.168.178.45:56460] PHP Notice: Undefined index: ID in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:34:15.734284 2022] [php7:notice] [pid 3037] [client 192.168.178.45:56460] PHP Notice: Undefined index: Number in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:34:31.221446 2022] [php7:notice] [pid 3038] [client 192.168.178.34:55961] PHP Notice: Undefined index: api_key in /var/www/html/post-card-data.php on line 25
[Tue Mar 22 09:34:40.817309 2022] [php7:notice] [pid 3039] [client 192.168.178.45:61443] PHP Notice: Undefined index: ID in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:34:40.817472 2022] [php7:notice] [pid 3039] [client 192.168.178.45:61443] PHP Notice: Undefined index: Number in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:36:09.224832 2022] [php7:notice] [pid 3034] [client 192.168.178.45:54582] PHP Notice: Undefined index: ID in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:36:09.225049 2022] [php7:notice] [pid 3034] [client 192.168.178.45:54582] PHP Notice: Undefined index: Number in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:36:21.192906 2022] [php7:notice] [pid 3035] [client 192.168.178.45:62371] PHP Notice: Undefined index: ID in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:36:21.193108 2022] [php7:notice] [pid 3035] [client 192.168.178.45:62371] PHP Notice: Undefined index: Number in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:37:50.379071 2022] [php7:notice] [pid 3036] [client 192.168.178.45:64760] PHP Notice: Undefined index: ID in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:37:50.379259 2022] [php7:notice] [pid 3036] [client 192.168.178.45:64760] PHP Notice: Undefined index: Number in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:37:50.379320 2022] [php7:notice] [pid 3036] [client 192.168.178.45:64760] PHP Notice: Undefined index: timeStamp in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:37:57.166172 2022] [php7:notice] [pid 3037] [client 192.168.178.45:60964] PHP Notice: Undefined index: ID in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:37:57.166355 2022] [php7:notice] [pid 3037] [client 192.168.178.45:60964] PHP Notice: Undefined index: Number in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:37:57.166454 2022] [php7:notice] [pid 3037] [client 192.168.178.45:60964] PHP Notice: Undefined index: timeStamp in /var/www/html/post-card-data.php on line 28
[Tue Mar 22 09:38:04.124064 2022] [php7:notice] [pid 3038] [client 192.168.178.34:56000] PHP Notice: Undefined index: api_key in /var/www/html/post-card-data.php on line 25
The folder /var/www/html belongs to the user www-data and the folder is set to 755, the file to 644. Still it doesnt work as expected. Maybe one of you guys is more knowledgeable than me and can help out.
Anyway, thank you very much in advance! Keep going!