Go Down

Topic: Simple HTTP POST to Wifi Thermostat. HELP NEEDED (Read 3671 times) previous topic - next topic

zoomkat

When using the POST method, the length of the data being posted should be included in the return header like below.

Code: [Select]

client.println("POST hxxp://yourdomain.com/SMSSite/XMLInterface/Postxml.aspx HTTP/1.0");
// add a header indicating you have content-data to send.  If you set this too low, the server won't see it.  If it is too high, the server may wait, or report an error.
client.print("content-length: ");
client.println(strlen(big_string));
// tell the server you are done with the header.
client.println();
// send the posted data.
client.print(big_string);
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

bseal84

Thank you everyone for your help. Finally working now....

In the interest of someone else stumbling onto this question, here is my working code:


Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,1,101); // Thermostats IP
EthernetClient client;

int temp = 64;

void setup()
{
Ethernet.begin(mac);
  Serial.begin(9600);

  delay(2500);
  Serial.println("connecting...");

  if (client.connect(server, 80))
  {
    Serial.println("connected");
    delay(2500);
    client.println("POST /tstat/remote_temp HTTP/1.1");
    client.println("Content-length: 15");
    client.println("Connection: Close");
    client.println();
    client.print("\"rem_temp\":");     //Set Remote temperture to ___
    client.print(temp);
    client.println("}");
  }
    else
    {
    Serial.println("connection failed");
    }
}
void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c); 
  }
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
;
  }
}

kimjessen

Hoping for a little HELP here, I know I'm jumping into the middle of a thread but here similar to my problem.
I have a system running where data are placed in the csvdata directory and it works fine
if I write in my browser xxx.yyyyyyy.dk:8000/invput?id=kim&data=1,2,3,4,5

I get a file:kim-20130218.csv ( 2013--> yer 0218 --> date )
in the library: csvdata directory
how it works I do not know!

is there anyone who can help me here.


PeterH


I have a system running
how it works I do not know!
is there anyone who can help me here.


Are you asking for somebody to explain how your solution works? If not, what are you asking?
I only provide help via the forum - please do not contact me for private consultancy.

kimjessen

#19
Feb 18, 2013, 10:36 pm Last Edit: Feb 18, 2013, 10:43 pm by kimjessen Reason: 1
No No ...
I have seemingly expressed me wrong, I'm sorry ...
what I'm trying to say is ..
I have access to a server where I can send data to.
I have used in different situations where I have used the browser as I mention.
after I have on the server been able to use the data in a SQL data base.
So now I would do it a little more automatic.
I wanted to do a few experiments with some temperature sensing.
what I know is that I can use: "xxxxxx.yyyyyyy.dk: 8000/invput? id = kim & data = 1,2,3,4,5" in an http browser, but how do I get it to work on a arduino.
I have tried to do something in this style:
Code: [Select]
/*
  Web client sketch for IDE v1.0.1 and w5100/w5200
  Uses POST method.
  Posted November 2012 by SurferTim
*/

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

byte mac[] = {  
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

//Change to your server domain
char serverName[] = "xxxx.yyyyyyyy.dk";

// change to the page on that server
char pageName[] = "/invput";

EthernetClient client;
int totalCount = 0;
int loopCount = 0;
// insure params is big enough to hold your variables
char params[32];

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

 // disable SD SPI
 pinMode(4,OUTPUT);
 digitalWrite(4,HIGH);

 Serial.print("Starting ethernet...");
 if(!Ethernet.begin(mac)) Serial.println("failed");
 else Serial.println(Ethernet.localIP());

 delay(2000);
 Serial.println("Ready");
}

void loop()
{
 if(loopCount < 30)
 {
   delay(1000);
 }
 else
 {
   loopCount = 0;
   // params must be url encoded.
   sprintf(params,"id=kim&data=1,2,3,4,5,6,7,8,9",totalCount);//"id=kim&data=1,2,3,4,5,6,7,8,9"-> This I change to a variable when things run.  
   if(!postPage(serverName,pageName,params)) Serial.print("Fail ");
   else Serial.print("Pass ");
   totalCount++;
   Serial.println(totalCount,DEC);
 }    

 loopCount++;
}


byte postPage(char* domainBuffer,char* page,char* thisData)
{
 int inChar;
 char outBuf[64];

 Serial.print("connecting...");

 if(client.connect(domainBuffer,8000))
 {
   Serial.println("connected");

   // send the header
   sprintf(outBuf,"POST %s HTTP/1.1",page);
   client.println(outBuf);
   sprintf(outBuf,"Host: %s",domainBuffer);
   client.println(outBuf);
   client.println("Connection: close\r\nContent-Type: application/x-www-form-urlencoded");
   sprintf(outBuf,"Content-Length: %u\r\n",strlen(thisData));
   client.println(outBuf);

   // send the body (variables)
   client.print(thisData);
 }
 else
 {
   Serial.println("failed");
   return 0;
 }

 int connectLoop = 0;

 while(client.connected())
 {
   while(client.available())
   {
     inChar = client.read();
     Serial.write(inChar);
     connectLoop = 0;
   }

   delay(1);
   connectLoop++;
   if(connectLoop > 10000)
   {
     Serial.println();
     Serial.println("Timeout");
     client.stop();
   }
 }

 Serial.println();
 Serial.println("disconnecting.");
 client.stop();
 return 1;
}


but it does not work. I get a 405 error see below:
Code: [Select]
Starting ethernet...192.168.10.150
Ready
connecting...connected
HTTP/1.1 405 Method Not Allowed
Date: Mon, 18 Feb 2013 13:24:36 GMT
Server: Apache/2.2.22 (Ubuntu)
Allow: GET,HEAD
Content-Length: 742
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=UTF-8


   <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
   <html>
       <head>
           <title>Error: 405 Method Not Allowed</title>
           <style type="text/css">
             html {background-color: #eee; font-family: sans;}
             body {background-color: #fff; border: 1px solid #ddd;
                   padding: 15px; margin: 15px;}
             pre {background-color: #eee; border: 1px solid #ddd; padding: 5px;}
           </style>
       </head>
       <body>
           <h1>Error: 405 Method Not Allowed</h1>
           <p>Sorry, the requested URL <tt>&#039;http://xxxx.yyyyyyy.dk/invput&#039;</tt>
              caused an error:</p>
           <pre>Method not allowed.</pre>
       </body>
   </html>

disconnecting.
Pass 1


I hope that you now understand my problem.

billroy

The server 405 error is telling you it doesn't like POST operations but would be happy to receive a GET operation from the Arduino and respond to it like it responds to the browser GET.

For starters, change POST to GET here and see if it helps:
Code: [Select]

    sprintf(outBuf,"POST %s HTTP/1.1",page);


-br


kimjessen

Great thanks I will try it first in the morning

kimjessen

#22
Feb 19, 2013, 08:26 am Last Edit: Feb 19, 2013, 01:25 pm by kimjessen Reason: 1
Hello Have just tried to change to your suggestion, it also do not work. =( =( =(
Code: [Select]
Starting ethernet...192.168.10.150
Ready
connecting...connected
HTTP/1.1 400 Bad Request
Date: Tue, 19 Feb 2013 07:22:27 GMT
Server: Apache/2.2.22 (Ubuntu)
Content-Length: 730
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=UTF-8


   <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
   <html>
       <head>
           <title>Error: 400 Bad Request</title>
           <style type="text/css">
             html {background-color: #eee; font-family: sans;}
             body {background-color: #fff; border: 1px solid #ddd;
                   padding: 15px; margin: 15px;}
             pre {background-color: #eee; border: 1px solid #ddd; padding: 5px;}
           </style>
       </head>
       <body>
           <h1>Error: 400 Bad Request</h1>
           <p>Sorry, the requested URL <tt>&#039;http://xxxx.yyyyyyy.dk/invput&#039;</tt>
              caused an error:</p>
           <pre>Invalid format string</pre>
       </body>
   </html>

disconnecting.
Pass 1


else any Ider :~ :~ :~

billroy

The reason I said "for starters" is that there is more to think through and re-code. ;)

The code currently sends data in the post body.  That doesn't work for GET requests, which is why you see the complaint about a malformed request.  The data has to be part of the url that is sent to the server after GET (where currently just the page is sent).

Read up on HTTP GET requests and you'll see the proper format to send.

Happy debugging...

-br

kimjessen

Yes of course.

I will then try.
I'm not sure I fully understand it all but I'll try. :smiley-sad-blue:

Go Up