"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The data that we use throughout this example was collected from a Raspberry Pi Sense HAT, located by a window on the 8th floor of LowFlyingHawk. A simple Python script periodically collects temperature, humidity, and pressure readings from the Sense HAT, and publishes the data as messages to an MQTT topic served by the AWS IoT Core MQTT broker. From the AWS IoT Analytics service console, we define a channel that subscribes to the topic. A pipeline moves data from the channel to a data store, and a data set query makes that data available to the AWS Python client (boto3).\n",
"\n",
"We can use boto3 to load the data set to a Jupyter notebook and analyze that data!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Step 1 | Import prerequisite libraries
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We need to import some Python libraries to interact with the AWS IoT service, and to work with our data."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import matplotlib\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"from itertools import islice\n",
"import boto3\n",
"import json\n",
"import os\n",
"from six.moves.urllib import request"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Step 2 | Import data
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before we load any data from an IoT Analytics dataset to our notebook, we need to initiliaze the client:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Download the IoT Analytics service model to access the dataset\n",
"request.urlretrieve('https://s3.amazonaws.com/iotanalytics-templates/service-2.json', '/home/ec2-user/service-2.json')\n",
"service_model_dir = boto3.__file__.replace('boto3/__init__.py', 'botocore/data/iotanalytics/2017-11-27')\n",
"os.system('echo %s | %s' % ('', \"sudo -S mkdir -p \" + service_model_dir))\n",
"os.system('echo %s | %s' % ('', \"sudo -S cp /home/ec2-user/service-2.json \" + service_model_dir + \"/service-2.json\"))\n",
"\n",
"# Detect the current region and create IoT Analytics client\n",
"region_name = json.loads(request.urlopen('http://169.254.169.254/latest/dynamic/instance-identity/document').read().decode())['region']\n",
"client = boto3.client('iotanalytics', region_name)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Define URL and read in data\n",
"dataset = \"office_dataset\"\n",
"dataset_url = client.get_dataset_content(datasetName = dataset)['entries'][0]['dataURI']\n",
"weather_df = pd.read_csv(dataset_url)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"