Ultrasonic sensor & HTTP Post

hello,

I am new to Arduino and not very skillfull in C.
I have collect some bits over the Internet and I would like your help to complete the following code.


#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
EthernetClient client;

long duration, cm;
int trigPin = 12;    //Trig - green Jumper
int echoPin = 11;    //Echo - yellow Jumper
String data;


void setup() {
  //Serial Port begin
  Serial.begin (9600);

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP"); 
  }

  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  data = "";

}

void loop()
{

  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);


  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  cm = (duration/2) / 29.1;


  //  Serial.println(data);

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  Serial.println("Posting!");
  postdata();
  Serial.println(data);



  delay(5000);
}


void postdata(){

  data+="";
  data+="tank1=";
  data+=cm;
  data+="&tank2=";
  data+=0;



  if (client.connect("www.f2d.gr",80)) { // SERVER ADDRESS
    client.println("POST /tankmonitor/add.php HTTP/1.1"); 
    client.println("Host: www.f2d.gr"); // SERVER ADDRESS AGAIN
    client.println("Content-Type: application/x-www-form-urlencoded"); 
    client.println("User-Agent: Arduino/1.0");
    client.print("Content-Length: "); 
    client.println(data.length()); 
    Serial.println(data.length()); 
    client.println(); 
    client.print(data); 
  } 

  if (client.connected()) { 
    client.stop();	// DISCONNECT FROM THE SERVER
  }


}

What I need to do is to post the data of the ultrasonic sensor to our server. Therefore I have create a php page and a MySQL database to store data. I have test functionality of the php page and it works. It inserts data to DB properly.

Here are the contents of the add.php page:

<?php
    include("connect.php");
    
    $link=Connection();

 	$temp1=$_POST["tank1"];
 	$hum1=$_POST["tank2"];

 	$query = "INSERT INTO tankLog (`tank1`, `tank2`) VALUES ('$tank1','$tank2')"; 
    
    mysql_query($query,$link);
	mysql_close($link);

    header("Location: index.php");
?>

I know it might looks funny for some more experience programmers but any help would be really apreciated.

thank you in advance

Hello,

I know nothing about php, but you do:

$temp1=$_POST["tank1"];
$hum1=$_POST["tank2"];

And then

$query = "INSERT INTO tankLog (`tank1`, `tank2`) VALUES ('$tank1','$tank2')";

Shouldn't you do this instead? :

$temp1=$_POST["tank1"];
$hum1=$_POST["tank2"];
$query = "INSERT INTO tankLog (`tank1`, `tank2`) VALUES ('$temp1','$hum1')";

or

$tank1=$_POST["tank1"];
$tank2=$_POST["tank2"];
$query = "INSERT INTO tankLog (`tank1`, `tank2`) VALUES ('$tank1','$tank2')";

Sorry if I'm wrong :P

hello,

thank you very much for your help. However PHP code works properly. The problem is with the Arduino code.

As I said I can't make the sensor reading and POST at the same time

Anyone?

msolomos: hello,

thank you very much for your help. However PHP code works properly. The problem is with the Arduino code.

As I said I can't make the sensor reading and POST at the same time

Anyone?

int trigPin = 12;    //Trig - green Jumper
int echoPin = 11;    //Echo - yellow Jumper

Using the SPI pins IS going to interfere with ethernet communications.

And, your PHP code may work properly, but it does LOOK stupid, as guix pointed out. I was going to, too, be guix beat me to it.

$query = "INSERT INTO tankLog (`tank1`, `tank2`) VALUES ('$tank1','$tank2')";

I’m another PHP know-nothing, but why are the single quotes different, and is it important?

PauIS thank you!!

As soon as I changed the ping to 7,6 it worked!!!
I have been trying for over 3 days but I never thought that.

Now I receive data on the server and is been inserted in the database.
http://www.f2d.gr/tankmonitor/index.php

However it inserts the same data (e.g. 171 cm , 171 cm ,171 cm) no matter if the distance is different …
I think it’s the first value / measure. Obviously my loop is wrong.

The tank2 value is missing as I have only once sensor connected.

here is the code so far

#include <Ethernet.h>
#include <SPI.h>

byte mac[] = {
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
EthernetClient client;

long duration, cm;
int trigPin = 7;    //Trig - green Jumper
int echoPin = 6;    //Echo - yellow Jumper
String data;



void setup() {
  //Serial Port begin
  Serial.begin (9600);

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP"); 
  }

  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  //  data = "";

}

void loop(){

  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  cm = (duration/2) / 29.1;


  //Gather Data
  data+="";
  data+="tank1=";
  data+=cm;
  data+="&tank2=";
  data+=0;


  if (client.connect("www.f2d.gr",80)) { // SERVER ADDRESS
    client.println("POST /tankmonitor/add.php HTTP/1.1"); 
    client.println("Host: www.f2d.gr"); // SERVER ADDRESS AGAIN
    client.println("Content-Type: application/x-www-form-urlencoded"); 
    client.println("User-Agent: Arduino/1.0");
    client.print("Content-Length: "); 
    client.println(data.length()); 
    client.println(); 
    client.print(data); 

    Serial.print(data); 
    Serial.println();
  } 

  if (client.connected()) { 
    client.stop();	// DISCONNECT FROM THE SERVER
  }

  delay(5000);


}

I have change the delay to 15 minutes to avoid overloading the database…

please help

Obviously my loop is wrong.

Maybe. What value are you getting for duration, in the program? What value are you getting for cm?

The value of 171 is what you would get if pulseIn() returned 10000, which sounds like the timeout limit, meaning that the sensor did not get an echo.

Maybe. What value are you getting for duration, in the program? What value are you getting for cm?

The value of 171 is what you would get if pulseIn() returned 10000, which sounds like the timeout limit, meaning that the sensor did not get an echo.

The sensor is working properly. I have proper data on the serial monitor. 171 equals to 171 cm which is the distance from the first obstacle. If I put my hand in front of the sensor the serial monitor reports different value (e.g. 10cm). However the value been posted are still the same (171).

This is why I have come to the conclusion that either the loop or the string is wrong. Or the string is not been updated properly.

Has anyone got this sensor and may try my code? Any advise / guidance is more than welcome

    Serial.print(data); 
    Serial.println();

Does this, or does it not, show something other than 171? Why are you printing anonymous data, anyway?

Serial.print("Data to POST: [");
Serial.print(data);
Serial.println("]");

conveys so much more information.

I should drop you a line and let you know that problem has been solved! I had forgot to clear the string for next read on the loop statement.

At the end of the loop statement:

 //clearing string for next read
    data = "";

thank you all for your help.