DEV Community

Austin Spivey for Wia

Posted on • Updated on

Publish an Event to Wia Using Your SODAQ SARA N211 & NB-IoT

alt text

In this tutorial, we're going through the initial steps to get setup with NB-IoT and publish an Event to Wia.

Components

Installing Python and Pyserial

If you haven’t already, you need to install Python on your computer to complete this tutorial.

  • Python
  • Version 2.7.x
  • Follow the download instructions for your operating system

Note: Python Environment Variable
When using Windows, you'll need to add python to your path to make use of it anywhere in your OS. More on that here.

Now to install the pyserial library:

  • For Windows, open PowerShell (or Command Prompt) and run the command pip install pyserial
  • For Mac/Linux open Terminal and run the command pip install pyserial

Note: Issues running the command?
In the rare case if pip install pyserial doesn't work, try the following instead:python -m pip install pyserial

Download the code

  • For Windows, open PowerShell in the location of your choosing and run git clone git@github.com:wiaio/nb-iot-utilities.git in the PowerShell terminal
  • For Mac/Linux open a terminal in the location of your choosing and run git clone git@github.com:wiaio/nb-iot-utilities.git in the terminal

Alternatively you can download the zip file here.

  • Extract the zip in the location of your choosing

Finding your device's Serial port

Linux and Mac OS X

  • Download and install the USB to UART bridge drivers from here. Select the appropriate version for your operating system and architecture
  • Open a terminal window and run the command ls /dev/tty*
  • Look for a device with the name that begins with /dev/tty e.g. /dev/tty.usbmodemPy343431 on MAC or /dev/ttyUSB0/dev/ttyACM0 on Linux

Note: For Linux, you may need to run the two commands below. Once you've completed that, reboot your computer. This will add permissions that will allow you to upload a sketch to the board.
sudo usermod -a -G tty ${USER}
sudo usermod -a -G dialout ${USER}

Windows

  • Download and install the USB to UART bridge drivers from here. Select the appropriate version for your operating system and architecture
  • Open the Windows start menu and search for Device Manager
  • The COM port for the Pycom device will be listed as USB Serial Device or something similar
    • Keep note of the COM port (e.g. COM4)

Uploading setup code to your Arduino

In order to communicate with your NB-IoT shield through your Arduino, you'll need to flash the Arduino with code that gives power to the Shield and provides a tunnel for the instructions.

  • Install the Arduino IDE. You can download it for Mac OS X, Windows and Linux here

In the Arduino IDE:

  • Go to menu: Tools > Board > Boards Manager
  • Search for Arduino SAMD. When found, click Install

alt text

In the Arduino IDE:

  • Select the Arduino M0 board type by going to Tools > Board
  • Select the port that displays Arduino M0
initial_config.ino
 #include "Arduino.h"

#if defined(ARDUINO_AVR_LEONARDO)
#define USB Serial
#define UBLOX Serial1

#elif defined(ARDUINO_SODAQ_EXPLORER)
#define USB SerialUSB
#define UBLOX Serial

#elif defined(ARDUINO_SAM_ZERO)
#define USB SerialUSB
#define UBLOX Serial1

#else
#error "Please select a Sodaq ExpLoRer, Arduino Leonardo or Arduino m0."
#endif

// Pin to turn on/off the nb-iot module
#define powerPin 7
unsigned long baud = 9600;  //start at 9600 allow the USB port to change the Baudrate


void setup()
{
  // Turn the nb-iot module on
  pinMode(powerPin, OUTPUT);
  digitalWrite(powerPin, HIGH);

  // Start communication
  USB.begin(baud);
  UBLOX.begin(baud);
}

// Forward every message to the other serial
void loop()
{
  while (USB.available())
  {
    uint8_t c = USB.read();
    UBLOX.write(c);
  }

  while (UBLOX.available())
  {
    USB.write(UBLOX.read());
  }

  // check if the USB virtual serial wants a new baud rate
  if (USB.baud() != baud) {
    baud = USB.baud();
       UBLOX.begin(baud);
  }
}
Enter fullscreen mode Exit fullscreen mode
  • Now copy and paste the initial_config.ino code into the Arduino IDE
  • Click Upload to flash the board Now you should be ready to communicate to your shield.

Updating nb-iot.py

Before we interface with our device, there are a few variables that need to be updated the file nb-iot.py.
In your favourite text editor, open nb-iot.py and follow these steps:

  • Find and replace the serial_name with the port obtained earlier in the tutorial
  • Find and replace the accessToken with your device's secret key found in your Wia dashboard
  • Find and replace the apn with your operators APN (Default: Vodafone Ireland)
  • Find and replace the network_operator with your operators ID (Default: Vodafone Ireland)

Note: Wia CoAP API Endpoint Details
52.17.209.228 is the IP address of the Wia CoAP API and 5683 is the port.

alt text

Interfacing with the device

  • For Windows, in PowerShell, run python nb-iot.py -a in the PowerShell terminal
  • For Mac/Linux open a terminal and run python nb-iot.py -a in the terminal This command checks if the device is working with our code by running the simplest command on the device (AT).

For a full list of commands available:

  • For Windows, in PowerShell, run python nb-iot.py -h in the PowerShell terminal
  • For Mac/Linux open a terminal and run python nb-iot.py -h in the terminal

Publishing an Event to Wia

To publish an Event to Wia with your nb-iot device, run the following:

  • For Windows, in PowerShell, run the command python `nb-iot.py -aknt
  • For Mac/Linux open Terminal and run the command python nb-iot.py -aknt

This script does the following:

  • Checks if the code can interface with the Device
  • Resets the Device
  • Attaches the Device to the network
  • Opens a socket and sends a message via UDP to Wia
  • The output should look like the image below:

alt text

Common errors

Some Devices might be on older firmware that's not compatible with the network you are trying to connect with. You can update your Devices firmware by following the instructions here.

If you are still having trouble connecting drop us a note.

Top comments (0)