Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
IoTSmartMeter
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
kristian.tan
IoTSmartMeter
Commits
8467fad4
Unverified
Commit
8467fad4
authored
Nov 13, 2019
by
Kristian Tan
Committed by
GitHub
Nov 13, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Inputs (#2)
Added ability to add new device and change cost per kWh
parent
095a5350
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
149 additions
and
67 deletions
+149
-67
app.py
app.py
+95
-46
main.css
static/css/main.css
+16
-2
main.html
templates/main.html
+38
-19
No files found.
app.py
View file @
8467fad4
...
...
@@ -16,6 +16,7 @@ GPIO.setmode(GPIO.BCM)
GPIO
.
setwarnings
(
False
)
db
=
SQLAlchemy
(
app
)
os
.
environ
[
'cost_per_kWh'
]
=
'0.1622'
# TODO: Move this into daily_usage class file
...
...
@@ -34,21 +35,54 @@ class DailyUsage(db.Model):
return
'<DailyUsage
%
r,
%
r,
%
r>'
%
(
self
.
id
,
self
.
date
,
self
.
kwhUsed
)
db
.
create_all
()
# TODO: be able to query db by date
daily_total
=
0
latest_entry
=
db
.
session
.
query
(
DailyUsage
)
.
order_by
(
DailyUsage
.
id
.
desc
())
.
first
()
if
latest_entry
:
def
get_todays_usage
():
latest_entry
=
db
.
session
.
query
(
DailyUsage
)
.
order_by
(
DailyUsage
.
id
.
desc
())
.
first
()
if
latest_entry
:
latest_entry_date
=
date
(
latest_entry
.
date
.
year
,
latest_entry
.
date
.
month
,
latest_entry
.
date
.
day
)
if
latest_entry_date
==
datetime
.
today
()
.
date
():
daily_total
=
format
(
latest_entry
.
kwhUsed
,
'.7f'
)
return
format
(
latest_entry
.
kwhUsed
,
'.7f'
)
return
0
def
get_todays_cost
():
return
format
(
float
(
get_todays_usage
())
*
float
(
os
.
environ
[
'cost_per_kWh'
]),
'0.5f'
)
def
create_entry
(
change_pin
):
latest_entry
=
db
.
session
.
query
(
DailyUsage
)
.
order_by
(
DailyUsage
.
id
.
desc
())
.
first
()
start_time
=
pins
[
change_pin
][
'on_time'
]
# Get the elapsed time and strip away milliseconds
elapsed
=
int
((
datetime
.
now
()
-
start_time
)
.
total_seconds
())
start_date
=
pins
[
change_pin
][
'on_date'
]
# Formula to calculate kWh based on time and wattage
kwh
=
pins
[
change_pin
][
'Wattage'
]
*
(
elapsed
/
3600
)
/
1000
# If there is already an entry for today, update on time
# if latest_entry:
if
latest_entry
:
latest_entry_date
=
date
(
latest_entry
.
date
.
year
,
latest_entry
.
date
.
month
,
latest_entry
.
date
.
day
)
if
latest_entry_date
==
start_date
:
latest_entry
.
kwhUsed
+=
kwh
else
:
# If no entry for today, make one
entry
=
DailyUsage
(
date
=
start_date
,
kwhUsed
=
kwh
)
db
.
session
.
add
(
entry
)
db
.
session
.
commit
()
pins
[
change_pin
][
'on_time'
]
=
None
pins
[
change_pin
][
'on_date'
]
=
None
todays_cost
=
format
(
float
(
daily_total
)
*
0.1622
,
'0.5f'
)
db
.
create_all
()
daily_total
=
get_todays_usage
()
todays_cost
=
get_todays_cost
()
# Create dictionary to store pin info
pins
=
{
25
:
{
'name'
:
'Light'
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
,
'Wattage'
:
15
}
25
:
{
'name'
:
'Light'
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
,
'Wattage'
:
15
},
8
:
{
'name'
:
None
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
,
'Wattage'
:
0
},
7
:
{
'name'
:
None
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
,
'Wattage'
:
0
},
12
:
{
'name'
:
None
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
,
'Wattage'
:
0
}
}
...
...
@@ -68,7 +102,10 @@ def main():
template_data
=
{
'pins'
:
pins
,
'daily_total'
:
daily_total
,
'todays_cost'
:
todays_cost
'todays_cost'
:
todays_cost
,
'cost_per_kWh'
:
os
.
environ
[
'cost_per_kWh'
],
'display_new_device_form'
:
False
,
'display_change_kWh'
:
False
}
return
render_template
(
'main.html'
,
**
template_data
)
...
...
@@ -92,60 +129,72 @@ def toggle_pin(change_pin):
for
pin
in
pins
:
pins
[
pin
][
'state'
]
=
GPIO
.
input
(
pin
)
latest_entry
=
db
.
session
.
query
(
DailyUsage
)
.
order_by
(
DailyUsage
.
id
.
desc
())
.
first
()
if
latest_entry
:
latest_entry_date
=
date
(
latest_entry
.
date
.
year
,
latest_entry
.
date
.
month
,
latest_entry
.
date
.
day
)
if
latest_entry_date
==
datetime
.
today
()
.
date
():
daily_total
=
format
(
latest_entry
.
kwhUsed
,
'.7f'
)
else
:
daily_total
=
0
template_data
=
{
'pins'
:
pins
,
'daily_total'
:
get_todays_usage
(),
'todays_cost'
:
get_todays_cost
(),
'cost_per_kWh'
:
os
.
environ
[
'cost_per_kWh'
]
}
return
render_template
(
'main.html'
,
**
template_data
)
todays_cost
=
format
(
float
(
daily_total
)
*
0.1622
,
'0.5f'
)
@
app
.
route
(
'/handle_change_kWh'
,
methods
=
[
'POST'
])
def
handle_change_kWh
():
new_price
=
request
.
form
[
'kWhprice'
]
os
.
environ
[
'cost_per_kWh'
]
=
str
(
new_price
)
template_data
=
{
'pins'
:
pins
,
'daily_total'
:
daily_total
,
'todays_cost'
:
todays_cost
'todays_cost'
:
todays_cost
,
'cost_per_kWh'
:
os
.
environ
[
'cost_per_kWh'
]
}
return
render_template
(
'main.html'
,
**
template_data
)
@
app
.
route
(
"/devices/add_device"
)
def
add_new_device
():
template_data
=
{
'pins'
:
pins
,
'daily_total'
:
daily_total
,
'todays_cost'
:
todays_cost
,
'cost_per_kWh'
:
os
.
environ
[
'cost_per_kWh'
],
'display_new_device_form'
:
True
}
return
render_template
(
'main.html'
,
**
template_data
)
def
create_entry
(
change_pin
):
latest_entry
=
db
.
session
.
query
(
DailyUsage
)
.
order_by
(
DailyUsage
.
id
.
desc
())
.
first
()
start_time
=
pins
[
change_pin
][
'on_time'
]
# Get the elapsed time and strip away milliseconds
elapsed
=
int
((
datetime
.
now
()
-
start_time
)
.
total_seconds
())
start_date
=
pins
[
change_pin
][
'on_date'
]
@
app
.
route
(
'/handle_new_device'
,
methods
=
[
'POST'
])
def
handle_new_device
():
new_name
=
request
.
form
[
'new_device_name'
]
new_wattage
=
float
(
request
.
form
[
'new_device_wattage'
])
for
key
in
pins
:
if
pins
[
key
][
'name'
]
is
None
:
pins
[
key
][
'name'
]
=
new_name
pins
[
key
][
'Wattage'
]
=
new_wattage
break
# Formula to calculate kWh based on time and wattage
kwh
=
pins
[
change_pin
][
'Wattage'
]
*
(
elapsed
/
3600
)
/
1000
template_data
=
{
'pins'
:
pins
,
'daily_total'
:
daily_total
,
'todays_cost'
:
todays_cost
,
'cost_per_kWh'
:
os
.
environ
[
'cost_per_kWh'
],
}
return
render_template
(
'main.html'
,
**
template_data
)
# If there is already an entry for today, update on time
# if latest_entry:
if
latest_entry
:
latest_entry_date
=
date
(
latest_entry
.
date
.
year
,
latest_entry
.
date
.
month
,
latest_entry
.
date
.
day
)
if
latest_entry_date
==
start_date
:
latest_entry
.
kwhUsed
+=
kwh
else
:
# If no entry for today, make one
entry
=
DailyUsage
(
date
=
start_date
,
kwhUsed
=
kwh
)
db
.
session
.
add
(
entry
)
db
.
session
.
commit
()
pins
[
change_pin
][
'on_time'
]
=
None
pins
[
change_pin
][
'on_date'
]
=
None
@
app
.
route
(
'/handle_data'
,
methods
=
[
'POST'
])
def
handle_data
():
projectpath
=
request
.
form
[
'kWhprice'
]
print
(
projectpath
)
@
app
.
route
(
"/update_info/kWh"
)
def
change_kWh
():
template_data
=
{
'pins'
:
pins
,
'daily_total'
:
daily_total
,
'todays_cost'
:
todays_cost
'todays_cost'
:
todays_cost
,
'cost_per_kWh'
:
os
.
environ
[
'cost_per_kWh'
],
'display_change_kWh'
:
True
}
return
render_template
(
'main.html'
,
**
template_data
)
if
__name__
==
'__main__'
:
app
.
run
(
host
=
'0.0.0.0'
,
port
=
8090
)
static/css/main.css
View file @
8467fad4
...
...
@@ -9,11 +9,24 @@ body {
/*background-color: aqua;*/
}
.container
{
.inputs
{
display
:
flex
;
flex-direction
:
column
;
margin-right
:
5%
;
margin-left
:
5%
;
margin-bottom
:
2%
;
border-top-style
:
solid
;
border-top-width
:
1px
;
border-top
:
black
;
/*background-color: green;*/
/*height: 10%;*/
}
.containerRows
{
display
:
flex
;
flex-direction
:
row
;
/*background-color: darkkhaki;*/
height
:
100%
;
/*height: 90%;*/
}
.device_list
{
...
...
@@ -31,6 +44,7 @@ body {
width
:
65%
;
margin-right
:
5%
;
margin-left
:
5%
;
margin-bottom
:
2%
;
/*background-color: coral;*/
border-style
:
solid
;
border-color
:
black
;
...
...
templates/main.html
View file @
8467fad4
...
...
@@ -9,29 +9,33 @@
</div>
<body>
<div
class=
"container"
>
<form
action=
"{{ url_for('handle_data') }}"
method=
"post"
>
<input
type=
"text"
name=
"kWhprice"
>
<input
type=
"submit"
>
</form>
<div
class=
"containerRows"
>
<div
class=
"device_list"
>
<h3
class=
"device_list_title"
>
Device Listing and Status
</h3>
{% for pin in pins %}
<p>
{{ pins[pin].name }} :
{% if pins[pin].name != None %}
<p>
{{ pins[pin].name }} :
{% if pins[pin].state == true %}
(
<a
href=
"/toggle/{{pin}}"
>
turn off
</a>
)
{% else %}
(
<a
href=
"/toggle/{{pin}}"
>
turn on
</a>
)
{% endif %}
</p>
{% endif %}
{% endif %}
{% endfor %}
<p><a
href=
"/devices/add_device"
>
Add device
</a></p>
</div>
<div
class=
"smart_meter"
>
<div
class=
"smart_meter_info"
>
{# Implement some way to display "No devices" message if none are in use#}
<p>
Cost per kWh: £{{ cost_per_kWh }}
<a
href=
"/update_info/kWh"
>
(Update)
</a></p>
<p>
Total energy used today: {{ daily_total }} kWh.
</p>
<p>
Total cost of today's energy usage: £{{ todays_cost }}
</p>
<p>
Devices currently in use:
</p>
<ul>
{% for pin in pins %}
...
...
@@ -40,13 +44,28 @@
{% endif %}
{% endfor %}
</ul>
{# Implement some way to display "No devices" message if none are in use#}
<p>
Total energy used today: {{ daily_total }} kWh.
</p>
<p>
Total cost of today's energy usage: £{{ todays_cost }}
</p>
</div>
</div>
</div>
<div
class=
"inputs"
>
{% if display_change_kWh == True %}
<form
action=
"{{ url_for('handle_change_kWh') }}"
method=
"post"
>
Change cost per kWh:
<input
type=
"text"
name=
"kWhprice"
>
<input
type=
"submit"
>
</form>
{% endif %}
{% if display_new_device_form == True %}
<div
class=
".add_new_device_form"
>
<form
action=
"{{ url_for('handle_new_device') }}"
method=
"post"
>
New device name:
<input
type=
"text"
name=
"new_device_name"
>
<br>
New device wattage:
<input
type=
"number"
name=
"new_device_wattage"
>
<br>
<input
type=
"submit"
>
</form>
</div>
{% endif %}
</div>
</body>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment