Problem about Send data via POST to website

Hi! i'm trying to send data via post to a website from Arduino YUN and insert them into a database, i'm trying to send a not-dynamic query string as test, but it doesn't work..

// include il bridge
#include <Bridge.h>
// include 
// #include <HttpClient.h>
#include <BridgeClient.h>
#include <BridgeServer.h>




IPAddress server(XXX,XX,XXX,XXX);

void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Bridge.begin();
  Serial.begin(9600);
  while(!Serial);
}

void loop() {
 
 BridgeClient client;
  
}



void runCurl()
{
  
Process p;


String cmd = "curl --data-urlencode \"amount=300&moment=2015-11-28 24:00:00\" http://mysite.com/addVal.php";

p.runShellCommand(cmd);
p.close();
}

i tested the php file and it works, i think the problem is into the sketch.

<?php
require("config.php");

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$xmlStr); 
return $xmlStr; 
} 

// Opens a connection to a MySQL server
$connection = mysql_connect (SQL_HOST, SQL_USER, SQL_PASS);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db(SQL_DB, $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
}

        
         $query = "INSERT INTO luminosity " .
                  "(id, amount, moment) " .
                    "VALUES (NULL, " . mysql_real_escape_string($_POST['amount']) . ", '" . mysql_real_escape_string(urldecode($_POST['moment'])) . "')";            
     
        $result = mysql_query($query);

             if (!$result) {
                
              echo "ERROR! index.php - query INSERT INTO - no results: " . $query;

            }



        $query2 = "SELECT * FROM luminosity WHERE 1 ORDER BY id";



         $result2 = mysql_query($query2);

                    if (!$result2) {

                      echo "ERROR! index.php - query2 SELECT - no results: " . $query2;

                    } else {
                        
                        header("Content-type: text/xml");
                        // Start XML file, echo parent node

                        echo "<vals>";

                           while ($row = @mysql_fetch_assoc($result2)){
                               
                               
                               echo "<val>" .
                                      "<id>" . parseToXML($row['id']) . "</id>" .
                                      "<amount>" . parseToXML($row['amount']) . "</amount>" .  
                                      "<moment>" . parseToXML($row['moment']) . "</moment>" .
                                    "</val>";
  
                           } // fine while
                        
                        echo "</vals>";

                    } // fine if-else result


?>

Thank you all!

I assume you intend for the function runCurl() to upload a sample to your PHP page. But while you define that function, you don't actually call it anywhere.

Your loop() function does nothing but create an instance of BridgeClient, and then it exits, throwing away the BridgeClient object. Of course, it does this over and over again. What is BridgeClient, BridgeClient.h, and BridgeServer.h? Where did you get them and what are you trying to accomplish with them? You also define an IPAddress object that is never used.

And just to be sure, you do realize that this statement means the sketch will stop and wait there until you open a connection through the micro-USB serial connection, right?

 while(!Serial);

Normally this is added to the beginning of a sketch so that all of the serial output can be seen on the serial monitor, but I question it because you don't actually print out any serial output.

Use either Data Access Layer or Business Logic Layer.

Data Access Layer:

TIMESTAMP with Mysql

http://ibuyopenwrt.com/index.php/8-yun-compatible/57-mysql-with-timestamp

Business Logic Layer ( PHP Layer):

...
$moment=date("Y-m-d H:i:s");
$query = "INSERT INTO luminosity " .
                  "(id, amount, moment) " .
                    "VALUES (NULL, " . mysql_real_escape_string($_POST['amount']) . ", '" . $moment . "')";  
...
String cmd = "curl --data-urlencode \"amount=300&moment=2015-11-28 24:00:00\" http://mysite.com/addVal.php";

"24:00:00" is invalid time!

Thanks all for your help,

I understand enough PHP but I'm new with Arduino. I tried to take inspiration from other sketckes but i didn't succeed with anyone of them, that's why my sketch was a little "confused"..

I tried in this way:

// include il bridge
#include <Bridge.h>


void setup() {

  // Initialize Bridge
  Bridge.begin();

    }


// eseguo il loop che esegue runCurl

void loop()
    {
      // inizializzo la funzione runCurl

      runCurl();

      delay(5000);
    }

// dichiaro la funzione runCurl

void runCurl()

    {
  
      Process p;

      String cmd = "curl --data-urlencode \"amount=300&moment=2015-11-28 23:59:00\"www.mysite.com/addVal.php?";

      p.runShellCommand(cmd);

      p.close();

    }

Thanks even for the suggestion about the TIMESTAMP, i didn't know about it.. anyway, I didn't use dynamic values for time in order to make the Arduino sketch simpler.

Thanks again!

Use "http://httpbin.org/post" to test code!

http://httpbin.org

nano /root/curl.sh
#!/bin/ash
curl -d amount=$1  http://httpbin.org/post
chmod 755 /root/curl.sh

Testing it!

/root/curl.sh 120
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "amount": "120"
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "10",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.29.0"
  },
  "json": null,
  "origin": "XX.XX.XX.XX",
  "url": "http://httpbin.org/post"
}

Change url back to yours

#!/bin/ash
curl -d amount=$1  http://mysite.com/addVal.php

Arduino code:

#include <Process.h>
void setup() {
 Bridge.begin();  // Initialize Bridge
}
void loop() {
 int temperature = random(0, 100);
 Process p;              
 p.begin("/root/curl.sh");      
 p.addParameter(String(temperature)); 
 while (p.running());
 while (p.available()) {
    char c = p.read();
    Serial.print(c);
 }
 delay(1000); 
}

Ok, thanks, I'll study it and I'll try!

Could you explain me why this code is not working? and why you are using p.addParameter and not p.runShellCommand(cmd)?

#include <Bridge.h>
#include <Process.h>
void setup() {
  // Initialize Bridge
  Bridge.begin();
  // Initialize Serial
  Serial.begin(9600);
  // inizializzo la funzione runCurl
  runCurl();
  String startString;
void loop()
    {
    }
// dichiaro la funzione runCurl
void runCurl() {
      Process p;
      p.begin ("curl");
      String cmd = "curl --data-urlencode \"amount=300&moment=2015-11-30 23:59:00\"www.mysite.org/addVal.php?";
      p.runShellCommand(cmd);
    }

Thanks again, and sorry, I'm trying to understand the differences between the commands...

elenabarbi:
...
and why you are using p.addParameter and not p.runShellCommand(cmd)?
...

It is variable, should not be hard code!

Hi, I think I guessed the code.
At the moment it works ...
What do you think about it?

//includo le librerie Bridge e Process
#include <Bridge.h>
#include <Process.h>

void setup() {

  // Initializzo Bridge
  Bridge.begin();

  // Initializzo Serial
  Serial.begin(9600);

  // Wait until a Serial Monitor is connected.
  while (!Serial);
  Serial.println("Ready");

   runCurl();   
   delay(100000);

}

void loop() {
   
}

void runCurl() {
  // Launch "curl" command and get Arduino ascii art logo from the network
  // curl is command line program for transferring data using different internet protocols
  Process p;        // Create a process and call it "p"
  p.begin("curl");  // Process that launch the "curl" command
  String myUrl = "http://mysite.com/addVal.php?amount=300&moment=2015-11-29+23%3A23%3A23";
  p.addParameter(myUrl); // Add the URL parameter to "curl"
  p.run();      // Run the process and wait for its termination

  // A process output can be read with the stream methods
  while (p.available()>0) {
    char c = p.read();
    Serial.print(c);
  }
  // Ensure the last bit of data is sent.
  Serial.flush();
}

Thanks a lot

E.

elenabarbi:
...
At the moment it works ...
...

Great!