Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtnorthcott committed Aug 7, 2016
1 parent d64a3be commit 0bd5378
Show file tree
Hide file tree
Showing 11 changed files with 614 additions and 0 deletions.
47 changes: 47 additions & 0 deletions conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Spot Price Monitor
# Created by Matthew Northcott
# 21-07-2016
# Python 3.4.4

import configparser

PRICE_LIMIT_UPPER = None
PRICE_LIMIT_LOWER = None
BUZZER_DURATION = None
UPDATE_INTERVAL = None

FILE_NETWORK_SUMMER = None
FILE_NETWORK_WINTER = None
FILE_NETWORK_WEEKEND = None
PROVIDER_CHARGE = None

FILE_CFG = "/home/pi/spotprice/spotprice.cfg"

OPTIONS = ["PriceLimitLower", "PriceLimitUpper", "BuzzerDuration", "UpdateInterval", "FileNetworkSummer",
"FileNetworkWinter", "FileNetworkWeekend"]


def read(filename):
conf = configparser.ConfigParser()
conf.read(filename)

spotprice = conf["Spotprice"]
network = conf["Network"]

global PRICE_LIMIT_LOWER, PRICE_LIMIT_UPPER, BUZZER_DURATION, UPDATE_INTERVAL, FILE_NETWORK_SUMMER, \
FILE_NETWORK_WINTER, FILE_NETWORK_WEEKEND, PROVIDER_CHARGE

PRICE_LIMIT_LOWER = float(spotprice["PriceLimitLower"])
PRICE_LIMIT_UPPER = float(spotprice["PriceLimitUpper"])
BUZZER_DURATION = float(spotprice["BuzzerDuration"])
UPDATE_INTERVAL = float(spotprice["UpdateInterval"])
PROVIDER_CHARGE = float(network["ProviderCharge"])
FILE_NETWORK_SUMMER = network["FileNetworkSummer"]
FILE_NETWORK_WINTER = network["FileNetworkWinter"]
FILE_NETWORK_WEEKEND = network["FileNetworkWeekend"]


def main():
read(FILE_CFG)

main()
117 changes: 117 additions & 0 deletions lcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
__author__ = "Matthew Northcott"

# Spot Price Monitor
# Created by Matthew Northcott
# 29-06-2016
# Python 3.4.3

# IMPORTS
import RPi.GPIO as GPIO
import time


# GLOBALS
# GPIO to LCD mapping
LCD_RS = 7
LCD_E = 8
LCD_D4 = 25
LCD_D5 = 24
LCD_D6 = 23
LCD_D7 = 18
# LED_ON = 15

# Device constants
LCD_WIDTH = 16 # Maximum characters per line
LCD_CHR = True
LCD_CMD = False

# RAM addresses
LCD_LINE_1 = 0x80
LCD_LINE_2 = 0xC0

# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005

class LCD(object):
def __init__(self):
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
GPIO.setup(LCD_E, GPIO.OUT) # E
GPIO.setup(LCD_RS, GPIO.OUT) # RS
GPIO.setup(LCD_D4, GPIO.OUT) # DB4
GPIO.setup(LCD_D5, GPIO.OUT) # DB5
GPIO.setup(LCD_D6, GPIO.OUT) # DB6
GPIO.setup(LCD_D7, GPIO.OUT) # DB7
# GPIO.setup(LED_ON, GPIO.OUT) # Backlight enable

self._byte(0x33,LCD_CMD) # 110011 Initialise
self._byte(0x32,LCD_CMD) # 110010 Initialise
self._byte(0x06,LCD_CMD) # 000110 Cursor move direction
self._byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
self._byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
self._byte(0x01,LCD_CMD) # 000001 Clear display

time.sleep(E_DELAY)

def _byte(self, bits, mode):
GPIO.output(LCD_RS, mode)

GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)

if bits&0x10==0x10:
GPIO.output(LCD_D4, True)
if bits&0x20==0x20:
GPIO.output(LCD_D5, True)
if bits&0x40==0x40:
GPIO.output(LCD_D6, True)
if bits&0x80==0x80:
GPIO.output(LCD_D7, True)

# Toggle 'Enable' pin
self._toggle_enable()

# Low bits
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x01==0x01:
GPIO.output(LCD_D4, True)
if bits&0x02==0x02:
GPIO.output(LCD_D5, True)
if bits&0x04==0x04:
GPIO.output(LCD_D6, True)
if bits&0x08==0x08:
GPIO.output(LCD_D7, True)

# Toggle 'Enable' pin
self._toggle_enable()

def _toggle_enable(self):
time.sleep(E_DELAY)
GPIO.output(LCD_E, True)
time.sleep(E_PULSE)
GPIO.output(LCD_E, False)
time.sleep(E_DELAY)

def string_out(self, string, line=1, justify="left"):
if justify == "left":
string = string.ljust(LCD_WIDTH, " ")
elif justify == "centre":
string = string.center(LCD_WIDTH, " ")
elif justify == "right":
string = string.rjust(LCD_WIDTH, " ")

if line == 1:
line = LCD_LINE_1
else:
line = LCD_LINE_2

self._byte(line, LCD_CMD)

for c in string:
self._byte(ord(c), LCD_CHR)
36 changes: 36 additions & 0 deletions logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
__author__ = "Matthew Northcott"

# Spot Pricer
# Created by Matthew Northcott
# 27-02-2016
# Python 3.4.3

# IMPORTS
import datetime

# GLOBALS

# MAIN
class Logger(object):
def __init__(self, filename, is_verbose=True, timestamp_format="%Y-%m-%d %H:%M:%S.%f"):
self.filename = filename
self.is_verbose = is_verbose
self.timestamp_format = timestamp_format

# Create the file
with open(self.filename, 'a'):
pass

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
del self

def write(self, message):
timestamp = datetime.datetime.now().strftime(self.timestamp_format)
line = "[ {timestamp} ] {message}\n".format(timestamp=timestamp, message=message)
with open(self.filename, 'a') as log:
log.write(line)
if self.is_verbose:
print(line, end="")
3 changes: 3 additions & 0 deletions network_summer.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
00 00 1.106
07 00 8.462
21 00 1.106
1 change: 1 addition & 0 deletions network_weekend.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
00 00 1.106
6 changes: 6 additions & 0 deletions network_winter.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
00 00 1.106
07 00 17.22
10 00 8.462
17 00 17.22
19 00 8.462
21 00 1.106
17 changes: 17 additions & 0 deletions read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import datetime

def read(filename):
now = datetime.datetime(day=29, month=7, year=2016, hour=5).time()

with open(filename, 'r') as cfg:
data = [(datetime.time(int(h), int(m)), float(price)) for h, m, price in (line.split() for line in cfg)]

data.sort(key=lambda x: x[0])

for i in range(1, len(data)):
time_a, time_b = data[i-1][0], data[i][0]

if time_a <= now < time_b:
return data[i-1][1]

return data[-1][1]
11 changes: 11 additions & 0 deletions spotprice.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Spotprice]
PriceLimitLower = 23.0
PriceLimitUpper = 33.0
BuzzerDuration = 1.00
UpdateInterval = 300.0

[Network]
FileNetworkSummer = /home/pi/spotprice/network_summer.dat
FileNetworkWinter = /home/pi/spotprice/network_winter.dat
FileNetworkWeekend = /home/pi/spotprice/network_weekend.dat
ProviderCharge = 1.5
Loading

0 comments on commit 0bd5378

Please sign in to comment.