View Single Post
Old 01-25-2014, 04:54 AM   #118
andyh2000
Avid reader
andyh2000 ought to be getting tired of karma fortunes by now.andyh2000 ought to be getting tired of karma fortunes by now.andyh2000 ought to be getting tired of karma fortunes by now.andyh2000 ought to be getting tired of karma fortunes by now.andyh2000 ought to be getting tired of karma fortunes by now.andyh2000 ought to be getting tired of karma fortunes by now.andyh2000 ought to be getting tired of karma fortunes by now.andyh2000 ought to be getting tired of karma fortunes by now.andyh2000 ought to be getting tired of karma fortunes by now.andyh2000 ought to be getting tired of karma fortunes by now.andyh2000 ought to be getting tired of karma fortunes by now.
 
andyh2000's Avatar
 
Posts: 826
Karma: 6377682
Join Date: Apr 2009
Location: UK
Device: Samsung Galaxy Z Flip 4 / Kindle Paperwhite
Latest in the line of "what can I do with my Kobo?" - pushing position updates to it over the mqtt protocol and showing an openstreetmap map using osmviz. Python code on the Kobo is based on stuff in this thread. You could use this for showing realtime weather info from sensors as well - anything where you want to push data to the Kobo rather than have it polling.



Code:
import urllib2
import pygame, os
import platform
import time
import mosquitto

from osmviz.manager import PygameImageManager, OSMManager
from pygame.locals import *
from datetime import date, datetime, timedelta
from subprocess import call


def on_connect(mosq, obj, rc):
    mosq.subscribe("hello/#", 0)
    print("rc: "+str(rc))

def on_message(mosq, obj, msg):
    print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
    if (platform.system() == 'Windows'):
        update_time = "Last updated at " + datetime.now().strftime("%H:%M:%S %Y-%m-%d")
    else:
        update_time = "Last updated at " + datetime.now().strftime("%l:%M%P")

    last_update = tiny_font.render(update_time, True, gray, white)

    me = tiny_font.render("o", True, black, white)

    #Clear screen
    screen.fill(white)

    # Map image
    latlng = msg.payload.split(',')
    lat = float(latlng[0])
    lng = float(latlng[1])
    minlat = lat - 0.01
    maxlat = lat + 0.01
    minlng = lng - 0.01
    image,bnds = osm.createOSMImage( (minlat, maxlat, minlng, maxlng), 16)
    screen.blit(image, (0, 0))
    imgr.destroy_image()

    #Me marker
    screen.blit(me, (299, 399))

    #Update date/time
    screen.blit(last_update, (185, 770))

    if (platform.system() == 'Windows'):
        graphic = pygame.transform.rotate(screen, 0)
    else:
        graphic = pygame.transform.rotate(screen, 90)

    display.blit(graphic, (0, 0))
    pygame.display.update()

    call(["./full_update"])

def on_publish(mosq, obj, mid):
    print("mid: "+str(mid))

def on_subscribe(mosq, obj, mid, granted_qos):
    print("Subscribed: "+str(mid)+" "+str(granted_qos))

def on_log(mosq, obj, level, string):
    print(string)

os.environ['SDL_NOMOUSE'] = '1'

if (platform.system() == 'Windows'):
    pygame.init()
else:
    pygame.display.init()

pygame.font.init()
pygame.mouse.set_visible(False)

white = (255, 255, 255)
black = (0, 0, 0)
gray = (125, 125, 125)

if (platform.system() == 'Windows'):
    display = pygame.display.set_mode((600, 800))
else:
    display = pygame.display.set_mode((800, 600), pygame.FULLSCREEN)

screen = pygame.Surface((600, 800))

tiny_font = pygame.font.Font("Cabin-Regular.otf", 18)
##small_font = pygame.font.Font("Fabrica.otf", 22)
#font = pygame.font.Font("Forum-Regular.otf", 40)
#bigfont = pygame.font.Font("Forum-Regular.otf", 120)
#comfortaa = pygame.font.Font("Comfortaa-Regular.otf", 80)
#comfortaa_small = pygame.font.Font("Comfortaa-Regular.otf", 35)

imgr = PygameImageManager()
osm = OSMManager(image_manager=imgr)

# Rotate the display to portrait view.

if (platform.system() == 'Windows'):
    graphic = pygame.transform.rotate(screen, 0)
else:
    graphic = pygame.transform.rotate(screen, 90)

display.blit(graphic, (0, 0))
pygame.display.update()

mqttc = mosquitto.Mosquitto()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
# Uncomment to enable debug messages
#mqttc.on_log = on_log
mqttc.connect("mqttbroker.local", 1883, 60)

if (platform.system() != 'Windows'):
    running = True
    while running:
        mqttc.loop(timeout=0)
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
                pygame.event.post(pygame.event.Event(QUIT))

    mqttc.disconnect()
    pygame.quit()
Andrew
andyh2000 is offline   Reply With Quote