paint-brush
Building a Cellular-Connected IoT Electric Scooter w/ Soracom + Raspberry Pi in One Hour [Part 1]by@aahuang
9,087 reads
9,087 reads

Building a Cellular-Connected IoT Electric Scooter w/ Soracom + Raspberry Pi in One Hour [Part 1]

by Aaron HuangSeptember 8th, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Using Soracom, a SIM card, a Raspberry Pi, and GPS sensor, we can create a working prototype of an eScooter company. We chose the Xiaomi Mijia M365 electric scooter because it can be easily controlled via Bluetooth. To access these controls long-distance, we needed some kind of connection to the internet. We found that the only real solution was to use a cellular connection, which would ensure access from anywhere in the world. The data is sent securely through SORACOM Harvest (one of SoracOM’s many products available for their SIMs) and stored in the Soracome cloud.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Building a Cellular-Connected IoT Electric Scooter w/ Soracom + Raspberry Pi in One Hour [Part 1]
Aaron Huang HackerNoon profile picture


With the growth and visibility of ride sharing e-scooter companies like Lime and Bird, we were curious how hard it would be to build a working prototype using the same scooter they do (Xiaomi Mijia M365 Electric Scooter).

We (Devin and I) recently participated in the Hackster.io Cellular IoT Challenge sponsored by Soracom where we learned about Soracom’s cellular connectivity, management console, and several advanced features (cloud integration, data visualization, and remote management).  We realized that using Soracom, a Raspberry Pi, and GPS sensor, we could create a comprehensive working prototype of an eScooter company very easily and very quickly.  

In this multi-part series, we’ll be documenting how we built key components of a cellular IoT electric scooter company, roughly following the outline below. We’ll walk you through each step of our development process so that you can build your own cloud-connected electric scooter

  1. Connecting an electric scooter to the cloud with a cellular modem, SIM card, and Raspberry Pi
  2. Sending electric scooter data to the cloud and visualizing it
  3. Remotely managing the electric scooter
  4. Optimizing electric scooter operations and profits with serverless cloud functions

Here’s what you’ll need to follow along:

We chose the Xiaomi electric scooter because it is both widely used by existing eScooter companies (Bird, Lime, etc.) and because it can be easily controlled via Bluetooth. However, to access these controls long-distance, we needed some kind of connection to the internet. We found that the only real solution for an eScooter company was to use a cellular connection, which would ensure access from anywhere in the world. So, the Soracom kit was a perfect match. Included is a SIM card with global coverage and a Raspberry Pi, which has a built-in Bluetooth antenna and is easy to set up.

The Raspberry Pi acts as the “brains” of the eScooter. Not only will it communicate directly with the electric scooter’s Bluetooth controls, but it will also report various data points (location, battery, usage) to the Soracom cloud. Using the provided Soracom services for sending and displaying data, we can easily manage the electric scooter remotely. The data is sent securely through SORACOM Harvest (one of Soracom’s many products available for their SIMs) and stored in the Soracom cloud. This means that it is very difficult for eScooter data to be erased, allowing us to better keep track of exactly where the electric scooter’s been.

The GPS module here makes up for the lack of location tracking built into the electric scooter. Though the SIM card automatically gathers location data through its cellular network, its location estimate is very inaccurate. We attached an inexpensive GPS module onto the Raspberry Pi for more accurate tracking.

Raspberry Pi Setup

Setting up the Raspberry Pi in the next two steps will turn our normal Xiaomi electric scooter into a smart eScooter by connecting it to the cloud. Communications will pass through the Raspberry Pi, and it will be responsible for commanding the electric scooter through Bluetooth. 

The SORACOM Cellular IoT Starter Kit includes a Raspberry Pi Zero W, which fits the job perfectly. It is essentially a low-power computer with many different wireless connection options and a fairly easy setup process. 

The Raspberry Pi that came with our kit didn’t work straight out of the box, so there’s still a couple steps to do before that can happen. If you’ve never set up a Raspberry Pi before, we recommend the official Raspberry Pi setup guide.

Connect your Device to the Cloud

After this next step is completed, the Raspberry Pis will be able to connect to the internet regardless of whether or not it has a WiFi connection. This means that we’ll be able to send commands to and receive data from the electric scooter so that it can be used anywhere in the world. It also helps eliminate connection loss issues that might make it difficult for us to monitor the eScooter. We’ll do this by setting up the Raspberry Pi for use with the included Soracom SIM card. 

We’ll utilize Soracom’s SIM card offering called SORACOM Air, which is Soracom’s solution to connecting IoT devices to the internet. It acts as the gateway to the Soracom platform, but unlike many cellular IoT providers and services, Air also works with network providers and carriers around the world to provide global coverage.

The following is adapted from Soracom’s official documentation here.


To use the SORACOM Air SIM card with your Raspberry Pi, you’ll first need to add it to your Soracom account. Do this by visiting the user console and clicking “register SIM”. If you’ve added the card successfully, you should see the page refresh with a new offline entry in the table.


Next, you’ll need to add some dependencies. Open up a terminal on the Raspberry Pi or SSH into it and run the following commands: 

$ sudo curl -O https://soracom-files.s3.amazonaws.com/starterkit/setup.sh
$ sudo chmod +x setup.sh
$ sudo ./setup.sh

After this step, you should see a message asking you to plug the Huawei 3G modem in. At this point, pop out the SIM card and insert it into the modem dongle. The Huawei 3G modem’s case reveals a SIM card slot if you gently slide the top cover away from the USB port. 

Slide the SIM card into the modem, close it up, and connect it to the Raspberry Pi with the included cable.

The modem’s status indicator should now be a solid blue if a connection was established. If you did your setup correctly, you should now see that your SIM card is online on your SORACOM console.

If you’re having trouble getting the Huawei 3G modem to connect, you might need to add a new connection to the Raspberry Pi by running

$ sudo nmcli con add type gsm ifname "*" con-name soracom apn soracom.io user sora password sora

and rebooting the Raspberry Pi with 

$ sudo reboot 

Congrats! Your Raspberry Pi is now connected to SORACOM Air! Not only can you use SORACOM Air to access private and public services, but you can also use Air to track each IoT device’s data consumption, cellular location, and more. 

Connect the GPS Module

On the Soracom dashboard, there is a map indicating where the SIM card thinks it is. If SIM’s connection to their cloud is anything like ours, you’ll find that the estimate is not nearly accurate enough for locating an electric scooter. Therefore, we’ll need to attach an additional GPS module to the Raspberry Pi to keep track of exactly where the eScooter is. Of course, with the now-connected SIM card, we’ll be able to access this information from anywhere through the cloud.

We found this inexpensive GPS module called the Beitian BN-180 on Amazon. It seems to work surprisingly well. Before we start wiring the module up, let’s configure the Raspberry Pi to use hardware serial connections.

Type

$ sudo raspi-config

Navigate to

Interfacing Options
and press
ENTER

Go to

Serial
and press
ENTER

Disable the login shell by selecting

No
and press
ENTER

Enable the hardware serial by selecting

Yes
and press
ENTER

Reboot the Raspberry Pi.

You’ll want to connect the

red
wire to 3.3v, the
black
to ground, the
green
to TX, and finally
white
to RX. If your Raspberry Pi is powered on, the GPS module should now blink blue. If it’s flashing red as well, that means the module has locked onto the GPS satellites and is receiving data. We were able to get it blinking red indoors, but you may need to place the antenna on a windowsill or set the device up outside for it to connect.

If you’re using SSH and having trouble connecting after powering on the module, try disconnecting the GPS module, retrying SSH, and then reconnecting the GPS module.

To test our GPS module, we simply need to install a couple of packages. Open a terminal or SSH again and run

$ sudo apt install gpsd gpsd-clients

After that’s finished installing, let’s run the GPSD client

$ sudo gpsd /dev/ttyS0 -F /var/run/gpsd.sock

For my Raspberry Pi, `

/dev/ttyS0
` is the serial port that receives data from the GPS module. Let’s check the connection

$ cat /dev/ttyS0

If this logs out a continuous stream of data, the GPS module is working! This is raw GPS data in NMEA format. If you see a bunch of

unknown
messages in the log, try this command to silence those messages

$ stty -F /dev/ttyS0 -echo

Finally, let’s output this data into a nice format

$ cgps -s

The command may take a while for data to show up. If the

cat
command shows data while the
cgps
command does not, edit the
/etc/default/gpsd
file. Change the line

DEVICES=””

to include your serial port. For example, ours became

DEVICES=”/dev/ttyS0”

And restart the GPSD service

$ sudo systemctl restart gpsd

Now rerun the command

$ cgps -s

Great! The GPS module was set up successfully.

Connecting your IoT Device to the eScooter

Now that you have a Raspberry Pi ready to go with Soracom, let’s set it up for the electric scooter. We’ll finally connect our Raspberry Pi to the eScooter via the Xiaomi M365’s Bluetooth interface. By doing so, we’ll have access to trip and battery data, as well as switches for locking the eScooter. This makes managing the electric scooter programmatically and remotely much easier.

Thankfully, most of the reverse engineering required to communicate with the device has been completed. We just have to link the functions from the Python module to the Python controller.

I found this Xiaomi M365 Bluetooth module on GitHub by Anton Hakansson. This module will work great for our project. Let’s set up our Raspberry Pi to use this module

$ sudo apt install libglib2.0-dev python3-pip git vim -y

Before we can install the module, we need to install its dependency, Bluepy

$ sudo pip3 install bluepy

Let’s install the library

$ sudo pip3 install git+https://github.com/AntonHakansson/m365py.git#egg=m365py

And let’s download some examples to test out the module

$ git clone https://github.com/AntonHakansson/m365py

Before we can run the example programs, we need to get the eScooter’s Bluetooth MAC address. Luckily, the library provides a function that lets us do just that. Turn on the electric scooter and run

$ sudo python3 -m m365py

After five seconds, it should output an object with a MAC address. Copy this MAC address. Now in the cloned repository,

cd
into the
examples
directory. Edit the
main.py
file and change the contents of the
scooter_mac_address
variable on line 30 with the MAC address you copied from earlier. Save and close the file. Now run the program!

$ python main.py

Great! If all went well, the electric scooter’s Bluetooth data should be logging to the screen.

Conclusion

Once you’ve completed the steps above, the hardest part of setting up your electric scooter company should be done!  We created a cellular connection to Soracom and a Bluetooth connection to the Xiaomi Mijia M365 electric scooter on the Raspberry Pi. We can now access the internet on Raspberry Pi, and we are now able to collect data about the eScooter using its Bluetooth interface. 

For us, seeing each step be completed was really exciting. We were able to see the GPS module receive location data in real-time and even while the eScooter was moving. It was cool to see the Raspberry Pi connect to the SIM card and overcome the limitation of needing to be in range of a WiFi network to access the internet.

Our setup process went smoothly and took around forty minutes, including time spent waiting for packages to download. Following the commands were fairly easy as well, because many of the setup scripts were luckily bundled together by Soracom or other GitHub users. The most difficult part was working with the GPS module, which we’ve hopefully made more straightforward with our instructions above. 

Join us in the next article, where we’ll discuss how we can use the Soracom platform for data analytics and visualization. See you next time!