I have got this quick sketch that post data to mysql db using php add.php module.
#include <SFE_BMP180.h>
#include <SPI.h>
#include <Ethernet.h>
#include<dht.h>
dht DHT;
// You will need to create an SFE_BMP180 object, here called "pressure":
SFE_BMP180 bmp;
#define DHT11_PIN 2
double tempInC;
int humidity;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
char server[] = "192.168.1.3";
EthernetClient client;
byte php_server[] = {192 , 168 , 1 , 3};
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL
String data;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
if (bmp.begin()) {
Serial.println("BMP init Success");
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
data = "";
}
void loop() {
int chk = DHT.read11(DHT11_PIN);
char status;
status = bmp.startTemperature();
if (status != 0) {
delay(status);
status = bmp.getTemperature(tempInC);
}
else Serial.println("error retrieving temperature measurement\n");
humidity = DHT.humidity;
data = "?temp1=" + String(tempInC) + "&hum1=" + String(humidity);
if (client.connect(server, 80)) {
Serial.print("connected. My IP is ");
Serial.println(Ethernet.localIP());
client.println("POST /arduino/add.php HTTP/1.1");
client.println("Host: 192.168.1.3");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
}
else {
Serial.println("connection failed");
}
Serial.println(data);
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}
delay(300000); // WAIT FOR A WHILE BEFORE SENDING AGAIN
}
but its not updating the database, using the below add.php file from arduino web-client.ino
<?php
include("connect.php");
$link=Connection();
$temp1=$_POST["temp1"];
$hum1=$_POST["hum1"];
$query = "INSERT INTO `tempLog` (`temperature`, `humidity`)
VALUES ('".$temp1."','".$hum1."')";
mysql_query($query,$link);
mysql_close($link);
header("Location: index.php");
?>
i checked in mysql prompt which is also the result displayed if I open the server url : 192.168.1.3/arduino/index.php
mysql> select * from tempLog;
+---------------------+-------------+----------+
| timeStamp | temperature | humidity |
+---------------------+-------------+----------+
| 2016-11-28 11:50:37 | 0 | 0 |
| 2016-11-28 12:30:25 | 0 | 0 |
+---------------------+-------------+----------+
2 rows in set (0.00 sec)
I don't know how the first entry got created but second one I created by manually executing the url 192.168.1.3/arduino/add.php?temp1=26.64&hum1=18
the index page is showing he data from db as shown above in sql, that means connection is established and mysql and table is fetched from correct db. i.e. arduinoSensorData in my case.
my serial monitor window got updated:
BMP init Success
connecting...
connected. My IP is 192.168.1.131
?temp1=26.64&hum1=18
connected. My IP is 192.168.1.131
?temp1=26.64&hum1=18
connected. My IP is 192.168.1.131
?temp1=26.57&hum1=18
which means arduino is not able to put data to db using add.php
?
. Also you can't get GET parameters (from URL), from the$_POST
variable.