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() diff --git a/lib/sim800.py b/lib/sim800.py index ebd3da5..cb5c53a 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(":"))+1 + payload = uart.read(dlen) + if server_callback: + micropython.schedule(server_callback, payload[1:]) # Check for app callbacks for entry in callbacks: if line.startswith(entry[0]): @@ -847,6 +854,28 @@ def callbuttonpressed_internal(nullparam=None): def endbuttonpressed_internal(nullparam=None): hangup() +#GPRS and TCP server functions + +def setup_gprs(): + 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): + command("AT+CSTT=\""+apn+"\"", response_timeout=10000) + command("AT+CIICR", response_timeout=10000) + command("AT+CIFSR") + +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...