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
954ac304
Commit
954ac304
authored
Nov 12, 2019
by
Kristian Tan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow control over GPIO
parent
b9f892fa
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
17 deletions
+50
-17
app.py
app.py
+28
-5
main.html
templates/main.html
+22
-12
No files found.
app.py
View file @
954ac304
from
flask
import
Flask
,
render_template
import
datetime
#
import datetime
import
RPi.GPIO
as
GPIO
app
=
Flask
(
__name__
)
# Create a dictionary called pins to store the pin number, name, and pin state:
pins
=
{
24
:
{
'name'
:
'coffee maker'
,
'state'
:
GPIO
.
LOW
},
25
:
{
'name'
:
'lamp'
,
'state'
:
GPIO
.
LOW
}
25
:
{
'name'
:
'Light'
,
'state'
:
'GPIO.LOW'
}
}
#
# Set each pin as an output and make it low:
for
pin
in
pins
:
GPIO
.
setup
(
pin
,
GPIO
.
OUT
)
...
...
@@ -20,7 +19,7 @@ for pin in pins:
def
main
():
# For each pin, read the pin state and store it in the pins dictionary:
for
pin
in
pins
:
pins
[
pin
][
'state'
]
=
GPIO
.
input
(
pin
)
pins
[
pin
][
'state'
]
=
'GPIO.input(pin)'
# Put the pin dictionary into the template data dictionary:
template_data
=
{
'pins'
:
pins
...
...
@@ -29,6 +28,30 @@ def main():
return
render_template
(
'main.html'
,
**
template_data
)
# The function below is executed when someone requests a URL with the pin number and action in it:
@
app
.
route
(
"/<change_pin>/<action>"
)
def
action
(
change_pin
,
action
):
change_pin
=
int
(
change_pin
)
device_name
=
pins
[
change_pin
][
'name'
]
if
action
==
"on"
:
GPIO
.
output
(
change_pin
,
GPIO
.
HIGH
)
message
=
"Turned "
+
device_name
+
" on."
if
action
==
"off"
:
GPIO
.
output
(
change_pin
,
GPIO
.
LOW
)
message
=
"Turned "
+
device_name
+
" off."
if
action
==
"toggle"
:
GPIO
.
output
(
change_pin
,
not
GPIO
.
input
(
change_pin
))
message
=
"Toggled "
+
device_name
+
"."
template_data
=
{
'message'
:
message
,
'pins'
:
pins
}
return
render_template
(
'main.html'
,
**
template_data
)
@
app
.
route
(
'/button'
)
def
button
():
print
(
"Button press"
)
...
...
templates/main.html
View file @
954ac304
<!DOCTYPE html>
<head>
<title>
{{ title }}
</title>
</head>
<body>
<h1>
Hello, World!
</h1>
<h2>
The date and time on the server is: {{ time }}
</h2>
<form
action=
"button"
>
<button
type=
"submit"
>
Press Button!
</button>
<form>
</body>
<head>
<title>
Current Status
</title>
</head>
<body>
<h1>
Device Listing and Status
</h1>
{% for pin in pins %}
<p>
The {{ pins[pin].name }}
{% if pins[pin].state == true %}
is currently on (
<a
href=
"/{{pin}}/off"
>
turn off
</a>
)
{% else %}
is currently off (
<a
href=
"/{{pin}}/on"
>
turn on
</a>
)
{% endif %}
</p>
{% endfor %}
{% if message %}
<h2>
{{ message }}
</h2>
{% endif %}
</body>
</html>
\ No newline at end of file
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