IoT

Publishing Temperature Data in Real time with Tessel Climate

0 MIN READ • Apr 21, 2015

Welcome! We hope you’re excited to hack on the Tessel with PubNub. The Tessel board, a small microcontroller that runs JavaScript, can be connected to several modules and sensors. In this blog post, we’re going to use the Tessel Climate Sensor and PubNub to send real-time temperature and humidity data over a WiFi network.

In Part Two of this series, we're going to receive that climate data and visualize it on an interactive, live-updating chart.

This is the Tessel in action:

Climate sensors are important. They regulate our heat and air conditioning. They make sure your tropical fish stay warm. Climate sensors also regulate greenhouses. Any time you need to grow or take care of something by regulating environmental factors, you need a climate sensor. For example you may use them to build a custom spray system for your plants.

Intial Set Up

Depending on your system and network connection, follow the steps to install node.js and Tessel.

In your Tessel project directory:

  • brew install node
  • npm install -g tessel
  • tessel update
  • npm init

Then follow the steps to connect to WiFi. The Tessel board does not support 802.11n or channels 12 – 14, so you may need to set up a WiFi hotspot or find another network.

  • tessel wifi -n [network name] -p [password] -s [security type*]
  • tessel -l to confirm that you’re connected successfully

Install the PubNub library with npm install pubnub.

Be sure to check your climate module number, we’re using si-7020. You may also have si-7005. Install the appropriate module:

  • npm install climate-si7005
  • npm install climate-si7020

tessel climate

tessel climate

When connecting the Tessel Climate module to the Tessel board, use port B or D to avoid thermal effects of Tessel itself. In this example, we’re using port D. If one port isn’t working for you, try the other.

tessel climate

Writing the JavaScript to Control the Tessel

To begin, we require Tessel, the climate module, and specify the port (B or D).

1.js
(function() {
  var PUBNUB, channel, climate, climatelib, tessel, uuid;
  tessel = require('tessel');
  climatelib = require('climate-si7020');
  climate = climatelib.use(tessel.port['D']);

We’ll also define the channel and uuid.

2.js
channel = process.argv[2];
uuid = tessel.deviceId();

Next, we initialize the PubNub service. To get your unique pub/sub keys, you’ll first need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Dashboard. The Free plan gives you up to 200 MAU or 1 million transactions a month to build, test, and launch your messaging app.

3.js
PUBNUB = require("pubnub").init({
  publish_key: "publish_key_here",
  subscribe_key: "subscribe_key_here",
  uuid: uuid
});

tessel climate

Now, we are ready to start using the climate sensor. When the sensor is ready, we will want to be notified in the console.

4.js
climate.on('ready', function(version) {
  console.log('Ready to read climate sensor');

We’re going to read the temperature and humidity every twenty seconds by setting an interval. Once we read the temperature and humidity, we’ll grab that data from the JSON, format it for readability, and publish it in the channel.

5.js
  return setInterval(function() {
    return climate.readTemperature(function() {
      var temp_args;
      temp_args = arguments;
      return climate.readHumidity(function() {
        var humid_args;
        humid_args = arguments;
        console.log("Temp in C : ", JSON.stringify(temp_args[1].toFixed(4)), " ",
                    "Humidity: ", JSON.stringify(humid_args[1].toFixed(4)), "%RH");
        return PUBNUB.publish({
          channel: channel,
          message: {
            temperature: temp_args,
            humidity: humid_args
          }
        });
      });
    });
  }, 2000);
});

If there’s an error, we will see an error message in the console.

6.js
  climate.on('error', function(err) {
    return console.error(err);
  });
}).call(this);

To run the code you just wrote work with the Tessel hardware, run the Tessel. That’s it! Did you have fun?

Wrapping Up Tessel

In this blog post, we had fun showing you a how to integrate the Tessel Climate Sensor with the PubNub real-time library. We hope you find this information to be useful — it is really cool to see the Internet of Things grow!

In Part Two, we're going to take the published data, receive it, and visualize the data on a live-updating chart!

May 19, 2015

Sending RFID Events in Real time w/ Tessel RFID Module

In this part, we'll use the Tessel to detect a Tessel RFID module scan and send the information in real time to trigger an action.
Michael Carroll
Michael Carroll
IoT
Sep 22, 2014

Tessel + PubNub Power IoT in 4 Lines of JavaScript

Power IoT in 4 lines of JavaScript with Tessel and PubNub. Tessel is a small, powerful microcontroller that runs entirely on JavaScript.
Michael Carroll
Michael Carroll
IoT
Jul 9, 2015

Raspberry Pi Humidity/Temperature Sensor, Dashboard

How to collect humidity and temperature data using a Raspberry Pi humidity sensor (DHT 22), and stream the data to a real-time, live-updating dashboard.
Michael Carroll
Michael Carroll