How to Control your IoT Electric Scooter Fleet Remotely with SORACOM Napter [Part 3]

Written by aahuang | Published 2019/10/15
Tech Story Tags: transportation | raspberry-pi | python | business-ideas | iot | scooters | latest-test-stories | soracom

TLDR How to Control your IoT Electric Scooter Fleet Remotely with SORACOM Napter [Part 3] In this article, we’ll walk you through how we set up Napter on the Raspberry Pi and how we used it to remotely manage the electric scooter. Napter enables us to open ports to communicate with cellular connected devices. This allows us to set up remote maintenance, troubleshooting, and other remote access tasks on our IoT eScooter without traditional relay servers or software agents.via the TL;DR App

We’ll assume that you’ve completed the steps from the first part of our eScooter series already. For a broader overview of this project, check out the first article here.
In the previous article, we were able to get our IoT eScooter sending data to the SORACOM cloud within just a few minutes. After we collect our data, we will need a way to remotely control and manage our eScooter based on the information we collected. 
To do that, we need a tool to access and manage our IoT devices. It also needs to be secure despite a remote connection, so that it will be difficult for malicious hackers to tamper with the electric scooter but also allow us to send commands to it regardless of location.
Once again, Soracom’s diverse range of services has one suited for our purpose: SORACOM Napter. This service lets us expose a TCP port for a limited time through the SORACOM Air SIM card, which we’ll use to control the connected Raspberry Pi. 
In this article, we’ll walk you through how we set up Napter on the Raspberry Pi, and how we used it to remotely manage the electric scooter.

SSH Setup with SORACOM Napter

To manage a Raspberry Pi on our local network, we could issue commands to it via SSH without much trouble. However, our fleet of cellular-connected electric scooters will need to be connected to a number of different cellular towers rather than on our local WiFi network. 
Napter enables us to open ports to communicate with our cellular connected devices. We can use a TCP protocol like SSH or HTTP to communicate with the Raspberry Pi from our computer. This allows us to set up remote maintenance, troubleshooting, and other remote access tasks on our IoT eScooter without traditional relay servers or software agents.
For our project, we’ll use Napter to connect to our scooter via SSH. We’ll also expose a port for HTTP purposes for remote maintenance and troubleshooting purposes through a more visual interface than the terminal. 
Like the other Soracom services we’ve used so far, Napter’s setup is quite simple. Rather than spending hours deploying relay servers, we only need a few clicks to get communications with our device up and running. We don’t even need to write any code!
On the Soracom console, simply click the SIM you would like remote access to. Click the Actions button to reveal a drop down menu. 
Click On-demand Remote Access.
This will open a dialog where you can request a port to be forwarded. Here, we will choose to forward port 22, as it is the standard SSH port. 
Under Port to expose, enter 22. You can also change the Access allowed for timeout so that the port will automatically close after a certain length of time. This will make sure someone else using your computer after you cannot access the Raspberry Pi through SSH after the port times out. To further limit access to the port, you can enter in an IP address range.
Since an IP address identifies your computer to other computers, entering an address or address range here will restrict access to only those computers whose IP address fall within the range.
This field defaults to the IP address of the computer used to connect to the website, so we’ll leave it blank. You can add more addresses in this field if you want another user to have access to the Raspberry Pi’s SSH (i.e. another admin).
After that’s finished, the dialog will display a SSH command. 
Now, let’s connect to our Raspberry Pi through SSH with this link. Copy and pasting the above command into our computer’s terminal will let us SSH into the Raspberry Pi if it is connected to Soracom’s cellular network.
Note that you’ll have to change
user
to
pi
to login as the
pi
user on the Raspberry Pi. 
$ ssh -p {port} pi@{ip}
Now we can start up our Python script and manage the filespace from anywhere in the world without needing to be on the same WiFi network!

Creating a Web Interface for the Raspberry Pi

What if we just want a simple way to manage and view scooter data without the terminal? For example, we might want to make a mobile app for our users later on, and most phones do not have access to a shell to type commands into. Luckily, SORACOM Napter can also expose ports for HTTP in addition to SSH. 
Since we’ve finished setting up SORACOM Napter in the previous step, we can now work on adding some functionality to the Raspberry Pi. The code we’ll use below isn’t part of Soracom’s setup, but it’s what we’re going to use to communicate with Napter. It uses HTTP, which is a standardized protocol for sending data over the internet. 
If you already have an existing app which utilizes HTTP requests and a server on the Raspberry Pi, feel free to skip this step. As long as its connected to the SORACOM Air SIM, it should already be accessible!
HTTP lets us send and receive requests from a server hosted on the Raspberry Pi. With HTTP, we can send HTML to visualize scooter information rather than reading through SSH logs. Though we could easily build an app for the electric scooter in this step, we’ll save that for the next article and instead focus on setting up the server below. 
If you’ve been following along and already cloned the GitHub repository, feel free to skip this step. Otherwise open up a terminal or SSH into the Raspberry Pi and enter
$ git clone git@github.com:DevinMui/soracom-escooter-device.git
and install the requirements
$ pip install -r requirements.txt
In this repository, we have a set of scripts that connect to different Soracom services. For the web server, we can use the aptly named
napter.py
script to run a Flask web server on port 5000. 
$ python3 napter.py
With this command, we’ll now have a web server up and running! Using it, we’ll be able to control our scooter by sending HTTP requests and view scooter data.
GET /unlock
will unlock our scooter,
GET /lock
will lock our scooter, and
GET /
will show the scooter information. Neat!

Lock/Unlock the Scooter with Napter

Let’s set up another remote access configuration with SORACOM Napter. This time, we’ll expose the Flask server so we can access it from our computer
Now just visit the address given by Napter on screen by copy-pasting it into your web browser
http://{ip}
You should see a simple web page with a list of information (GPS coordinates, battery, and speed). 
On the scooter, there are options to lock the device which prevents the scooter from moving. This would be useful to avoid scooter thefts and to prevent non customers from accessing the device and draining its battery.
Luckily, the Bluetooth interface exposes this lock feature which we take advantage of in the program. To lock your device, you can send an HTTP
GET
request to the
http://{ip}/lock
. For example, using cURL
$ curl http://{ip}/lock
To unlock your device, you can send the same request to
http://{ip}/unlock
$ curl http://{ip}/unlock
Note that an HTTP GET request is the same as visiting the web page, so if you’re unfamiliar with HTTP requests, you can simply open the page in a browser.

Conclusion

Using a browser, you should now be able to lock/unlock your eScooter and view its current battery, location, and speed. We used SORACOM Napter to expose an HTTP port so we can remotely access a web server on the Raspberry Pi. We also exposed the Raspberry Pi’s SSH port securely, so we could start/stop scripts on our eScooter from anywhere in the world.
Because of its ease of use, SORACOM Napter has proven to be a useful service for remotely managing our cellular-connected IoT eScooter. We were able to access our server in just a few minutes. Most importantly, Napter takes care of security for us. Security is always a serious and difficult challenge when building an IoT device, and the electric scooter is no exception.
Napter’s ability to give only a select few IP ranges access to SSH and HTTP gives us ease of mind; we don’t have to worry about hackers breaking into the electric scooter and running commands without our consent. We could see a service like Napter being used in other IoT applications where security is paramount, like in asset monitoring and payment systems.
In the next and last article of our four part series, we’ll demonstrate how all these services can work in conjunction with one another. We’ll create an AWS Lambda application hooked up to a MongoDB database to communicate with our electric scooter and wrap the series up with a client-side app built with React Native to enable surge pricing on scooters. See you next time!

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