Skip to content

Commit 40228d0

Browse files
committed
example starter app
1 parent 0e0a30f commit 40228d0

File tree

10 files changed

+209
-20
lines changed

10 files changed

+209
-20
lines changed

.cfignore

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
5+
# C extensions
6+
*.so
7+
8+
# Distribution / packaging
9+
.Python
10+
env/
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
lib/
17+
lib64/
18+
parts/
19+
sdist/
20+
var/
21+
*.egg-info/
22+
.installed.cfg
23+
*.egg
24+
25+
# PyInstaller
26+
# Usually these files are written by a python script from a template
27+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
28+
*.manifest
29+
*.spec
30+
31+
# Installer logs
32+
pip-log.txt
33+
pip-delete-this-directory.txt
34+
35+
# Unit test / coverage reports
36+
htmlcov/
37+
.tox/
38+
.coverage
39+
.cache
40+
nosetests.xml
41+
coverage.xml
42+
43+
# Translations
44+
*.mo
45+
*.pot
46+
47+
# Django stuff:
48+
*.log
49+
50+
# Sphinx documentation
51+
docs/_build/
52+
53+
# PyBuilder
54+
target/

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: python hello.py
1+
web: python server.py

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
Hello World in Python on Cloud Foundry using Flask
1+
Python app to demonstrate using inputs and outputs on a Raspberry Pi
22
================================================================================
33

4-
This is a sample application showing how to deploy a simple hello world app
5-
using Cloud Foundry and the Python build pack.
4+
This is a sample application shows how to use simple inputs and outputs on a
5+
Raspberry Pi. There is a server side component that runs in Bluemix (Cloud Foundry) and
6+
a Python client that runs on a Raspberry Pi.
7+
8+
The input is a simple button. The output is a simple LED. If you click on/off
9+
in the web UI in Bluemix it will turn a LED on/off. If you press the button
10+
it will send a text message using Twilio to the phone number and message you
11+
type in the web ui.
612

713

814

@@ -13,7 +19,7 @@ To Use
1319
cf push myappname
1420
```
1521

16-
Replace myapp name with the name of your app (ex. hello-world)
22+
Replace myapp name with the name of your app (ex. python-raspberry-pi)
1723

1824
or click the button below
1925

__init__.py

Whitespace-only changes.

client.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import RPi.GPIO as GPIO
2+
import time
3+
import os, json
4+
import ibmiotf.application
5+
import uuid
6+
7+
GPIO.setmode(GPIO.BCM)
8+
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
9+
GPIO.setup(17, GPIO.OUT)
10+
11+
client = None
12+
13+
def myCommandCallback(cmd):
14+
if cmd.event == "light":
15+
command = cmd.payload["d"]["command"]
16+
print command
17+
if command == "on":
18+
GPIO.output(17, True)
19+
elif command == "off":
20+
GPIO.output(17, False)
21+
22+
try:
23+
options = ibmiotf.application.ParseConfigFile("/home/pi/device.cfg")
24+
options["deviceId"] = options["id"]
25+
options["id"] = "aaa" + options["id"]
26+
print options
27+
client = ibmiotf.application.Client(options)
28+
client.connect()
29+
client.deviceEventCallback = myCommandCallback
30+
client.subscribeToDeviceEvents(event="light")
31+
32+
while True:
33+
GPIO.wait_for_edge(18, GPIO.FALLING)
34+
print "Button Pushed"
35+
myData = {'buttonPushed' : True}
36+
client.publishEvent("raspberrypi", options["deviceId"], "input", myData)
37+
time.sleep(0.2)
38+
39+
except ibmiotf.ConnectionException as e:
40+
print e
41+

hello.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

manifest.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
---
22
applications:
3-
- name: hello-world-flask
3+
- name: python-iot-hackathon
44
memory: 128M
5+
services:
6+
- iot-python
7+
- twilio

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
Flask
2+
ibmiotf
3+
twilio

server.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from flask import Flask,redirect
2+
from flask import render_template
3+
from flask import request
4+
import os, json
5+
import time
6+
import ibmiotf.application
7+
from twilio.rest import TwilioRestClient
8+
9+
vcap = json.loads(os.getenv("VCAP_SERVICES"))
10+
twilioAccount = vcap["user-provided"][0]["credentials"]["accountSID"]
11+
twilioToken = vcap["user-provided"][0]["credentials"]["authToken"]
12+
twilioClient = TwilioRestClient(twilioAccount, twilioToken)
13+
14+
client = None
15+
16+
phoneNumberTo = ""
17+
textMessage = "Button Pushed"
18+
phoneNumberFrom = os.getenv("PHONE_NUMBER_FROM")
19+
deviceId = os.getenv("DEVICE_ID")
20+
21+
def myCommandCallback(cmd):
22+
global phoneNumberTo
23+
global textMessage
24+
25+
buttonPushed = cmd.payload["d"]["buttonPushed"]
26+
message = twilioClient.messages.create(to=phoneNumberTo, from_=phoneNumberFrom, body=textMessage)
27+
print buttonPushed
28+
try:
29+
options = {
30+
"org": vcap["iotf-service"][0]["credentials"]["org"],
31+
"id": vcap["iotf-service"][0]["credentials"]["iotCredentialsIdentifier"],
32+
"auth-method": "apikey",
33+
"auth-key": vcap["iotf-service"][0]["credentials"]["apiKey"],
34+
"auth-token": vcap["iotf-service"][0]["credentials"]["apiToken"]
35+
}
36+
client = ibmiotf.application.Client(options)
37+
client.connect()
38+
39+
client.deviceEventCallback = myCommandCallback
40+
client.subscribeToDeviceEvents(event="input")
41+
42+
except ibmiotf.ConnectionException as e:
43+
print e
44+
45+
app = Flask(__name__)
46+
47+
if os.getenv("VCAP_APP_PORT"):
48+
port = int(os.getenv("VCAP_APP_PORT"))
49+
else:
50+
port = 8080
51+
52+
@app.route('/')
53+
def hello():
54+
return render_template('index.html')
55+
56+
@app.route('/light/<command>', methods=['GET', 'POST'])
57+
def light_route(command):
58+
print command
59+
myData = {'command' : command}
60+
client.publishEvent("raspberrypi", deviceId, "light", myData)
61+
return redirect("/", code=302)
62+
63+
@app.route('/phoneNumber', methods=['POST'])
64+
def phone_number_route():
65+
global phoneNumberTo
66+
global textMessage
67+
68+
phoneNumber = request.form['phoneNumber']
69+
textMessage = request.form['message']
70+
phoneNumberTo = "+1" + phoneNumber
71+
72+
return redirect("/", code=302)
73+
74+
if __name__ == '__main__':
75+
app.run(host='0.0.0.0', port=port)

templates/index.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Hello from Flask</title>
5+
</head>
6+
<body>
7+
<h1>Simple Python App to Turn a Light on an off</h1>
8+
<br />
9+
<form action="/phoneNumber" method="POST">
10+
<input name="phoneNumber" type="text" placeholder="5555551212"/>
11+
<input name="message" type="text" placeholder="Button Pushed"/>
12+
<input type="submit" value="Save my phone number and text message" />
13+
</form>
14+
<br />
15+
<form action="/light/on" method="POST">
16+
<input type="submit" value="Turn Light On">
17+
</form>
18+
<form action="/light/off" method="POST">
19+
<input type="submit" value="Turn Light Off">
20+
</form>
21+
</body>
22+
</html>

0 commit comments

Comments
 (0)