IoT Electric Scooter Cloud Data Collection and Visualization with Soracom + Raspberry Pi [Part 2]

Written by aahuang | Published 2019/09/22
Tech Story Tags: transportation | raspberry-pi | python | business-ideas | iot | scooters | latest-test-stories | hackernoon-top-story

TLDR IoT Electric Scooter Cloud Data Collection and Visualization with Soracom + Raspberry Pi [Part 2] The first part of our eScooter series will help secure the electric scooter by storing data remotely. We'll send each data packet through the secure cellular network, so it’ll be incredibly difficult for a malicious hacker to tamper with the scooter and access its data. In this step, we'll transition away from collecting data about each individual eScooters to aggregating data in the cloud.via the TL;DR App

We’ll assume that you’ve completed the steps from the first part of our eScooter series already. For broader overview of this project, check out the first article here.
Now that you have your electric scooter connected to the internet with SORACOM Air, let’s send some data to the cloud. This step will help secure the electric scooter by storing data remotely.
We'll send each data packet through Soracom's secure cellular network, so it’ll be incredibly difficult for a malicious hacker to tamper with the electric scooter and access its data. We’ll also be able to easily track the electric scooter’s location, battery, and speed. 
Normally, we’d have to create and manage our own database and server solutions to do all this, but luckily, Soracom has their own solution for managing data. We can just use SORACOM Harvest to collect and store data. It’s super easy to set up too. All the Raspberry Pi has to do is send an API request to the Harvest endpoint which can be accomplished in a couple lines of code. Soracom conveniently has another service on their platform called SORACOM Lagoon that lets us easily visualize our data with charts, maps, and tables. We don’t even have to write any code! Lagoon can connect to Harvest automatically, so we don’t need to worry about shuffling data between different servers and services.

Data Collection Setup

We’ll start by configuring SORACOM Harvest. This step will create a place to receive and store eScooter data from the Raspberry Pi, once we set that up later in this article. We’ll then be able to access this data for visualizations. 
Normally this step would consist of setting up a database and server endpoints for data entry. We would also have to worry about authentication and IoT security. However, Harvest combines and greatly simplifies this process. It provides a unified endpoint for data collection, abstracting away the need to construct your own server API. It also securely stores data in the cloud and provides authentication using the Air SIM card, preventing malicious hackers from falsifying data. All we have to do is configure SORACOM Harvest and configure our IoT device to send data to our Harvest endpoint.
Open up your Soracom console and click on your group. Find the SORACOM Harvest tab and open it. Now just toggle the switch on.
Once we have that set up, we’re done — no coding necessary!

Push Data into the Cloud with Raspberry Pi and Python

Let’s get back to our electric scooter. Now that we have endpoints for collecting data set up, we’ll need to configure the Raspberry Pi to actually use them. In this step, we’ll transition away from collecting data about each individual eScooter to aggregating data in the cloud. We’ll be able to see a broader overview of all our electric scooters, rather than manually querying each one.
The script on our Raspberry Pi from the last article let us access useful eScooter information such as its battery life, current speed, and GPS coordinates through the Xiaomi Mijia M365’s Bluetooth protocols. In this section, we’ll send this information to the SORACOM Harvest cloud.
To get started, let’s clone the code necessary to connect to SORACOM Harvest
$ git clone https://github.com/DevinMui/soracom-escooter-device
And let’s install some necessary modules
$ pip3 install -r requirements.txt
Inside
harvest.py
, we send an HTTP
POST
request to the SORACOM Harvest endpoint we set up in the last step. We’ll collect device data from the device object’s properties, convert it into a JSON object through the
requests
module, and return SORACOM Harvest’s response.
Before running
harvest.py
with Python, we’ll need to change the Bluetooth MAC address in the file to match the scooter we’re working on. This information should be available from the Bluetooth module we set up in the first article. Open up the file in a text editor and change line 7 to use your address. 
Now we can run the program by calling
$ python3 harvest.py
In a matter of seconds, data will start pouring into your Soracom user console. Take a look!

Visualize IoT Data 

After we have collected eScooter data, we need to be able to quickly access it. Through Harvest, we can access the raw data or simple tables and graphs, but it would be overwhelming to trudge through the data streams of each eScooter as more are added to the company or to generate more useful graphs like maps. We would instead rely on a visualization service to parse the data and return useful charts or graphs. Luckily, Soracom has provided Lagoon. It connects to Harvest and offers intuitive dashboards without setting up additional infrastructure. Using Lagoon, we can quickly see which electric scooters need maintenance or a charge. The map functionality is also useful to find out exactly where each eScooter is. Lagoon also lets us set up alerts about the data it’s getting, so we can get notified when an eScooter needs a quick trip to repairs. 
Let’s begin by setting up Lagoon to show every eScooter on a map.
Let’s list out every eScooter and its corresponding data. Click on add a panel and table. Now in the settings, select your data source as Harvest. This will automatically pull data from Harvest. You can then select what data points you want to view in your chart.
A simple list is kind of boring though so let’s make a map to visualize all the eScooters in our location. Again, click new panel. This time, click Soracom Map Panel. In the settings, select Harvest as your data source. Your map should look like this now.
Now, let’s graph our electric scooter’s battery usage. Again, click new panel. Select Harvest as the data source in the options. Select battery as one of the data points to view. Your graph should look something like this now. 
We can even set up real-time alerts by email, Slack, or webhook! Let’s make it so that we get an email whenever a eScooter is running low on power.
Under the graph settings panel, there should be an alert tab. Click on alert. Name your alert and change the settings however you like. We set it up to alert us when the last battery value is below 1% and set it to evaluate every 60 seconds. In the notifications tab, edit the send to value to be an email address or phone number. When the alert is triggered, you will be notified through this value.
Now, when the eScooter drops to under 1% battery, we’ll get an email notification along with the electric scooter’s GPS coordinates on a map along with its current data.

Conclusion

You should now be able to securely stream and visualize important eScooter metrics, like position and battery. We created a SORACOM Harvest endpoint and used the networking capabilities of the Raspberry Pi to send data to the cloud. We also created a Lagoon dashboard, which provides us with useful charts and graphs generated from our data.
We could see the potential uses for eScooter data as we watched the data flow in from the Raspberry Pi. We especially liked the convenience and speed of the service and thought about applications that could leverage these benefits. For example, we could set up Harvest and Lagoon to help manage a maintenance crew or reward users for charging eScooters. We could also manipulate eScooter pricing based on the data we collected and add additional electric scooters in areas with increased usage. 
Setting up the two services was fairly easy and took much less time than the setup process in the first article. Both Lagoon and Harvest have fairly intuitive user interfaces and it didn’t take long to configure it to do exactly what we wanted. We didn’t even need to install any packages on the Raspberry Pi. 
In the next article, we’ll remotely connect to and manage our scooter via SSH and HTTP. See you next time!


Written by aahuang | Software Engineer and Student @ UC Berkeley
Published by HackerNoon on 2019/09/22