Handle Ambient data in Python

We will introduce an example of reading the data of the sensor data visualization service "Ambient" in Python, converting it to a pandas DataFrame, and graphing it. The data treated as an example is the data such as temperature, humidity, and atmospheric pressure measured by ESP8266 Arduino, and it is "Veranda Environmental Monitor @ Setagaya Sakuraoka" Is published as.

The environment in which Python was executed

Ambient

Ambient is an IoT data visualization service that receives, stores, and visualizes (graphs) sensor data sent from microcomputers. You can easily send sensor data from Arduino, mbed, Raspberry Pi, etc. to Ambient and graph it.

Ambient structure

For more information on how to use Ambient, see "Try Ambient". For an example of the ambient monitor, see "Making a home ambient monitor with ESPr Developer + Ambient".

Installing the Ambient Python library

Ambient's Python library has the ability to send data to Ambient and the ability to read the data stored in Ambient.

You can install the library from Github as follows.

$ pip install git+https://github.com/TakehikoShimojima/ambient-python-lib.git

For more information on the library, see "Python Library".

Loading the library

From here on, I'm running on jupyter notebook. Load the library as follows.

import

Read data from Ambient

First, specify the channel Id, write key, and read key to create an instance of Ambient. If you only want to read, you can specify "Write key". In the case of public channel, the read key can be omitted. The data handled as an example this time is published as "Veranda Environment Monitor @ Setagaya Sakuragaoka". , Channel Id is 102, so you can create an instance as below.

instance

There are two ways to read data: specify the number of data items, specify the date, and specify the period.

Both methods return an array in dictionary format (JSON format) that looks like this:

[
    {'created': '2017-02-25T15:01:48.000Z', 'd1':Numerical value, 'd2':Numerical value, 'd3':Numerical value},
    {'created': '2017-02-25T15:06:47.000Z', 'd1':Numerical value, 'd2':Numerical value, 'd3':Numerical value},
    ...
]

The data creation time'created'is displayed in Coordinated Universal Time (UTC). The data is sorted in ascending order of generation time (oldest to newest).

read

Convert to pandas DataFrame

Load the modules you will need in the future.

pandas

Then convert this data to a pandas DataFrame. It's really easy, just do the following:

dataframe

The data generation time df ['created'] is the character string. If this is left as it is, the subsequent processing will be inconvenient, so convert this to pandas Timestamp.

type created

convert

The character string is converted to Datetime with strptime (), and Japan ('Asia / Tokyo') is set as the time zone.

Since read_csv of pandas has a function to parse the time in string format and convert it to Timestamp, I think that there is a similar function when creating a DataFrame from an array in dictionary format, but I could not find it. , I converted it by myself as above. If anyone knows an easier way to do this, please let me know.

Graph the data with matplotlib

Next, let's graph the data with matplotlib.

matplotlib

First, let's make a line graph of the read temperature on March 3rd.

temp graph

Next, let's graph the correlation between temperature and humidity.

scatter

Let's graph the changes in daily temperature and humidity with temperature on the left axis and humidity on the right axis.

two axes

Up to this point, you can graph with the Ambient function, but with Python + pandas + matplotlib, you can graph more flexibly.

Let's calculate and graph the moving average of the same temperature and humidity as above. You can calculate the moving average of width 7 by using df ['d1']. Rolling (window = 7, min_period = 1) .mean () instead of df ['d1'] as data.

rolling

Let's make a scatterplot matrix of temperature, humidity, barometric pressure, power supply voltage, and illuminance.

scattermatrix

Summary

By combining Ambient with Python, pandas, and matplotlib, sensor data controlled by a microcomputer can be accumulated, simple visualization (graphing) can be performed with Ambient, and more flexible analysis and visualization can be performed with Python. I hope it will be helpful when analyzing actual sensor data with Python.

Recommended Posts

Handle Ambient data in Python
Handle NetCDF format data in Python
Handle markdown in python
Handle Parquet in Python
Display UTM-30LX data in Python
Handle environment variables in Python
Handle complex numbers in Python
Get Leap Motion data in Python.
Read Protocol Buffers data in Python3
Get data from Quandl in Python
Handle posix message queues in python
Handle GDS II format in Python
Hashing data in R and Python
How to handle Japanese in Python
Handle multiple python versions in one jupyter
Get additional data in LDAP with python
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Data input / output in Python (CSV, JSON)
Meta-analysis in Python
Ant book in python: Sec. 2-4, data structures
Unittest in python
Try working with binary data in Python
Data analysis python
Epoch in Python
Discord in Python
Get Google Fit API data in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Python: Preprocessing in machine learning: Data acquisition
Get Youtube data in Python using Youtube Data API
Plink in Python
Constant in python
Easily graph data in shell and Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Python: Preprocessing in machine learning: Data conversion
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python