Luigi Francesco Cerfeda
Published © GPL3+

How to Program Particle Electron (Cellular IoT) in Python

This shows how to program Particle Electron in Python to develop cellular-connected electronics products in a few clicks.

BeginnerProtip1 hour1,647
How to Program Particle Electron (Cellular IoT) in Python

Things used in this project

Story

Read more

Schematics

Particle Electron - Pinmap

Code

Zerynth - HTTP Time GSM Example

Python
################################################################################
#
# HTTP Time GSM Example
#
################################################################################
 
# the classic wifi requests example, very little changes accessing
# the net through a gsm connection!
 
# import our gsm chip specific driver
from ublox.g350 import g350
# and the generic gsm module
from wireless import gsm
import streams
import requests
 
 
streams.serial()
 
# init the gsm driver!
# The driver automatically registers itself to the gsm interface
# with the correct configuration for the selected board
g350.auto_init()
print("Connecting...")
try:
    # connect to our APN
    gsm.connect_net('spark.telefonica.com')
except Exception as e:
    print("ooops, something wrong while connecting :(", e)
    while True:
        sleep(1000)
 
# from now on everything is exactly identical to wifi HTTP Time Example ;)
 
# let's try to connect to timeapi.org to get the current UTC time
for i in range(3):
    try:
        print("Trying to connect...")
        # we need to impersonate a web browser: as easy as setting the http user-agent header
        user_agent = {"user-agent":"Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"}
        # go get that time!
        # url resolution and http protocol handling are hidden inside the requests module
        response = requests.get("http://www.timeapi.org/utc/now",headers=user_agent)
        # let's check the http response status: if different than 200, something went wrong
        print("Http Status:",response.status)
        # if we get here, there has been no exception, exit the loop
        break
    except Exception as e:
        print(e)
 
 
try:
    # check status and print the result
    if response.status==200:
        print("Success!!")
        print("-------------")
        print("And the result is:",response.content)
        print("-------------")
except Exception as e:
    print("ooops, something very wrong! :(",e)

Credits

Luigi Francesco Cerfeda

Luigi Francesco Cerfeda

6 projects • 95 followers
Yet another Tony Stark wannabe

Comments