Pages: [1]   Go Down
Author Topic: Trying to control 2 RGB LED's using the Ajax example in the Webduino library  (Read 1531 times)
0 Members and 1 Guest are viewing this topic.
South UK
Offline Offline
God Member
*****
Karma: 5
Posts: 581
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all, I am just playing around with the Web_AjaxRGB_mobile sketch in the examples and trying to control 2 LED's

I have duplicated the bits I thought I needed to and added variables for a second LED. It compiles and looks OK on a browser (two sets of silders show up) but I can see through the serial monitor that the values for reda, greena, bluea, redb, greenb and blueb don't update. I'm really stuck now, what have I missed?

Thanks

Code:
/* Web_AjaxRGB_mobile.pde - example sketch for Webduino library */
/* -  offers web-based slider controllers for RGB led  - */

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

// CHANGE THIS TO YOUR OWN UNIQUE VALUE
static uint8_t mac[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// CHANGE THIS TO MATCH YOUR HOST NETWORK
static uint8_t ip[4] = { 192, 168, 0, 177 }; // area 51!

/* all URLs on this server will start with /rgb because of how we
 * define the PREFIX value.  We also will listen on port 80, the
 * standard HTTP service port */
#define PREFIX "/rgb"
WebServer webserver(PREFIX, 80);

#define REDA_PIN 2
#define GREENA_PIN 3
#define BLUEA_PIN 4

#define REDB_PIN 5
#define GREENB_PIN 6
#define BLUEB_PIN 7

int reda = 0;            //integer for red darkness
int bluea = 0;           //integer for blue darkness
int greena = 0;          //integer for green darkness

int redb = 0;            //integer for red darkness
int blueb = 0;           //integer for blue darkness
int greenb = 0;          //integer for green darkness

/* This command is set as the default command for the server.  It
 * handles both GET and POST requests.  For a GET, it returns a simple
 * page with some buttons.  For a POST, it saves the value posted to
 * the red/green/blue variable, affecting the output of the speaker */
void rgbCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
{
  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, "reda") == 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 */
        reda = strtoul(value, NULL, 10);
      }
      if (strcmp(name, "greena") == 0)
      {
        greena = strtoul(value, NULL, 10);
      }
      if (strcmp(name, "bluea") == 0)
      {
        bluea = strtoul(value, NULL, 10);
      }
     
      if (strcmp(name, "redb") == 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 */
        redb = strtoul(value, NULL, 10);
      }
      if (strcmp(name, "greenb") == 0)
      {
        greenb = strtoul(value, NULL, 10);
      }
      if (strcmp(name, "blueb") == 0)
      {
        blueb = strtoul(value, NULL, 10);
      }
    } 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;
  }

  /* for a GET or HEAD, send the standard "it's all OK headers" */
  server.httpSuccess();

  /* we don't output the body for a HEAD request */
  if (type == WebServer::GET)
  {
    /* store the HTML in program memory using the P macro */
    P(message) =
"<!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>"
  "<link rel=\"stylesheet\" href=\"http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css\" />"
  "<script src=\"http://code.jquery.com/jquery-1.6.4.min.js\"></script>"
  "<script src=\"http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js\"></script>"
  "<style> body, .ui-page { background: black; } .ui-body { padding-bottom: 1.5em; } div.ui-slider { width: 88%; } #reda, #greena, #bluea { display: block; margin: 10px; } #reda { background: #f00; } #greena { background: #0f0; } #bluea { background: #00f; } </style>"
  "<style> body, .ui-page { background: black; } .ui-body { padding-bottom: 1.5em; } div.ui-slider { width: 88%; } #redb, #greenb, #blueb { display: block; margin: 10px; } #redb { background: #f00; } #greenb { background: #0f0; } #blueb { background: #00f; } </style>"
  "<script>"
// causes the Arduino to hang quite frequently (more often than Web_AjaxRGB.pde), probably due to the different event triggering the ajax requests
    "$(document).ready(function(){ $('#reda, #greena, #bluea').slider; $('#reda, #greena, #bluea').bind( 'change', function(event, ui) { jQuery.ajaxSetup({timeout: 110}); /*not to DDoS the Arduino, you might have to change this to some threshold value that fits your setup*/ var id = $(this).attr('id'); var strength = $(this).val(); if (id == 'red') $.post('/rgb', { red: strength } ); if (id == 'green') $.post('/rgb', { green: strength } ); if (id == 'blue') $.post('/rgb', { blue: strength } ); });});"
  "$(document).ready(function(){ $('#redb, #greenb, #blueb').slider; $('#redb, #greenb, #blueb').bind( 'change', function(event, ui) { jQuery.ajaxSetup({timeout: 110}); /*not to DDoS the Arduino, you might have to change this to some threshold value that fits your setup*/ var id = $(this).attr('id'); var strength = $(this).val(); if (id == 'red') $.post('/rgb', { red: strength } ); if (id == 'green') $.post('/rgb', { green: strength } ); if (id == 'blue') $.post('/rgb', { blue: strength } ); });});"
  "</script>"
"</head>"
"<body>"
  "<div data-role=\"header\" data-position=\"inline\"><h1>Zone A</h1></div>"
    "<div class=\"ui-body ui-body-a\">"
      "<input type=\"range\" name=\"slider\" id=\"reda\" value=\"0\" min=\"0\" max=\"255\"  />"
      "<input type=\"range\" name=\"slider\" id=\"greena\" value=\"0\" min=\"0\" max=\"255\"  />"
      "<input type=\"range\" name=\"slider\" id=\"bluea\" value=\"0\" min=\"0\" max=\"255\"  />"
    "</div>"
    "<div data-role=\"header\" data-position=\"inline\"><h1>Zone B</h1></div>"
    "<div class=\"ui-body ui-body-a\">"
      "<input type=\"range\" name=\"slider\" id=\"redb\" value=\"0\" min=\"0\" max=\"255\"  />"
      "<input type=\"range\" name=\"slider\" id=\"greenb\" value=\"0\" min=\"0\" max=\"255\"  />"
      "<input type=\"range\" name=\"slider\" id=\"blueb\" value=\"0\" min=\"0\" max=\"255\"  />"
    "</div>"
  "</body>"
"</html>";

    server.printP(message);
  }
}

void setup()
{
  pinMode(REDA_PIN, OUTPUT);
  pinMode(GREENA_PIN, OUTPUT);
  pinMode(BLUEA_PIN, OUTPUT);
 
  pinMode(REDB_PIN, OUTPUT);
  pinMode(GREENB_PIN, OUTPUT);
  pinMode(BLUEB_PIN, OUTPUT);

Serial.begin(9600);

  // setup the Ehternet library to talk to the Wiznet board
  Ethernet.begin(mac, ip);

  /* register our default command (activated with the request of
   * http://x.x.x.x/rgb */
  webserver.setDefaultCommand(&rgbCmd);

  /* start the server to wait for connections */
  webserver.begin();
}

void loop()
{
  // process incoming connections one at a time forever
  webserver.processConnection();
Serial.print(reda);
Serial.print(" ");
Serial.print(greena);
Serial.print(" ");
Serial.println(bluea);
Serial.print(redb);
Serial.print(" ");
Serial.print(greenb);
Serial.print(" ");
Serial.println(blueb);
  analogWrite(REDA_PIN, reda);
  analogWrite(GREENA_PIN, greena);
  analogWrite(BLUEA_PIN, bluea);
 
  analogWrite(REDB_PIN, redb);
  analogWrite(GREENB_PIN, greenb);
  analogWrite(BLUEB_PIN, blueb);
}
Logged

South UK
Offline Offline
God Member
*****
Karma: 5
Posts: 581
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any ideas, I'm mega stuck on this smiley-sad
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3352
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      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, "reda") == 0)

Seems like this would be a good place for a Serial print of what is contained within name and value.
Logged

Pages: [1]   Go Up
Jump to: