Pages: [1]   Go Down
Author Topic: Small Arduino "Webserver" -> blinkm rgb  (Read 5646 times)
0 Members and 1 Guest are viewing this topic.
Zürich
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!

I made a simple application for the ethernet shield to control a blinkm RGB LED. It has a small form on a webpage to change the colors as you wish.

I used several libraries as you can see in my code..

Hope you like it smiley-wink

Code:
// Small Arduino Webserver
// by Andreas Cahen
//
// Used libraries and examples:
//
// Arduino Ethershield, server.pde
// TextString Library, WString.h
// Blinkm Library, BlinkMfuncs.h


#include <Ethernet.h>
#include <WString.h>
#include <Wire.h>
#include <BlinkM_funcs.h>
#define maxLength 25
#define blinkm_addr 0x00

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
String inString = String(maxLength);
int val;
int r;
int g;
int b;
Server server(80);

void setup()
{
  Ethernet.begin(mac, ip);
  server.begin();
  BlinkM_beginWithPower();  
  BlinkM_stopScript( blinkm_addr );  
}
void loop()
{
  Client client = server.available();
  if (client) {
    boolean current_line_is_blank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (inString.length() < maxLength) {
          inString.append(c);
         }        
        if (c == '\n' && current_line_is_blank) {
          if (inString.contains("?")) {
           int Pos_r = inString.indexOf("r");
           int Pos_g = inString.indexOf("g");
           int Pos_b = inString.indexOf("b");
           int End = inString.indexOf("H");
           r = atoi(inString.substring((Pos_r+2), (Pos_g-1)));
           g = atoi(inString.substring((Pos_g+2), (Pos_b-1)));
           b = atoi(inString.substring((Pos_b+2), (End-1)));  
           BlinkM_fadeToRGB( blinkm_addr, r, g, b );
         }
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html><head></head><body>");
          client.println("<h1>blinkm & ethershield colorpicker</h1>");
          client.println("<form method=get>R:<input type=text size=3 name=r>G:<input type=text size=3 name=g>B:<input type=text size=3 name=b>&nbsp;<input type=submit value=submit></form></body></html>");      
          break;
        }
        if (c == '\n') {
          current_line_is_blank = true;
        } else if (c != '\r') {
          current_line_is_blank = false;
        }
      }
    }
    delay(1);
    inString = "";
    client.stop();
  }
}

Here some pictures...

http://www.flickr.com/photos/boolean/3399886646/
http://www.flickr.com/photos/boolean/3399075841/

Cheers,

Andreas
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is great!
I've learned to use the ethernet en webpage modules from your code.
Thank you so much!

Now I'm going to find out if I can build something like:

(Controlling the light via a graphical hue interface via a browser)
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

updated for Arduino 0021. Thanks for the original code Andreas!

Code:
// Small Arduino Webserver --- for BlinkM
// by Andreas Cahen ---** updated for Arduino 0021 - by PsycleSam and Co. 20101219 tested on arduino uno with updated ethernet shield
//
// Used libraries and examples:
//
// Arduino Ethershield, server.pde
// Blinkm Library, BlinkMfuncs.h +++**SPI Library, SPI.h
// ---** no longer used - TextString Library, WString.h




#include <SPI.h>
#include <Ethernet.h>
//#include <WString.h>
#include <Wire.h>
#include <BlinkM_funcs.h>
#define maxLength 25
#define blinkm_addr 0x09

byte mac[] = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //update with your MAC and IP
byte ip[] = {
  192, 168, 0, 200 };
String inString = String(maxLength);
int val;
int r = 0;
int g = 0;
int b = 0;
char colorBuff[4];
Server server(80);

void setup()
{
  //Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
  BlinkM_beginWithPower();  
  BlinkM_stopScript( blinkm_addr );  
}
void loop()
{
  int bufLength;
  Client client = server.available();
  if (client) {
    boolean current_line_is_blank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (inString.length() < maxLength) {
          //inString.append(c);
          inString += c;
        }        
        if (c == '\n' && current_line_is_blank) {
          if (inString.indexOf("?") > -1) {
            int Pos_r = inString.indexOf("r");
            int Pos_g = inString.indexOf("g", Pos_r);
            int Pos_b = inString.indexOf("b", Pos_g);
            int End = inString.indexOf("H", Pos_b);
            if(End < 0){
              End =  inString.length() + 1;
            }
            bufLength = ((Pos_g) - (Pos_r+2));
            if(bufLength > 4){  //dont overflow the buffer
              bufLength = 4;
            }    
            inString.substring((Pos_r+2), (Pos_g-1)).toCharArray(colorBuff, bufLength);  //transfer substring to buffer
            r = atoi(colorBuff);
            bufLength = ((Pos_b) - (Pos_g+2));
            if(bufLength > 4){  //dont overflow the buffer
              bufLength = 4;
            }      
            inString.substring((Pos_g+2), (Pos_b-1)).toCharArray(colorBuff, bufLength);  //transfer substring to buffer
            g = atoi(colorBuff);
            bufLength = ((End) - (Pos_b+2));
            if(bufLength > 4){  //dont overflow the buffer
              bufLength = 4;
            }      
            inString.substring((Pos_b+2), (End-1)).toCharArray(colorBuff, bufLength);  //transfer substring to buffer
            b = atoi(colorBuff);
            // Serial.print("Red = ");
            // Serial.println( r );
            // Serial.print("Grn = ");
            // Serial.println( g );
            // Serial.print("Blu = ");
            // Serial.println( b );
            BlinkM_fadeToRGB( blinkm_addr, r, g, b );

          }
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html><head></head><body>");
          client.println("<h1>BlinkM - enter values 0 to 255</h1>");
          client.print("<form method=get>R:<input type=text size=3 name=r value=");
          client.print(r);
          client.print(">G:<input type=text size=3 name=g value=");
          client.print(g);
          client.print(">B:<input type=text size=3 name=b value=");
          client.print(b);
          client.println(">&nbsp;<input name=H type=submit value=submit></form>");
          client.println("</body></html>");
          break;
        }
        if (c == '\n') {
          current_line_is_blank = true;
        }
        else if (c != '\r') {
          current_line_is_blank = false;
        }
      }
    }
    delay(1);
    inString = "";
    client.stop();
  }
}
Logged

Pages: [1]   Go Up
Jump to: