Last active
September 30, 2016 04:56
-
-
Save vigevenoj/163f71f980efd0b53281180f8c5fa4fa to your computer and use it in GitHub Desktop.
poop latest phone location onto ssd1306 lcd
This file contains 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
!# /usr/bin/env ruby | |
require 'mqtt' | |
require 'json' | |
require 'yaml' | |
require 'bigdecimal' | |
require 'logger' | |
require 'SSD1306' | |
require 'rufus-scheduler' | |
class LocationLCD | |
version = '0.0.1' | |
def initialize(arguments, stdin) | |
@sched = Rufus::Scheduler.new | |
@client = MQTT::Client.new | |
@client.host = "" | |
@client.port = 8883 | |
@client.ssl = 'TLSv1' | |
@client.ca_file = "" | |
@client.username = "" | |
@client.password = "" | |
@lcd = SSD1306::Display.new | |
@mutex = Mutex.new | |
@logger = Logger.new(STDOUT) | |
#@logger = Logger.new('locationlcd.log', 10, 1048576) | |
@logger.level = Logger::INFO | |
end | |
# Fetch a location update and set our local latitude & longitude | |
def fetch_location | |
topic,message = @client.get('owntracks/#') | |
begin | |
parsed = JSON.parse(message) | |
if(parsed["_type"] == "location") | |
@mutex.synchronize { | |
@latitude = BigDecimal(parsed["lat"], 7) | |
@longitude = BigDecimal(parsed["lon"], 7) | |
if (@latitude > 0) | |
@latNS = 'N' | |
else | |
@latNS = 'S' | |
@latitude = 0 - @latitude | |
end | |
if (@longitude > 0) | |
@lonEW = 'E' | |
else | |
@lonEW = 'W' | |
@longitude = 0 - @longitude | |
end | |
} | |
end | |
rescue JSON::ParserError => e | |
# message is invalid json | |
end | |
end | |
def tick | |
# format the message for our lcd. | |
# The first two lines are yellow; all other lines are blue | |
@mutex.synchronize { | |
lcdmessage = sprintf("%s\n\n%.3f %s\n%.3f %s", | |
Time.now.strftime("%Y-%m-%d %H:%M:%S"), | |
@latitude, @latNS, @longitude, @lonEW) | |
@lcd.clear | |
@lcd.print(lcdmessage) | |
@lcd.display! | |
@logger.info(lcdmessage) | |
} | |
end | |
def run | |
@client.connect() | |
fetch_location | |
@sched.every '30s' do | |
fetch_location | |
end | |
@sched.every '1s' do | |
tick | |
end | |
@sched.join | |
end | |
end | |
if __FILE__ == $0 then | |
locationlcd = LocationLCD.new(ARGV, STDIN) | |
locationlcd.run | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment