Last active
April 12, 2018 02:36
-
-
Save mokjpn/6b7e7c8eb0c5e4b318b4593075306604 to your computer and use it in GitHub Desktop.
Display ownTracks location on the M5Stack LCD.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from m5stack import lcd | |
from mqtt import MQTTClient | |
import time | |
import ujson | |
import ussl | |
import socket | |
import machine | |
MACHINE=hex(int.from_bytes(machine.unique_id(), 'big'))[2:] | |
def split_list(l, s): | |
count = 0 | |
ni = [] | |
for i in range(0, len(l)-len(s)): | |
if l[i] == s[count]: | |
count=count+1 | |
if count == len(s): | |
ni.append(i+1) | |
count = 0 | |
else: | |
count = 0 | |
if len(ni) == 0: | |
return l | |
else: | |
r = [] | |
pi = 0 | |
for j in range(0, len(ni)): | |
r.append(l[pi:(ni[j]-len(s))]) | |
pi = ni[j] | |
r.append(l[pi:len(l)]) | |
return r | |
def http_getFile(url,file): | |
_, _, host, path = url.split('/', 3) | |
print(host) | |
print(path) | |
addr = socket.getaddrinfo(host, 443)[0][-1] | |
s = socket.socket() | |
s.connect(addr) | |
s = ussl.wrap_socket(s) | |
s.write(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8')) | |
while True: | |
data = s.read(500) | |
if data: | |
l = split_list(data,[13,10,13,10]) | |
if len(l) == 2: | |
file.write(l[1]) | |
break | |
while True: | |
data = s.read(500) | |
if data: | |
file.write(data) | |
else: | |
break | |
s.close() | |
file.close() | |
def read_msg(tpc, msg): | |
t = time.localtime() | |
try: | |
c2.publish(bytearray("ImaDokoStatus"), bytearray(MACHINE+", "+str(t[0])+'/'+str(t[1])+'/'+str(t[2])+', '+str(t[3])+':'+str(t[4])+':'+str(t[5])+', '+str(count))) | |
except: | |
print("status publish error") | |
p = ujson.loads(msg) | |
f = open('map.jpg','w') | |
try: | |
http_getFile("https://YOUR.STATICMAP.SERVER/xxx/staticmap.php?center="+str(p['lat'])+","+str(p['lon'])+"&zoom=16&size=320x240", f) | |
lcd.image(0, 0, file="map.jpg", scale=0, type=lcd.JPG) | |
except: | |
print("http getfile Error") | |
def watchdog(timer): | |
print("Periodical Reboot") | |
machine.reset() | |
wdt = machine.Timer(2) | |
wdt.init(period=1800000, mode=wdt.ONE_SHOT, callback=watchdog) # periodical reboot per 30 minutes | |
lcd.clear() | |
lcd.setCursor(0, 0) | |
lcd.setColor(lcd.WHITE) | |
lcd.setBrightness(55) | |
client = MQTTClient("ImaDokoStack"+MACHINE, "YOUR.MQTT.SERVER", 1883, "MQTT USERNAME", "MQTT Password") | |
client.connect() | |
client.set_callback(read_msg) | |
c2 = MQTTClient("ImaDokoStatus"+MACHINE, "YOUR.MQTT.SERVER", 1883, "MQTT USERNAME", "MQTT Password") | |
c2.connect() | |
client.subscribe(bytearray("owntracks/XXXXXXX")) # ownTracks MQTT Topic | |
count = 0 | |
while True: | |
client.check_msg() | |
time.sleep(5) | |
if count % 60 == 1: # once per 5 minutes, reconnect to MQTT. | |
print("Reconnect to MQTT") | |
gc.collect() | |
try: | |
client.disconnect() | |
c2.disconnect() | |
client.connect() | |
client.subscribe(bytearray("owntracks/XXXXXXX")) # ownTracks MQTT Topic | |
c2.connect() | |
except: | |
print("MQTT reconnect error") | |
count = count + 1 | |
machine.reset() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment