
Der Code mag zwar nicht ganz sauber sein, aber er funktioniert super!
der attribute "action" hat nun 4 Parameter. open, close, stop und status.
Lösung sieht wie folgt aus:
Code:
#define WEBDUINO_AUTH_REALM "Access Logs"
#include "SPI.h"
#include "Ethernet.h"
#include "WebServer.h"
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
static uint8_t ip[] = { 192, 168, 0, 198 };
#define WEBDUINO_FAIL_MESSAGE "<h1>404</h1>"
#define PREFIX "/garage"
#define OPEN_PIN 5
#define CLOSE_PIN 3
#define STOP_PIN 6
#define DOOR_CONTACT 7
#define NAMELEN 32
#define VALUELEN 32
int doorState = 0;
char open = LOW;
char close = LOW;
char stop = LOW;
String outputmsg;
int cnt=0;
WebServer webserver(PREFIX, 80);
void defaultCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
if (server.checkCredentials("dXNlcjp1c2Vy"))
{
URLPARAM_RESULT rc;
char name[NAMELEN];
int name_len;
char value[VALUELEN];
int value_len;
server.httpSuccess(); //alles ok header
/* 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;
if (strlen(url_tail))
{
while (strlen(url_tail))
{
rc = server.nextURLparam(&url_tail, name, NAMELEN, value, VALUELEN);
if (rc != URLPARAM_EOS)
{
if (strcmp(name, "action") == 0) //wenn action= ...
{
if (strcmp(value, "open") == 0)
{
server.print("Tor wird geöffnet");
stop=LOW;
close=LOW;
open=HIGH;
}
if (strcmp(value, "close") == 0)
{
server.print("Tor wird geschlossen");
stop=LOW;
close=HIGH;
open=LOW;
}
if (strcmp(value, "stop") == 0)
{
server.print("Tor wird angehalten");
stop=HIGH;
close=LOW;
open=LOW;
}
if (strcmp(value, "status") == 0)
{
if (doorState == HIGH)
{
server.print("Tuer geschlossen:<br>");
}
else
{
server.print("Tuer offen:<br>");
}
}
}
}
}
}
}
else
{
/* send a 401 error back causing the web browser to prompt the user for credentials */
server.httpUnauthorized();
}
}
void setup()
{
pinMode(OPEN_PIN, OUTPUT);
pinMode(CLOSE_PIN, OUTPUT);
pinMode(STOP_PIN, OUTPUT);
pinMode(DOOR_CONTACT, INPUT);
Ethernet.begin(mac, ip);
webserver.setDefaultCommand(&defaultCmd);
webserver.addCommand("index.html", &defaultCmd);
webserver.begin();
}
void loop()
{
char buff[64];
int len = 64;
doorState = digitalRead(DOOR_CONTACT);
digitalWrite(OPEN_PIN, open);
digitalWrite(CLOSE_PIN, close);
digitalWrite(STOP_PIN, stop); // set the LED on
if(open == HIGH || close == HIGH || stop == HIGH)
{
cnt++;
if(cnt >= 5000)
{
open =LOW;
close=LOW;
stop=LOW;
cnt=0;
}
}
/* process incoming connections one at a time forever */
webserver.processConnection(buff, &len);
}
#include "SPI.h"
#include "Ethernet.h"
#include "WebServer.h"
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
static uint8_t ip[] = { 192, 168, 0, 198 };
#define WEBDUINO_FAIL_MESSAGE "<h1>404</h1>"
#define PREFIX "/garage"
#define OPEN_PIN 5
#define CLOSE_PIN 3
#define STOP_PIN 6
#define DOOR_CONTACT 7
#define NAMELEN 32
#define VALUELEN 32
int doorState = 0;
char open = LOW;
char close = LOW;
char stop = LOW;
String outputmsg;
int cnt=0;
WebServer webserver(PREFIX, 80);
void defaultCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
if (server.checkCredentials("dXNlcjp1c2Vy"))
{
URLPARAM_RESULT rc;
char name[NAMELEN];
int name_len;
char value[VALUELEN];
int value_len;
server.httpSuccess(); //alles ok header
/* 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;
if (strlen(url_tail))
{
while (strlen(url_tail))
{
rc = server.nextURLparam(&url_tail, name, NAMELEN, value, VALUELEN);
if (rc != URLPARAM_EOS)
{
if (strcmp(name, "action") == 0) //wenn action= ...
{
if (strcmp(value, "open") == 0)
{
server.print("Tor wird geöffnet");
stop=LOW;
close=LOW;
open=HIGH;
}
if (strcmp(value, "close") == 0)
{
server.print("Tor wird geschlossen");
stop=LOW;
close=HIGH;
open=LOW;
}
if (strcmp(value, "stop") == 0)
{
server.print("Tor wird angehalten");
stop=HIGH;
close=LOW;
open=LOW;
}
if (strcmp(value, "status") == 0)
{
if (doorState == HIGH)
{
server.print("Tuer geschlossen:<br>");
}
else
{
server.print("Tuer offen:<br>");
}
}
}
}
}
}
}
else
{
/* send a 401 error back causing the web browser to prompt the user for credentials */
server.httpUnauthorized();
}
}
void setup()
{
pinMode(OPEN_PIN, OUTPUT);
pinMode(CLOSE_PIN, OUTPUT);
pinMode(STOP_PIN, OUTPUT);
pinMode(DOOR_CONTACT, INPUT);
Ethernet.begin(mac, ip);
webserver.setDefaultCommand(&defaultCmd);
webserver.addCommand("index.html", &defaultCmd);
webserver.begin();
}
void loop()
{
char buff[64];
int len = 64;
doorState = digitalRead(DOOR_CONTACT);
digitalWrite(OPEN_PIN, open);
digitalWrite(CLOSE_PIN, close);
digitalWrite(STOP_PIN, stop); // set the LED on
if(open == HIGH || close == HIGH || stop == HIGH)
{
cnt++;
if(cnt >= 5000)
{
open =LOW;
close=LOW;
stop=LOW;
cnt=0;
}
}
/* process incoming connections one at a time forever */
webserver.processConnection(buff, &len);
}