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.
Data Collection Setup
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
, we send an HTTP
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
module, and return SORACOM Harvest’s response.
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
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.
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!