What I Did in January

January turned out to be a good time for tidying up my gitHub repos, because it was cold and there was no incentive to travel, and I had to quarantine for a few days in the midst of it (no symptoms, thankfully). As a result, I updated a few things. All of this is going into my connected devices class this semester.

Just enough HTML for Connected Devices – If a device connects to your phone or laptop, you’re going to need to build an interface for it at some point, and the Document Object Model (HTML/CSS/JavaScript) is a quick way to do it. I put together some examples for various ways to do that. Most of this is a thin wrapper around the Mozilla Developer Network pages, with a focus on connecting. A couple of highlights:

MQTT Examples – I’ve been working on this one since last summer in bits and pieces, but now there’s enough there to be useful. All microcontroller examples were written with the Arduino Nano 33 IoT, though any of the WiFi-enabled Arduinos should do the trick. The browser-based stuff is all in p5.js, but I’ll add some plain vanilla JS examples soon. There are some nice GUI tools available now too, like MQTT Explorer and shiftr.io’s desktop broker. My favorite example in this repo is the WebMIDI to MQTT examples.

Websocket Examples – nothing special here, but the topic comes up enough that having a set of examples on hand was something I needed. Easy to compare and contrast Websockets and MQTT now too. A lot of folks don’t know the ArduinoHTTPClient library can also do websockets, so I threw that in.

Pi Recipes – this is an older repo, but I updated it to make it a bit easier to find things. Quite pleased with the timelapse webcam example that Nun Tansrisakul inspired me to write.

WiFi101/WiFiNINA Examples – another older repo, but a bit better organized now, I hope. Moved some things to their own repos when it was merited.

Display Examples – still working on this one, but I got obsessed with making QR code examples from an Arduino, so I wrote something using Richard Moore’s arduino qrcode library, then duplicated it for ePaper, TFT, and OLED screens. Figured it was as good a time as any to put together what experience I have working with screens and microcontrollers.

HID Examples – not prettied up yet, but put together a few more notes on using the HID examples for Arduino.

Node Examples – also not prettied up yet, but added notes for using these on Glitch or on a CLI.

This Week in Code: MQTT Fun

Work’s been quite busy lately, with several things going on at once, and few things completed. Many pieces of what I am doing are scattered over the web, on my gitHub account, my various class sites, etc. Since I’m expecting students to keep a blog in my classes, I thought it might be useful to revive my own blogs as a place to keep track of some of the projects I’m in the midst of.

I’ve been having a good time making examples using the Message Queueing Telemetry Transfer (MQTT) protocol lately. MQTT is a lightweight networking protocol used in many industrial networking (read: internet of things) applications. There are a number of good libraries out there for it on a variety of platforms, and a couple of useful public brokers (read: servers) that you can test your apps with, like shiftr.io and mosquitto.org. I like Shiftr because of the fancy motion graphic (okay, call me shallow). I’ve got examples for Arduino using the ArduinoMqttClient library and p5.js using the Eclipse PAHO library. I just added one using p5.serialport as well, so you can connect Arduino projects to each other through p5.serialport -> p5.js -> MQTT.

Programming a Christmas Tree

As we planned for Christmas this year, my partner suggested that maybe we should replace the lights for the tree. “While we’re at it,” she asked, “is there anything out there that’s more controllable than just the ones that twinkle on and off?”

“Well,” I said, “there are NeoPixels…”

We decided that it would be great to have a tree where the lights looked and acted like candles, flickering gently in a soft, warm glow, and fading from a bright orange down to a low reddish glow by the end of the evening. We also wanted to realize the cheesy quote from It’s A Wonderful Life that “every time a bell rings, an angel gets its wings.”

Here’s a timelapse of the tree in action:

When you ring the bell, this happens:

The process of making it gave a chance to work with:

You can find the code and circuit in the gitHub repository for this project.
Continue reading “Programming a Christmas Tree”

Node.js on the Arduino Yún

Recently, Federico Fissore added node.js to the package repository for the Arduino Yún. Here’s how you get node to communicate with the Arduino processor on the Yún via the Bridge library.

To do this, you’ll need an Arduino Yún, a microSD card, a microUSB cable and a wifi connection. You should be familiar with the basics of the Arduino Yún and node.js in order to get the most out of this post.

All of the code for this post can be found on my GitHub repository.

Continue reading “Node.js on the Arduino Yún”

BlinkyTape using NeoPixel Library

Blinkinlabs is shipping their product now after their successful KickStarter campaign, and I got mine in the mail a couple days ago. I had a few minutes and thought I’d get a basic example up and running. The Blinkinlabs documentation was confusing to me, however. The “Simplest BlinkyTape Sketch” wasn’t. The code was more complex than I had hoped, and I couldn’t find documentation of the API, but I noticed there were some examples that used the Adafruit NeoPixel library. Those examples didn’t run for me, but I figured if there were NeoPixel examples in the BlinkyTape repository, it should be possible to run the thing with that library.

A quick look at the schematics for BlinkyTape confirmed that it was indeed running the Atmega32U4, same as the Arduino Leonardo (their documentation’s good on setting the tape up as a Leonardo). Furthermore, the data in of the lights is connected to pin 13.  From there, it was simply a matter of modifying the Adafruit NeoPixels to work on pin 13.

Like the BlinkyTape examples, most of the Adafruit NeoPixel library examples were all about color scrolling. I dislike color scrolling. Call me old and cranky, but I remember a time when lighting designers made color choices and stood by them; when the designers of lighting tools gave us controls that let us choose those colors easily. I’m hoping that someday we’ll look back on this era  see color scrolling as a lighting design choice the same way as we see the mullet in hairstyle choice: a bad idea we had to go through to get to something more pleasant. Now you kids, get outta my yard! </cranky old man rant>

My sketch works like this: you send a serial string to the Arduino starting with a capital C, followed by the pixel number, the red level (0-255), the green level (0-255) and the blue level (0-255). The NeoPixel library then sets the color of the pixel you chose. That’s it.

Now that I know this thing’s so simple to use, I can see a lot of projects with it. Love the built-in processor too, makes it even more convenient. Makes me want to order a whole lot more NeoPixels from Adafruit too.

Here’s the code. You can also get it from gitHub.


[github_cv url='https://github.com/tigoe/NeoPixel_examples/blob/master/BlinkyTape/BlinkyTape.ino']

Sending Data Serially To Arduino

One question I get all the time is this:

I know how to send data from Arduino to (Processing, OpenFrameworks, etc). But how to I send data from (Processing, OpenFrameworks, etc) to Arduino?

Many people don’t seem to know that Arduino has built-in functions for parsing streams of data. Back with version 1.0.1 Michael Margolis’ excellent TextFinder library was merged into the core Stream library. So you can send an ASCII string like this:

123, 456, 789

And Arduino can read it and convert the numeric characters back into numeric values.

For example, let’s say you want to send a string to set the values of two LEDs that you’re going to fade using the analogWrite() command. Your data might look like this:

P1, 255\n
P2, 127\n

(the \n represents a newline character, ASCII 10)

Using the parsing functions, you can look for the initial P character, then parse the numeric string that follows it until a non-numeric character (like the comma) comes along. Then you can do another parse until the next non-numeric character (like the newline at the end of the line) comes along. It’s as simple as this:

if (Serial.find("P")) {
    // read and parse characters before the comma:
    int ledNumber = Serial.parseInt(); 
    // read and parse characters after the comma:
    int brightness = Serial.parseInt();
}

Here’s a full example.

So the next time you’re trying to figure out how to read data in Arduino, first decide what your data looks like, then get to know the Stream functions. In addition to find() and parseInt(), there’s also readBytes(), readBytesUntil(), parseInt(), parsefloat(), and a few other goodies.

The really nice thing about these functions is that they work on any library based on Stream. So, for example, you can also use them on the Ethernet and WiFi and GSM Client and Server classes.  This makes parsing network data much simpler.

 

Using REST as a command protocol for web-to-serial applications

Updated 19 Jan 2014

The address you type into your browser’s address bar is often the location of a particular document on a server. For example, http://tigoe.net/index.html refers to an HTML document living in the main directory of my server, tigoe.net. But URLs can be used to represent more than a file address. They can also be used to set or get the state of web-based application. For example, http://www.mystore.com/item/3045/price could be used to tell the application you want the price of item 3045. If you want the set the price, you could use http://www.mystore.com/item/3045/price/4.99. Web frameworks like Sinatra (for Ruby), Flask (for Python) and Express (for JavaScript through node.js) make it possible for you to build a web server that uses Representational State Transfer, or REST, as the control protocol for your application.

REST is a style of formatting URLs such that the URL itself describes the state of the thing it’s addressing. In the store example above, the URL is a representation of the item in the store (http://www.mystore.com/item/3045/price).  To change the state of the item, you use another URL to represent that change (http://www.mystore.com/item/3045/price/4.99).  At its simplest, REST means organizing your web application so that the URLs provide clear, sensible meaning as to what’s going on. For a more detailed explanation, see Understanding REST or Building Web Services the REST way. In this post, you’ll learn how to use a RESTian scheme as a communications protocol between a microcontroller and a web page using node.js and a little client-side JavaScript in the middle.

Continue reading “Using REST as a command protocol for web-to-serial applications”

Serial to JSON in Node.js and Arduino

Node.js is great for making web services, and node-serialport makes it very easy to connect to serial devices on your computer. In my last post, I showed how to connect an Arduino microcontroller application to a web page using Node.  This post expands on that, introducing how to use JavaScript Object Notation (JSON) from Arduino all the way through to your HTML page.

The beauty of node is that it’s JavaScript, so you get to use everything Javacript gives you, including its wonderful lightweight data format, JSON, or JavaScript Object Notation. JSON describes data objects using arrays of key-value pairs separated by colons. If you want to add more properties to an object, just add another array element. the value of an element can be a JSON object itself. When you’ve got an application that uses JavaScript on the server side and on the client side, it makes sense to use JSON to describe your data all the way through, so you can just pass it around without a lot of conversion.

Arduino doesn’t speak JSON natively. There are a couple JSON parser libraries out there for Arduino, but I haven’t seen one that I like yet. All of the ones I’ve seen expect more pointer knowledge from the user than I’d like.  So for this example you’ll assemble your own JSON string using Arduino’s String class, and letting node.js turn it into a JSON object.

To make this happen you’ll need:

This post assumes you understand:

  • How to use Arduino, including how to send serial data
  • How to write a web page, and what a server is
  • A little about JavaScript
  • A little about the command line
  • Beginning experience with node.js.  For my brief intro, see this post.

Continue reading “Serial to JSON in Node.js and Arduino”

Serial to Browser using node.js

Updated 28 July 2015

This is a brief introduction to using node.js and websockets to connect a serial device, like an Arduino microcontroller, to a browser.

To make this happen you’ll need:

This post assumes you understand:

Datalogging with Arduino

There are several ways to save data from a sensor attached to an Arduino. If you’re connected to a personal computer, you can simply send the data from the Arduino to the personal computer serially, and save it to a file. If you’ve got an SD card attached to the microcontroller, you can save the data to the card. Or, if you have access to the internet and a device that can connect to a server, you can save the data to a server. In the tutorial below, you’ll read a DHT11 temperature and humidity sensor and log data in three ways:

  • Serial transmission to a personal computer, and serial capture to a file.
  • Saving data to an SD card mounted on the Arduino
  • HTTP upload to xively.com (formerly pachube.com) via an Ethernet shield or Ethernet Arduino.

Hardware you need:

  • personal computer
  • Ethernet Arduino and USB-to-serial connector, or Arduino Uno and Ethernet shield (or equivalents)
  • SD Micro card
  • DHT11 temperature and humidity sensor
  • 10-kilohm resistor

Software you need:

Concepts you should know:

  • Basic understanding of Arduino code
  • Basic electrical concepts
  • Serial communication concepts
  • How to install libraries in Arduino
  • HTTP request concepts

For more on the DHT sensors, see Adafruit’s tutorial.

To get started, install the Arduino IDE and download the DHT library. Unzip the library and change the directory name to DHT, then copy it to the libraries/ directory of your Arduino sketch directory. If this is your first time using Arduino, the default location is in your user directory, called Arduino/ . You might have to create the libraries directory inside the sketch directory. Then download the example sketches from my gitHub repository, unzip them and save them to your sketch directory as well. Then start the Arduino IDE.

Connecting the sensor

Connect the DHT11 sensor to the Arduino as follows:

  • Vcc (pin 1) – Digital pin 8
  • Output (pin 2) – Digital pin 7
  • Ground (pin 4) – Digital pin 5

Since the DHT11 sensor uses very little current, you can use the output pins of the microcontoller to power the sensor, using pins 5 and 8. Then attach the output pin of the sensor to pin 7. The output pin will also need a pullup resistor so that it goes high when no data is transmitted. To do this, connect the 10-kilohm resistor from pin 8 to pin 7, or to the +5V pin.

Serial Data Capture to a Graph

The simplest way to get data off the sensor is to print the results out serially and capture them directly on your computer. To make this happen, you’ll need your Arduino attached via a USB-to-serial connection. The sketch SerialTempHumidityReader uses the Adafruit DHT library to read the sensor and sends the results back to the computer serially. Download it and copy it into a new window in the Arduino IDE.

To upload the sketch to your board, connect the board to the computer via USB-to-serial, and look for the board type in the Tools > Board menu. Assuming you’re using an Arduino Ethernet, choose that:

To find your serial port, check the Tools >Serial Port menu with the USB-to-serial adapter NOT connected. Then plug the adapter into your computer again and check the menu again.  The new port that shows up is your USB-to-serial adapter’s port.

Once you’ve selected the board type and the port, upload the sketch by clicking the upload button in the toolbar, or ctrl-U:

When you’ve got the sketch loaded, open the serial monitor by clicking the Serial Monitor button on the right hand side of the toolbar.

You should see readings like this:

The sketch is outputting tab-separated data, and prints out column headers at the beginning.

When you’ve got several readings, click in the serial monitor window, select all (ctrl-A), and copy (ctrl-C). Then open a blank spreadsheet in OpenOffice and paste the results into the sheet. When the Text Import dialog comes up, make sure you click Separated by Tab in the Separator options:

Select the two columns of numbers, and click the graph icon. In the Graph dialog box, choose Line type, the Lines Only option, then click Finish. Your graph will be inserted in your spreadsheet. Voila, graphing made simple!

Serial Data Capture to a File

If you have a serial terminal program like CoolTerm on your computer, you can also capture to a file. To do so, open CoolTerm, and choose your serial port in the Options menu. Click the Connect icon, then from the Connection Menu, choose Capture to TextFile… and Start. Give your file a name and save.

Note: if you want the file to start from the beginning of your sketch, hold down the reset until you’ve started capture.

To stop capture, choose Connect > Capture to TextFile… > Stop. Then you can use your file in any application you want. Change the file extension to .csv and you can open it in a spreadsheet and graph as above.

Saving Data to an SD Card

Sometimes you want to save data when you’re not connected to a personal computer. Attaching an SD card to an Arduino is fairly straightforward. There are several different shields that have SD cards on board, and the Arduino Ethernet has an SD card right on the main board. The SD card library makes it simple to save files to your SD card.

Format a microSD card as FAT16 or FAT32 on your personal computer, and load it onto your Arduino. Then upload the SDCardDataLogger sketch. This sketch reads the sensor using the Adafuit DHT library, and if there is an SD card present and initialized, it saves the results to a file called “DATALOG.CSV”. Transfer this file to your computer and use it as you wish. You can open it in a spreadsheet and graph it as you did above, or anything else you wish.

Posting data to Xively.com

If your Arduino is connected to the internet via Ethernet, you can connect to pachube.com and program it to post data there. Then you can use Pachube’s data storage and graphing tools to save and visualize your data.

Set up a xively.com account. Click Create a Feed. Give it a name, add any descriptive tags you want, and add a location if you wish. Add two datastreams called rH (relative humidity) and temp (temperature) as follows:

Write down the feed number and fill it into the feed variable in the sketch (PachubeDataLogger). You’ll also need your API key, which is a long string that identifies you to pachube.com. Click My Keys from the pachube menu to get your key. Copy it into the apiKey variable in the sketch below.

Upload this sketch to your Arduino, and connect the board to the net via Ethernet. Open the serial monitor. When the sketch starts, it will look for an IP address using DHCP. Then every ten seconds, it will read the sensor and try to upload to Pachube. A successful upload will print out something like this:

192.168.2.6
rH,26.00
temp,30.00
connecting...
data uploaded
HTTP/1.1 200 OK
Date: Sat, 16 Feb 2012 10:03:14 GMT
Content-Type: text/plain; charset=utf-8
Connection: close
X-Pachube-Logging-Key: logging.G5DkQtnxEdwyKbk0KSKI
X-PachubeRequestId: 0b4efe30282b715276cf1f849f7123ed3eaba234
Cache-Control: max-age=0
Content-Length: 1
Age: 0
Vary: Accept-Encoding

When you look at your feed, you’ll see a graph of your temperature and humidity.

Now you’ve got several methods to log data from an Arduino. You can change the sensor to any sensor you wish, and revise the sketches shown here to read that sensor instead of the DHT11. Enjoy!

Update

I’ve added code to the repository for this tutorial that shows how to use the Pachube code with a BMP085 barometric pressure sensor. Thanks to Adafruit for the excellent library for this sensor.  I’ve also added an example showing how to use the BMP085 with the SD card, and with a Realtime Clock. Again, Adafruit’s library for the RTC makes this easy.

Update 2

Pachube became xively.com, and then they became part of the Google IoT Cloud platform, so I can’t vouch for the validity of any of the pachube examples on this page now. They may work, if xively/Google didn’t change the API.