Pages: [1]   Go Down
Author Topic: New to Arduino - Need advice with a project...  (Read 818 times)
0 Members and 1 Guest are viewing this topic.
San Marcos, Ca.
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
#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");
  }
}
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 108
Posts: 4021
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

San Marcos, Ca.
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I should have known that was coming... smiley-grin
The actual IP is 192.168.1.74
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The request should be written as:
Code:
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

Logged

San Marcos, Ca.
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

x=hello&y=world
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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".
Logged

UK
Offline Offline
Faraday Member
**
Karma: 101
Posts: 6191
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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".
Logged

Pages: [1]   Go Up
Jump to: