December 31

Cheerlights

For the Christmas tree I wanted to have a light controlled by the community via Cheerlights. Unfortunatly the RedFly shield had lots of problems, so I activated my Raspberry Pi, which lay around for have a year. The Raspi installation had no problems; Raspbian, Ethernet with DHCP, Wifi with fixed IP.

The C program to get the cheerlights status:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define URL "curl -s http://api.thingspeak.com/channels/1417/field/1/last.txt"
#define DEV "/dev/ttyACM0"
#define LINELEN 256

int main (int argc, char *argv[])
{
    int j;
    FILE *pp;
    FILE *fp;
    char buffer [LINELEN];
    char r, g, b;

    fp = fopen (DEV, "w");
    if (fp == NULL)
    {
        fprintf (stderr, "Couldn't open "%s".n", DEV);
    }
    sleep (5);
    while (1)
    {
        pp = popen (URL, "r");
        if (pp == NULL)
        {
            fprintf (stderr, "Error opening "%s"n", URL);
        }
        fgets (buffer, LINELEN, pp);
        fclose (pp);
        printf ("---%s---n", buffer);

        if (strcmp (buffer, "red") == 0)
        {  
            r = 255;
            g = 0;
            b = 0;
        }
        else if (strcmp (buffer, "green") == 0)
        {  
            r = 0;
            g = 255;
            b = 0;
        }
        else if (strcmp (buffer, "blue") == 0)
        {  
            r = 0;
            g = 0;
            b = 255;
        }
        else if (strcmp (buffer, "cyan") == 0)
        {
            r = 0;
            g = 255;
            b = 255;
        }
        else if (strcmp (buffer, "purple") == 0)
        {  
            r = 128;
            g = 0;
            b = 128;
        }
        else if (strcmp (buffer, "magenta") == 0)
        {
            r = 255;
            g = 0;
            b = 255;
        }
        else if (strcmp (buffer, "yellow") == 0)
        {
            r = 255;
            g = 255;
            b = 0;
        }
        else if (strcmp (buffer, "orange") == 0)
        {
            r = 255;
            g = 165;
            b = 0;
        }
        else if (strcmp (buffer, "warmwhite") == 0)
        {
            r = 255;
            g = 223;
            b = 223;
        }
        else if (strcmp (buffer, "white") == 0)
        {  
            r = 255;
            g = 255;
            b = 255;
        }
        else if (strcmp (buffer, "black") == 0)
        {
            b = 0;
            g = 0;
            r = 0;
        }
        fprintf (fp, "%c%c%c", r, g, b);
        fflush (fp);

        sleep (60);
    }
}

Every minute this programs gets the current cheerlights color and sends the corresponding RGB values to a serial interface. From there the Arduino reads the values and controls a RGB LED

/*   cheerlights - controls a RGB LED from cheerlights.com
 *
 *   2013-12-15   danimath   erzeugt
 *
 **********************************************************/

const byte REDLED   = 6;
const byte GREENLED = 9;
const byte BLUELED  = 3;

int r, g, b, old_r, old_g, old_b;

void setup ()
{
    analogWrite (REDLED, 255);
    delay (500);
    analogWrite (REDLED, 0);
    analogWrite (GREENLED, 255);
    delay (500);
    analogWrite (GREENLED, 0);
    analogWrite (BLUELED, 255);
    delay (500);
    analogWrite (REDLED, 255);
    analogWrite (GREENLED, 255);
    analogWrite (BLUELED, 255);
    delay (500);
    Serial.begin (9600);
    old_r = 0;
    old_g = 0;
    old_b = 0;
    r = 255;
    g = 255;
    b = 255;
}

void loop ()
{
    if (Serial.available ())
    {
        old_r = r;
        old_g = g;
        old_b = b;
        r = Serial.read ();
        g = Serial.read ();
        b = Serial.read ();
        for (int j = 0;   j < 10;   j++)
        {
            analogWrite (REDLED, r);
            analogWrite (GREENLED, g);
            analogWrite (BLUELED, b);
            delay (250);
            analogWrite (REDLED, old_r);
            analogWrite (GREENLED, old_g);
            analogWrite (BLUELED, old_b);
            delay (250);
        }
    }
    analogWrite (REDLED, r);
    analogWrite (GREENLED, g);
    analogWrite (BLUELED, b);
    delay (250);    
}

Now we are connected to the world, and the world can change our Christmas Light.


Tags: , , ,
Copyright © 2014. All rights reserved.

Posted 2013-12-31 by Daniel LeMath in category "Arduino", "Raspberry Pi

Leave a Reply

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