For a project I need to get data (integers) from an Arduino Uno to a MySQL Database on my laptop. I'm running this for testing purposes within XAMPP. I have setup with a tutorial a write_data.php file for inserting data.
This works fine with the command in Firefox: [pre]http://localhost/write_data.php?meldingtype=100[/pre]
write_data.php
<?php
// Prepare variables for database connection
// enter database username and password
$dbusername = "arduinouser";
$dbpassword = "P@ssw0rd"; // enter database password
// IMPORTANT: if you are using XAMPP enter "localhost"
$server = "localhost";
// Connect to your database
$dbconnect = mysqli_connect($server, $dbusername, $dbpassword, "arduino");
$dbselect = mysqli_select_db($dbconnect,"arduino");
// Prepare the SQL statement
$sql = "INSERT INTO arduino.opslaantest (meldingtype) VALUES ('".$_GET["meldingtype"]."')";
// Execute SQL statement
mysqli_query($dbconnect, $sql);
?>
Now I am running an Arduino Uno with a Wifi Shield ontop of it. With some searching online I've managed to make it connect to my network and communicate with my laptop. Now with help from the tutorial I want to have the Arduino sent a GET request so it does the same as the link in the browser except meldingtype= will be an integer of my choosing. No matter what I've tried it does not work. Firewall is turned off, I've tried many different versions of code I've found online and I'm at my wits end. If anyone could help that would be greatly appreciated. Hereby my Arduino code:
#include <Arduino.h>
#include <SPI.h>
#include <WiFi.h>
// Constants
char ssid[] = "networkname"; // your network SSID (name)
char pass[] = "thepassword"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
char server[] = "192.168.1.60";
// Initialize the client library
WiFiClient client;
IPAddress ip;
int datatoevoer = 100;
void setup() {
// initialize serial:
Serial.begin(9600);
// attempt to connect using WPA2 encryption:
Serial.println("Attempting to connect to WPA network...");
status = WiFi.begin(ssid, pass);
// if you're not connected, stop here:
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while (true);
}
// if you are connected, print out info about the connection:
else {
Serial.println("*Chuckles* I'm inside the mainframe.");
ip = WiFi.localIP();
Serial.println(ip);
}
}
void loop() {
if (client.connect(server, 80))
{
Serial.println("Connected, sending data...");
Serial.print("GET /write_data.php?"); // debug
client.print("GET /write_data.php?");
Serial.print("meldingtype="); // debug
client.print("meldingtype=");
Serial.print(datatoevoer); // debug
client.print(datatoevoer);
Serial.println(" HTTP/1.1"); // debug
client.println(" HTTP/1.1");
Serial.println("Host: 192.168.1.60"); // debug
client.println("Host: 192.168.1.60");
Serial.println("Connection: close"); // debug
client.println("Connection: close");
client.println();
client.println();
client.stop();
Serial.println("Finished sending data");
}
else
{
Serial.println("Connection has failed\n");
}
delay(50000);
}
a) What have you changed in your XAMPP settings to allow other clients than localhost ?(for usual only the local PC can access the webserver!)
b) What response to you get with the Arduino? Show the serial output
c) What log entries do you see on your XAMPP logs after Arduino tries to connect?
d) Try to connect to your XAMPP Server from another PC/smartphone - what response do you get from your XAMPP?
e) downgrade your request to:
Thank you for helping,
A) I have changed some settings (Require all granted) within Apache. My phone could not connect to 192.168.1.60/phpmyadmin and now it can. The request in browser on the phone works.
B)
Attempting to connect to WPA network...
*Chuckles* I'm inside the mainframe.
192.168.1.160
Connected, sending data...
GET /write_data.php?meldingtype=100 HTTP/1.0
Host: 192.168.1.60
Connection: close
Finished sending data
Then it repeats like normal.
C) The MySQL logs do not show anything useful. I could find some records in the Apache Access log that came from the Arduino IP address. These are a tiny bit older though than the requests I'm sending right now. There are more than these 3 in the logs, all during times I was testing.
After a lot of extra troubleshooting, googling and pulling my hair I finally got it to work. Turns out I needed a delay before the client.stop(); My code now looks like this, I hope it can help others. It's possible some other errors were in my code that I've since changed you should be able to compare.
#include <Arduino.h>
#include <SPI.h>
#include <WiFi.h>
// Constants
char ssid[] = "ssid"; // your network SSID (name)
char pass[] = "password"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
IPAddress server(192,168,1,60);
// Initialize the client library
WiFiClient client;
IPAddress ip;
int datatoevoer = 100;
void setup() {
// initialize serial:
Serial.begin(9600);
// attempt to connect using WPA2 encryption:
Serial.println("Attempting to connect to WPA network...");
status = WiFi.begin(ssid, pass);
// if you're not connected, stop here:
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while (true);
}
// if you are connected, print out info about the connection:
else {
Serial.println("*Chuckles* I'm inside the mainframe.");
ip = WiFi.localIP();
Serial.println(ip);
}
}
void loop() {
if (client.connect(server, 80))
{
Serial.println("Connected, sending data...");
Serial.print("GET /write_data.php?"); // debug
client.print("GET /write_data.php?");
Serial.print("meldingtype="); // debug
client.print("meldingtype=");
Serial.print(datatoevoer); // debug
client.print(datatoevoer);
Serial.println(" HTTP/1.1"); // debug
client.println(" HTTP/1.1");
Serial.println("Host: 192.168.1.60"); // debug
client.println("Host: 192.168.1.60");
Serial.println("Connection: close"); // debug
client.println("Connection: close");
client.println();
client.println();
delay(5000);
client.stop();
Serial.println("Finished sending data");
}
else
{
Serial.println("Connection has failed\n");
}
delay(10000);
}