Sunrise Machine with the Tessel 2
Get the Software
If you have git installed and feel comfortable using that tool, open your terminal and run:
git clone https://github.com/bocoup/j5ik-sunrise-machine;
cd j5ik-sunrise machine;
git is not your thing, you can also download the project as a zip file:
You can always find the most up to date content at in the GitHub repository as well.
Extract the contents however you prefer. Open your terminal, and navigate to the extracted project directory.
Install the Software
First, install some dependencies. In your project directory, run the command:
The only code changes you'll need to make from the supplied code with the Sunrise Machine are in its configuration. Copy the included
config.js.example file to a new file named
This configuration object is organized such that the properties you're most likely to change are near the top.
- You'll want to change, at the very least, the
longvalues to the coordinates of where you are located.
- To account for the Tessel 2's proclivity for UTC, you can provide a
-04:00is the offset for daylight savings time on the east coast of the U.S. (EDT)—four hours "behind" UTC. This setting isn't critical—the dates used by the Tessel to figure out to record will still be accurate, but if you want logging and folder names for your images and movies to use your local time zone (i.e. not UTC), change this value.
autoSchedulecan be set to any of suncalc's sun event names, or can be set to
falseto disable scheduled recording (you can always trigger manual movies by pressing the Sunrise Machine's pushbutton).
- If you enable
postToTwitter, you'll need to fill in the Twitter API credentials that follow.
- I encourage you to experiment with the value of
calibrateCamera. If you find that your camera is taking great stills without calibration, you can save power and time by disabling calibration (set this property to
falseto turn it off).
- You can disable the
statusLEDif you like. This can save power and your nerves if you find the light or blinking annoying (or it is reflecting off a window during capture or something).
At this point, your Sunrise Machine is configured and ready. If you're in a hurry, you can skip to the Run It! section.
Exploring the Code
When the Sunrise Machine's code is complete, your project directory will look like this:
language:console ├── av.js ├── config.js ├── node_modules ├── recording.js ├── index.js └── tweet.js
config.js is the configuration file you just created.
node_modules is where the project's dependencies—installed above—live.
The other components of the Sunrise Machine are:
tweet.js, which integrates with the Twitter API and can upload animated GIFs
recording.js, which provides a class that encapsulates the tasks of a time-lapse recording session (scheduling still capture, kicking off the processing of stills into movies, etc.)
av.js, which contains lower-level code to execute the capture and processing of images and video
index.js, which is the code that controls the Tessel's inputs and outputs and pulls it all together. This is the script you'll run on your Tessel.
We encourage you to dig into the module files and see how the pieces all fit together, if you're curious!
Capturing and Processing Video with the Tessel 2: av.js
The image capture, video building and animated-GIF creating powers of the Sunrise Machine are significantly influenced by the existing
tessel-av module, which is a great starting place for grabbing still images or streaming video. Like
tessel-av, the Sunrise Machine's image-capture code takes advantage of the cross-platform
ffmpeg software, which is available for you already on your Tessel. No installation or configuration needed!
ffmpeg is a hugely-powerful command-line tool that can encode and decode (and transcode and mux and demux and stream and filter and play and and and) a dizzying array of different kinds of audio and video. Just learning how to put together commands to do simple video capture and storage can take a while (trust me).
Keep in mind that the Tessel 2 is able to do all this despite its mere 64MB of RAM. It's quite a trooper.
ffmpeg processes using Node.js' built-in
child_process module. The
av module contains an appropriately-titled
ffmpeg function. The
ffmpeg function spawns an
ffmpeg child process with the arguments provided and returns a
Each of the four exported functions (
animatedGIFFromVideo) are intended to fulfill one AV task by invoking the
ffmpeg function with the needed arguments. These functions also return a Promise. Here's a condensed view of the rest of the
Arguments are cobbled together in each function. Here's the entirety of the
captureStill function, for instance:
Note that the
animatedGIFFromVideo function is a two-step process that first creates a palette from a movie and subsequently creates an animated GIF from the movie based on that palette. GIFs are restricted to 256 colors, so using an intelligently-generated palette can result in a higher-quality GIF.
Tweeting Animated GIFS: tweet.js
tweet.js uses the
npm package to communicate with the Twitter API, making the several API calls necessary to upload an animated GIF. Once the GIF is uploaded, it posts a tweet containing the GIF. It exports a function,
index.js can call this function to tweet a GIF.
Taking Care of Details: recording.js
class exported by
Recording, encapsulates some of the tasks of managing a time-lapse recording session, like naming of files and managing timeouts between captures. A
Recording instance can be started with its
start method and canceled with its
cancel method. The
Recording handles the rest, invoking specific functions in the
av module to accomplish tasks like calibration, capturing stills and building movies and GIFs.
Where it all Happens: index.js
We've met the supporting cast. Now let's walk through, in entirety,
index.js, which is the script you'll run directly on your Tessel to make the Sunrise Machine go! Here it is, with lots of comments.