Arduino SMD RGB controller through HTTP portal (Or Web) using Ethernet Shield

I haven’t played with Arduino for a while so I decided to start playing with Ethernet Shields.

What’s SMD? It’s Surface Mounted Device. And because it consumes less power and this RGB module produces more light, I decided to start using it for upcoming moodlight projects (Or other stuff).

Any way, since I already know how to control RGB lights, I decided to pull out a web server and make it accept requests and control lights.

There’s really no circuit diagram or anything, just attach the Ethernet Shield then connect the RGB pins to Pin #3, Pin #5, and Pin #6 (Since they work in PWM which are analog-output compatible).

What we need to do coding-wise is to include SPI.h and Ethernet.h, set up the IP Addresses (Since it’ll be a server, not a client.). You can use whatever Mac Address you want, so I used the DEADBEEFFEED mac address for teh lulz.

Then you let it listen for connections.

When it connects, it dumps lines in a string variable, parse it (I did a retarded parsing, you can do it on your own) then writes analog output to the mentioned pins above and we’re done!

Done explaining the code? Here’s the code below:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 15 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server(80);

String readString;
int pinThree = 0;
int pinFive = 0;
int pinSix = 0;

void setup()
{

   pinMode(3, OUTPUT);
   pinMode(5, OUTPUT);
   pinMode(6, OUTPUT);

   Ethernet.begin(mac, ip, gateway, subnet);
   server.begin();

   for (int x=0; x<255; x++)
   {
      SetColor(x,x,x);
      delay(100);
   }
}

void loop()
{
   EthernetClient client = server.available();
   if (client)
   {
      while (client.connected())
      {
         if (client.available())
         {
            char c = client.read();
            if (readString.length() < 100)
            {
               readString += c;
            }
            if (c == '\n')
            {
               ProcessGet(readString);

               client.println("HTTP/1.1 200 OK");
               client.println("Content-Type: text/html");
               client.println();

               client.println("<HTML>");
               client.println("  <HEAD>");
               client.println("    <TITLE>RGB SMD TEST</TITLE>");
               client.println("  </HEAD>");
               client.println("<BODY>");
               client.println("  <H1>RGB SMD TEST</H1>");
               client.println("  <hr />");
               client.println("  <br />");

               client.println("  <form method=\"GET\">");
               client.println("    <label>Green (000 to 255)</label><br/>");
               client.print("    <input name=\"three\" value=\"");
               client.print(pinThree);
               client.println("\"><br/>");
               client.println("    <label>Red (000 to 255)</label><br/>");
               client.print("    <input name=\"five\" value=\"");
               client.print(pinFive);
               client.println("\"><br/>");
               client.println("    <label>Blue (000 to 255)</label><br/>");
               client.print("    <input name=\"six\" value=\"");
               client.print(pinSix);
               client.println("\"><br/>");
               client.println("    <input type=\"submit\" value=\"Set color\"/>");
               client.println("  </form>");

               client.println("</BODY>");
               client.println("</HTML>");

               delay(1);
               client.stop();
               readString = "";
            }
         }
      }
   }
}

void SetColor(int Three,int Five, int Six)
{
   analogWrite(3, Three);
   analogWrite(5, Five);
   analogWrite(6, Six);
   pinThree = Three;
   pinFive = Five;
   pinSix = Six;
}

void ProcessGet(String strGet)
{
   //Three = Green
   //Five = Reg
   //Six = Blue
   String temp = strGet.substring(12,15);
   pinThree = temp.toInt();
   temp = strGet.substring(21,24);
   pinFive = temp.toInt();
   temp = strGet.substring(29,32);
   pinSix = temp.toInt();
   SetColor(pinThree,pinFive,pinSix);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.