Spivey
Published © MIT

Quickly Publish Any Event to Wia Using Your GPy

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

BeginnerFull instructions provided1 hour519
Quickly Publish Any Event to Wia Using Your GPy

Things used in this project

Hardware components

GPy
Pycom GPy
×1

Software apps and online services

Wia
Wia

Story

Read more

Schematics

GPy

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

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

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(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:
            req = requests.post(url=url, headers=headers, json=json_data)
            if req.status_code is not 200:
                machine.reset()
            else:
                print(json_data)
            return req.json()
        else:
            pass
    except:
        pass

# Run this loop continuously
while True:
    temperature = 21.5
    post_event("temperature", temperature)
    if not wlan.isconnected():
         print("Not connected to WiFi")
    time.sleep(DELAY)
    machine.idle()

Credits

Spivey
82 projects • 59 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
Contact

Comments

Please log in or sign up to comment.