Guten Tag, folgendes Problem ist bei mir gestern aufgetreten:
Ich habe versucht, einen RFID-Kartenleser (MFRC522) mit einem Arduino Uno zu verbinden. Nun soll dieser mittels Ethernet Shield (EthernetShield 2) die Daten vom RFID-Chip an eine MySQL Datenbank senden. Hierfür habe ich über "XAMPP" ein .php Skript geschrieben. Mittels URL ließen sich auch problemlos Daten in die MySQL Tabelle einzutragen. Leider ist aus mir unerklärlichen Gründen keine Verbindung des Arduinos zum Webserver möglich. Anbei sende ich noch die PHP Datei + den Arduino Code mit. Über schnelle Hilfe wäre ich sehr verbunden.
Liebe Grüße
Hier einmal der Arduino Code:
#include <SPI.h>
#include <Ethernet.h>
#include <SoftwareSerial.h>
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x94, 0xC9 }; //Setting MAC Address
EthernetClient client;
SoftwareSerial RFID(9, 8); // RX, TX
void setup() {
Serial.begin(9600);
RFID.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, IPAddress(127, 0, 0, 1));
}
delay(1000);
}
void loop() {
if (RFID.available() > 0) {
String tagData = "";
while (RFID.available() > 0) {
tagData += char(RFID.read());
}
tagData.trim();
if (client.connect("127.0.0.1", 3306)) {
Serial.println("connected");
client.print("POST /datenbank_projekt/rfid.php?tag=");
client.print(tagData);
client.print(" HTTP/1.1\r\n");
client.print("Host: 127.0.0.1\r\n"); //!!!!!!!!!!!!!!!!!!!!!!
client.print("Connection: close\r\n");
client.print("\r\n");
} else {
Serial.println("connection failed");
}
while (client.connected()) {
if (client.available()) {
String response = client.readStringUntil('\n');
Serial.println(response);
}
}
client.stop();
delay(500);
}
}
und hier einmal der PHP Code:
<?php
// Connect to MySQL database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "rfid";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get the tag data from the query parameter
$tagData = $_GET["tag"];
// Insert the tag data into the database
$sql = "INSERT INTO rfid_data (tag) VALUES ('$tagData')";
if ($conn->query($sql) === TRUE) {
echo "Tag data inserted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close the database connection
$conn->close();
?>
Im englischen Teil des Forum müssen die Beiträge und Diskussionen in englischer Sprache verfasst werden. Deswegen wurde diese Diskussion in den deutschen Teil des Forums verschoben.
mfg ein Moderator.
Als erstes und ganz wichtig: WELCHEN?
Entschuldige, Einen Arduino Uno und das Shield ist das EthernetShield 2
1 Like
OK - damit ist zumindest klar, das es kein MEGA ist
...
Bekommst Du eine IP zugewiesen?
Mit dem lokalen loopback
if (client.connect("127.0.0.1", 3306))
wird das nämlich nichts.
PS: Du willst mit dem Port 3306 direkt die DB ansprechen. Das dürfte ebenfalls schief gehen. Wenn Dein PHP-Skript auf dem XAMPP läuft, ist die Adresse im Normalfall 80.
ja, alternativ habe ich den Xampp Server über die 192.168.1.176 erreicht. Habe auch versucht den Code diesbezüglich anzupassen. Hat allerdings nichts gebracht
Zustätzlich dazu spuckt der PHP Code auch folgenden fehler aus:
Warning : Undefined array key "tag" in C:\xampp\htdocs\datenbank_projekt\rfid.php on line 15
Tag data inserted successfully
Der folgende Code kompiliert.
#include <SPI.h>
#include <Ethernet.h>
#include <SoftwareSerial.h>
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x94, 0xC9 }; //Setting MAC Address
EthernetClient client;
SoftwareSerial RFID(9, 8); // RX, TX
void setup()
{
Serial.begin(9600);
RFID.begin(9600);
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, IPAddress(192,168,1,201));
}
delay(1000);
}
void loop()
{
if (RFID.available() > 0)
{
String tagData = "";
while (RFID.available() > 0)
{
tagData += char(RFID.read());
}
tagData.trim();
if (client.connect("192.168.1.176", 80))
{
Serial.println("connected");
client.print("GET /datenbank_projekt/rfid.php?tag=");
client.print(tagData);
client.print("\r\n");
}
else
{
Serial.println("connection failed");
}
while (client.connected())
{
if (client.available())
{
String response = client.readStringUntil('\n');
Serial.println(response);
}
}
client.stop();
delay(500);
}
}
Ob der Eintrag funktioniert, kann ich Dir nciht sagen, da ich mit PHP() nix am Hut habe. Ich schreibe direkt in die DB's ohne Serverskripte. Auch, weil ich keine Webserver habe..
Aber einen Versuch ist es wert.
PS: Ich mach das via get() - fürs Post müsste das vermutlich umfanmgreicher sein.
nach langem warten hat die Serial Console folgendes ausgespuckt: 11:19:51.231 -> Failed to configure Ethernet using DHCP
11:19:51.231 -> Failed to configure Ethernet using DHCP
Dann hast Du keine Adresse zugewiesen bekommen.
Für den Fall habe ich Dir die 192.168.1.201 vergeben.
Der xampp müsste zumindest im log einen Connect vond er IP haben.
Wenn nciht, dann klemmts vorher schon.
habe jetzt nachgeschaut und finde nichts in den logs, weder xampp unter apache noch unter mysql
Ok.
Dann musst Du erstmal sicherstellen, das Du einen Connect bekommst.
Dazu muss das shield fehlerfrei funktionieren.
Nimm das Beispiel WebClient aus der IDE und passe die IP-Adressen an.
Frage nicht den server google ab, sondern eine Zeile drüber ist das Muster, wie Du die IP abfragst. Da trägst Du die IP des XAMPP ein.
Erst wenn Du da vernünftig einen Connect bekommst, geht es weiter.
Die Verbindung funktioniert nicht, ich kann mir ehrlich gesagt nicht erklären warum hier einmal der Code abschnitt:
/*
Web client
This sketch connects to a website (http://www.google.com)
using an Arduino WIZnet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(192,168,1,176); // numeric IP for Google (no DNS)
//char server[] = "192.168.1.176"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 177);
//IPAddress myDns(192, 168, 1, 1);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; // set to false for better speed measurement
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH Shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to configure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.print("connecting to ");
Serial.print(server);
Serial.println("...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.print("connected to ");
Serial.println(client.remoteIP());
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
beginMicros = micros();
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
int len = client.available();
if (len > 0) {
byte buffer[80];
if (len > 80) len = 80;
client.read(buffer, len);
if (printWebData) {
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
}
byteCount = byteCount + len;
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
endMicros = micros();
Serial.println();
Serial.println("disconnecting.");
client.stop();
Serial.print("Received ");
Serial.print(byteCount);
Serial.print(" bytes in ");
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
Serial.print(seconds, 4);
float rate = (float)byteCount / seconds / 1000.0;
Serial.print(", rate = ");
Serial.print(rate);
Serial.print(" kbytes/second");
Serial.println();
// do nothing forevermore:
while (true) {
delay(1);
}
}
}
Ethernet.init(10);?
In Router (welcher?) unbekannte MAC frei geschaltet?
Gruß Tommy
Ist ein Lokaler Server, hat nix mitm router zu tun. Mit Ethernet.init(10); funkt auch nicht
Stell erst mal sicher dass der XAMPP im lokalen Netz erreichbar ist.
Standard-mäßig sollte ein Zugriff <> localhost nicht möglich sein.
Guter test wäre ein anderer PC oder auch ein Smartphone.
Bitte bestätige dass der Zugriff auf deinen XAMPP Server VON EINEM ANDEREN GERÄT in deinem Netz möglich ist.
Dann geht es weiter:
das macht imho keinen Sinn:
client.print("POST /datenbank_projekt/rfid.php?tag=");
client.print(tagData);
client.print(" HTTP/1.1\r\n");
client.print("Host: 127.0.0.1\r\n"); //!!!!!!!!!!!!!!!!!!!!!!
client.print("Connection: close\r\n");
client.print("\r\n");
wenn du daten mit POST übertragen möchtest dann musst du die Daten im Body übertragen.
in deinem PHP fragst du aber explizit auf GET Parameter ab.
stell mal deinen Arduino Code auf GET um.
Und wer macht in Deinem Netz das DHCP? Der Osterhase?
Gruß Tommy
hello, ip adresse wird ja im code weiter unten fest gelegt
habe den code bereits mit get und mit post versucht. Ich war so verzweifelt das ich sogar chat gpt gefragt habe allerdings hatte das auch keine antwort
Nochmal:
Du wirst keinen Schritt weiter kommen, bevor nicht Dein Netzwerksetup AUF DEM ARDUINO funktioniert!
Dazu aus den Beispielen das Webclient-Example nehmen, die IPAdressen anpassen und loslegen.
Wenn das nicht geht, musst Du solange suchen, bis es geht.
Dann, und nur dann, kannst Du Dich daran machen eine Verbindung mit der DB zu bewerkstelligen.
(Den Hinweis mit INIT hast Du anscheinend ignoriert - bitte nochmal lesen und machen)