Skip to content

Commit c12e382

Browse files
authored
Merge pull request #152 from intel-iot-devkit/feature/python/examples/close-call-reporter
Merge Python Close Call Reporter Example
2 parents 2ef0e90 + d0d88d2 commit c12e382

File tree

18 files changed

+1418
-0
lines changed

18 files changed

+1418
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2015 - 2016 Intel Corporation.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

close-call-reporter/python/README.md

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# Close Call Reporter in Python*
2+
3+
## Introduction
4+
5+
This Close Call Reporter application is part of a series of how-to Intel® Internet of Things (IoT) code sample exercises using the Intel® IoT Developer Kit, Intel® Edison development platform, cloud platforms, APIs, and other technologies.
6+
7+
From this exercise, developers will learn how to:<br>
8+
- Connect the Intel® Edison development platform, a computing platform designed for prototyping and producing IoT and wearable computing products.<br>
9+
- Interface with the Intel® Edison platform IO and sensor repository using MRAA and UPM from the Intel® IoT Developer Kit, a complete hardware and software solution to help developers explore the IoT and implement innovative projects.<br>
10+
- Store the Close Call Reporter data using Azure Redis Cache\* from Microsoft\* Azure\*, Redis Store\* from IBM\* Bluemix\*, or ElastiCache\* using Redis\* from Amazon Web Services\* (AWS), different cloud services for connecting IoT solutions including data analysis, machine learning, and a variety of productivity tools to simplify the process of connecting your sensors to the cloud and getting your IoT project up and running quickly.
11+
- Set up a MQTT-based server using IoT Hub from Microsoft\* Azure\*, IoT from IBM\* Bluemix\*, or IoT from Amazon Web Services\* (AWS), different cloud machine to machine messaging services based on the industry standard MQTT protocol.
12+
13+
## What it is
14+
15+
Using an Intel® Edison board, this project lets you create a close call fleet driving reporter that:<br>
16+
- monitors the Grove* IR Distance Interrupter.<br>
17+
- monitors the Grove* GPS.<br>
18+
- keeps track of close calls and logs them using cloud-based data storage.
19+
20+
## How it works
21+
22+
This close call reporter system monitors the direction the IR Distance sensor is pointed to.
23+
24+
It also keeps track of the GPS position of the Intel® Edison board, updating the position frequently to ensure accurate data.
25+
26+
If a close call is detected (that is, the IR Distance sensor is tripped), the Intel® Edison board, if configured, notifies the Intel® IoT Examples Datastore or an MQTT server running in your own Microsoft\* Azure\*, IBM\* Bluemix\*, or AWS account.
27+
28+
## Hardware requirements
29+
30+
Grove* Transportation and Safety Kit, containing:
31+
32+
1. Intel® Edison with an Arduino* breakout board
33+
2. [Grove\* IR Distance Interrupter](http://iotdk.intel.com/docs/master/upm/node/classes/rfr359f.html)
34+
3. [Grove\* GPS](http://iotdk.intel.com/docs/master/upm/node/classes/ublox6.html)
35+
36+
DFRobot* Starter Kit for Intel® Edison, containing:
37+
38+
1. Intel® Edison with an Arduino* breakout board
39+
2. [IR Distance Sensor](http://www.dfrobot.com/index.php?route=product/product&product_id=572)
40+
3. [GPS](http://iotdk.intel.com/docs/master/upm/node/classes/ublox6.html)
41+
4. [I/O Expansion Shield](http://www.dfrobot.com/index.php?route=product/product&product_id=1009)
42+
43+
## Software requirements
44+
45+
1. Microsoft\* Azure\*, IBM\* Bluemix\*, or AWS account (optional)
46+
47+
### How to set up
48+
49+
To begin, clone the **How-To Intel IoT Code Samples** repository with Git* on your computer as follows:
50+
51+
$ git clone https://github.com/intel-iot-devkit/how-to-code-samples.git
52+
53+
To download a .zip file, in your web browser go to <a href="https://github.com/intel-iot-devkit/how-to-code-samples">https://github.com/intel-iot-devkit/how-to-code-samples</a> and click the **Download ZIP** button at the lower right. Once the .zip file is downloaded, uncompress it, and then use the files in the directory for this example.
54+
55+
### Connecting the Grove\* sensors
56+
57+
![](./../../images/js/access-control.jpg)
58+
59+
You need to have a Grove\* Shield connected to an Arduino\*-compatible breakout board to plug all the Grove devices into the Grove\* Shield. Make sure you have the tiny VCC switch on the Grove\* Shield set to **5V**.
60+
61+
1. Plug one end of a Grove\* cable into the Grove\* PIR Motion Sensor, and connect the other end to the D4 port on the Grove\* Shield.
62+
63+
2. Plug one end of a Grove\* cable into the Grove RGB LCD, and connect the other end to any of the I2C ports on the Grove\* Shield.
64+
65+
### Connecting the DFRobot\* sensors
66+
67+
![](./../../images/js/access-control-dfrobot.jpg)
68+
69+
You need to have a LCD Display Shield connected to an Arduino\*-compatible breakout board to plug all the DFRobot\* devices into the LCD Display Shield.
70+
71+
1. Plug one end of a DFRobot\* cable into the PIR (Motion) Sensor, and connect the other end to the A2 port on the LCD Display Shield.
72+
73+
### Intel® Edison board setup
74+
75+
If you're running this code on your Intel® Edison board, you need to install some dependencies by establishing an SSH session to the Edison and run the commands in the sections below.
76+
77+
#### Update the opkg repo
78+
79+
To add the Intel opkg repository:
80+
81+
$ echo "src mraa-upm http://iotdk.intel.com/repos/3.5/intelgalactic/opkg/i586" > /etc/opkg/mraa-upm.conf
82+
$ opkg update
83+
84+
You'll only need to perform this step once.
85+
86+
#### Git
87+
88+
To install Git\* on the Intel® Edison board (if you don’t have it yet):
89+
90+
$ opkg update
91+
$ opkg install git
92+
93+
#### MRAA and UPM Dependencies
94+
95+
To install the latest versions of the MRAA\* and UPM\* libraries:
96+
97+
$ opkg update
98+
$ opkg install mraa
99+
$ opkg install upm
100+
101+
#### Python Package Manager (pip)
102+
103+
To install the Python\* package manager needed to install and run the example:
104+
105+
$ pip install --upgrade pip setuptools
106+
107+
108+
#### Install the example
109+
110+
Once all dependencies are installed you can install the example itself with the following command:
111+
112+
$ pip install --src ~/python/examples/ -e "git+https://github.com/intel-iot-devkit/how-to-code-samples.git#egg=iot_close_call_reporter&subdirectory=close-call-reporter/python"
113+
114+
The `pip` command will install required Python dependencies, save the source code for the example in `~/python/examples/iot_close_call_reporter/` and link the package to the global Python `site-packages` folder.
115+
116+
### Intel® IoT Gateway setup
117+
118+
You can run this example using an Intel® IoT Gateway connected to an Arduino\*/Genuino\* 101.
119+
120+
Make sure your Intel® IoT Gateway is setup, by following the directions on the web site here:
121+
122+
https://software.intel.com/en-us/node/633284
123+
124+
You must install the Intel® XDK on the Intel® IoT Gateway, by following the directions on the above link, under the section "Connecting to the Intel® XDK".
125+
126+
The Arduino\*/Genuino\* 101 needs to have the Firmata\* firmware installed. If you have IMRAA installed on your gateway, this will be done automatically. Otherwise, install the StandardFirmata or ConfigurableFirmata sketch manully on to your Arduino\*/Genuino\* 101.
127+
128+
You will also need to configure the `config.json` in the example to use the Arduino\*/Genuino\* 101. See the section "Configuring the example" below.
129+
130+
### Data store server setup
131+
132+
Optionally, you can store the data generated by this sample program in a back-end database deployed using Microsoft\* Azure\*, IBM\* Bluemix\*, or AWS, along with Node.js\*, and a Redis\* data store.
133+
134+
For information on how to set up your own cloud data server, go to:
135+
136+
[https://github.com/intel-iot-devkit/intel-iot-examples-datastore](https://github.com/intel-iot-devkit/intel-iot-examples-datastore)
137+
138+
### MQTT server setup
139+
140+
You can also optionally store the data generated by this sample program using MQTT, a machine-to-machine messaging server. You can use MQTT to connect to Microsoft\* Azure\*, IBM\* Bluemix\*, or AWS.
141+
142+
For information on how to connect to your own cloud MQTT messaging server, go to:
143+
144+
[https://github.com/intel-iot-devkit/intel-iot-examples-mqtt](https://github.com/intel-iot-devkit/intel-iot-examples-mqtt)
145+
146+
## Configuring the example
147+
148+
When the example is installed through `pip` the `config.json` file that holds the configuration for the example lives in `~/python/examples/iot_close_call_reporter/close-call-reporter/python/iot_close_call_reporter/config.json`.
149+
150+
To configure the example for the Grove* kit, just leave the `kit` key in the `config.json` set to `grove`. To configure the example for the DFRobot* kit, change the `kit` key in the `config.json` to `dfrobot` as follows:
151+
152+
```JSON
153+
{
154+
"kit": "dfrobot"
155+
}
156+
```
157+
158+
To configure the example for the optional Microsoft\* Azure\*, IBM\* Bluemix\*, or AWS data store, add the `SERVER` and `AUTH_TOKEN` keys in the `config.json` file below the "CODE" key as follows:
159+
160+
```JSON
161+
{
162+
"kit": "grove",
163+
"SERVER": "http://intel-examples.azurewebsites.net/logger/access-control",
164+
"AUTH_TOKEN": "s3cr3t"
165+
}
166+
```
167+
168+
For information on how to configure the example for the optional Microsoft\* Azure\*, IBM\* Bluemix\*, or AWS MQTT messaging server, go to:
169+
170+
[https://github.com/intel-iot-devkit/intel-iot-examples-mqtt/](https://github.com/intel-iot-devkit/intel-iot-examples-mqtt/)
171+
172+
## Running the program manually
173+
174+
Once the example is installed through `pip` you can run the program by running the following command in an SSH session to the board:
175+
176+
$ python -m iot_close_call_reporter
177+
178+
### Determining the Intel® Edison board's IP address
179+
180+
You can determine what IP address the Intel® Edison board is connected to by running the following command:
181+
182+
ip addr show | grep wlan
183+
184+
You will see the output similar to the following:
185+
186+
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
187+
inet 192.168.1.13/24 brd 192.168.1.255 scope global wlan0
188+
189+
The IP address is shown next to `inet`. In the example above, the IP address is `192.168.1.13`.
190+
191+
IMPORTANT NOTICE: This software is sample software. It is not designed or intended for use in any medical, life-saving or life-sustaining systems, transportation systems, nuclear systems, or for any other mission-critical application in which the failure of the system could lead to critical injury or death. The software may not be fully tested and may contain bugs or errors; it may not be intended or suitable for commercial release. No regulatory approvals for the software have been obtained, and therefore software may not be certified for use in certain countries or environments.

close-call-reporter/python/iot_close_call_reporter/__init__.py

Whitespace-only changes.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright (c) 2015 - 2016 Intel Corporation.
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining
4+
# a copy of this software and associated documentation files (the
5+
# "Software"), to deal in the Software without restriction, including
6+
# without limitation the rights to use, copy, modify, merge, publish,
7+
# distribute, sublicense, and/or sell copies of the Software, and to
8+
# permit persons to whom the Software is furnished to do so, subject to
9+
# the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be
12+
# included in all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
from __future__ import print_function, division
23+
from time import sleep
24+
from signal import SIGINT, signal
25+
from atexit import register as register_exit
26+
from .runner import Runner
27+
28+
def main():
29+
30+
"""
31+
Start main function.
32+
"""
33+
34+
runner = Runner()
35+
print("Running {0} example.".format(runner.project_name))
36+
37+
def signal_handler(signum, frame):
38+
raise SystemExit
39+
40+
def exit_handler():
41+
print("exiting")
42+
exit(0)
43+
44+
register_exit(exit_handler)
45+
signal(SIGINT, signal_handler)
46+
47+
try:
48+
signal.pause()
49+
except AttributeError:
50+
while True:
51+
sleep(0.5)
52+
53+
if __name__ == "__main__":
54+
main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"kit": "grove"
3+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright (c) 2015 - 2016 Intel Corporation.
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining
4+
# a copy of this software and associated documentation files (the
5+
# "Software"), to deal in the Software without restriction, including
6+
# without limitation the rights to use, copy, modify, merge, publish,
7+
# distribute, sublicense, and/or sell copies of the Software, and to
8+
# permit persons to whom the Software is furnished to do so, subject to
9+
# the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be
12+
# included in all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
from collections import namedtuple
23+
from pkg_resources import resource_stream
24+
from simplejson import load as load_json
25+
26+
# hardware platform
27+
HardwareConfig = namedtuple("HardwareConfig", "kit platform")
28+
29+
KIT = "kit"
30+
PLATFORM = "PLATFORM"
31+
32+
Kits = namedtuple("Kits", "grove dfrobot")
33+
KNOWN_KITS = Kits(
34+
grove="grove",
35+
dfrobot="dfrobot"
36+
)
37+
38+
Platforms = namedtuple("Platforms", "firmata")
39+
KNOWN_PLATFORMS = Platforms(
40+
firmata="firmata"
41+
)
42+
43+
# app specific
44+
45+
# MQTT server
46+
MqttConfig = namedtuple("MqttConfig", "server port client_id username password cert key topic")
47+
48+
MQTT_SERVER = "MQTT_SERVER"
49+
MQTT_PORT = "MQTT_PORT"
50+
MQTT_CLIENTID = "MQTT_CLIENTID"
51+
MQTT_USERNAME = "MQTT_USERNAME"
52+
MQTT_PASSWORD = "MQTT_PASSWORD"
53+
MQTT_CERT = "MQTT_CERT"
54+
MQTT_KEY = "MQTT_KEY"
55+
MQTT_TOPIC = "MQTT_TOPIC"
56+
57+
# remote data store
58+
DataStoreConfig = namedtuple("DataStoreConfig", "server auth_token")
59+
60+
SERVER = "SERVER"
61+
AUTH_TOKEN = "AUTH_TOKEN"
62+
63+
# Load configuration data from `project/config.json` file.
64+
# Edit this file to change to correct values for your configuration
65+
66+
RESOURCE_PACKAGE = __name__
67+
RESOURCE_PATH = "config.json"
68+
69+
70+
with resource_stream(RESOURCE_PACKAGE, RESOURCE_PATH) as data:
71+
72+
RAW_CONFIG = load_json(data)
73+
74+
HARDWARE_CONFIG = HardwareConfig(
75+
kit=RAW_CONFIG.get(KIT, KNOWN_KITS.grove),
76+
platform=RAW_CONFIG.get(PLATFORM)
77+
)
78+
79+
MQTT_CONFIG = MqttConfig(
80+
server=RAW_CONFIG.get(MQTT_SERVER),
81+
port=RAW_CONFIG.get(MQTT_PORT),
82+
client_id=RAW_CONFIG.get(MQTT_CLIENTID),
83+
username=RAW_CONFIG.get(MQTT_USERNAME),
84+
password=RAW_CONFIG.get(MQTT_PORT),
85+
cert=RAW_CONFIG.get(MQTT_CERT),
86+
key=RAW_CONFIG.get(MQTT_KEY),
87+
topic=RAW_CONFIG.get(MQTT_TOPIC)
88+
) if {MQTT_SERVER, MQTT_CLIENTID, MQTT_TOPIC} <= set(RAW_CONFIG) else None
89+
90+
DATA_STORE_CONFIG = DataStoreConfig(
91+
server=RAW_CONFIG.get(SERVER),
92+
auth_token=RAW_CONFIG.get(AUTH_TOKEN)
93+
) if {SERVER, AUTH_TOKEN} <= set(RAW_CONFIG) else None

close-call-reporter/python/iot_close_call_reporter/hardware/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)