Controlling a Raspberry Pi Server Remotely

The Alibaba Cloud IoT Platform enables you to implement pseudo-Intranet penetration and remote control of Raspberry Pi servers without public IP addresses. This article uses remote control over a Raspberry Pi server as an example to demonstrate the implementation of pseudo-Intranet penetration and provides development code samples.

Background

Assume that you want to build a Raspberry Pi-based server to run some simple tasks, such as starting a script and downloading a file. However, if the Raspberry Pi server does not have a public IP address, you can only manage this server when you are either at the office or at home. If you use other Intranet penetration tools, you may have encountered frequent disconnection issues. To solve this problem, you can use Alibaba Cloud IoT Platform’s RRPC (Revert Remote Procedure Call, or revert-RPC) feature with the JSch library to implement remote control over the Raspberry Pi server.

Remote control implementation steps

Image for post
Image for post

Use the IoT Platform to implement remote control over the Raspberry Pi server by following these steps:

  • Call the IoT Platform RRPC interface on a computer to send an SSH directive.
  • After receiving this directive, the IoT Platform sends this SSH directive to the Raspberry Pi server by using the MQTT protocol.
  • The server runs the SSH directive.
  • The server encapsulates the result of this SSH directive into an RRPC response and reports it to the IoT Platform by using the MQTT protocol.
  • The IoT Platform returns this RRPC response to the computer.

Note: The RRPC timeout is five seconds. If the server does not receive response in five seconds, a timeout error is thrown. If it takes a long time to run your directive, you can ignore the timeout error.

Download the SDKs and Demo

To implement remote control over this Raspberry Pi server on the IoT Platform, you need to develop the server-side SDK and the device-side SDK.

The following sections give examples of server-side and device-side SDK development.

Note: Code samples provided in this article only support simple Linux commands like uname, touch, and mv and do not support complex directives like editing files. To run complex directives, you can write your own code.

Device-side SDK Development

After downloading and installing the device-side SDK and downloading the SDK Demon, you need to add the project dependencies and the following Java files.

The project can be exported as a jar package to run on Raspberry Pi.

1. Add dependencies to the pom.xml file.

2. Add the SSHShell.java file to run SSH directives.

3. Add the SSHUserInfo.java file to verify the SSH account and password.

4. Add the Device.java file to establish MQTT connections.

5. Add the SSHDevice.java file. The SSHDevice.java file includes the main method. This file is used to receive revert-RPC directives, invokes SSHShell to run SSH directives, and return revert-RPC response. Device certificate information (ProductKey, DeviceName, and DeviceSecret) and the SSH account and password are required in SSHDevice.java.

Server-side SDK Development

After downloading and installing the server-side SDK and downloading the SDK Demon, you need to add the project dependencies and the following Java files.

1. Add dependencies to the pom.xml file.

2. Add the OpenApiClient.java file to invoke the open interfaces on the IoT Platform.

3. Add the SSHCommandSender.java file. The SSHCommandSender.java file includes the main method. The file is used to send SSH directives and receive response to SSH directives. Your account AccessKey, device certificate information (ProductKey and DeviceName), and SSH directives are required in SSHCommandSender.java.

Original Source

https://www.alibabacloud.com/blog/controlling-a-raspberry-pi-server-remotely_594924?spm=a2c41.13056867.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store