Skip navigation

Arduino

8 Posts authored by: Frederick Vandenbosch Top Member

A colleague recently made an Arduino-based thermometer to measure how hot it gets in the office. I thought I'd recreate and document the project with the components I have at hand

 

Circuit

 

The circuit is based around the TMP36 temperature sensor. It's cheap, easy to use and doesn't require any external calibration. The voltage output is linearly proportional to temperature in degrees Celsius, making the conversion easy to calculate.

Any Arduino or microcontroller having at least one analog input pin can be used to make the reading. Finally, some form of display is required. This could be a 7-segment display, a character LCD or even an OLED display.

 

I'll be using an Arduino Micro combined with a small OLED display.

 

The resulting circuit is the following:

Screen Shot 2015-07-04 at 13.05.30.png

 

Code

 

The temperature sensor can be used without any additional libraries, the analogRead() function and some math will do the trick.

 

The OLED display however, requires two libraries:

 

The completed code looks as follows (comments in the code for explanation):

 

 

Result

 

Now I finally know how hot it is in my room ... Outside temperature is not bad either

photo (16).JPGphoto (17).JPG

 

Extra

 

As requested in the comments, I have made an updated version of the above project. It includes a button to toggle between degrees C, F and K.

 

All temperatures are calculated, and a button connected to an interrupt pin is used to toggle the displayed value.

 

The updated code:

 

 

Some pics:

 

photo 1.JPGphoto 2.JPG

Previous posts in this project:

 

 

Introduction

 

OK, this will be the final post for the Internet of Holiday Lights RoadTest Plus. It's been fun, frustrating, collaborative, etc ...

  • Fun, because I really enjoyed working with the Yun. I love Arduinos and the Yun's wireless programming and network connectivity just make it even better!
  • Frustrating, because I didn't get to where I wanted to be, due to unforeseen difficulties with the build.
  • Collaborative, because of the sharing and teaming up for special features.

 

Get ready for some picture overload! (the gallery function would've come in handy )

 

Building the Tree

 

The biggest (and hardest as turned out) part of this project was the build. I wanted to make a Christmas tree with both classic and modern elements.

 

The idea was to hollow a log and stuff the electronics inside. Unfortunately, hollowing the log did not work as planned, and my tools were not suited for this kind of work. I ended up going for an alternative with a plastic cylinder for the base after having wasted too much time on the log.

IMG_5672.JPGIMG_5724.JPGIMG_5725.JPG

The tree itself was easier. Using threaded rods cuts in different lengths, I created a tree structure with branches. The expectation was to have a long LED strip go from branch to branch in a spiral shape. This lead to the next problem with the build: the strip was not rigid enough to keep the spiral shape and would just hang loosely. To solve that issue, I came up with the idea to use some fence wire to which the LED strip would be attached. That worked.

IMG_5727.JPGIMG_5730.JPGDSCN3171.JPG

Finally, all the wires needed to be guided to the base of the tree and hidden as wel as possible. Zip ties came in extremely handy!

The tree is ready! It's not late for Christmas 2014, it's just ready a bit early for Christmas 2015

DSCN3174.JPGDSCN3175.JPGDSCN3176.JPG

Now, where do I store this until Christmas ... ?

 

Conclusion

 

We are now the proud owners of a digital Christmas tree which can be controlled either manually using our smartphone or tablet or automatically depending on the Christmas song that is currently playing.

 

Oh ... I almost forgot. It also tells me when other fellow e14 members' project is online!

 

This was fun building, I hope you had fun reading!

Previous posts in this project:

 

 

Introduction

 

One of the features I wanted to implement in this project, was to have the Christmas tree change colors and animations based on the title of the song that is being played at that moment.

The idea is to stream from an internet radio station, parse the song title and find certain keywords. Depending on the keyword, a combination of colors and animations would be applied.

 

Music

 

The play the music, I'm using the Raspberry Pi on which I'm running openHAB. Using MPD and MPC it is possible to play an internet radio stream and depending on the channel, obtain the title and performer.

 

Installing MPD (Music Player Daemon) and MPC (Music Player Controller) is easy:

pi@HolidayLights ~ $ sudo apt-get install mpd mpc







 

Once it is installed, it is possible to add an internet radio station to the playlist.

pi@HolidayLights ~ $ sudo mpc add http://helsinki.radiostreamlive.com/radiosantaclaus_mp3-high







 

The next thing to do, is to start playing the item in the playlist:

pi@HolidayLights ~ $ sudo mpc play
http://helsinki.radiostreamlive.com/radiosantaclaus_mp3-high
[playing] #1/1   0:00/0:00 (0%)
volume:  0%   repeat: off   random: off   single: off   consume: off







 

Once the item is playing, the status can be queried. This should provide information on the song as well. This specific command will be useful to parse the song title from.

pi@HolidayLights ~ $ sudo mpc status
Radio Santa Claus: Mariah Carey - O Holy Night
[playing] #1/1   0:03/0:00 (0%)
volume:  0%   repeat: off   random: off   single: off   consume: off







 

OK, with this, the music is taken care of, and the song information is available for further processing ... let's do that now!

 

Parsing

 

To process the song title, I'm using a Python script, called from openHAB using the "exec" binding.

 

The script takes care of parsing the song title from the "mpc status" command's output. This is the script:

 

pi@HolidayLights /opt/openhab/addons $ cat /home/pi/xmasradio.py

#!/usr/bin/env python
import subprocess

output = subprocess.check_output("mpc status | grep 'Radio Santa Claus:'", shell=True, stderr=subprocess.STDOUT)
output = output[output.find("-")+2:]

print output






 

Executing the script manually returns the following:

 

pi@HolidayLights ~ $ sudo ./xmasradio.py
All I Want For Christmas Is You

 

To do the same, but from openHAB, an entry is added to the items and the sitemap to call the script and bind to its output.

 

Item:

String xmasSong "Current song [%s]" (All) {exec="<[/home/pi/xmasradio.py:10000:REGEX((.*?))]"}





 

Sitemap:

        Frame label="Song" {
                Text item=xmasSong
        }





 

The result in the openHAB GUI:

Screen Shot 2015-01-21 at 19.40.27.png

 

Checking the openHAB logs, the song titles are properly updated with every new song:


2015-01-21 18:36:25 - xmasSong state updated to Oh Holy Night
2015-01-21 18:36:38 - xmasSong state updated to Oh Holy Night
2015-01-21 18:36:52 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:37:05 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:37:19 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:37:31 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:37:45 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:37:58 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:38:11 - xmasSong state updated to Visit now: radiosantaclaus.com -
2015-01-21 18:38:31 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:38:49 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:39:06 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:39:21 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:39:40 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:40:02 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:40:16 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:40:32 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:40:47 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:41:00 - xmasSong state updated to When My Heart Finds Christmas
2015-01-21 18:41:14 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:41:28 - xmasSong state updated to Visit now: radiosantaclaus.com -
2015-01-21 18:41:42 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:41:55 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:42:10 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:42:23 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:42:37 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:42:53 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:43:07 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:43:20 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:43:35 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:43:48 - xmasSong state updated to All I Want For Christmas (Is My Two Front Teeth)
2015-01-21 18:44:03 - xmasSong state updated to Sleigh Ride
2015-01-21 18:44:17 - xmasSong state updated to Sleigh Ride
2015-01-21 18:44:30 - xmasSong state updated to Sleigh Ride
2015-01-21 18:44:44 - xmasSong state updated to Sleigh Ride
2015-01-21 18:44:57 - xmasSong state updated to Visit now: radiosantaclaus.com -
2015-01-21 18:45:10 - xmasSong state updated to Sleigh Ride
2015-01-21 18:45:23 - xmasSong state updated to Sleigh Ride
2015-01-21 18:45:36 - xmasSong state updated to Sleigh Ride
2015-01-21 18:45:51 - xmasSong state updated to Sleigh Ride
2015-01-21 18:46:02 - xmasSong state updated to Merry Christmas For Everyone
2015-01-21 18:46:15 - xmasSong state updated to Merry Christmas For Everyone
2015-01-21 18:46:29 - xmasSong state updated to Merry Christmas For Everyone
2015-01-21 18:46:40 - xmasSong state updated to Merry Christmas For Everyone
2015-01-21 18:46:55 - xmasSong state updated to Merry Christmas For Everyone





 

Action

 

With the song title parsed, it's time to do something with it and have some pretty colors light up

 

An openHAB rule is in charge of identifying keywords and changing the colors based on those keywords. The rule could look like this, but can be customised at will:

 

rule "Parse Song Title"
when
        Item xmasSong changed
then
        var song = xmasSong.state.toString.trim
        logInfo("FILE", "Song: " + song)


        if(song.contains("radiosantaclaus")) {
                // do nothing, not a song title
                logInfo("FILE", "Match for 'radiosantaclaus', skip")
        } else if(song.contains("Christmas")) {
                logInfo("FILE", "Match for 'Christmas'")
                sendCommand( TreeLightColor, "255,0,0" )
                sendCommand( TopperLightColor, "0,255,20" )
                sendCommand( TopperLightColor2, "0,0,0" )
        } else if(song.contains("Santa")) {
                logInfo("FILE", "Match for 'Santa'")
                sendCommand( TreeLightColor, "255,0,0" )
                sendCommand( TopperLightColor, "255,255,255" )
                sendCommand( TopperLightColor2, "0,0,0" )
        } else if(song.contains("Snow")) {
                logInfo("FILE","Match for 'Snow'")
                sendCommand( TreeLightColor, "255,255,255" )
                sendCommand( TopperLightColor, "255,255,255" )
                sendCommand( TopperLightColor2, "0,0,0" )
        } else {
                logInfo("FILE","No match")
                sendCommand( TreeLightColor, "0,255,0" )
                sendCommand( TopperLightColor, "255,0,0" )
                sendCommand( TopperLightColor2, "0,0,0" )
        }
end




 

I gradually added more keywords and matching colors as songs played and I discovered more song titles.

The song titles are logged, and when there is no match, this is also reported. This allows me to check back later and add keywords for those songs as well.

 

2015-01-21 20:33:02.148 [INFO ] [org.openhab.model.script.FILE ] - Song: Silent Night
2015-01-21 20:33:03.653 [INFO ] [org.openhab.model.script.FILE ] - Match for 'Night'
2015-01-21 20:34:15.824 [INFO ] [org.openhab.model.script.FILE ] - Song: Visit now: radiosantaclaus.com -
2015-01-21 20:34:16.206 [INFO ] [org.openhab.model.script.FILE ] - Match for 'radiosantaclaus', skip
2015-01-21 20:34:40.592 [INFO ] [org.openhab.model.script.FILE ] - Song: Silent Night
2015-01-21 20:34:40.780 [INFO ] [org.openhab.model.script.FILE ] - Match for 'Night'
2015-01-21 20:36:44.820 [INFO ] [org.openhab.model.script.FILE ] - Song: Frosty The Snowman
2015-01-21 20:36:45.263 [INFO ] [org.openhab.model.script.FILE ] - Match for 'Snow'
2015-01-21 20:37:48.321 [INFO ] [org.openhab.model.script.FILE ] - Song: Visit now: radiosantaclaus.com -
2015-01-21 20:37:48.350 [INFO ] [org.openhab.model.script.FILE ] - Match for 'radiosantaclaus', skip
2015-01-21 20:38:00.437 [INFO ] [org.openhab.model.script.FILE ] - Song: Frosty The Snowman
2015-01-21 20:38:00.596 [INFO ] [org.openhab.model.script.FILE ] - Match for 'Snow'
2015-01-21 20:41:05.943 [INFO ] [org.openhab.model.script.FILE ] - Song: Visit now: radiosantaclaus.com -
2015-01-21 20:41:05.965 [INFO ] [org.openhab.model.script.FILE ] - Match for 'radiosantaclaus', skip
2015-01-21 20:41:31.507 [INFO ] [org.openhab.model.script.FILE ] - Song: Frosty The Snowman
2015-01-21 20:41:31.541 [INFO ] [org.openhab.model.script.FILE ] - Match for 'Snow'
2015-01-21 20:42:45.844 [INFO ] [org.openhab.model.script.FILE ] - Song: Under The Christmas Tree
2015-01-21 20:42:46.168 [INFO ] [org.openhab.model.script.FILE ] - Match for 'Christmas'




 

In the logging above, you'll see some titles return a matching keyword and others don't. There is also the occasional radio channel name appearing instead of the song title. This is filtered out and skipped.

 

The tree happily reacts to the changes in keywords and changes colors automatically:

DSCN3182.JPGDSCN3180.JPGDSCN3186.JPGDSCN3188.JPG

 

I'll be covering the actual build of the tree in my next and final post! Stay tuned! (here: [Christmas Tree] Internet of Holiday Lights - Finished Build)

Previous posts in this project:

 

 

Introduction

 

With the holiday period over, I finally have some time to focus on my projects again.

For this post, I worked on the star which will go on top of my digital Christmas tree.

 

NeoPixel Star

 

I originally intended to use an LED strip, which I would cut into pieces and place in a star shape, but I then came across the NeoPixel Sticks from Adafruit.

One stick has eight LEDs which are placed very close to each other. They seemed perfect for the job.

 

Based on some measurements, I created a simple 3D printed star to test the positioning of the sticks. You'll notice the different branches are a bit off center. That is to compensate for the fact that the pixels themselves are not centered on the stick either.


After that, I modified the star to be able to clip the sticks on and pass the wires for power and data.

IMG_5588.JPG

With the holder tested and fitting, I soldered the connections between the sticks, resulting in one strip consisting of five sticks.

To test the construction, I loaded the default "strandtest" sketch from the NeoPixel library and configured the proper number of pixels.

DSCN3158.JPG

 

 

You can find the final model I designed on thingiverse: http://www.thingiverse.com/thing:624811

Previous posts in this project:

 

 

Introduction

 

I was approached by jancumps some time ago, as he wanted to give the challenge an extra IoT twist.

 

After some messages going back and forth, the conclusion was that we would symbolise each other's project in our own design and have that component light up when the other design was online.

 

 

Heartbeat

 

Both our designs are making use of MQTT messages on the same broker (iot.eclipse.org).

 

We came up with the idea of having our designs send periodic messages (e.g. every minute) on a specific topic with a specific payload as a heartbeat mechanism.

Once a heartbeat is received, a timer is started and the symbol lit up. When a new heartbeat is received, the timer is reset, ensuring the symbol remains lit up.

However, if the timer expires because no heartbeat is received, the symbol is turned off as we assume the other design is offline.

 

Here's the code used to send my heartbeat (I removed the unrelated bits of code):

 

#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <Bridge.h>
#include <YunClient.h>
#include <Wire.h>
#include <Infineon.h>
#include <Console.h>

long isAlive = 0;

YunClient yunClient;
PubSubClient client("iot.eclipse.org", 1883, callback, yunClient);

void setup() {
  Bridge.begin();
  Console.begin();
  Console.println("Connected to console.");
  Wire.begin();

  // connect to MQTT broker and subscribe to topic
  if (client.connect("arduinoYunClient")) {
    client.publish("19e5983adc0f","fvan");
    isAlive = millis();
    delay(50);
  }
}

void loop() {
 client.loop();
  // send heartbeat
  sendHeartbeat();
}

void sendHeartbeat() {
if(millis() - isAlive >= 30000) {
  client.publish("19e5983adc0f","fvan");
  isAlive = millis();
}
}






 

And the code used to process Jan's heartbeat (unrelated bits removed as well):

 

#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <Bridge.h>
#include <YunClient.h>
#include <Wire.h>
#include <Infineon.h>
#include <Console.h>

long checkAlive = 0;

Adafruit_NeoPixel widget = Adafruit_NeoPixel(12, 7, NEO_GRB + NEO_KHZ800);
YunClient yunClient;
PubSubClient client("iot.eclipse.org", 1883, callback, yunClient);

void callback(char* topic, byte* payload, unsigned int length) {
  // Check for messages on subscribed topics
  Console.print("Topic: ");
  Console.println(String(topic));

  if(String(topic) == "1cfee8dd0afc") {
    String value = String((char*)payload);

    if(value == "jancumps"){
      checkAlive = millis();

      Console.println("Widget heartbeat received");
    }
  }
}

void setup() {
  Bridge.begin();
  Console.begin();
  Console.println("Connected to console.");
  Wire.begin();

  widget.begin();
  widget.show();

  // connect to MQTT broker and subscribe to topic
  if (client.connect("arduinoYunClient")) {
    client.subscribe("1cfee8dd0afc");
    delay(50);
  }
}

void loop() {
  client.loop();
  // check heartbeat
  receiveHeartbeat();
}

void receiveHeartbeat() {
  if(millis() - checkAlive >= 120000) {
    // turn widget off
    for(uint16_t i=0; i<widget.numPixels(); i++) {
      widget.setPixelColor(i, widget.Color(0, 0, 0));
    }
    widget.show();
  } else {
    //turn widget on
    for(uint16_t i=0; i<widget.numPixels(); i++) {
      if(i%3 == 0) {
        widget.setPixelColor(i, widget.Color(255, 0, 0));
      } else {
        widget.setPixelColor(i, widget.Color(0, 255, 0));
      }
    }
    widget.show();
  }
}










 

 

Symbol

 

Jan's design is a Christmas wreath. To symbolise that in my own design, I chose a NeoPixel ring lighting up in green and red.

photo 1 (1).JPGphoto 2 (1).JPG

 

Testing

 

To test this new feature, I used MQTT Lens and the Console output of the Yun.

 

MQTT Lens allowed me to confirm both Jan's and my heartbeats were "on air", with the expected payload. The console output on the Yun then confirmed I received and processed Jan's heartbeat as expected.

Screen Shot 2015-01-02 at 14.25.28.pngScreen Shot 2015-01-02 at 13.24.48.png

 

Demonstration

 

Here's a video of my widget being activated by Jan's MQTT heartbeat.

 

I shortened the timeout interval for demo purposes to 30 seconds, so the widget would turn off between heartbeats which are sent every minute. The timeout is otherwise configured to 120 seconds.

 

Previous posts in this project:

 

 

Introduction

 

After setting everything up in last post, I started working on the actual configuration of openHAB and control of the Arduino Yun via MQTT.

 

This is the result so far ...

 

openHAB

 

To be able to control the colors and animations of my digital Christmas tree, I opted for openHAB. I discovered openHAB during the Forget Me Not Design Challenge and like it very much. I find it easy to work with and very versatile (home automation, pet feeding, controlling Christmas lights).

 

In my previous post, I covered the installation and basic configuration. Now, i will document how I've created following interface to control my lights:

Screen Shot 2014-12-23 at 22.16.52.pngScreen Shot 2014-12-23 at 22.16.45.pngScreen Shot 2014-12-23 at 22.17.01.png

 

Items

 

The openHAB documentation on MQTT was sufficient to get me started. It describes how to define items to subscribe to or publish MQTT messages.

 

At this stage, I'm using openHAB to publish values selected in the web interface such as color or animation.

 

Here are some example items (the others are just duplicates of this with other names and topics):

Group All

Color TreeLight "Tree Color" (All)
String TreeLightColor (All) {mqtt=">[eclipse:/fvanTestOpenHAB/tree/color:command:*:default]"}
Number TreeAnimation "Tree Animation" (All) {mqtt=">[eclipse:/fvanTestOpenHAB/tree/animation:command:*:default]"}






 

You'll notice that I've separated the MQTT binding from the color picker. This is because I want to process the color picker's value before sending it over MQTT.

The processing of the color picker's value is done with a rule and then assigned to another item which then triggers the binding.

 

Rules

 

The color picker's default value format was not really usable in my case, so I needed to convert it to something I could use.

Taking the state of the color picker, I extracted the R, G, B and brightness values and put everything in one big string.

 

import org.openhab.core.library.types.*

rule "Set RGB value TreeLight"
when
        Item TreeLight changed
then
        val hsbValue = TreeLight.state as HSBType

        val brightness = hsbValue.brightness.intValue
        val redValue   = ((((hsbValue.red.intValue * 255) / 100) * brightness) / 100).toString
        val greenValue = ((((hsbValue.green.intValue * 255) / 100) * brightness) / 100).toString
        val blueValue  = ((((hsbValue.blue.intValue *255) / 100) * brightness) / 100).toString

        val color = redValue + "," + greenValue + "," + blueValue

        sendCommand( TreeLightColor, color )
end






 

The processed data is then sent to the correct variable, triggering the MQTT binding.

 

Sitemap

 

Finally, there is the sitemap, in charge of the layout of the web interface. This configuration defines how items are ordered in the GUI.

The configuration is very basic and categorises everything per LED strip. You've seen the result in one of the screenshots above.

 

sitemap xmas label="Holiday Lights" {
        Frame label="Topper" {
                Colorpicker item=TopperLight icon="slider"
                Colorpicker item=TopperLight2 icon="slider"
                Selection item=TopperAnimation mappings=[0=OFF, 1=Random_position, 2=Side_fill, 3=Middle_fill, 4=Alternating_colors]
        }
        Frame label="Tree" {
                Colorpicker item=TreeLight icon="slider"
                Selection item=TreeAnimation mappings=[0=OFF, 1=Pulse_slow, 2=Pulse_fast]
        }
}






 

Retain

 

One more thing ... Because some of my functions (animations) take longer to execute, I might miss certain MQTT messages. Also, if power is lost, no state is kept in the Arduino to restore colors and animations.

 

To remedy this, I enabled the option to "retain" messages. This will ensure the MQTT broker will keep the messages even after being sent, so that new or late subscribers are sent the messages again instead of having to wait for an update.

 

This is change in the "openhab.cfg" file:

 

# Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.
mqtt:eclipse.retain=true


 

 

Arduino

 

Benjamin Cabé did a great job to help us forward with his post on MQTT with the Paho client.

 

Still, I decided to give another library a try, not because I'm stubborn (perhaps a little bit ) but because I believe in multiple solutions to a certain problem.

This is why I used the following library: Arduino Client for MQTT « knolleary

 

The library is easy to use. The following bit of code reflects the general use:

 

#include <PubSubClient.h>
#include <Bridge.h>
#include <YunClient.h>

YunClient yunClient;
PubSubClient client("iot.eclipse.org", 1883, callback, yunClient);

void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
}

void setup() {
  if (client.connect("arduinoClient")) {
    client.publish("outTopic","hello world");
    client.subscribe("inTopic");
  }
}

void loop() {
  client.loop();
}








 

The hardest part (for me) was converting the received messages in a format I could use to compare/parse/etc ...

As you can see in the code above, the payload and topic are not strings.

 

For example, the MQTT messages sent for color codes, are formatted in RGB format with values ranging from 0, 0, 0 to 255, 255, 255.

 

To parse these, I did the following:

void callback(char* topic, byte* payload, unsigned int length) {
  // check for messages on subscribed topics
  Console.print("Topic: ");
  Console.println(String(topic));

  // check topic to identify type of content
  if(String(topic) == "\/fvanTestOpenHAB\/topper/color") {
    // convert payload to String
    String value = String((char*)payload);

    // split string at every "," and store in proper variable
    // convert final result to integer
    TopperR = value.substring(0,value.indexOf(',')).toInt();
    TopperG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
    TopperB = value.substring(value.lastIndexOf(',')+1).toInt();

    // print obtained values for debugging
    Console.print("RED: ");
    Console.println(TopperR);

    Console.print("GREEN: ");
    Console.println(TopperG);

    Console.print("BLUE: ");
    Console.println(TopperB);
  }
}








 

I repeated the "if" statement for every topic and am now able to receive all the messages required to apply color and animation of both strips.

 

 

Troubleshooting

 

During testing of this project, there are two "tools" I'm using quite often: MQTT Lens and Arduino Console.

 

MQTT Lens

 

MQTT Lens is an MQTT client capable of subscribing to and publishing MQTT messages.

Screen Shot 2014-12-23 at 21.04.04.png

 

Using this tool, I was able to verify the following:

  • MQTT messages are being sent properly
  • the content of the messages is correct
  • the content is published for the correct topic

 

Arduino Console

 

The Arduino Yun makes it possible to upload sketches wirelessly, so it must be able to debug wirelessly as well.

 

I initially tried to use "Serial.println()" statements for debugging, but that only works when connected to the Yun via USB.

After some digging around, I found the command to be used is very similar: "Console.println()".

 

In short, you can use the console as follows:


#include <Bridge.h>

#include <Console.h>


void setup() {
  Bridge.begin();
  Console.begin();
}

void loop() {
  Console.println("Output");
}











When placed at relevant places in your code, the output becomes very useful:

Screen Shot 2014-12-23 at 21.44.16.png

As you can see in the screenshot above, I'm using the console to verify the incoming MQTT messages on both topic and content.

This is very useful to know if the received content is parsed properly before being passed to the next function.

 

 

Demo

 

Finally, here's a short video demonstrating the control of both analog and digital LED strips connected to the Arduino Yun via openHAB and MQTT.

 

 

 

Happy Holidays

 

If you've come this far reading this blog post (or if you skipped ahead ), I'd like to wish you and your family happy holidays! May they be fun and festive.

 

I'll be back with more blogs for you to read in 2015! But in the mean time, enjoy this painting my 4yo daughter made:

kerstman.png

Previous posts in this project:

 

 

Introduction

 

This post is mainly about setting a lot of things up: the Arduino Yun, the Infineon Shield, LED strips and the Raspberry Pi.

 

You can jump to the topic of interest using the table of content links above. Enjoy!

 

Arduino

 

Intro

 

This will be my first time working with the Yun. The thing that prevented me from getting one, was mainly the price: at approximately €70, it is not cheap!

But thanks to this Road Test, I can finally find out what the Yun is all about.

 

The UNO on the other hand is much more affordable (€30). It does not have the networking capabilities built-in, but is a great starting point for anyone interested in microcontrollers.

I've worked with it many times before, but haven't planned to use it in this project.

 

IDE

 

To find out more about the Yun, I headed over to the Arduino website's "getting started" page. At some point, it is mentioned that a special beta version of the IDE is to be used when working with the Yun.

Screen Shot 2014-12-16 at 20.29.53.png

Download and installation were straightforward.

 

 

OpenWrt

 

When scrolling further down the page, to the "other software" paragraph, there is another piece of software relevant for the Yun: an updated openWrt image.

Screen Shot 2014-12-16 at 20.32.43.png

OpenWrt is a linux distribution for embedded devices. If you want to learn more about OpenWrt, go to https://openwrt.org/.

 

The OpenWrt image for the Yun can be installed in two ways:

  • using the web interface
  • using the command line interface (via SSH)

 

You can find both procedures described in detail here: Arduino - YunSysupgrade

 

There are two ways to connect to the Yun:

  • connect an ethernet cable to the Yun and have it receive an IP address via DHCP
  • connect to the Yun wifi access point

Screen Shot 2014-12-16 at 21.00.07.png

 

Once connected via either way, you should be able to access http://arduino.local/

 

The following page should appear:

Screen Shot 2014-12-16 at 20.52.45.png

 

The password is not mentioned in the procedure. I first tried to log in without entering any password, but that was refused. Next, I tried "arduino" and I was in. Lucky guess I suppose

Screen Shot 2014-12-17 at 09.29.01.png

I pressed the big "RESET" button at the bottom of the page and waited for approximately 3 minutes for the upgrade to finish.

Screen Shot 2014-12-17 at 09.30.35.png

Once the onboard WLAN LED stopped blinking, I tried to connect to the Yun via the wired network. Unfortunately, that wasn't working, so I connected to the Yun's access point instead.

 

On the web interface, it reported the wired network was disconnected. I removed the ethernet cable and plugged it back in. The Yun then reported it was connected to the wired network. Strange ...

Screen Shot 2014-12-17 at 09.38.47.pngScreen Shot 2014-12-17 at 09.39.46.png

 

But, the Yun is now ready to go!

 

 

Infineon Shield

 

The infineon Shield comes without headers on the board, and none are included in the box either.

I find that rather strange. Someone buying a product usually expects to be able to use it right away, or at least have all the parts to do so.

 

On the other hand, this gives you the opportunity to choose your own set of headers: long, short, stackable, ...

 

I went for the extra long stackable headers, which I found online. I wanted to make sure there was enough clearance for the Yun's vertical USB port.

photo 1.JPG photo 2.JPG

After soldering, I checked the shield with both the UNO and the Yun. Clearance was good!

photo 3.JPGphoto 4.JPG

 

One thing I missed is that my headers didn't were not for the correct number of pins, leaving the I2C pins unconnected.

And let that be the two pins required for communication with the Yun. So I came up with a small workaround to fix that.

photo (8).JPG

 

Anyway, after the hardware, comes the software part.

 

It seems there is no library from Infineon available for this shield. They have a variety of examples on their website though.

Unfortunately, all example files have a ".exe" extension. Why ?? What about Linux and Mac OSX users? Plain ".ino" files would have sufficed.

 

Luckily, peteroakes came to the rescue with following blog post: BYOB Party #3, Infineon Library Available

He created a library to make our lives easier! Thank you, Peter!

 

I created a folder called "Infineon" in the libraries folder, and put his library files there. As such:

Screen Shot 2014-12-17 at 16.15.41.png

 

After restarting the Arduino IDE, the example was available.

Screen Shot 2014-12-17 at 16.16.57.png

 

 

Lights

 

I have to LED strips to experiment with:

  • a non-addressable, 12V, 100cm, 60 LEDs strip (analog)
  • an addressable, 5V, 100cm, 60 LEDs strip (digital)

 

DSCN3155.JPGDSCN3156.JPG

 

The analog stip will be controlled using the Infineon shield. The digital strip will be controlled directly by the Yun, using Adafruit's NeoPixel library.

 

I ordered more online, but until then, this will have to do. Experimenting with both strips will allow me to prepare the code until the new strips arrive.

 

Analog

 

Connecting the analog LED strip to the Infineon shield was easy.

 

I stripped off the connector, tinned the wires and plugged them into the socket. On the other side, I did the same with a barrel jack connector.

photo (10).JPG

 

The barrel jack connector is then attached to a 12V power supply.

 

Digital

 

I connected the digital LED strip to the Yun as follows:

Screen Shot 2014-12-17 at 11.28.42.png

The nice thing about these digital strips, is that they run on 5V, which can be used to power the Yun simultaneously. On top of that, the strip only requires one pin to be controlled!

 

I've prepared some animations for this strip:

  • setting the even LEDs to a color and the odd ones to another and then toggling
  • gradually fill up the LED strip with random colors
  • ...

 

Here's a video of the animations in action:

 

 

The functions I created are the following, feel free to use/improve them if you want

 

As jancumps already pointed out to me, yes, there is a tiny bug in the last animation where the last LED doesn't light up. I'll fix that by next post

 

Function 1

// alternate between two colors on even and odd LEDs
void alternateColor(uint32_t c1, uint32_t c2, uint8_t wait) {
  for(uint16_t i=0; i<dstrip.numPixels(); i++) {
    if(i%2 == 0) { // set even LED to color 1
      dstrip.setPixelColor(i, c1);
    } else { // set odd LED to color 2
      dstrip.setPixelColor(i, c2);
    }
  }

  dstrip.show(); // apply the colors
  delay(wait);

  for(uint16_t i=0; i<dstrip.numPixels(); i++) {
    if(i%2 == 0) { // set even LED to color 2
      dstrip.setPixelColor(i, c2);
    } else { // set odd LED to color 1
      dstrip.setPixelColor(i, c1);
    }
  }

  dstrip.show(); // apply the colors
  delay(wait);
}










 

Function 2

// gradually fill up the strip with random colors
void randomColorFill(uint8_t wait) {
  clearStrip();

  for(uint16_t i=0; i<dstrip.numPixels(); i++) { // iterate over every LED of the strip
    int r = random(0,255); // generate a random color
    int g = random(0,255);
    int b = random(0,255);

    for(uint16_t j=0; j<dstrip.numPixels()-i; j++) { // iterate over every LED of the strip, that hasn't lit up yet
      dstrip.setPixelColor(j-1, dstrip.Color(0, 0, 0)); // turn previous LED off
      dstrip.setPixelColor(j, dstrip.Color(r, g, b)); // turn current LED on
      dstrip.show(); // apply the colors
      delay(wait);
    }
  }
}










 

Function 3

// pick a random LED to light up until entire strip is lit
void randomPositionFill(uint32_t c, uint8_t wait) {
  clearStrip();

  int used[dstrip.numPixels()]; // array to keep track of lit LEDs
  int lights = 0; // counter

  for(int i = 0; i < dstrip.numPixels(); i++){ // fill array with 0
    used[i] = 0;
  }

  while(lights<dstrip.numPixels()-1) {
    int j = random(0,dstrip.numPixels()-1); // pick a random LED
    if(used[j] != 1){ // if LED not already lit, proceed
      dstrip.setPixelColor(j, c);
      used[j] = 1; // update array to remember it is lit
      lights++;
      dstrip.show(); // display
      delay(wait);
    }
  }
}










 

Function 4

// Light up the strip starting from the middle
void middleFill(uint32_t c, uint8_t wait) {
  clearStrip();

  for(uint16_t i=0; i<(dstrip.numPixels()/2); i++) { // start from the middle, lighting an LED on each side
    dstrip.setPixelColor(dstrip.numPixels()/2 + i, c);
    dstrip.setPixelColor(dstrip.numPixels()/2 - i, c);
    dstrip.show();
    delay(wait);
  }

  for(uint16_t i=0; i<(dstrip.numPixels()/2); i++) { // reverse
    dstrip.setPixelColor(i, dstrip.Color(0, 0, 0));
    dstrip.setPixelColor(dstrip.numPixels() - i, dstrip.Color(0, 0, 0));
    dstrip.show();
    delay(wait);
  }
}










 

Function 5

// Light up the strip starting from the sides
void sideFill(uint32_t c, uint8_t wait) {
  clearStrip();

  for(uint16_t i=0; i<(dstrip.numPixels()/2); i++) { // fill strip from sides to middle
    dstrip.setPixelColor(i, c);
    dstrip.setPixelColor(dstrip.numPixels() - i, c);
    dstrip.show();
    delay(wait);
  }

  for(uint16_t i=0; i<(dstrip.numPixels()/2); i++) { // reverse
    dstrip.setPixelColor(dstrip.numPixels()/2 + i, dstrip.Color(0, 0, 0));
    dstrip.setPixelColor(dstrip.numPixels()/2 - i, dstrip.Color(0, 0, 0));
    dstrip.show();
    delay(wait);
  }
}










 

 

Raspberry Pi

 

Intro

 

The Raspberry Pi will be responsible to run openHAB, which will be used to:

  • control the LEDs connected to the Arduino
  • report sensor data from the Arduino

 

The Pi will also be in charge of tuning in to an internet radio channel playing exclusively Christmas music.

 

Raspbian

 

First thing to do with the Raspberry Pi, is to prepare the SD card which will run the operating system.

 

1) Identify the SD card

Fredericks-MacBook-Air:~ fvan1$ sudo diskUtil list
/dev/disk0
  #:                      TYPE NAME                    SIZE      IDENTIFIER
  0:      GUID_partition_scheme                        *251.0 GB  disk0
  1:                        EFI EFI                    209.7 MB  disk0s1
  2:          Apple_CoreStorage                        250.1 GB  disk0s2
  3:                Apple_Boot Recovery HD            650.0 MB  disk0s3
/dev/disk1
  #:                      TYPE NAME                    SIZE      IDENTIFIER
  0:                  Apple_HFS Macintosh HD          *249.8 GB  disk1
                                Logical Volume on disk0s2
                                5642627E-CEC4-458D-AB8D-376EF3FB568C
                                Unencrypted
/dev/disk2
  #:                      TYPE NAME                    SIZE      IDENTIFIER
  0:    FDisk_partition_scheme                        *7.9 GB    disk2
  1:            Windows_FAT_32 boot                    58.7 MB    disk2s1
  2:                      Linux                        7.9 GB    disk2s2



















 

2) Unmount SD card

Fredericks-MacBook-Air:~ fvan1$ sudo diskUtil unmountDisk /dev/disk2
Unmount of all volumes on disk2 was successful



















 

3) Write image to SD card

Fredericks-MacBook-Air:~ fvan1$ sudo dd if=Downloads/2014-09-09-wheezy-raspbian.img of=/dev/disk2 bs=1m
3125+0 records in
3125+0 records out
3276800000 bytes transferred in 1596.406567 secs (2052610 bytes/sec)


















 

4) Unmount SD card

Fredericks-MacBook-Air:~ fvan1$ sudo diskUtil unmountDisk /dev/disk2
Unmount of all volumes on disk2 was successful


















 

The SD card is now ready and can be inserted in the Raspberry Pi. I connected the Pi to the network using an ethernet cable and powered it on.

 

After finding out the IP address via my home router, I SSH'ed to the Pi.

 

Fredericks-MacBook-Air:~ fvan1$ ssh pi@192.168.0.129
The authenticity of host '192.168.0.129 (192.168.0.129)' can't be established.
RSA key fingerprint is 02:4f:ed:b3:42:1d:e5:83:1c:1e:2f:7f:63:83:9c:bd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.129' (RSA) to the list of known hosts.
pi@192.168.0.129's password:
Linux raspberrypi 3.12.28+ #709 PREEMPT Mon Sep 8 15:28:00 BST 2014 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

NOTICE: the software on this Raspberry Pi has not been fully configured. Please run 'sudo raspi-config'

pi@raspberrypi ~ $

















 

There is a clear notice asking to run the raspi-config tool to fully configure the Pi, so I did.

Screen Shot 2014-12-18 at 21.37.00.png

 

I used the "raspi-config" tool to:

  • expand the filesystem
  • enable SSH, I2C, SPI and camera (even though not all are needed for this project)
  • change the hostname
  • update the system

 

openHAB

 

OK, with the system set up and updated, the next step is to install openHAB.

 

Step 1: Download the openHAB files:

pi@raspberrypi ~ $ wget https://github.com/openhab/openhab/releases/download/v1.6.0/distribution-1.6.0-runtime.zip
pi@raspberrypi ~ $ wget https://github.com/openhab/openhab/releases/download/v1.6.0/distribution-1.6.0-addons.zip



















 

Step 2: Deploy the openHAB runtime and addons:

pi@raspberrypi ~ $ sudo mkdir /opt/openhab
pi@raspberrypi ~ $ cd /opt/openhab/
pi@raspberrypi /opt/openhab $ sudo unzip /home/pi/distribution-1.6.0-runtime.zip
pi@raspberrypi /opt/openhab $ cd addons/
pi@raspberrypi /opt/openhab/addons $ sudo unzip /home/pi/distribution-1.6.0-addons.zip
pi@raspberrypi /opt/openhab/addons $ sudo mkdir unused
pi@raspberrypi /opt/openhab/addons $ sudo mv *.jar unused/
pi@raspberrypi /opt/openhab/addons $ cd ../configurations/
pi@raspberrypi /opt/openhab/configurations $ sudo cp openhab_default.cfg openhab.cfg



















 

Step 3: Make it executable:

pi@raspberrypi /opt/openhab $ sudo chmod +x start.sh














To have openHAB start automatically at boot, add following line to "/etc/rc.local":
sudo /opt/openhab/start.sh &



 

MQTT

 

I activated the MQTT binding by moving it from the "unused" folder to the "addons" folder:

 

Then, I configured the broker settings in the "openhab.cfg" file:

 

################################# MQTT Transport ######################################
#
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT# Persistence bundles. Replace <broker> with a id you choose.#
# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
mqtt:eclipse.url=tcp://iot.eclipse.org:1883
# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a default one is generated.
mqtt:eclipse.clientId=fvanPiOpenhab

 

The other parameters were left untouched.

 

OpenHAB still requires additional configuration files such as items and sitemaps. I'll focus on those in my next post.

 

 

Conclusion

 

With all of this already out of the way, I can start diving deeper in the actual configurations and actions of my project.


Introduction


Alright! I've been selected for the The Internet of Holiday Lights RoadTest Plus (or "mini challenge") along with 19 other competitors.


In this post, I will describe my submission and what I intend to realise for this RoadTest.


The idea


My internet holiday lights idea for this Road Test plus is about making a Christmas tree with both classic and modern elements.

 

The classic element would be the base of the tree, where I would hollow a wood log in order to stuff the electronics in (Arduino Yun + Infineon Shield).

The modern element will obviously be the lights. I'd like to use a combination of LED strips (addressable and non-addressable) to create a Christmas tree shape and tree topper.

 

This is a quick sketch of what I have in mind:

photo (6).JPG

 

The tree shape would be controlled by the Infineon shield to control color, brightness and other effects (glowing, fade in/out, ...).

For the tree topper, I'd like to use an addressable strip and neopixel ring to create more granular animations (rotation, inward/outward motion, random, ...).

 

The color of both the tree and the topper would be controlled individually via an app on the smartphone. The app would also allow for an automatic mode where the colors and animations would randomly change.

 

In "manual" mode, everything is controlled by the user via his/her smartphone: selection of colors and animation.

 

One of the types of animation I was thinking of, could be triggered by a local sensor. I've experimented with this before as demonstrated in the video below, using a distance sensor.

 

 

What I'd like to achieve is to have the tree topper have an inward motion when nearing the tree and outward motion when getting further away (or vice versa).

 

When in "automatic" mode, the colors and animations for the tree and the topper would be determined based on data gathered from the internet.

 

An example could be to change colors based on keywords found in song titles. By tuning in on an internet radio channel playing Christmas songs, the lights could adapt based on certain keywords in the title of the song playing at that specific moment:

  • "Let it snow": glowing white
  • "Mr. Santa": alternate between red and white
  • etc ...

 

Research

 

For the communication between the Yun and the smartphone, I was thinking of using MQTT and openHAB, which I would deploy on an earlier project: Internet Radio Player with Raspberry Pi, PiFace CAD and MPD/MPC

IMG_3148.JPG

 

While doing some research for this project, I came across an Arduino library for MQTT that is compatible with both the Yun and the UNO (combined with an ethernet shield).

I couldn't help myself, and already gave it a try with the UNO and ethernet shield.

Screen Shot 2014-11-19 at 10.51.57.pngScreen Shot 2014-11-19 at 10.44.49.png

Using an online MQTT client from 2lemetry, which I found via iot.eclipse.org — Ecosystem I was able to successfully publish data from the Arduino and visualise it online.

This is only part of the solution, as the actual project will also require the Arduino to subscribe to a certain topic in order to retrieve the colors and animations that need to be used.


Screen Shot 2014-11-24 at 20.30.20.pngScreen Shot 2014-11-24 at 20.34.51.png


While exploring MQTT for the CC3200, I discovered that a Paho Arduino client exists. Thanks to one of kartben's posts, I also discovered MQTTlens, to visualise MQTT message easily.

After my submission, Benjamin also posted a nice tutorial on how to use the Paho client with the Arduino Yun. You can find it here.

 

That's my project idea for this RoadTest, hope you like it!

Filter Blog

By date: By tag: