I'm trying to send arduino data to my online web server using a Arduino Uno and a Ethernet shield. I used POST method for sending data and tried sending data to my sever (add.php) using a online POST request generating site (https://www.codepunker.com/tools/http-requests) . That works perfect and it updated my database with sent data. But my database doesn't get any data when I try to send data with Arduino and Ethernet shield. Any suggestions to make this work? What changes should I do to my arduino code?
Serial Out
Initialize Ethernet with DHCP:
DHCP assigned IP 192.168.1.102
connected to 145.14.145.94
still working here
temp1=10&hum1=24
Successfull
Client Stopped
Arduino Code
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x00, 0xAB, 0xBA, 0xBC, 0xDD, 0x02 };
EthernetClient client;
String data;
int t;
int h;
IPAddress ip(192, 168, 1, 177);
IPAddress myDns(192, 168, 1, 1);
char server[] = "lattalottaroom2.000webhostapp.com";
void setup() {
Serial.begin(9600);
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 congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.print("connected to ");
Serial.println(client.remoteIP());
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
Serial.println("still working here");
}
void loop() {
t = 10;
h = 24;
data = "temp1=";
data.concat(t);
data.concat("&hum1=");
data.concat(h);
if (client.connect("lattalottaroom2.000webhostapp.com",80)) {
Serial.println(data);
client.print("POST /add.php HTTP/1.1");
client.print("Host: lattalottaroom2.000webhostapp.com");
client.print("Connection: close");
client.print("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
Serial.println("Successfull");
}
if (client.connected()) {
client.stop();
Serial.println("Client Stopped");
}
delay(20000);
}
connect.php
<?php
function Connection(){
$server="localhost";
$user="id10666900_arduinodata";
$pass="thunder";
$db="id10666900_arduinodata";
$connection = mysqli_connect($server,$user,$pass,$db);
if (!$connection) {
die('MySQL ERROR: ' . mysqli_connect_error());
}
return $connection;
}
?>
add.php
<?php
include("connect.php");
$link=Connection();
$query = "INSERT INTO `tempLog` (temp, hum) VALUES ('".$_POST["temp1"]."','"$_POST["hum1"]."')";
if (mysqli_query($link, $query)) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "" . mysqli_error($link);
}
$link->close();
header("Location: index.php");
?>
https://lattalottaroom2.000webhostapp.com/add.php?temp1=40&hum1=50
it echoINSERT INTO tempLog (temp, hum) VALUES ('40', '50')
and updates the DB. However after I changed the arduino code as follows, I was able to upload data to local database with add.php using my arduino and ethernet shield.client.print("GET /arduino/add.php?"); client.print("temp1="); client.print(t); client.print("&hum1="); client.println(h);
– Chathuranga Lakmal Aug 30 '19 at 16:03