From 76463fe172b218c9d86230efae8527a455a04093 Mon Sep 17 00:00:00 2001 From: Sam Machin Date: Tue, 11 Sep 2018 08:15:27 +0100 Subject: [PATCH 1/4] Update sim800.py Added functions to setup and connect GPRS data, starting a TCP server and callback handler for server data --- lib/sim800.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/sim800.py b/lib/sim800.py index ebd3da5..1c26ff8 100644 --- a/lib/sim800.py +++ b/lib/sim800.py @@ -25,6 +25,7 @@ dirtybuffer = False # Flag if the buffer could have residual end of reresponsesp # A list of callback functions callbacks = [] +server_callback = None # Globals for remembering callback data clip = "" @@ -105,6 +106,12 @@ def processcallbacks(line): # Check for Bluetooth pairing request if line.startswith("+BTPAIRING:"): btpairing = line[11:].strip() + # Handle TCP Server Data + if line.startswith("+RECEIVE"): + dlen = int(line.split(",")[2].rstrip(":")) + payload = uart.read(dlen) + if server_callback: + micropython.schedule(server_callback, payload) # Check for app callbacks for entry in callbacks: if line.startswith(entry[0]): @@ -847,7 +854,21 @@ def callbuttonpressed_internal(nullparam=None): def endbuttonpressed_internal(nullparam=None): hangup() +#GPRS and TCP server functions +def setup_gprs(): + sim800.command("AT+CIPSHUT", response_timeout=60000, custom_endofdata="SHUT OK") + sim800.command("AT+CGATT?", response_timeout=10000) + sim800.command("AT+CIPMUX=1", response_timeout=10000) + +def connect_gprs(apn): + sim800.command("AT+CSTT=\""+apn+"\"", response_timeout=10000) + sim800.command("AT+CIICR", response_timeout=10000) + sim800.command("AT+CIFSR") + +def start_server(port): + sim800.command("AT+CIPSERVER=1,"+port, response_timeout=10000) + sim800.registercallback('+RECEIVE', handle_data) # Startup... # Start turning on the SIM800 asynchronously From a19445f862fcff09cbf8829c9ea8d5c1cad851e3 Mon Sep 17 00:00:00 2001 From: Sam Machin Date: Tue, 11 Sep 2018 19:04:26 +0100 Subject: [PATCH 2/4] Update sim800.py Added GPRS support, `setup_gprs()` will prepare the sim800 for data, `connect_gprs("hologram")` will connect to the hologram APN Also added a function to start a tcp server on the SIM 800 using `start_server(4010)` to listen on port 4010, the payload will be a binary object passed to whatever is defined as `server_callback` --- lib/sim800.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/sim800.py b/lib/sim800.py index 1c26ff8..8db26f5 100644 --- a/lib/sim800.py +++ b/lib/sim800.py @@ -108,10 +108,10 @@ def processcallbacks(line): btpairing = line[11:].strip() # Handle TCP Server Data if line.startswith("+RECEIVE"): - dlen = int(line.split(",")[2].rstrip(":")) + dlen = int(line.split(",")[2].rstrip(":"))+1 payload = uart.read(dlen) if server_callback: - micropython.schedule(server_callback, payload) + micropython.schedule(server_callback, payload[1:]) # Check for app callbacks for entry in callbacks: if line.startswith(entry[0]): @@ -857,18 +857,18 @@ def endbuttonpressed_internal(nullparam=None): #GPRS and TCP server functions def setup_gprs(): - sim800.command("AT+CIPSHUT", response_timeout=60000, custom_endofdata="SHUT OK") - sim800.command("AT+CGATT?", response_timeout=10000) - sim800.command("AT+CIPMUX=1", response_timeout=10000) + command("AT+CIPSHUT", response_timeout=60000, custom_endofdata="SHUT OK") + command("AT+CGATT?", response_timeout=10000) + command("AT+CIPMUX=1", response_timeout=10000) def connect_gprs(apn): - sim800.command("AT+CSTT=\""+apn+"\"", response_timeout=10000) - sim800.command("AT+CIICR", response_timeout=10000) - sim800.command("AT+CIFSR") + command("AT+CSTT=\""+apn+"\"", response_timeout=10000) + command("AT+CIICR", response_timeout=10000) + command("AT+CIFSR") def start_server(port): - sim800.command("AT+CIPSERVER=1,"+port, response_timeout=10000) - sim800.registercallback('+RECEIVE', handle_data) + command("AT+CIPSERVER=1,"+port, response_timeout=10000) + # Startup... # Start turning on the SIM800 asynchronously From 098a07e0a4c04f4dd87ecb87236729aa8acc76fb Mon Sep 17 00:00:00 2001 From: Sam Machin Date: Fri, 14 Sep 2018 08:00:41 +0100 Subject: [PATCH 3/4] Update sim800.py Few more tweaks to GPRS handling after real world testing with an app --- lib/sim800.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/sim800.py b/lib/sim800.py index 8db26f5..cb5c53a 100644 --- a/lib/sim800.py +++ b/lib/sim800.py @@ -866,8 +866,16 @@ def connect_gprs(apn): command("AT+CIICR", response_timeout=10000) command("AT+CIFSR") -def start_server(port): - command("AT+CIPSERVER=1,"+port, response_timeout=10000) +def stop_gprs(): + command("AT+CIPSHUT", response_timeout=60000, custom_endofdata="SHUT OK") + +def start_server(port, callback): + global server_callback + server_callback = callback + command("AT+CIPSERVER=1,"+str(port), response_timeout=10000) + +def stop_server(): + command("AT+CIPSERVER=0", response_timeout=10000) # Startup... From 912cc34269b529e4e817eb402105b67deaecb51f Mon Sep 17 00:00:00 2001 From: Sam Machin Date: Fri, 14 Sep 2018 08:02:55 +0100 Subject: [PATCH 4/4] Create main.py Sample application for using Hologram with GPRS --- hologram_demo/main.py | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 hologram_demo/main.py diff --git a/hologram_demo/main.py b/hologram_demo/main.py new file mode 100644 index 0000000..25bcfbe --- /dev/null +++ b/hologram_demo/main.py @@ -0,0 +1,51 @@ +"""This app connects to the Hologram service via GPRS displays recieved data on the screen and sets the neopixles""" + +___title___ = "Hologram Demo" +___license___ = "MIT" +___dependencies___ = ["app", "sim800"] +___categories___ = ["EMF", "System"] +___bootstrapped___ = False + +#import ugfx, os, time, sleep, app, sim800 + +import ugfx, app, sim800 +import os +from tilda import Buttons +from time import sleep +from machine import Neopix + + +n = Neopix() + +ugfx.init() +ugfx.clear() +ugfx.set_default_font(ugfx.FONT_FIXED) + + +def callback(data): + payload=data.decode("utf-8") + ugfx.Label(5, 100, 240, 15, payload) + colour = int(payload) + n.display([colour,colour]) + +print('Launching Hologram Demo') +ugfx.Label(5, 20, 240, 15, "Starting....") +sim800.setup_gprs() +ugfx.Label(5, 20, 240, 15, "GPRS Ready") +sim800.connect_gprs('hologram') +ugfx.Label(5, 40, 240, 15, "GPRS Connected") +sim800.start_server(4010, callback) +ugfx.Label(5, 60, 240, 15, "Server Started") + + +ugfx.Label(5, 300, 240, 15, "** Hold A or B or MENU to exit **") + + +while (not Buttons.is_pressed(Buttons.BTN_A)) and (not Buttons.is_pressed(Buttons.BTN_B)) and (not Buttons.is_pressed(Buttons.BTN_Menu)): + sleep(2) + +ugfx.clear() +ugfx.Label(5, 20, 240, 15, "Stopping....") +sim800.stop_server() +sim800.stop_gprs() +app.restart_to_default()