Arduino yun with POST example

Below I have attached the http post example code.

 #include <Bridge.h>
          #include <Console.h>
          #include <FileIO.h>
          #include <HttpClient.h>
          #include <Mailbox.h>
          #include <Process.h>
          #include <YunClient.h>
          #include <YunServer.h>
          #include <dht.h>
          #include <SPI.h>
 
          //IP Address of the sever on which there is the WS: http://www.mywebsite.com/
          IPAddress server(XX,XX,XX,XX); //192.168.0.51
 
          YunClient client;
          dht DHT;
          #define DHT11_PIN 2
          int temp;
          int hum;
          int metano;
          int idrogeno;
          int led_giallo = 13;
          int led_rosso = 12;
          String parametri ="";           //String of POST parameters
 
          void setup()
          {
                 Bridge.begin();
                 Serial.begin(9600);
 
                 pinMode(led_rosso,OUTPUT);
                 pinMode(led_giallo,OUTPUT);
                 digitalWrite(led_giallo,HIGH);
                 digitalWrite(led_rosso,LOW);
                 delay(2500);
                 digitalWrite(led_giallo,LOW);
          }
 
          void loop()
          {
                 //measures
                 metano = analogRead(0);
                 idrogeno = analogRead(1);
                 hum = (int) DHT.humidity;
                 temp = (int) DHT.temperature;
 
                 if (client.connect(server, 80)) {
                          Serial.println("connected");
                          delay(2500);
                          parametri="temp="+String(temp) + "&hum="+ String(hum)+"&metano="+String(metano)+"&idrogeno="+String(idrogeno);
                          client.println("POST /test/arduino/add.php HTTP/1.1");
                          client.println("Host: XX.XX.XX.XX"); //192.168.0.51
                          client.print("Content-length:");
                          client.println(parametri.length());
                          Serial.println(parametri);
                          client.println("Connection: Close");
                          client.println("Content-Type: application/x-www-form-urlencoded;");
                          client.println();
                          client.println(parametri);  
                 }else{
                        Serial.println("connection failed");
                        digitalWrite(led_rosso,HIGH);
                        delay(1000);
                 }
                 if(client.connected()){
                             client.stop();   //disconnect from server
                 }
                 delay(2000);
    }

somebody please help me to solve this.
thank you

Your best bet would be to use the Linux curl command to do that, using a Process object. I'll let a curl expert (Jesse?) describe the proper command to use.

To get the code you have working, I think the changes are to put the server's IP address in where you have xx.xx.xx.xx (you have the right address in the comments that follow those two lines.) Then, use the 8080 port number instead of 80 in the client.connect() call. I don't believe that the server.thread_pool has any significance to the thread and you don't have to find a place to put that parameter.

I don't know if that's all of the changes you will have to make, but hopefully it gets you going in the right direction.

The most important change is to the client.connect() call: the first parameter is the address of the server (192.168.0.51 in your case) and the second parameter is the port number on that server (8080 in your case.) That will at least get you connected to the right service on the server.

Do some research on the HTTP protocol, which is the language used between web browsers and web servers for transferring web pages. Since you are trying to code an HTTP transaction, you should know at least a little about it. Trying to get something like this working is going to be difficult if you are just copying and modifying an example, and not having at least a basic understanding of what it does.

sarathkumark77:
client.println("POST /test/arduino/add.php HTTP/1.1");

The first line of an HTTP transaction gives the details of what you are asking the server to do during the session. The first word is a verb, and it says you are trying to POST data to the server - most web pages are retrieved using GET, and POST is usually used when submitting data for a form. Next comes the URL (web page address) you are trying to access on the server. Finally, the HTTP/1.1 says that you are going to be using version 1.1 of the HTTP standard to control this session.

So this says that you are sending the server the contents of a web form, and the page /test/arduino/add.php is supposed to get and process the data.

After this line comes a series of headers being sent to the server, describing more details about the transfer. Each line sent is considered a header, until a blank line is sent - what comes after the blank line is the content of the transfer.

client.println("Content-Type: application/x-www-form-urlencoded;");

This is telling the web server that the content of the transfer (which comes after the blank line after the headers) is going to be the contents of a form, and will be URL encoded (certain special characters are escaped using special escape sequences.) If you read up on HTTP and content types, you will get lots more information.

@sarathkumark77,

the code has mistakes.

Using Yun to talk to a server is not the best way. You can use the Linux part to do the communication.

What do you want to do? It is not clear what you want to do.

Jesse

Hi Jesse,

Actually I am running a python webserver which get the data from arduino yun and display the data to the webpage. For that I have used the code temperature web panel from bridge library and everything is working fine. But now I need to send data from arduino yun using post method for every 500ms or 1000ms so that the webserver will get the data and display to the webpage. My python webserver code to get the temperature web panel example data are

import os

import cherrypy
from cherrypy.lib.static import serve_file
from urllib import urlopen
import urllib2

path   = os.path.abspath(os.path.dirname("__file__"))
config = {
 'global' : {
   'server.socket_host' : '192.168.0.51',
   'server.socket_port' : 8080,
   'server.thread_pool' : 8
 }
}


class App:

 @cherrypy.expose
 def index(self):
   return serve_file(os.path.join(path, 'index.html')) 

 @cherrypy.expose
 @cherrypy.tools.json_out()
 def getData(self):

  response = urllib2.urlopen("http://arduino.local/arduino/temperature")
  page_source = response.read()
  str = unicode(page_source, errors='replace')
  #print(page_source)
  return {
   'sensorData' : str
  }



if __name__ == '__main__':
 cherrypy.quickstart(App(), '/', config)

The above one is working fine for receiving the data from temperature webpanel. But now I need to send the data from arduino using post method. So I used the below code.

#include <Bridge.h>
         #include <Console.h>
         #include <FileIO.h>
         #include <HttpClient.h>
         #include <Mailbox.h>
         #include <Process.h>
         #include <YunClient.h>
         #include <YunServer.h>
         #include <dht.h>
         #include <SPI.h>

         //IP Address of the sever on which there is the WS: http://www.mywebsite.com/
         IPAddress server(192,168,0,51); //my webserver ip address

         YunClient client;
         dht DHT;
         #define DHT11_PIN 2
         int temp;
         int hum;
         int metano;
         int idrogeno;
         int led_giallo = 13;
         int led_rosso = 12;
         String parametri ="";           //String of POST parameters

         void setup()
         {
                Bridge.begin();
                Serial.begin(9600);

                pinMode(led_rosso,OUTPUT);
                pinMode(led_giallo,OUTPUT);
                digitalWrite(led_giallo,HIGH);
                digitalWrite(led_rosso,LOW);
                delay(2500);
                digitalWrite(led_giallo,LOW);
         }

         void loop()
         {
                //measures
                metano = analogRead(0);
                idrogeno = analogRead(1);
                hum = (int) DHT.humidity;
                temp = (int) DHT.temperature;

                if (client.connect(server, 8080))  // 8080 is my port
{
                         Serial.println("connected");
                         delay(2500);
                         parametri="temp="+String(temp) + "&hum="+ String(hum)+"&metano="+String(metano)+"&idrogeno="+String(idrogeno);
                         client.println("POST /test/arduino/add.py HTTP/1.1"); //using post method to webserver
                         client.println("Host: 192.168.0.51:8080"); //my url
                         client.print("Content-length:");
                         client.println(parametri.length());
                         Serial.println(parametri);
                         client.println("Connection: Close");
                         client.println("Content-Type: application/x-www-form-urlencoded;");
                         client.println();
                         client.println(parametri);  
                }else{
                       Serial.println("connection failed");
                       digitalWrite(led_rosso,HIGH);
                       delay(1000);
                }
                if(client.connected()){
                            client.stop();   //disconnect from server
                }
                delay(2000);
   }

When I compile the above code server is connected but no data is received. I think I made a mistake in these two lines. client.println("POST /test/arduino/add.py HTTP/1.1"); //using post method to webserver
client.println("Host: 192.168.0.51:8080"); //my url

Kindly give the solution to solve this.

Regards,
Sarathkumar.
(Just changed my account)