The IoT is a network of connected devices. We typically think of laptops, tablets, phones and desktops as devices that connect to the internet. But more devices are internet connected, like the Nest thermostat, the dropcam camera, Cree WiFi lightbulbs, amazon echo and google home hubs.

This post came about from working with a class on accessing data from the ThingSpeak.com and internet of things cloud service. Small devices connected to the internet can push data up to ThingSpeak.com. Using a web API, data can be pulled down from ThingSpeak for analysis.

In this post we will build a plot from some data up on ThingSpeak.com. Each time a chuck of code runs, I'm going to compare it to the MATLAB code to accomplish the same thing.

In Python, the first thing we need to do is import the necessary packages. We'll import numpy as np, pandas as pd and matplotlib.pyplot as plt. These are the trifecta of Python packages for Engineers. The %matplotlib inline command is needed if working in Jupyter notebooks.

In MATLAB, none of these imports are necssary. All of the functions to pull down the data and plot it are built in.


In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

Next we'll ask the user for the weather type and number of data points. In Python and MATLAB this is pretty easy


In [2]:
wtype = input('What weather type: ')
num_pts = input('How many data points: ')


What weather type: humid
How many data points: 600

In [3]:
d = {'temp':'4','humid':'5','rain':'6','press':'7'}
url1='https://api.thingspeak.com/channels/12397/fields/'
url2 = d[wtype]
url3 = '.csv?results='
url4 = str(num_pts)
#url = 'https://api.thingspeak.com/channels/12397/fields/4.csv?results=20'
url = url1+url2+url3+url4

In [4]:
df = pd.read_csv(url)

In [5]:
df.head()


Out[5]:
created_at entry_id field5
0 2018-03-01 19:30:32 UTC 1762949 0.0
1 2018-03-01 19:31:33 UTC 1762950 0.0
2 2018-03-01 19:32:33 UTC 1762951 0.0
3 2018-03-01 19:33:35 UTC 1762952 0.0
4 2018-03-01 19:34:36 UTC 1762953 0.0

In [7]:
plt.plot(df['field5'])


Out[7]:
[<matplotlib.lines.Line2D at 0x1a25d04c3c8>]

In [ ]: