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");
}
}