Now that the NoCAN IoT platform is finally here, it's time to discuss what you can do with it! If you are not familiar with the NoCAN platform, you might want to check out our introduction to the NoCAN platform.

As a first example, let's look at a scenario where you want to collect weather data from a sensor placed in a remote area of your garden or in a greenhouse. For this task, we picked a BME280 sensor from Sparkfun, which provides temperature, humidity and barometric pressure with good precision (note: Adafruit also makes a similar sensor). We will connect the sensor to a CANZERO node and put it in a protective electrical box outside. The CANZERO node will be connected to a NoCAN network managed by a PiMaster HAT connected to a Raspberry Pi. Data collected by the sensor will be broadcasted every 10 seconds to the network.

Let's build it!

Connecting the sensor

The SparkFun BME280 sensor can be connected to an Arduino or any microcontroller with I2C or SPI, and works at 3.3V. For our connection to the CANZERO, we picked SPI but it would be easy to switch to I2C if needed. The CANZERO works at 3.3V, so no voltage level shifting is necessary here.

The CANZERO shares the same pinout as the Arduino MKR Zero. The connection is summarized in the table below.

CANZERO / MKR Zero pin BME280 pin
7 CS
8 (MOSI) SDI
9 (SCK) SCK
10 (MISO) SDO
GND GND
3.3V 3.3V

The connection is represented in the following diagram.

Connecting the CANZERO to a BME280

For simplicity, we used simple jumper wires to make our connection. We plugged them in the CANZERO and soldered the other ends to the BME280 sensor.

We are now ready to put the CANZERO node in our network.

Building the network

We installed a Raspberry Pi model B+ with a PiMaster HAT outside in the garden in a big electrical enclosure that is well protected from water (rated IP65). Two external cables bring electric mains and Ethernet to the enclosure. A small DC wall-wart transforms mains down to 12V DC, powering the PiMaster, the Raspberry Pi and the whole NoCAN network with just one connection. The Ethernet cable provides a robust network link between the house and the Raspberry Pi.

Our big tutorial describes the connection of the Raspberry Pi, the PiMaster, and the associated cabling. As suggested in the tutorial, we also used UTP (Ethernet) cables to build our NoCAN network, as shown in the picture below.

The UTP cables supporting the NoCAN network are then connected to the CANZERO node, which was itself installed in a small 8cm square electrical box, as shown below. The sensor was isolated from the CANZERO with some plastic wrapping, so as to expose the sensor to the outside environment while maintaining the CANZERO unexposed. Even though we sheltered the sensor from rainfall and any direct contact with water, the SparkFun BME280 board is probably not designed to be exposed in such a way to the elements and is likely to fail in the long term without additional protective measures (e.g. conformal coating).

CANZERO inside electrical box

For a description of the installation and connection of a CANZERO node, we refer the reader to our tutorial.

The UTP cables are placed in flexible electric tubing, isolating them from humidity in the garden.

The hardware is now complete: time to move to the software!

Launching nocand, the network manager

We log in to the Raspberry Pi with a terminal shell and install nocand, following the instructions detailed in our tutorial.

A good way to keep nocand running in the background is to use the screen utility, simply by typing:

screen ./nocand

If you lose the shell connection to the Raspberry Pi, you can log in again and reconnect to the session by typing screen -r. You can disconnect from a screen session at any time by typing Ctrl-a d on the keyboard.

At this stage, the firmware that is necessary to drive the BME280 is not installed on the CANZERO yet but it's good to check that the network is correctly connected, using the following indicators:

  • The green LED on the CANZERO node should be fully on, not blinking.
  • Using the nocanc tool and typing nocanc list-nodes should list the CANZERO node as installed in the network.

For more info on testing your setup, see the relevant section in our tutorial.

Building the firmware with Arduino

We will write the BME280 firmware of our CANZERO node as a sketch in the Arduino IDE. We assume that the CANZERO board description has already been installed in the IDE.

We will also need to install the libraries necessary to use the BME280 sensor. When we initially started playing with the BME280, we found out that the simplest solution was to use the "Adafruit BME280" library (despite using a SparkFun sensor). To download the Adafruit BME280 library, go to Sketch > Include Library > Manage Libraries and use the search box to locate adafruit bme280. You will also likely need the "Adafruit Unified Sensor" library, which can be found by going to Sketch > Include Library > Manage Libraries and searching for Adafruit Unified Sensor.

We used the following sketch.

#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <nocan.h>

#define BME_SCK 9
#define BME_MISO 10
#define BME_MOSI 8
#define BME_CS 7

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme(BME_CS); // hardware SPI

unsigned long delayTime;
NocanChannelId temperatureChannel, humidityChannel, pressureChannel, altitudeChannel;


void setup() {
    // put your setup code here, to run once:
    Nocan.open();
    Nocan.registerChannel("bme280/temperature", &temperatureChannel);
    Nocan.registerChannel("bme280/humidity", &humidityChannel);
    Nocan.registerChannel("bme280/pressure", &pressureChannel);
    Nocan.registerChannel("bme280/altitude", &altitudeChannel);

    bool status;

    // default settings
    status = bme.begin();  
    if (!status) {
        while (1);
    }

    delayTime = 10000;

    delay(100); // let sensor boot up
}


void loop() {

    publishFloat(temperatureChannel, bme.readTemperature());
    publishFloat(humidityChannel, bme.readHumidity());
    publishFloat(pressureChannel, bme.readPressure());
    publishFloat(altitudeChannel, bme.readAltitude(SEALEVELPRESSURE_HPA));
    delay(delayTime);
}

void publishFloat(NocanChannelId cid, float f)
{
  String f_s = String(f, 2);
  Nocan.publishMessage(cid, f_s.c_str());
}

The sketch creates 4 channels called "bme280/temperature", "bme280/humidity", "bme280/pressure", and "bme280/altitude" respectively. Then, every 10000 milliseconds (10 seconds), we read the data from the sensor and broadcast it on dedicated channels.

Note: Other CANZERO nodes could be added in the network and subscribe to these channels, and react to their value, e.g., activating a heater in a greenhouse when the temperature falls below a certain threshold.

Once compiled and exported to a compiled binary (see Sketch > Export Compiled Binary in the IDE), the firmware can be uploaded to the CANZERO with the nocanc tool, which should typically be installed on the same machine as the one running the Arduino IDE:

$ ./nocanc upload ~/Documents/Arduino/nocan_bme280/nocan_bme280.ino.omzlo_canzero.hex 1
Progress: 100%, 0 bytes, 0 bps
Done

Collecting the data

The simplest way to collect weather data is to use the nocanc utility to list existing channels. Assuming you have only one CANZERO node in your network, the output of the command should look like the example below.

$ ./nocanc list-channels  
# Channels:
UPDATED #0  bme280/temperature  "18.51"
UPDATED #1  bme280/humidity "85.07"
UPDATED #2  bme280/pressure "96464.69"
UPDATED #3  bme280/altitude "412.75"

To read a particular channel, you can use nocanc read-channel with the channel name as a parameter:

./nocanc read-channel bme280/temperature
UPDATED #0  bme280/temperature  "18.44"

You can further process the output of this command in a script. For example on a Linux or Mac OS X system, you could type:

TEMPERATURE=`./nocanc read-channel bme280/temperature | cut -f 4` 
echo "The temperature is $(eval echo $TEMPERATURE) Celcius"

If the bme280/temperature channel reads a value of "18.44", these commands will output The temperature is 18.44 Celcius.

That's it!

To stay updated on the NoCAN project, don't forget to follow us on Twitter or on our Facebook page.