element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Personal Blogs
  • Members
  • More
Personal Blogs
Legacy Personal Blogs Build an end-to-end Sigfox GPS tracker using Wia and Pycom
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: anpivey
  • Date Created: 26 Jul 2018 3:17 PM Date Created
  • Views 1628 views
  • Likes 6 likes
  • Comments 2 comments
  • tutorial
  • pycom gpy
  • sigfox
  • iot
Related
Recommended

Build an end-to-end Sigfox GPS tracker using Wia and Pycom

anpivey
anpivey
26 Jul 2018

image

Hi Folks,

Today I’m going to show you how to create a Sigfox GPS tracker using Wia and the Pycom SiPy.

This tutorial assumes that you have already connected Sigfox to Wia if you haven’t, please click here to find a tutorial for initial Sigfox setup and publishing data to Wia.


Components

  • Pycom SiPy
  • Pytrack Expansion Board
  • Sigfox compatible antenna
  • Mobile device (iOS or Android)
  • Wia Platform

 

Setup your board

If you haven’t already, you’ll need to upgrade the firmware on your Pytrack Expansion Board.You can follow the instructions here
  • Connect the SiPy to the Pytrack board. (The RGB LED on the SiPy should be on the side of the usb port on the Pytrack board)
  • Connect the Sigfox antenna to the SiPy. (The connection is just left of RGB LED)

Setup Your Project

  • Create a new folder for your project. I'm going to call mine pycom-pytrack
  • 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 it
  • 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 (Mac OS X), COM3 (Windows), /dev/ttyACM0 (Linux) then save the file

Add required libraries

  • 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 into that file then save it.
  • For Pytrack, additional libraries must also be added to the lib folder, these can be found here

Publish A Location Event

  • We'll need two files for our application:
    • boot.py which is run when the device is powered up
    • main.py which is where our main code is
  • 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
from machine import UART
from network
import Sigfox
import binascii
import machine
import os
# initalise Sigfox for RCZ1 (Europe) (You may need a different RCZ Region)
sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) 

# print Sigfox Device ID

print("ID: ", binascii.hexlify(sigfox.id())) 

# print Sigfox PAC number

print("PAC: ", binascii.hexlify(sigfox.pac())) 

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
from network import Sigfox 
from pytrack import Pytrack
import urequests as requests
from L76GNSS
import L76GNSS
import socket
import time import pycom
import struct 

py = Pytrack()
gps = L76GNSS(py, timeout=60) 

init_timer = time.time() 

print("connecting to Sigfox")
# init Sigfox for RCZ1 (Europe)
sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1) 

# create a Sigfox socket

s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW)
# make the socket blocking
s.setblocking(True)

s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) 

# Post an location to the Wia cloud via Sigfox backend

def post_location(latitude, longitude):
try:
   print(str(latitude), ":", str(longitude))
   s.send(struct.pack('f',float(latitude)) + struct.pack('f',float(longitude)))
except:
   pass 
# main loop
while True:
final_timer = time.time()
diff = final_timer - init_timer
# Get coordinates from pytrack
coord = gps.coordinates()

# If the GPS has coordinates and 15 minites has past. Post the location data
if not coord == (None, None) and diff < 900:
lat, lng = coord
post_location(lat, lng)
init_timer = time.time()
style="margin-bottom:1em"
  • L76GNSS.py
  • LIS2HH12.py
  • pytrack.py
  • boot.py
  • main.py
Click Upload in the Pymakr plugin at the bottom of your window in Atom and send the code to your Pycom board. Now go to the Wia dashboard and you should see data appearing in the debugger section of your dashboard.Note: It may take a few minutes before the Pytrack finds a GPS signal, also the Pytrack isn't suitable for indoor use.

Parsing the Data

Now for the next step, we must parse the data we received from Sigfox and do something useful with it. For this we need to build a Flow. Head over to your Wia dashboard and click the Space where your Sigfox device is held. From there click on the Flow icon in the left hand menu to go your Flows.
image
Now to create your Flow, you can name it whatever you like. Once you have created a Flow, you should be taken to the Flow studio.
In the Flow studio:
  • Drag the trigger event node from the left hand side onto the canvas
  • Click on the node and enter sigfoxDataUplink as the event name
  • Enable your Sigfox Device as the event source
  • Now add a function node from the logic section (We’ll parse the data here)
  • Add a location node from the output section

 

Click on the function node and add the following code:

 

if (input.body) {
let latLong = parseSigfoxLocation(input.body.data.sigfoxData);
output.body.latitude = latLong.latitude;
output.body.longitude = latLong.longitude;
} 
function parseSigfoxLocation(sigfoxData) {

let latHex = sigfoxData.slice(0, 8);

let longHex = sigfoxData.slice(8);

let result = {

latitude: Buffer(latHex, 'hex').readFloatLE(0).toFixed(6),

longitude: Buffer(longHex, 'hex').readFloatLE(0).toFixed(6)

};

  return result;

}

This code parses the Sigfox data from hexadecimal format to latitude and longitude in floating point.
  • Each of GPS coordinates is 32 bits or 4 bytes of data
  • They are 2 characters that can fit into 1 byte of data, hence why we split our hexadecimal string by 8

Send a Push Notification

Currently the Flow takes in the data from Sigfox, parses it and outputs a location. Now we are going to go one step further and get a notification of the newly created location so we can view it on a map on our phones. To do this, you will require the Wia mobile app. You can download it for iOS here and Android here.
image
In the Flow Studio editor:
  • Drag over a location node from the trigger section now. With this we can capture the node created from the previous Flow chain
  • Add the same Device as earlier to the location trigger node
  • Drag over a notification node and enter the following text
GPS Coordinates: ${input.body.latitude}, ${input.body.longitude}

image

 

Now you should receive Sigfox data to your mobile device.

 

image

  • Sign in to reply

Top Comments

  • DAB
    DAB over 4 years ago +3
    Nice post. You made it look easy. DAB
  • genebren
    genebren over 4 years ago +2
    Very nice blog entry. There are so many way to create connectivity in the IOT landscape, and sharing your experiences is a great help to others who are looking for new options. Welcome to the element14…
  • DAB
    DAB over 4 years ago

    Nice post.

     

    You made it look easy.

     

    DAB

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • genebren
    genebren over 4 years ago

    Very nice blog entry.  There are so many way to create connectivity in the IOT landscape, and sharing your experiences is a great help to others who are looking for new options.

     

    Welcome to the element14 community.  You did not waste any time sharing, which is what makes this a very good place to be, so thank you for jumping in.  I hope that you continue and also find subject matter experts that can help you too.

    Gene

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube