Using Pycom with Wifi and Sigfox


Spent some time playing around with the Pycom chip and connecting it to my local wifi and to the Sigfox network. It is surprisingly robust and easy to work with. I used the Sipy chip with their dev expansion board powered by USB. Still waiting for my lopo battery and sensor board.

The Pycom Sipy chips have loads of features including 512 Kb user available internal storage (external SD card also supported), up to 96 Kb of RAM available for user MicroPython code, up to 24 GPIO pins, 2 x UART and loads more. Networks included are Wifi, Bluetooth, Lora and of course Sigfox.

Connecting to a WIFI network

First goal was to hook it up via wifi and get a publish/subscribe feed open between Pycom device and a popular Iot Dev Cloud like iot.adafruit.com. So I setup and account with Ada (free) and wrote something simple to make it work. SiPy is using MicroPython as its main language. I used open-source MQTT library.

Connect to the AP:

wlan = WLAN(mode=WLAN.STA)
wlan.connect(config.WIFI_SSID, auth=(WLAN.WPA2, config.WIFI_PASS), timeout=5000)

Connect, set call back function and subscribe to a feed:

client = MQTTClient(client_id=config.ADA_DEVICE, server=config.ADA_SERVER, user=config.ADA_USERID, password=config.ADA_KEY, port=1883)
client.set_callback(sub_cb)
client.connect()

Publish a super simple message message to toggle a switch on the cloud or any mobile that is listening to the same channel:

client.publish(topic=config.ADA_FEED, msg="ON")

Back in the cloud interface, we can now see the messages coming in

 

Works well. I also wanted to subscribe to the same channel so if there is any change to this value in the cloud, it will be sent to the device. Easy to subscribe to the same channel we published to above and start watching for incoming messages every two seconds:

client.subscribe(topic=config.ADA_FEED)
client.check_msg()
time.sleep(2)

If a message comes back, the call back is simple. If we receive On, turn the onboard LED to Green. If OFF, turn LED to Red:

 if msg.decode("utf-8") == 'ON':
     pycom.rgbled(0x00ff00)
 else:
     pycom.rgbled(0xff0000)

Ada have some cool dashboard tools like slider switches. Adhering to conventional naming protocol, I call the button ‘matztest’ and connect its value to the ada-feed which SiPy is publishing and listening to:

Simple but really powerful. If I slide the switch to ON, the LED on SiPy goes green as does the button. Sliding it to OFF, Sipy LED goes green. If I publish a ‘ON’ message from SiPy, it will slide the switch to ON. Simple, subscription/publication channel. Ofcourse, any devices with a MTQQ client can listen to this channel if they have the authorising API Key.

This example was just a single, simple value. You can also send JSON strings and hook them up to something like a chart in the cloud. This sends a random value with lat and lngs so we can stick it on a map.

value = (uos.urandom(1)[0] / 256) + 3
client.publish(topic=config.ADA_FEED, msg='{"value":' + str(value) +',"lat":-16.34312,"lon":146.43234}')

Connecting using Sigfox:

The Sigfox protocol is super-energy efficient. The reason is that the messaging size is heavily limited. Max 12 bytes and no more than 6 messages per hour orĀ 140 messages /device /day. Still it claims an extra-ordinary range of 50km to base node.

First you have to register your SiPY. Super simple process takes less than a minute. As part of the registration, you pass your device’s ID and PAC which you get when doing a firmware upgrade:

 

The code to send is simple. Get the network, init the Sigfox built-in MicroPython class for Australia and send that puppy a <= 12 byte message. I am going with ‘Hello Me’:

from network import Sigfox
import socket

# init Sigfox for RCZ4 (Australia)
sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ4)

# create a Sigfox socket
s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW)

# send some bytes
s.send("Hello Me")

MessageĀ  turns up in the Sigfox backend interface and has to be decoded to ASCII:

 

Hard to write things to interact with the Sigfox backend so I want to forward this message to AWS Iot and stick it in a table that I can write web gauges for and monitor for changes. Easily done. That little green up arrow is a call back that I hooked up to AWS IoT connector. The connector then delivers the message into a topic on AWS so we can listen to the changes and also into AWS DynamoDb table that can be queried:

By changing the network mode on Sigfox network in the device, you can also write messages directly between devices.