Go Down

Topic: led control via http (Read 344 times) previous topic - next topic

OlegEX

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: [Select]
/* 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.

Osgeld

i dunno, this MIGHT work

Code: [Select]
  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?

zoomkat

Chose another pin. The ethernet shield uses pin 13.

Code: [Select]
int led = 13;
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

OlegEX


Chose another pin. The ethernet shield uses pin 13.

Code: [Select]
int led = 13;


Thank you very very much! :) so easy

Go Up