Blog

Connect Any Pycom Device to Wia

Hi All,
Today we're going to look at publishing data from a Pycom device to Wia. To do so, you'll need one of Pycom's development boards (go here to see them). For this tutorial, we will be connecting the devices via WiFi. Other options are available such as Sigfox, LoRa and Bluetooth. You can see the example code for these here.

Create Your Wia Account

If you haven't already got one, click here to sign up.

Setup a Space

Once logged in, setup a new Space for your devices. I'm going to call mine My Devices. When your Space is ready, you should see a screen like the one below.

Screen Shot 2018-01-01 at 12.38.44

Create a Device

Create a new Device by entering it's name in the panel. You will then be redirected to the Device Overview page. Here you will see the Device's secret key (it should start with d_sk). Keep note of this, we're going to use it later.

Screen Shot 2018-01-04 at 12.10.31

Setup Your Environment

We'll be using Atom as our development environment. You can download the latest version from here. Once you've got it setup, install the Pymakr plugin. Follow the steps in this tutorial to get it setup.

Connect to the Board

There are two ways to connect your board to your computer. You can do so either via the USB port on the Expansion Board or Pysense/Pytrack boards (more on that here). Alternatively you can connect it via a USB to TTL cable.

Once you've got it connected to your computer, get the name of your device using one of the following steps:

Linux and Mac OS X
  • Open a terminal window and run the command ls /dev/tty.*
  • Look for a device with the name that begins with /dev/tty.usbmodem e.g. /dev/tty.usbmodemPy343431.
Windows
  • Download and install the FTDI drivers from here.
  • 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)

Setup Your Project

  • Create a new folder for your project. I'm going to call mine publish-pycom-event.
  • In Atom, go to File > New Window to open a new window.
  • Add your newly created folder by clicking File > Add Project Folder and navigating to it.
  • If the Pymakr plugin is not open at the bottom of your Atom window, click on the arrow on the right hand side to open it.
  • Select Settings > Project Settings. In the address field replace the value with the device name from the step above e.g. /dev/tty.usbmodemPy343431 (Linux/Mac OS X), COM3 (Windows) then save the file.

Add the Request Library

  • Right click on the folder name in Atom and click Add Folder. Enter lib as the folder name.
  • Right click on the lib folder and click New File. Enter urequests.py as the file name.
  • Click on the file then copy and paste the code from here into that file then save it.

Publish An Event

  • In Atom, right click on your project and click New File. Enter boot.py as the filename.
  • Copy and paste the code below into the file. (It's also available here on GitHub).
from machine import UART
import machine
import os

uart = UART(0, baudrate=115200)
os.dupterm(uart)

machine.main('main.py')
  • Right click on your project and click New File. Enter main.py as the filename.
  • Copy and paste the code below into the file. (It's also available here on GitHub).
from network import WLAN
import urequests as requests
import machine
import time

# Your WiFi network credentials
WIFI_SSID = 'your-wifi-ssid'
WIFI_KEY = 'your-wifi-key'

# Get this from the Wia dashboard
DEVICE_SECRET_KEY = 'your-device-secret-key'

# Delay between each event
DELAY = 2

wlan = WLAN(mode=WLAN.STA)
nets = wlan.scan()

# Connect to the WiFi network
for net in nets:
    if net.ssid == WIFI_SSID:
        print('Network found!')
        wlan.connect(net.ssid, auth=(net.sec, WIFI_KEY), timeout=5000)
        # while not wlan.isconnected():
        #     machine.idle() # save power while waiting
        print('WLAN connection succeeded!')
        break

print("Connected to Wifi\n")

# Post an Event to the Wia cloud
def post_event(name, data):
    try:
        url = "https://api.wia.io/v1/events"
        headers = {"Authorization": "Bearer " + DEVICE_SECRET_KEY, "Content-Type": "application/json"}
        json_data = {"name": name, "data": data}
        if json_data is not None:
            print(json_data)
            req = requests.post(url=url, headers=headers, json=json_data)
            return req.json()
        else:
            pass
    except:
        pass

# Run this loop continuously
while True:
    temperature = 21.5
    post_event("temperature", temperature)
    time.sleep(DELAY)

Replace the following values of the following variables:

  • WIFI_SSID with your WiFi network name.
  • WIFI_KEY with your WiFi network password.
  • DEVICE_SECRET_KEY with your device secret key from earlier (the one that begins with d_sk).

Your folder structure should now look like this:

  • lib
    • urequests.py
  • boot.py
  • main.py

If all looks good, then click Upload in the Pymakr plugin at the bottom of your window in Atom and send the code to your Pycom board. Now go to the Wia dashboard and you should see it appearing in your device overview.

Note: If your board gets stuck with the message "Uploading project (main folder)...", try updating the firmware. For the development board click here. For additional firmware for the Pysense/Pytrack board click here.

(Optional) Send data from your Pysense board

There are a few more steps required to capture the data from your Pysense board.

  • Navigate to the Wia Pysense example code on GitHub here.
  • Copy the additional files in the lib folder to your local lib folder.
  • Replace your main.py file with the main.py file in the GitHub repository. Remember to change the WIFI_SSID, WIFI_KEY and DEVICE_SECRET_KEY variables.

Your folder structure should be:

  • lib
    • LIS2HH12.py
    • LTR329ALS01.py
    • MPL3115A2.py
    • SI7006A20.py
    • pysense.py
    • urequests.py
  • boot.py
  • main.py

Upload the code to your device and see the data coming through to the Wia dashboard.

That's all folks!

References
Pycom brings world firsts in no less than five modules, sensor shields and equivalent OEM solutions. Whatever network takes your fancy, we have all you need. Choose out of WiFi, Bluetooth, LoRa, Sigfox, LTE CAT M1 / NB1, or have them all


Recommended article

Ready to get started?
Talk to our sales team or setup an account.