Pages: [1]   Go Down
Author Topic: led control via http  (Read 295 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Trying to switch on/off led by http request.
When i open the following url: http://arduino/on LED should switch ON, and url is http://arduino/off LED should switch OFF.

Using code below:
Code:
/* Web_HelloWorld.pde - very simple Webduino example */

#include "SPI.h"
#include "Ethernet.h"
#include "WebServer.h"

/* CHANGE THIS TO YOUR OWN UNIQUE VALUE. The MAC number should be
* different from any other devices on your network or you'll have
* problems receiving packets. */
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
int led = 13;
int check_state=1;


/* CHANGE THIS TO MATCH YOUR HOST NETWORK. Most home networks are in
* the 192.168.0.XXX or 192.168.1.XXX subrange. Pick an address
* that's not in use and isn't going to be automatically allocated by
* DHCP from your router. */
static uint8_t ip[] = { 192, 168, 1, 210 };

/* This creates an instance of the webserver. By specifying a prefix
* of "", all pages will be at the root of the server. */
#define PREFIX ""
WebServer webserver(PREFIX, 80);

void doorOnCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
  /* this line sends the standard "we're all OK" headers back to the
browser */
  server.httpSuccess();

  /* if we're handling a GET or POST, we can output our data here.
For a HEAD request, we just stop after outputting headers. */
  if (type == WebServer::HEAD)
    return;

  server.print("on");
  digitalWrite(led, HIGH);
}

void doorOffCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
  /* this line sends the standard "we're all OK" headers back to the
browser */
  server.httpSuccess();

  /* if we're handling a GET or POST, we can output our data here.
For a HEAD request, we just stop after outputting headers. */
  if (type == WebServer::HEAD)
    return;

  server.print("off");
  digitalWrite(led, LOW);
}


void setup()
{
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  /* initialize the Ethernet adapter */
  Ethernet.begin(mac, ip);

// run the same command if you try to load /index.html, a common
// default page name
  webserver.addCommand("on", &OnCmd);
  webserver.addCommand("off", &OffCmd);

  /* start the webserver */
  webserver.begin();
}

void loop()
{
char buff[64];
  int len = 64;

  /* process incoming connections one at a time forever */
  webserver.processConnection(buff, &len);
 
 
}

Can't understand why LED is alway ON. Even if digitalwrite LOW received.
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i dunno, this MIGHT work

Code:
  if (type == WebServer::HEAD)
    return;

but if your going to forgo brackets keep it inline cause can you tell me when the if starts and stops?
Logged


0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Chose another pin. The ethernet shield uses pin 13.

Code:
int led = 13;
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Chose another pin. The ethernet shield uses pin 13.

Code:
int led = 13;

Thank you very very much! smiley so easy
Logged

Pages: [1]   Go Up
Jump to: