Wireless RGB lamp - The controller
18 Jun 2012In this part I will show you how to control RGB lamp using Netduino. The basics such as wiring XBee to Netduino and XBee setup are described in my previous article, so I will highlight only most interesting bits.
Method RequestReceived
handles incoming HTTP requests and decides which command send to XBee.
If URL is (your XBee’s IP)/fade/1000 where 1000 is delay in milliseconds, following code is executed. Because highest number we can squeeze to one byte is 255 we need to use two bytes to achieve more suitable range 0 - 65535. We then need to split that number into two bytes as shown below.
short fadeDelay = short.Parse(param[2]); // param[2] is delay in milliseconds
_xBee.SendData(0x0013A200407A26AA, 0xFFFE,
new byte[]
{
20,
(byte) (fadeDelay >> 8),
(byte) (fadeDelay & 0x00FF)
}, null);
Following two commands are simple. Just parse number and send it.
case "mode":
if (param.Length == 3)
{
byte fadeMode = byte.Parse(param[2]);
_xBee.SendData(0x0013A200407A26AA, 0xFFFE, new byte[] { 21, fadeMode }, null);
}
break;
case "color":
if (param.Length == 3)
{
byte color = byte.Parse(param[2]);
_xBee.SendData(0x0013A200407A26AA, 0xFFFE, new byte[] { 22, color }, null);
}
break;
Command colorx takes 3 arguments. So URL will look like this (your XBee’s IP)/colorx/(0-255 red)/(0-255 green)/(0-255 blue)
byte colorR = byte.Parse(param[2]);
byte colorG = byte.Parse(param[3]);
byte colorB = byte.Parse(param[4]);
_xBee.SendData(0x0013A200407A26AA, 0xFFFE, new byte[] { 23, colorR, colorG, colorB }, null);
Last command sends just one byte which requests current settings. When it’s received method XBeeFrameReceived
gets called. You can put breakpoint into the empty case
statement or print incoming data. Format is described in table below.
_xBee.SendData(0x0013A200407A26AA, 0xFFFE, new byte[] { 24 }, null);
Commands
ID | Name | Data | Description |
---|---|---|---|
20 | Fade delay | high byte, low byte | 0 - 65535 milliseconds |
21 | Mode | mode id | 0 - disabled, 1 - single color, 2 - fading, 3 - strobe |
22 | Color | color id | 0 - Red, 1 - Green, 2 - Blue, 3 - Yellow, 4 - Cyan, 5 - Orange, 6 - Magenta |
23 | Color | red byte, green byte, blue byte | 0 - 255 for each color |
24 | Status | returns current settings in format: 25, mode, [fade delay] high, low, [color] R, G, B |
Source code
Full source code is available on GitHub.com.