Joachim Kristensen
Published © GPL3+

MicroPython to Python via SigFox and Azure IoT Hub

This is to show how to get messages from a SiPy to your computer via SigFox and Azure IoT Hub.

IntermediateProtip3 hours2,193
MicroPython to Python via SigFox and Azure IoT Hub

Things used in this project

Hardware components

SiPy
Pycom SiPy
×1

Software apps and online services

Microsoft Azure
Microsoft Azure
Sigfox
Sigfox

Story

Read more

Code

Python script used

Python
from azure.servicebus import ServiceBusService, Message, Queue
import time 
import json
import re



bus_service = ServiceBusService(
	service_namespace ='devintservicebus',
	shared_access_key_name = 'RootManageSharedAccessKey',
	shared_access_key_value ='7pGz+TDQNvUJkozNOsWVzKRB/lSd/rzTlOKF80c2U8A=') 

queue_options = Queue()
queue_options.max_size_in_megabytes = '5120'
queue_options.default_message_time_to_live = 'PT1M'

#Get message through service bus. Message is received as an bytes class
msg = bus_service.receive_subscription_message('testsigfox', 'sigfoxsub')

print(msg.body)
#Changed encoding="ascii" to "utf-8"
def decode(s, encoding="utf-8", errors="ignore"):
	return s.decode(encoding=encoding, errors=errors)

#decode the message received. The decoding used is uft-8
data_decode = decode(msg.body)

#Make a function that can take a string from before a certain char
def before(value, a):
	pos_a = value.find(a)
	if pos_a == -1: return""
	return value[1:pos_a]

#remove string before { from the data as well as remove the first char (@)
data_cutstring = before(data_decode, "{")
data_substring = re.sub(data_cutstring, '', data_decode)
data_substring = data_substring[1:]

#Replace all null with a zero and remove the last char from string 
data_remove_final = data_substring[:-1]
data_remove_final = data_remove_final.replace("null","0")

data_json = json.loads(data_remove_final)
print(data_json)
print(type(data_json))

Credits

Joachim Kristensen

Joachim Kristensen

1 project • 3 followers

Comments