Pages: [1]   Go Down
Author Topic: Verarbeitung von Posts  (Read 566 times)
0 Members and 1 Guest are viewing this topic.
Hamburg
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?


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 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);
}
« Last Edit: May 17, 2012, 01:56:41 am by da2001 » Logged

im Neckartal
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mmmh,
kann sein, daß ich auf dem Holzweg bin und grade gewaltigen Mist erzähle, aber egal:

Dein string Vergleich zielt auf "open",  "close", "stop" - die netten beschreibenden Worte der jeweiligen Radiobuttons - das Formular schickt aber die values, also 1, 5, 10.
Dazu sind die values schließlich da.

Grüßle Bernd
Logged

Theoretisch gibt es keinen Unterschied zwischen Theorie und Praxis ...

Hamburg
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Stimmt, das war zu Testzwecken. Habe es korrigiert. Aber auch so läuft es leider nicht.
Ich weiß einfach nicht, wie ich diese Wert abfragen kann.
Logged

Hamburg
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Jippy! Habe es geschafft smiley
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&ouml;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);
}
Logged

Pages: [1]   Go Up
Jump to: