Go Down

Topic: working HTTP POST example (Read 12900 times) previous topic - next topic

enjrolas

I wanted to write some code that would POST a bunch of analog variables to my webserver.  I couldn't find a great POST example, so I muddled through it for a couple hours.  My code is working, so I'm posting it as a reference to anyone in the same spot. 

This example builds and sends a POST request to my server every .5 seconds.  It doesn't bother to look at the response, just barfs data into the server like there's no tomorrow.  I get the sense that not all my requests make it to the server, because the timestamps of my data in the server aren't evenly spaced.  Still!  It's data, going up!

I'm using a wifly shield, but this should work for anyone using the httpclient or Ethernet libraries.


Something I didn't know before writing this--POST parameters are serialized just like GET parameters, and they're just listed in the "content" part of the HTTP request.  For example, if you want to POST the parameters "larry"=5, "curly"=6, "moe"=7, your POST content just looks like

larry=5&curly=6&moe=7

how cool is that?

Hope this helps somebody out.  Happy Hacking!
--enjrolas

Code: [Select]

#include "WiFly.h"
#include "Credentials.h"

Timer t;
long lastUpdate=0;

byte server[] = { 106,187,94,198 }; // artiswrong.com

Client client("artiswrong.com", 80);

void setup() {
 
  Serial.begin(115200);

  WiFly.begin();
 
  if (!WiFly.join(ssid, passphrase)) {
    Serial.println("Association failed.");
    while (1) {
      // Hang on failure.
    }
  } 

}

void post()
{
  Serial.println("connecting...");
  String PostData="sample={\"fittingId\":1,";
  unsigned char i;
  for(i=0;i<6;i++)
  {
    PostData=PostData+"\"channel-";
    PostData=String(PostData+i);
    PostData=PostData+"\":";
    PostData=String(PostData + String(analogRead(i)));
    if(i!=5)
      PostData=PostData+",";
  }
    PostData=PostData+"}"; 
  Serial.println(PostData);
  if (client.connect()) {
    Serial.println("connected");
  client.println("POST /tinyFittings/index.php HTTP/1.1");
  client.println("Host:  artiswrong.com");
  client.println("User-Agent: Arduino/1.0");
  client.println("Connection: close");
  client.println("Content-Type: application/x-www-form-urlencoded;");
  client.print("Content-Length: ");
  client.println(PostData.length());
  client.println();
  client.println(PostData);
  } else {
    Serial.println("connection failed");
  }
}


void loop() {
    post();
    delay(500);
}


robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

gemo

Very interesting post !

Could you also tell us more details about your configuration :
- Arduino board model
- Arduino IDE used and the reference (link to download) of the librairie Wifly
- version number of your Wifly firmware

Thank you in advance.

microfun

Thanks for taking the time to post this source. It really helped me get started. On my server (ASP.NET MVC 4), I needed to replace this line:

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

with the following:

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

in order to get it to work without an error 500. Posting this just in case others run into the same issue.

wallaceb

the code in one of the projects i made supports both POST and GET commands as the arduino runs a full web-server within itself. the code is rather complicated as it carefully breaks the HTTP headers apart thanks to a tutorial i found here: http://playground.arduino.cc/Code/WebServer where you need to scroll to "Extended Functionality"
and the code works very very well.

http://forum.arduino.cc/index.php?topic=140740.0

I'm sending data to a webserver, and the webserver stores in mysql database. How i return back the stuff (ok) variable to arduino and make a Serial Print(#stuff)?

Code: [Select]
String request = "GET "+ repository + "sensor.php?value=" + value + " HTTP/1.0";
  send_request(request);

  void send_request (String request) {
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, port);
  if (client.connected()) {
    client.println(request);     
    client.println(F(""));
    Serial.println("Connected & Data sent");
  }
  else {
    Serial.println(F("Connection failed"));
  }
string buffer;
int counter = 0;
while (client.connected()) {
  while (client.available()) {
   buffer[counter++] = client.read();
  }
}
Serial.println("Closing connection");
Serial.println("Buffer value is: " + buffer);
client.close();
}


PHP FILE:

Code: [Select]
$stuff = "";
include("conec.php");
if ($_GET["value"]) {
$link=Conection();
$Sql="insert into table (VALUE) values ('".($_GET["value"])."')";
mysql_query($Sql,$link);
$stuff = "ok";
}

Go Up