So as you can se below in the attached sketch, I am trying to POST some data to a php file, which then inserts the data into a MySQL Database. I used mysql connector for that, but that was too unstable for long term usage. I tried the POST with another app and it works, the php file does exactly what it should do. However, I cannot get it to accept data from Arduino. It always returns no data. Any help would be highly appreciated.
The code below is only part of the final script, but I broke it down to the essentials. In the original script, the query is a char array and not a String, for memory reasons. I only changed it to a String, to eventually filter out any problems there, but that did not help.
#include <SPI.h>
#include <WiFiNINA.h>
// EDIT: Change the 'ssid' and 'password' to match your network
char ssid[] = "seid"; // wireless network name
char password[] = "pass"; // wireless password
int status = WL_IDLE_STATUS;
WiFiClient client;
String query = "temp=2300&humid=5000&pressure=95000&windd=31500&winds=300&windg=400&rainr=0&rainh=0";
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// this check is only needed on the Leonardo:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, password);
// wait 10 seconds for connection:
delay(10000);
}
//pinMode(7, OUTPUT);
if (client.connect("192.168.3.228",80))
{
// REPLACE WITH YOUR SERVER ADDRESS
Serial.println("connected");
client.println("POST /insertmysql.php HTTP/1.1");
client.println("Host: 192.168.3.228");
client.println("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
client.println();
client.print(query);
}
delay(2000);
Serial.println("Response: ");
}
void loop()
{
while (client.connected() || client.available())
{
Serial.write(client.read());
}
}
PHP File looks as follows:
<?php
$temp=$_POST["temp"];
$humid=$_POST["humid"];
$pressure=$_POST["pressure"];
$rainr=$_POST["rainr"];
$rainc=$_POST["rainh"];
$windd=$_POST["windd"];
$windg=$_POST["windg"];
$winds=$_POST["winds"];
echo('at least something');
echo json_encode($_POST);
// EDIT: Your mysql database; account information
$servername = "192.168.3.228";
$username = "name";
$password = "pass";
$dbname = "Wetter";
$test = 100;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO alldata (T, Tmax, Tmin, H, P, W, G, R, RR, D, A, B, S) VALUES ($temp, $temp, $temp, $humid, $pressure, $winds, $windg, $rainc, $rainr, $test, $test, $windd, $test)";
echo($sql);
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "
" . mysqli_error($conn);
}
mysqli_close($conn);
?>
I tried to echo the _POST to understand what is received but that always gives me nonsense too.
Any help, as said before, highly appreciated.