Living in a big city, the air pollution can have quite an impact on your health. About 1.5 years ago, the folks at Clairy put a Kickstarter campaign live, that targeted this problem: Clairy, the natural air purifier. A smart flower pot, that could cleanse your room's air from pollution. Apart from that the device also has some handy sensors for room temperature, humidity and indoor air quality (IAQ). Two months ago, the pots were finally delivered and I'm one of the lucky supporters of the project.
Clairy offers an app that gives you access to the sensor data, but as a data geek this is of course not satisfying enough. This is why I decided to find out how the app gets its data and if we can use it in a different way. For example inside a Python script.
Part 1 will be focusing on the basic setup to intercept traffic from your phone. The next part will dissect the API and use it in a Python script.
How does the app get data from our flower pot?
As the pot is integrated in my wireless network, the data gets sent to Clairy's servers. The app then calls an API to display the data. This means, we can use these API calls in our script as well.
But how to find out how the API calls must be structured?
As mobile devices usually are more closed of then a desktop os, we can't just see all the network connections on the device. This is at least true for Apple devices. What we can do instead, is to tunnel all network traffic from our smartphone through a proxy server. With this proxy, we can then see all incoming and outgoing network connections our phone apps make. Tools like Fiddler, Charles and Wireshark let us set up this kind of proxy on a desktop computer. We can then configure our phone's WiFi to use this proxy. For our test case I used Charles, as it is very straight forward and easy to use. The trial version should be sufficient.
Steps (MacBook and iPhone in my case)
1. Install Charles on your laptop or desktop computer
This should be pretty straight forward. Download Charles and follow the install instructions.
2. Configuration of Charles and your iPhone
Open Charles and go to "Help" --> "Local IP Adress". Here you can see all your network interfaces. Look out for the one you are connected with. The information you will need is the IP adress in the format xxx.xxx.xxx.xxx. This is the network adress, where your proxy is available to other devices.
Now open your phones WiFi settings. You need to be on the same WiFi as the device you run Charles on. Tab the "i" icon beneath the connected network and add a HTTP proxy (manual tab) with the IP from above and a port. The default Charles port will be 8888 (Be aware that Charles may interfere with other applications on your mac as Jupyter Notebook (same port 8888) for example. You can change the port in Charles' proxy settings Proxy --> Proxy Settings). (Link to Charles' documentation)
3. Now Charles asks if you want to allow traffic coming from your phone through the proxy (Of course we want that)
4. Decrypt SSL traffic
A major part of the connections made from your apps will be via SSL. This is a problem as it can't be read by Charles by default. Therefore we need to install a root certificate on our phone. This is just a matter of opening http://www.charlesproxy.com/getssl from your phone's browser and following the instructions. (Link to Charles' documentation)
- After that, you should see connections coming in from your phone. To check, you can open Safari on your phone and browse to http://google.com for example. All connections should show up in Charles.
Now we are good to go to fire up the Clairy app and find out what API calls it makes to load data. This will be part two of the tutorial, which is coming up next week. Be sure to subscribe to my blog.
Subscribe to All things Data Science
Get the latest posts delivered right to your inbox