Spivey
Published © MIT

Quickly Publish Any Event to Wia Using Your FiPy

How to set up a FiPy and publish an event or location to Wia.

BeginnerFull instructions provided1 hour499
Quickly Publish Any Event to Wia Using Your FiPy

Things used in this project

Hardware components

Pycom FiPy
×1

Software apps and online services

Wia
Wia

Story

Read more

Schematics

FiPy

Code

boot.py

Python
from machine import UART
import machine
import os

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

machine.main('main.py')

main.py

Python
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)

Credits

Spivey

Spivey

82 projects • 51 followers
Tourist in a Tutu || US Born || Melbourne/Mexico/California Raised || New Yorker at ❤️ || SF to Dublin to be COO of Wia the best IoT startup

Comments