Hallo, ich bin ziemlich neu im Arduino gebiet und stehe gerade vor ein paar für mich schwer löslichen Problemen.
Ich möchte später per Webbrowser ein Relais für wenige Sekunden schalten, dafür nutze ich die Webduino Libary.
Als Basis habe ich die Samples von Webduino Authentification mit Web_Ajax_RGB kombiniert.
Mein Problem ist es momentan die Parameter abzufragen und dann die jeweilige Funktion auszuführen.
Dabei verstehe ich den Aufbau von readPOSTparam nicht wirklich.
Ich möchte ein Parameter mit dem Namen "action" senden und dabei 3 verschiedene Values nutzen "open", "close" oder "stop"
Was stimmt an meinem Code nicht, bzw wie kann ich die Werte abfragen?
#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 PREFIX "/garage"
#define OPEN_PIN 5
#define CLOSE_PIN 3
#define STOP_PIN 6
int open = 0;
int close = 0;
char stop = HIGH;
String outputmsg;
WebServer webserver(PREFIX, 80);
void defaultCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
{
if (server.checkCredentials("dXNlcjp1c2Vy"))
{
if (type == WebServer::POST)
{
bool repeat;
char name[16], value[16];
do
{
/* readPOSTparam returns false when there are no more parameters
* to read from the input. We pass in buffers for it to store
* the name and value strings along with the length of those
* buffers. */
repeat = server.readPOSTparam(name, 16, value, 16);
/* this is a standard string comparison function. It returns 0
* when there's an exact match. We're looking for a parameter
* named red/green/blue here. */
if (strcmp(name, "open") == 0)
{
/* use the STRing TO Unsigned Long function to turn the string
* version of the color strength value into our integer red/green/blue
* variable */
open = strtoul(value, NULL, 10);
}
if (strcmp(name, "action") == 0)
{
close = strtoul(value, NULL, 10);
}
if (strcmp(name, "action") == 0)
{
stop = HIGH;
//outputmsg = value;
}
else
{
stop = LOW;
//outputmsg = "LOW";
}
}
while (repeat);
// after procesing the POST data, tell the web browser to reload
// the page using a GET method.
server.httpSeeOther(PREFIX);
// Serial.print(name);
// Serial.println(value);
return;
}
server.httpSuccess();
if (type == WebServer::GET)
{
/* store the HTML in program memory using the P macro */
P(message1) =
"<!DOCTYPE html><html><head>"
"<meta charset=\"utf-8\"><meta name=\"apple-mobile-web-app-capable\" content=\"yes\" /><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"><meta name=\"viewport\" content=\"width=device-width, user-scalable=no\">"
"<title>Webduino RGB</title>"
"</head>"
"<body>"
"<form id=\"form1\" name=\"form1\" method=\"post\" action=\"/garage\">"
"<input type=\"radio\" name=\"action\" id=\"radio\" value=\"open\" />"
"open<br />"
"<input type=\"radio\" name=\"action\" id=\"radio2\" value=\"close\" />"
"close<br />"
"<input type=\"radio\" name=\"action\" id=\"radio3\" value=\"stop\" />"
"stop<br />"
"<input type=\"submit\" name=\"button\" id=\"button\" value=\"Senden\" />"
"</form>";
P(message2) =
"</body>"
"</html>";
//server.printP(message1);
server.print(close);
//server.printP(message2);
}
}
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);
Ethernet.begin(mac, ip);
webserver.setDefaultCommand(&defaultCmd);
webserver.addCommand("index.html", &defaultCmd);
webserver.begin();
}
void loop()
{
char buff[64];
int len = 64;
analogWrite(OPEN_PIN, open);
analogWrite(CLOSE_PIN, close);
digitalWrite(STOP_PIN, stop); // set the LED on
/* process incoming connections one at a time forever */
webserver.processConnection(buff, &len);
}