I am trying to write DHT22 temperature and humidity data to a database using Arduino Ethernet Shield and Arduino UNO as in this example: https://create.arduino.cc/projecthub/muhammad-aqib/logging-data-to-database-using-arduino-ethernet-shield-3e9a0e. My problem is that data is passed to the serial monitor, but not passed to the database.
Arduino sketch:
#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 3, 2 }; //Enter the IP of ethernet shield
byte dns[] = { 192, 168, 0, 2 };
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 248, 0 };
byte serv[] = { 192, 168, 7, 1 }; //Enter the IPv4 address
EthernetClient cliente;
void setup() {
Serial.begin(9600); //setting the baud rate at 9600
Ethernet.begin(mac, ip, dns, gateway, subnet);
dht.begin();
}
void loop() {
float hum = dht.readHumidity(); //Reading the humidity and storing in hum
float temp = dht.readTemperature(); //Reading the temperature as Celsius and storing in temp
float fah = dht.readTemperature(true); //reading the temperature in Fahrenheit
float heat_index = dht.computeHeatIndex(fah, hum); //Reading the heat index in Fahrenheit
float heat_indexC = dht.convertFtoC(heat_index); //Converting the heat index in Celsius
if (cliente.connect(serv, 80)) { //Connecting at the IP address and port we saved before
Serial.println("connected");
cliente.print("GET /arduino-test/data.php?"); //Connecting and Sending values to database
cliente.print("temperature=");
cliente.print(temp);
cliente.print("&humidity=");
cliente.print(hum);
cliente.print("&heat_index=");
cliente.println(heat_indexC);
//Printing the values on the serial monitor
Serial.print("GET arduino-test/data.php?");
Serial.print("temperature=");
Serial.print(temp);
Serial.print("&humidity=");
Serial.print(hum);
Serial.print("&heat_index=");
Serial.println(heat_indexC);
cliente.stop(); //Closing the connection
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
delay(5000);
}
data.php
<?php
include ('connection.php');
$sql_insert = "INSERT INTO arduino_test (temperature, humidity, heat_index) VALUES ('".$_GET['temperature']."', '".$_GET['humidity']."', '".$_GET['heat_index']."')";
if(mysqli_query($con,$sql_insert))
{
echo "Done";
mysqli_close($con);
}
else
{
echo "error is ".mysqli_error($con );
}
?>
What's the problem? Thanks in advance.


