BREAKING NEWS

05 November 2015

Arduino RGB LED Web

Continuing the sequence of tutorials involving Arduino and RGB LEDs have you ever thought to decide the color of your chandelier or your lamp or your balls Christmas web? For example with your iPhone or Android smartphone?



And here accontantato, the project that you can make today is devoted to how to use RGB LEDs, Arduino Ethernet Shield and your smartphone for fun games of light to display in your home or with which to make a lamp or chandelier as my .

The sketch is not easy but I'll describe line by line to help in its understanding and we can make the changes that you feel is most appropriate:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

 
#include <SPI.h>
#include <Ethernet.h>
#define maxLength 25
 
mac byte [] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
ip byte [] = {192, 168, 2, 237};
InString String = String (maxLength);
int val;
int r = 0;
int g = 0;
int b = 0;
char colorBuff [4];
Server server (80);
 
int PINR = 3;
int = Table 5;
int pinB = 6;
 
void setup ()
{
  Ethernet.begin (mac, ip);
  server.begin ();
  pinMode (PINR, OUTPUT);
  pinMode (ping, OUTPUT);
  pinMode (pinB, OUTPUT);
 
  analogWrite (PINR, 0);
  analogWrite (ping, 0);
  analogWrite (pinB, 0);
 
  Serial.begin (9600);
}
void loop ()
{
  int bufLength;
  Client client = server.available ();
  if (client) {
    current_line_is_blank boolean = 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 ( "S" , 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);  // substring to the transfer 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);  // substring to the transfer 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);  // substring to transfer buffer
            b = atoi (colorBuff);
 
            analogWrite (PINR, r);
            analogWrite (ping, g);
            analogWrite (pinB, b);
 
          }
          client.println ( "HTTP / 1.1 200 OK" );
          client.println ( "Content-Type: text / html" );
          client.println ();
          client.println ( "<html> <head>" );
          client.println ( "<meta http-equiv = 'Content-Type' content = 'text / html; charset = iso-8859-1'> <title> :: Server :: Arduino </ title>" );
          client.println ( "<style>" );
          client.println ( "body {text-align: center; font-family: Arial, Helvetica, sans-serif; font-size: 50px; font-weight: normal;}" );
          client.println ( "h1, input {font-size: 100%; text-align: center;}" );
          client.println ( ".Dim {border: 1px solid # f0731c; width: 100%; text-align: center;}" );
          client.println ( ".pulsante {width: 60px; height: 60px; border: 1px solid # 000; margin: 5px;}" );
          client.println ( "</ style>" );
          client.println ( "</ head> <body>" );
 
          client.println ( "<div class = 'dim'> <h1> RGB Arduino </ h1>" );
          client.print ( "<form method = get>" );
 
          client.println ( "<table border = 0 align = center>" );
          client.println ( "<tr>" );
          client.println ( "<td align = center> <div class = button style = \" background-color: # FF0000; \ "> <br> R </ div> </ td>" );
          client.println ( "<td align = center> <div class = button style = \" background-color: # 00FF00; \ "> <br> G </ div> </ td>" );
          client.println ( "<td align = center> <div class = button style = \" background-color: # 0000FF; \ "> <br> B </ div> </ td>" );
          client.println ( "</ tr>" );
          client.println ( "<tr>" );
          client.println ( "<td align = center> <input type = text size = 3 name = r r = id value = \" " ); client.print (r); client.println ( "\"> </ td > " );
          client.println ( "<td align = center> <input type = text name = size = 3 g = g id value = \" " ); client.print (g); client.println ( "\"> </ td > " );
          client.println ( "<td align = center> <input type = text size = 3 name = b = b id value = \" " ); client.print (b); client.println ( "\"> </ td > " );
          client.println ( "</ tr>" );
 
          client.println ( "</ table>" );
 
          client.println ( "<br> <input name = \" D \ "type = \" submit \ "value = \" SEND \ ">" );
          client.println ( "</ form>" );
          client.println ( "</ div>" );
          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 ();
  }
}
You state that you will need to make extensive use of the libraries present Arduino IDE such as String, Ethernet, SPI.
We begin by successive lines to my comments, lines 11-12: include libraries that you've indicated, each of them will simplify the management of Ethernet shield or string handling;
Line 13: Define a constant maxLength value of 25 that is the maximum length of the text string that contains the values ​​passed from the web interface;
Lines 15-16: Define two arrays of type byte each containing a configuration value of the ethernet shield, the mac with which the card is present on your network, the IP with which you can reach the application you're writing;
Lines 17-22: Define some variables needed to sketch during his eseguzione, in particular the variable inStringlength of 25 (maxLength) characters, is the variable in which the values ​​memorize proveninenti from the web;
Line 23: initializes the web server on port 80 (default http for browser) so that the application responds on that port, it will facilitate access to the server Arduino you are making with this sketch;
lines 25-27: definisi pins for connections to the LEDs R, G and B, with this setting you can vary the connections between Arduino and led at any time, simply by changing the respective line;
line 29: define the function setup () that now you know and you know very well be necessary for the proper operation and initialization of your sketch;
Line 31: Sets initializing around sketching the mac and ip defined in lines 15 and 16;
Line 32: initializes the server itself, so to say, to use the Arduino Ethernet library to create a server that meets the IP address that you have defined and the door 80;
Lines 33-35: Define the pin which will be connected to the three terminals for the colors R, G and B as kindOUTPUT that they use to send signals from Arduino to the LED. WARNING : the definition of the 3-pin to link the terminals of the LEDs R, g and B must always urilizzare digital outputs of the PWM type, this setting is required to use the comendo analogWrite that sends values ​​from 0 to 255 only on those outputs; ( Note : You can also use the normal digital outputs with PWM signals using the Liberian softPWM) ;
lines 37-39: using the command analogWrite impostia value to 0 to inciato led across power dell'arduino;
Line 41: Initializes serial communication at 9600 baud, the serial monitor you can not use it at a later stage when the sketch is running smoothly, but when debugging is really useful;
line 43: define the function loop () cyclically perform the sketch you're writing.
Line 45: initialize a variable of type int (integer) bufLength 'll use shortly;
line 46: define an object of type Client for storing the presence of a request from a Web client, in practice when you connect your browser to the web interface, Arduino will receive a request for type Client that you can read with the method available () class server initializes the line 32;
Line 47: Read if the client variable contains a request from a browser then performs what is indicated in the following lines;
Line 48: sets the variable current_line_is_blank used later;
Line 49: starts a cycle set to the presence of a connection from a web browser, such verification will avoid making calculations during the absence of connection;
Line 50: Check the connection as well as being present is also used;
Line 51: Read the value inciato by the client with the method read () object client assign it to a variable of type char;
lines 52-55: verify that the length of the value contained in inString is less than the setting  maxLength defined above, and in function of that length to the inString appends the new characters read from the connection with the client; The method append () commented it was the way used by the previous version of the String to concatenate a new character to a string, the 0021 version of the IDE the String class has been included in the IDE and the append () method is no longer available, instead you can use the + = syntax;
Line 56: Check that the character c read is a "\ n" (new line) and that simultaneously the variablecurrent_line_is_blank  is set to true, in the last lines of the sketch you will see that a further check defines each loop of the program whether to continue reading and append characters or whether to proceed with processing;
Line 57: Use the method indexOf () of the String object clase inString to determine if the line is a "?" character that character is automatically entered in the string sent by a browser when you send a command GET or POST, if you want more Information of Interest on GET and POST methods you should look at how the Internet communications and HTTP; for your sketch it is enough to know that the "?" is reserved for the HTTP communication and identifying a transition of data from an HTML form to a program able to process it;
lines 58-60: using the method indexOf () also used in the previous line, not only individuals the presence of a character "r", "g" or "b" but the method indicated will also returns the numerical value for the position to this character occupies in the string that you are processing;
Line 61: also identifies the point in the string, which is positioned in the character "S" to use as the end of the string to be analyzed;
Digression: to better understand the views and lines just to avoid confusion later imagines the string that the client sends to the server (Arduino in this case), is this:
1
2
1.1 GET /? R = g = xxx yyy zzz & b = S = submit
012345678901234567890123456789012345
I intentionally added in the numbering of the characters to better understand the issue of posizoni, when using the method indexOf ("?") on the string you returned 9, the position of the character sought in the string, the same way for "r" will get 10 , for "g" 16, a "b" and 22 "S" 28.
lines 62-64: verify that the search for the character "S" does not return a value less than 0, in such a case to avoid errors during subsequent processing sets End (which should contain 28, in the previous example) equal to the length of the string passed +1; in this way you could fail to pass the value S = submit, however, you should readjust the sketch making sure that the rest of the string is the same;
lines 66-71,72-77,78-83: perform the same sequence of commands only that each line group does it for one of the three values ​​r, g and b; I will analyze only one of the sequences 66-71;
line 66: sets the variable bufLength as the difference between the position of g (16) and the position of r (10) + 2 characters, in practice 16-10 + 2 = 16 -12 = 4;
lines 67-69: execute a control variable length bufLength so that does not exceed 4, otherwise you set it to 4, this control bases its veracity that each of the values ​​passed is composed of 3 digits ( 255 maximum) plus the & separator character, each character is no longer needed;
Line 70: could you write this line in two for a clearer indication of what it does, it would be:
1
2
String _tmpStr = inString.substring ((Pos_r + 2), (Pos_g-1));
_tmpStr.toCharArray (colorBuff, bufLength);
Ie defining a variable of type String  _tmpStr to which checks the portion of the string between r + 2 (12), and g-1 (15) in practice the value of _tmpStr would xxx, the weight to be assigned to the red (0-255) ; the next line would assign to the variable colorBuff value extracted, bufLength indicates the method toCharArray () the size of the value to be assigned +1;
Line 71: assigns r value, converted in whole, just found;
lines 85-87: assign values ​​r, g and b to their Arduino pin, causing the resulting power LEDs;
lines 90 to 123, using the method println () assigns the object client lines to be sent to html browser to view the HTML page, for further information, I invite you to read some manual HTML online even if you do not already have basic knowledge in this sense.
Line 124: stop while loop indicating exiting because processing is finished;
Lines 126-131: define cntrolli of which I have spoken above, in practice if you find a value of type "\ n" (new line - new line) set the variable current_line_is_blank so that the next loop is developed by the code obtained, alternatively if you do not find a type character "\ r" (carriege return - return) and you have not found, obviously not a character "\ n" set to false the current_line_is_blank , so that the next loop is not carried out any processing queued but the character read from the client to inString;
134 line: after all the processing and each cycle wait about 1 millisecond before continuing, it is a nice and simple delay that allows the loop process commands being given;
Line 135: resets the value of inString to prepare a subsequent request from the client;
Line 136: stop comnunicazione with lient, this is vital for the browser | show that communication is finished and should not expect more data to process.
You just have to upload the sketch of Arduino, and connect your iPhone or browser the IP indicated in my case 192.168.2.237, the result should look like this:
iphome interface Arduino ethernet shield


Share this:

Post a Comment

 
Back To Top
Distributed By Blogger Templates | Designed By OddThemes