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
a80a855a
Commit
a80a855a
authored
Nov 14, 2019
by
Kristian Tan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Display device details
parent
1d14a9d5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
92 additions
and
88 deletions
+92
-88
app.py
app.py
+9
-10
main.html
templates/main.html
+83
-78
No files found.
app.py
View file @
a80a855a
...
...
@@ -4,6 +4,7 @@ from flask_sqlalchemy import SQLAlchemy
from
sqlalchemy
import
asc
,
desc
import
os
from
datetime
import
datetime
,
date
,
timedelta
# from daily_usage import DailyUsage
app
=
Flask
(
__name__
)
...
...
@@ -56,8 +57,8 @@ def create_entry(change_pin):
start_date
=
pins
[
change_pin
][
'on_date'
]
# Formula to calculate kWh based on time and wattage
kwh
=
pins
[
change_pin
][
'
W
attage'
]
*
(
elapsed
/
3600
)
/
1000
print
(
pins
[
change_pin
][
'
W
attage'
])
kwh
=
pins
[
change_pin
][
'
w
attage'
]
*
(
elapsed
/
3600
)
/
1000
print
(
pins
[
change_pin
][
'
w
attage'
])
print
(
elapsed
)
print
(
kwh
)
print
(
"LATEST ENTRY: "
)
...
...
@@ -109,19 +110,17 @@ todays_cost = get_todays_cost()
# Create dictionary to store pin info
pins
=
{
25
:
{
'name'
:
'Light'
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
,
'
W
attage'
:
15
},
8
:
{
'name'
:
None
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
,
'
W
attage'
:
0
},
7
:
{
'name'
:
None
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
,
'
W
attage'
:
0
},
12
:
{
'name'
:
None
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
,
'
W
attage'
:
0
}
25
:
{
'name'
:
'Light'
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
,
'
w
attage'
:
15
},
8
:
{
'name'
:
None
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
,
'
w
attage'
:
0
},
7
:
{
'name'
:
None
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
,
'
w
attage'
:
0
},
12
:
{
'name'
:
None
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
,
'
w
attage'
:
0
}
}
# Setup each pin
for
pin
in
pins
:
GPIO
.
setup
(
pin
,
GPIO
.
OUT
)
GPIO
.
output
(
pin
,
GPIO
.
LOW
)
labels
,
values
,
max
=
generate_graph_data
()
...
...
@@ -217,7 +216,7 @@ def handle_new_device():
for
key
in
pins
:
if
pins
[
key
][
'name'
]
is
None
:
pins
[
key
][
'name'
]
=
new_name
pins
[
key
][
'
W
attage'
]
=
new_wattage
pins
[
key
][
'
w
attage'
]
=
new_wattage
break
template_data
=
{
...
...
@@ -238,7 +237,7 @@ def delete_pin(delete_pin):
for
key
in
pins
:
if
key
==
delete_pin
:
pins
[
delete_pin
][
'name'
]
=
None
pins
[
delete_pin
][
'
W
attage'
]
=
None
pins
[
delete_pin
][
'
w
attage'
]
=
None
break
template_data
=
{
...
...
templates/main.html
View file @
a80a855a
<!DOCTYPE html>
<head>
<title>
Current Status
</title>
<title>
Current Status
</title>
<link
rel=
"stylesheet"
href=
"{{ url_for('static', filename='css/main.css') }}"
>
<script
src=
'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js'
></script>
</head>
<div
class=
"heading"
>
<h1
>
IoT Smart Meter and Lighting Control
</h1>
<h1>
IoT Smart Meter and Lighting Control
</h1>
</div>
<body>
<div
class=
"containerRows"
>
<div
class=
"device_list"
>
<h3
class=
"device_list_title"
>
Device Listing and Status
</h3>
{% for pin in pins %}
{% if pins[pin].name != None %}
<p>
{{ pins[pin].name }} :
<div
class=
"containerRows"
>
<div
class=
"device_list"
>
<h3
class=
"device_list_title"
>
Device Listing and Status
</h3>
{% for pin in pins %}
{% if pins[pin].name != None %}
<p>
{{ pins[pin].name }} :
{% if pins[pin].state == true %}
(
<a
href=
"/toggle/{{
pin
}}"
>
turn off
</a>
)
(
<a
href=
"/remove/{{
pin
}}"
>
remove
</a>
)
(
<a
href=
"/toggle/{{
pin
}}"
>
turn off
</a>
)
(
<a
href=
"/remove/{{
pin
}}"
>
remove
</a>
)
{% else %}
(
<a
href=
"/toggle/{{pin}}"
>
turn on
</a>
)
(
<a
href=
"/remove/{{pin}}"
>
remove
</a>
)
</p>
(
<a
href=
"/toggle/{{ pin }}"
>
turn on
</a>
)
(
<a
href=
"/remove/{{ pin }}"
>
remove
</a>
)
</p>
<p>
<ul>
<li>
Last turned on: {{ pins[pin].on_date }} {{ pins[pin].on_time }}
</li>
<li>
Device Wattage: {{ pins[pin].wattage }}
</li>
</ul>
</p>
{% endif %}
{% endif %}
{% endfor %}
<p><a
href=
"/devices/add_device"
>
Add device
</a></p>
{% endif %}
{% endfor %}
<p><a
href=
"/devices/add_device"
>
Add device
</a></p>
{% if display_change_kWh == True %}
{% if display_change_kWh == True %}
<form
action=
"{{ url_for('handle_change_kWh') }}"
method=
"post"
>
Change cost per kWh:
<input
type=
"text"
name=
"kWhprice"
required
>
<input
type=
"submit"
>
...
...
@@ -38,75 +43,75 @@
{% endif %}
{% if display_new_device_form == True %}
<form
action=
"{{ url_for('handle_new_device') }}"
method=
"post"
>
<label>
New device name:
</label>
<input
type=
"text"
name=
"new_device_name"
required
>
<br>
<label>
New device wattage:
</label>
<input
type=
"number"
name=
"new_device_wattage"
required
>
<br>
<input
type=
"submit"
>
</form>
<form
action=
"{{ url_for('handle_new_device') }}"
method=
"post"
>
<label>
New device name:
</label>
<input
type=
"text"
name=
"new_device_name"
required
>
<br>
<label>
New device wattage:
</label>
<input
type=
"number"
name=
"new_device_wattage"
required
>
<br>
<input
type=
"submit"
>
</form>
{% endif %}
</div>
</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>
<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 %}
<p>
Devices currently in use:
</p>
<ul>
{% for pin in pins %}
{% if pins[pin].state == true %}
<li>
{{ pins[pin].name }}
</li>
<li>
{{ pins[pin].name }}
</li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
</ul>
{#
<div
class=
"chart"
>
#}
<h3
class=
"chart_title"
>
Energy usage for the last 7 days
</h3>
<canvas
id=
"chart"
class=
"chart"
></canvas>
<script>
// bar chart data
var
barData
=
{
labels
:
[
{
%
for
item
in
labels
%
}
"
{{ item }}
"
,
{
%
endfor
%
}
],
datasets
:
[{
fillColor
:
"
rgba(151,187,205,0.2)
"
,
strokeColor
:
"
rgba(151,187,205,1)
"
,
pointColor
:
"
rgba(151,187,205,1)
"
,
data
:
[
{
%
for
item
in
values
%
}
"
{{ item }}
"
,
{
%
endfor
%
}
]
}
{#
<div
class=
"chart"
>
#}
<h3
class=
"chart_title"
>
Energy usage for the last 7 days
</h3>
<canvas
id=
"chart"
class=
"chart"
></canvas>
<script>
// bar chart data
var
barData
=
{
labels
:
[
{
%
for
item
in
labels
%
}
"
{{ item }}
"
,
{
%
endfor
%
}
],
datasets
:
[{
fillColor
:
"
rgba(151,187,205,0.2)
"
,
strokeColor
:
"
rgba(151,187,205,1)
"
,
pointColor
:
"
rgba(151,187,205,1)
"
,
data
:
[
{
%
for
item
in
values
%
}
"
{{ item }}
"
,
{
%
endfor
%
}
]
}
]
}
// get bar chart canvas
var
mychart
=
document
.
getElementById
(
"
chart
"
).
getContext
(
"
2d
"
);
steps
=
1
;
max
=
{{
max
}}
// draw bar chart
new
Chart
(
mychart
).
Bar
(
barData
,
{
scaleOverride
:
true
,
scaleSteps
:
steps
,
scaleStepWidth
:
Math
.
ceil
(
max
/
steps
),
scaleStartValue
:
0
,
scaleShowVerticalLines
:
true
,
scaleShowGridLines
:
true
,
barShowStroke
:
true
,
scaleShowLabels
:
true
}
);
</script>
}
// get bar chart canvas
var
mychart
=
document
.
getElementById
(
"
chart
"
).
getContext
(
"
2d
"
);
steps
=
1
;
max
=
{{
max
}}
// draw bar chart
new
Chart
(
mychart
).
Bar
(
barData
,
{
scaleOverride
:
true
,
scaleSteps
:
steps
,
scaleStepWidth
:
Math
.
ceil
(
max
/
steps
),
scaleStartValue
:
0
,
scaleShowVerticalLines
:
true
,
scaleShowGridLines
:
true
,
barShowStroke
:
true
,
scaleShowLabels
:
true
}
);
</script>
{#
</div>
#}
</div>
{#
</div>
#}
</div>
</div>
</div>
</body>
</html>
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