6. Raspberry Pi Server


1. Introduction

In this project, Raspberry Pi is a USB host for Arduino, Bluetooth Slave with the Andriod device (Nexus tablet) and also a Wifi Access Point with the PC. It serves as a router to forward messages to the intended destinations.


2. Raspberry Pi Server

To achieve the requirements mentioned above, we build our R PI server using Python3, for its perfect support of different network protocols and complete documentations.

Our implementation includes the use of the following library:

  1. Wifi TCP socket
  2. Serial port (PySerial)
  3. Bluetooth socket (PyBluz)

3. Robustness Enhancement of Our R PI Server

The KPI for R Pi server is its robustness when handling unexpected situations. Although the protocols we choose (i.e. TCP for Wifi) have already done a good job in some sense, we want to add in an additional layer to make sure 100% integrity of our data and stability of our network. We achieve this by the following techniques:

  1. API design & Integrity check
    • We standardise our communication API so that all messages towards a certain destination must follow a certain format. This would allow us to perform integrity check on our data to ensure the message we received from the sender is safe to send to its receiver.
  2. Sending buffer & Critical section access
    • We receive the messages from different protocols on three separate receiving threads and send them out using our main thread. So we need a buffer which stores the messages temporarily so that we can send it out when main thread wakes up.
    • To avoid concurrent access to the buffer, we place a lock on the buffer. Whenever we need to access the buffer, we need to acquire the lock. After accessing, we release the lock.
    • Python provides an elegant solution – the thread-safe ‘queue’ class.
  3. Automatically reconnect whenever exception occurs
    • It’s not rare that exceptions occur when sending/receiving messages. It just happens. But we don’t want to restart the whole server and the corresponding client programs just because of such stupid reason. Whenever it happens, we close sockets/connections in both server and client side, wait a few seconds, and reconnect. So the transmission continues.