-1

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

5
  • When you manually executed the command it didn't write the temp&hum to the DB. So you have a fundamental problem on the server side, before you even start involving an Arduino. (Are you sure its not a firewall issue?) Nov 29, 2016 at 8:07
  • I know on my router I have firewall turned off Nov 29, 2016 at 8:27
  • I would suggest, that you give IP as another Parameter to the add.php and the insert-statement. With this you could test from where the call is made. If it is also 0. Then you got either a problem with the firewall as Matt stated, or it can be a probleml with the Variables of the add.php. (I don't know php so good) Nov 29, 2016 at 10:26
  • 1
    POST data doesn't need a ?. Also you can't get GET parameters (from URL), from the $_POST variable.
    – gre_gor
    Nov 29, 2016 at 15:57
  • yes, yesterday I realized and fixed it. thanks Nov 30, 2016 at 4:11

1 Answer 1

1

I came to learn in my php code posted above, I am using $_POST variable when I should rather be using $_GET

In the PHP script, there are $_GET and $_POST variables that contain the data passed to the script, depending on the method used to to activate the script.

so using $temp1=$_GET["temp1"]; and $hum1=$_GET["hum1"]; fixes the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.