Skip to content

Commit b9f937a

Browse files
authored
Merge pull request intel-iot-devkit#151 from intel-iot-devkit/feature/python/examples/sound-detector
Merge Python Sound Detector Example
2 parents c12e382 + 3e7ea91 commit b9f937a

18 files changed

+1453
-0
lines changed

sound-detector/python/LICENSES.md

+20
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.

sound-detector/python/README.md

+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
# Sound Detector in Python*
2+
3+
## Introduction
4+
5+
This Sound Detector 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 Sound Detector 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 smart loudness detector system that:<br>
16+
- monitors a microphone sensor to detect the loudness in the area.<br>
17+
- keeps track of access, using cloud-based data storage.
18+
19+
## How it works
20+
21+
This sound detector monitor uses the sensor to constantly keep track of the decibel level in a room.
22+
Every 1sec the sensor finds the average sound samples and prints it to the RGB lcd screen.
23+
24+
Additionally, various events (looking-for-motion, motion-detected, invalid-code, etc.) are logged.
25+
Optionally, all data can be stored using the Intel® IoT Examples Datastore running in your own Microsoft Azure account.
26+
Also, optionally, the monitor stores the loudness data using the Intel® IoT Example Data store running in your own Microsoft Azure* account.
27+
28+
## Hardware requirements
29+
30+
Grove* Starter Kit Plus containing:
31+
32+
1. Intel® Edison platform with an Arduino* breakout board.
33+
2. [Grove Sound Sensor](http://iotdk.intel.com/docs/master/upm/node/classes/microphone.html).
34+
3. [Grove RGB LCD](http://iotdk.intel.com/docs/master/upm/node/classes/jhd1313m1.html).
35+
36+
DFRobot\* Starter Kit for Intel® Edison, containing:
37+
38+
1. Intel® Edison with an Arduino\* breakout board.
39+
2. [Analog Sound Sensor](http://iotdk.intel.com/docs/master/upm/node/classes/loudness.html).
40+
3. [LCD Keypad Shield](http://iotdk.intel.com/docs/master/upm/node/classes/sainsmartks.html).
41+
42+
## Software requirements
43+
44+
1. Microsoft\* Azure\*, IBM\* Bluemix\*, or AWS account (optional)
45+
46+
### How to set up
47+
48+
To begin, clone the **How-To Intel IoT Code Samples** repository with Git* on your computer as follows:
49+
50+
$ git clone https://github.com/intel-iot-devkit/how-to-code-samples.git
51+
52+
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.
53+
54+
### Installing the program manually on the Intel® Edison board
55+
56+
You can set up the code manually on the Intel® Edison board.
57+
58+
Clone the **How-To Intel IoT Code Samples** repository to your Intel® Edison board after you establish an SSH connection to it, as follows:
59+
60+
$ git clone https://github.com/intel-iot-devkit/how-to-code-samples.git
61+
62+
Then, navigate to the directory with this example.
63+
64+
To install Git\* on the Intel® Edison board (if you don’t have it yet), establish an SSH connection to the board and run the following command:
65+
66+
$ opkg install git
67+
68+
### Connecting the Grove\* sensors
69+
70+
![](./../../images/java/sound-detector.jpg)
71+
72+
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**.
73+
74+
1. Plug one end of a Grove cable into the Grove Sound Sensor, and connect the other end to the A0 port on the Grove Shield.
75+
76+
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.
77+
78+
### Connecting the DFRobot\* sensors
79+
80+
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.
81+
82+
1. Plug one end of a DFRobot\* cable into the Analog Sound Sensor, and connect the other end to the A2 port on the LCD Display Shield.
83+
84+
### Intel® Edison board setup
85+
86+
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.
87+
88+
#### Update the opkg repo
89+
90+
To add the Intel opkg repository:
91+
92+
$ echo "src mraa-upm http://iotdk.intel.com/repos/3.5/intelgalactic/opkg/i586" > /etc/opkg/mraa-upm.conf
93+
$ opkg update
94+
95+
You'll only need to perform this step once.
96+
97+
#### Git
98+
99+
To install Git\* on the Intel® Edison board (if you don’t have it yet):
100+
101+
$ opkg update
102+
$ opkg install git
103+
104+
#### MRAA and UPM Dependencies
105+
106+
To install the latest versions of the MRAA\* and UPM\* libraries:
107+
108+
$ opkg update
109+
$ opkg install mraa
110+
$ opkg install upm
111+
112+
#### Python Package Manager (pip)
113+
114+
To install the Python\* package manager needed to install and run the example:
115+
116+
$ pip install --upgrade pip setuptools
117+
118+
119+
#### Install the example
120+
121+
Once all dependencies are installed you can install the example itself with the following command:
122+
123+
$ pip install --src ~/python/examples/ -e "git+https://github.com/intel-iot-devkit/how-to-code-samples.git#egg=iot_sound_detector&subdirectory=sound-detector/python"
124+
125+
The `pip` command will install required Python dependencies, save the source code for the example in `~/python/examples/iot_sound_detector/` and link the package to the global Python `site-packages` folder.
126+
127+
### Intel® IoT Gateway setup
128+
129+
You can run this example using an Intel® IoT Gateway connected to an Arduino\*/Genuino\* 101.
130+
131+
Make sure your Intel® IoT Gateway is setup, by following the directions on the web site here:
132+
133+
https://software.intel.com/en-us/node/633284
134+
135+
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".
136+
137+
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.
138+
139+
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.
140+
141+
### Data store server setup
142+
143+
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.
144+
145+
For information on how to set up your own cloud data server, go to:
146+
147+
[https://github.com/intel-iot-devkit/intel-iot-examples-datastore](https://github.com/intel-iot-devkit/intel-iot-examples-datastore)
148+
149+
### MQTT server setup
150+
151+
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.
152+
153+
For information on how to connect to your own cloud MQTT messaging server, go to:
154+
155+
[https://github.com/intel-iot-devkit/intel-iot-examples-mqtt](https://github.com/intel-iot-devkit/intel-iot-examples-mqtt)
156+
157+
## Configuring the example
158+
159+
When the example is installed through `pip` the `config.json` file that holds the configuration for the example lives in `~/python/examples/iot_sound_detector/sound-detector/python/iot_sound_detector/config.json`.
160+
161+
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:
162+
163+
```JSON
164+
{
165+
"kit": "dfrobot"
166+
}
167+
```
168+
169+
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:
170+
171+
```JSON
172+
{
173+
"kit": "grove",
174+
"SERVER": "http://intel-examples.azurewebsites.net/logger/access-control",
175+
"AUTH_TOKEN": "s3cr3t"
176+
}
177+
```
178+
179+
For information on how to configure the example for the optional Microsoft\* Azure\*, IBM\* Bluemix\*, or AWS MQTT messaging server, go to:
180+
181+
[https://github.com/intel-iot-devkit/intel-iot-examples-mqtt/](https://github.com/intel-iot-devkit/intel-iot-examples-mqtt/)
182+
183+
## Running the program
184+
185+
Once the example is installed through `pip` you can run the program by running the following command in an SSH session to the board:
186+
187+
$ python -m iot_sound_detector
188+
189+
### Determining the Intel® Edison board's IP address
190+
191+
You can determine what IP address the Intel® Edison board is connected to by running the following command:
192+
193+
ip addr show | grep wlan
194+
195+
You will see the output similar to the following:
196+
197+
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
198+
inet 192.168.1.13/24 brd 192.168.1.255 scope global wlan0
199+
200+
The IP address is shown next to `inet`. In the example above, the IP address is `192.168.1.13`.
201+
202+
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.

sound-detector/python/iot_sound_detector/__init__.py

Whitespace-only changes.
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()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"kit": "grove"
3+
}
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

sound-detector/python/iot_sound_detector/hardware/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)