New to Arduino - Need advice with a project...

Hello,
First off I would like to say what a great little device the Arduino is.
I have recently purchased a SunFounder Mega 2560 R3 ATmega2560-16AU Board and a W5100 Ethernet Shield for a Project I am working on for my home automation system. Also I am some what new to coding so please bear with me. I have created a series of switch inputs for my hard wired door and window magnetic switches that are hooked to the Arduino. The programing for that was pretty straight forward so I won't go into that. Where I am having an issue is getting the Anduino to send a POST command to my main automation system via RESTful Web. A friend that is much better at coding than I suggested the following code as an example but I cant seem to get it work. In essence each switch state requires a discrete RESTful Web call to the automation systems API. For the moment I just need to figure out why the following code doesn't work, from there I should be able to figure the rest out.

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 10 };                     
byte gateway[] = { 192, 168, 0, 1 };                 
byte subnet[] = { 255, 255, 255, 0 };
byte server[] = { 192, 168, 0, 100 };               

Client client(server, 8732);

void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);
  
  delay(1000);
  Serial.println("connecting...");
  
  if (client.connect()) {
    Serial.println("connected");
    client.println("POST http://xxx.xxx.xxx.xxx:8732/API/Sensor%201/ON HTTP/1.1");
    client.println("Content-type: application/x-www-form-urlencoded");
    client.println("Content-length: 28");                             
    client.println("Connection: Close");
    client.println();

  } else {
    Serial.println("connection failed");
  }
}
    client.println("POST http://xxx.xxx.xxx.xxx:8732/API/Sensor%201/ON HTTP/1.1");

xxx.xxx.xxx.xxx ?

I have never seen an IP address like that before.

I should have known that was coming... :smiley:
The actual IP is 192.168.1.74

The request should be written as:

POST /API/Sensor%201/ON HTTP/1.1
Host: 192.168.0.10:8732
Connection: Close
Content-Type: application/x-www-form-urlencoded
Content-Length: 15

x=hello&y=world

There are three lines of code I don't understand. Could you please explain there function?

Content-Type: application/x-www-form-urlencoded
Content-Length: 15

x=hello&y=world

An HTTP POST request is made up from a set of request headers, and the request body.
Content-Type tells the server the format of the request body, application/x-www-form-urlencoded means the data is a set of name=value pairs serparated by '&' and is the format web browsers send when submitting html forms.
Content-Length tells the server how long the request body is.
The x=hello&y=world is the request body, here I just did an example for sending two parameters, one called x with the value "hello", the second called y with the value "world".

Have you tried making a similar request from your PC using Python or Ruby (for example). I suspect you would find it easier to get advice for that sort of project. Then if it works on your PC it should work with an Arduino.

...R

An HTTP POST request is made up from a set of request headers, and the request body.
Content-Type tells the server the format of the request body, application/x-www-form-urlencoded means the data is a set of name=value pairs serparated by '&' and is the format web browsers send when submitting html forms.
Content-Length tells the server how long the request body is.
The x=hello&y=world is the request body, here I just did an example for sending two parameters, one called x with the value "hello", the second called y with the value "world".