Setup a Pycom FiPy and Publish an Event to Wia Using Python
Tags: python, beginners, opensource, tutorials
Published: 2018-06-18 12:29:34

---
title: Setup a Pycom FiPy and Publish an Event to Wia Using Python
published: true
description: How to setup a Pycom FiPy and publish an event to Wia using Python. 
tags: #python #beginners #opensource #tutorials
---
![Pycom FiPy](https://files.readme.io/e8473b6-fipyTop_copy.png)

###**Setup Your Development Environment**
We'll be using Atom as our development environment. You can download the latest version from [here](https://atom.io/).

Once you've got it setup, install the [Pymakr](https://atom.io/packages/pymakr) plugin. Follow the steps in [this tutorial](https://docs.pycom.io/chapter/gettingstarted/installation/pymakr.html) to get it setup.

###**Connect to the Board**
There are two ways to connect your board to your computer either via USB or Serial connection.

####(Option 1) Via USB
You can do so either via the USB port on the Expansion Board, Pysense or Pytrack boards (more on that [here](https://docs.pycom.io/chapter/gettingstarted/)).

####(Option 2) Via Serial (USB to TTL)
To communicate with the board via a USB to TTL cable, connect the following pins:

FiPy <-> USB TTL
GND <-> GND
TX0 <-> RX
RX0 <-> TX
3.3V <-> VCC

![Pycom WiPy Pinout Diagram](https://files.readme.io/e157bd7-wipy_v01_0_pinout_rc1.png)

###**Get Device Name**
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
* Download and install the FTDI drivers from [here](https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers). 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 FTDI 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)

####Using Windows 7?
Pytrack and Pysense will work out of the box for Windows 8/10/+, Mac OS as well as Linux. If using Windows 7, drivers to support the boards will need to be installed. You can find them here .

###**Setup Your Project**
* Create a new folder for your project. Let's call it `wia-publish-pycom-event`. Remember where you put it.
* 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 the folder made previously.

![alt text](https://files.readme.io/451f586-Screen_Shot_2018-03-27_at_16.48.09.png)

####Can't see the tree view pane in Atom?
In the top menu in Atom, click on `View > Toggle Tree View`. This should make it appear.

* 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), `COM4` (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](https://raw.githubusercontent.com/wiaio/wia-board-examples/master/Pycom/LoPy/PublishEventWifi/lib/urequests.py) 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. View [here](https://github.com/wiaio/wia-board-examples/tree/master/Pycom/LoPy/PublishEventWifi) on GitHub.

**boot.py**
```
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. View [here](https://github.com/wiaio/wia-board-examples/tree/master/Pycom/LoPy/PublishEventWifi) on GitHub.

**main.py**
```
from network import WLAN
import urequests as requests
import machine
import time
import pycom
import gc
import micropython

# Garbage collection to save memory
gc.enable()

# 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 = 3

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)
        print('Connecting...')
        while not wlan.isconnected():
             machine.idle() # save power while waiting
        print('WLAN connection succeeded!')
        break


# Post an Event to the Wia cloud
def post_event(json):
    try:
        if json is not None:
            req = requests.post(url=url, headers=headers, json=json_data)
            if req.status_code is not 200:
                machine.reset()
            else:
                print("posting", json, "to Wia")
            return req.json()
        else:
            pass
    except:
        pass

# Data
temperature = 24.6
name = "temperature"
json_data = {"name": name, "data": temperature}

# Run this loop continuously
while True:
    gc.collect()
    #micropython.mem_info()
    post_event(json_data)
    if not wlan.isconnected():
        wlan.connect(net.ssid, auth=(net.sec, WIFI_KEY), timeout=5000)
        print("reconnecting...")
        while not wlan.isconnected():
             machine.idle()
    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. You can find this in your Device Configuration page (see screenshot below).

![Wia dashboard device configuration page](https://files.readme.io/4e21733-Screen_Shot_2018-03-27_at_16.56.11.png)


Your folder structure should now look like this:
* `lib`
  * `urequests.py`
* `boot.py`
* `main.py`

![Project folder structure](https://files.readme.io/fdcc56d-Screen_Shot_2018-03-27_at_16.53.15.png)

Click `Upload` in the Pymakr plugin at the bottom of your window in Atom and send the code to your Pycom board.

![Publish an event code running on Pycom board](https://files.readme.io/4c15ed2-Screenshot_from_2018-03-26_15-37-01.png)

**Note: If you get "Uploading project (main folder)..." Error**
* Try updating the firmware. For the development board click [here](https://docs.pycom.io/chapter/gettingstarted/installation/firmwaretool.html#first).
* For additional firmware for the Pysense/Pytrack board click [here](https://docs.pycom.io/chapter/gettingstarted/installation/firmwaretool.html#first).

Now go to your device in the Wia dashboard and you should see the data appearing in the debugger.

![Wia Device Debugger](https://files.readme.io/4fed030-Debugger_Wia.png)