Build

Tessel + PubNub Power IoT in 4 Lines of JavaScript

3 min read Michael Carroll on Sep 22, 2014

UPDATE: Get excited, because PubNub and Tessel are hosting a Hack Day on October 25.

Tessel is a small, powerful microcontroller that runs JavaScript. Since each Tessel contains a built-in wifi chip, internet-enabled JavaScript programs can be run directly from the device, even if it is not connected to a computer. This combination of WiFi and JavaScript means that Tessel works great with PubNub!

How does PubNub make Tessel awesome?

It’s easy to program your Tessel to communicate over the internet, but you can make Tessel even more powerful using PubNub’s global Data Stream network. Using PubNub, Tessels can instantly publish data that they’re collecting on a global scale — for example:

  • The current temperature or humidity from the climate module
  • Changes in orientation detected by the accelerometer module
  • The data from your contactless key-cards through their RFID module

This data can be collected and processed on the Tessel through your JavaScript program, and then pushed immediately to all subscribed devices via the PubNub real-time network.

Show me the 4 Lines of JavaScript!

Here’s a super-simple PubNub/Tessel integration:

In this super-simple example, an LED on your Tessel will toggle on or off whenever a message is received on the channel “tessel-light“. To test this, you can send a message by using the PubNub Developer Console. Simply change the channel name in the Developer Console from my_channel to tessel_light, and click Send (the red button in the message box.)

How do I get this code on my Tessel?

Tessel has an amazingly simple code deployment system, which they have documented in their quick-start tutorial. Briefly, plug your tessel into your computer’s USB port and issue these commands:

  1. brew install node
  2. npm install -g tessel
  3. tessel update
  4. tessel wifi -n [network name] -p [password]
  5. Download the example PubNub gist file: pubnub-tessel.js
  6. tessel run pubnub-tessel.js

At this point, your Tessel should be up and running and connected to your WiFi network. To confirm, you can execute the Tessel blinking lights demo or the PubNub blinking lights demo above.

Publishing to the Internet of Things using Tessel and PubNub

In the above example, we’ve seen how Tessel can consume a PubNub data stream and react to it by blinking an LED. We can also get our Tessel to publish information about its environment, which can be aggregated and acted upon by other devices (including other Tessels!)

Let’s look at an example which detects loud sounds in its vicinity using the ambient module, and notifies the world about them:

If you snap your fingers within a few feet of the Tessel’s ambient module, you’ll be able to see a message sent out over the tessel-light channel notifying you that a sound event has occurred. We could use another channel name for these events, but we chose tessel-light because it works well with our prior LED demo. In fact, if you run this example on one Tessel and the prior LED example on another Tessel, you’ll see the LEDs on the later tessel blink whenever the first Tessel senses an audio event!

Publishing to the Internet of Things using Tessel and PubNub

This is the power of PubNub with Tessel: you can easily build a large number of smart, connected devices and have them communicate seamlessly with each other, whether it is two devices communicating together, or hundreds of millions of devices. PubNub allows your Tessels to communicate with each other and the Internet of Things at scale, without having to worry about resource bottlenecks.

Update: We also have a PubNub-Tessel examples GitHub repo.

0