UnaMKR, Ardunio Sigfox devkit

We previously discovered the UnaMKR devkit. This devkit has two boards. The module board with the radio module and the sensors. It is the one I talked about in my previous post. The Arduino board is the second one. By programming this Arduino MKR Zero you can create custom program to use sensors and radio module to experiment Sigfox. The big advantage is a single board where ever you are located as it supports all the Sigfox zones.

During this second step, we are going to see how we can use the devkit to make a simple sensor measuring and reporting.

Configure Arduino environment

The Arduino board is a MKR Zero board, the first step is to install the corresponding board on the Arduino environment.

From the board manager, you search for “ZERO” to get it and install it.

The second step is to install the UnaMKR library. This library manages the LiteOn module communication. It supports Sigfox communication and Bluetooth.

To get the last version of the library, you need to go to github UnaMKR repository page. Then you download the whole repository as a Zip file.

Then, you add the library going in sketch >> Include library >> add .ZIP library.

Take a look at the examples

The library contains multiple examples to easily test all the features of this kit. You find examples for BLE, Monarch, the different sensors, Sigfox communications.

Setup the sketch for a first transmission

Once you have created a sketch for your first try with the board, you have to include the headers in it. Go to sketch >> Include library >> UnaMKR

Then you may verify the selected board type is Ardunio MKRZERO and the Port is the one.

For this example, we are going to install two more libraries: Arduino Low Power and RTC Zero. You can install them from the library manager searching for these keywords. Then add them in the sketch exactly as you did previously.

The first simple sketch to transmit a short message on Sigfox network is the following.

#include <ArduinoLowPower.h>
#include <UnaMkrBle.h>
#include <UnaMkrMessage.h>
#include <TSL2540.h>
#include <UnaMKR.h>
#include <UnaMkrBufferQueue.h>

/* Options */
#define  PUBLIC_KEY_USAGE       (false)         // set 'true' to enable public key (Emulator)
#define  LOG_MESSAGE_DISPLAY    (false)         // set 'true' to enable displaying all message on serial port

/* Components */
UnaMKR  my_mkr;

void setup() {
   // Serial port for Arduino UART over USB
   Serial.begin(115200);
   // Initialize component
   my_mkr.begin(PUBLIC_KEY_USAGE);
   // Eventually Print response data
   my_mkr.logEnable(LOG_MESSAGE_DISPLAY);
   // ensure not being in sleep during programming phase after a reset
   delay(7000);
}

void loop() {
  uint8_t msg[12] = {0,1,2,3,4,5,6,7};
  uint8_t len=8;
  
  my_mkr.uplink(msg,len);
  if ( !my_mkr.getData_CheckOk(10000) ) {
    Serial.println("Transmission failed");
  } else {
    Serial.println("Transmission success");
  }
  my_mkr.sleep();
  my_mkr.getData_CheckOk(1000);
  
  while (true) {
    LowPower.sleep(10000);
  } 
}

Thanks to the low-power switch it is possible to make the overall circuit to reduce its power consumption down to 400uA (which is large but due to the MKR zero board).

If you want to use the overall system with a LiPo battery you can connect it to the Arduino MKR Zero board battery connector. But for making it working, you need to connect the MKR Zero VCC pin to the 3v3 pin on the UnaMKR JTAG connector (see picture).

When using Low-Power mode, serial programing port is becoming unavailable. You can double-click on the reset button to switch on boot-loader mode.

Arduino MKR Zero Low-power tips

Let’s add the environmental sensor

The UnaMKR board has a nice environmental sensor BME680. You can get temperature, humidity, pressure and COV from this single device. This is all you need for a smart home environmental device.

As we did previously, you need to add the Adafruit Unified Sensor and Adafruit BME680 Library then import it.

#include <ArduinoLowPower.h>
#include <bme680.h>
#include <Adafruit_BME680.h>
#include <bme680_defs.h>

#include <ArduinoLowPower.h>
#include <UnaMkrBle.h>
#include <UnaMkrMessage.h>
#include <TSL2540.h>
#include <UnaMKR.h>
#include <UnaMkrBufferQueue.h>

/* Options */
#define  PUBLIC_KEY_USAGE       (false)         // set 'true' to enable public key (Emulator)
#define  LOG_MESSAGE_DISPLAY    (false)         // set 'true' to enable displaying all message on serial port
#define  SLEEP_DURATION_MS      (30*1000)

/* Components */
UnaMKR  my_mkr;
Adafruit_BME680 my_bme;

uint32_t last_tx, t, t0;
void setup() {
  
   // No serial port - LowPower mode disable USBDevice
   // Have not been able to wake it up.
   pinMode(LED_BUILTIN, OUTPUT);

   // Initialize component
   my_mkr.begin(PUBLIC_KEY_USAGE);
   // Eventually print response data
   my_mkr.logEnable(LOG_MESSAGE_DISPLAY);

   // BME608 Sensor
   my_bme.begin();
   my_bme.setTemperatureOversampling(BME680_OS_8X);
   my_bme.setHumidityOversampling(BME680_OS_2X);
   my_bme.setPressureOversampling(BME680_OS_4X);
   my_bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
   my_bme.setGasHeater(320, 150); // 320*C for 150 ms
   my_bme.performReading();

   my_mkr.sleep();
   my_mkr.getData_CheckOk(1000);
      
   t0=millis();
   last_tx=t0;
   t=t0;
   
   // ensure not being in deepsleep during programming phase after a reset
   for ( int i = 0 ; i < 70 ; i++ ){
      digitalWrite(LED_BUILTIN, HIGH);
      delay(50);
      digitalWrite(LED_BUILTIN, LOW);
      delay(50);
   }
}

void loop() {
  uint8_t msg[4] = { 01,00,00,00 };
  uint8_t len=4;
  float gas;
  int igas;
  t0=millis();

  if ( (t - last_tx) > (15*60*1000) ) {
    last_tx=t;
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);

    my_bme.performReading();
    // Low resistance = High level of VOC 
    gas = (my_bme.gas_resistance)/1000.0;
    igas = (int)gas;

    if ( igas < 0 ) {
       msg[1] = 0;
    } else if ( igas > 255 ) {
      msg[1] = 255;
    } else {
      msg[1] = (uint8_t)igas;
    }

    my_mkr.wakeup();
    my_mkr.uplink(msg,len);
    my_mkr.getData_CheckOk(10000);
    my_mkr.sleep();
    my_mkr.getData_CheckOk(1000);

  } else {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(100);
    digitalWrite(LED_BUILTIN, LOW);
  }
  t0=millis()-t0;
  LowPower.sleep(SLEEP_DURATION_MS);  
  t+=SLEEP_DURATION_MS+t0;
  
}

The program above reports the Air Quality information on Sigfox every 15 minutes ( about ). Lower the value is, worst the air quality is. You can test this by approaching a white board marker.

This program uses the sleep mode to preserve battery.

There is no Serial logging because it sounds not possible to wake the USB serial port from sleep correctly. I’ve taken a look on Internet and found some non working solutions around USBDevice.de/attach(). Let me know if you have found a working solution in the comments.

The use of a software serial with a FTDI cable is a solution for debugging if you need it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.