Programming Model

Inter-processor communication

The Raspberry Pi and  [email protected] can communicate over both Serial and I2C, but Serial is used for the main inter-processor communication.

When the Raspberry Pi is active, it is the master on the I2C bus, and is responsible for setup of the Ardhat I2C peripherals (e.g. IMU, Altimeter, RTC). When the Raspberry Pi is inactive, the Arduino can master the bus and interrogate the peripherals. For example by watching the RTC, the Raspberry Pi can be started when the next crontab entry is about to come due.

However, access to regular Arduino-compatible ports such as Analog and PWM is performed over Serial using a standardised protocol layer, or Data Link layer in OSI parlance.

This protocol layer is called Firmata, and allows applications on the Pi to communicate with  [email protected] provided they have a Firmata language binding. Although javascript is the recommended language (using the node.js Firmata library), other languages such as Python can easily be used instead.

As an example, the factory image that ships with  [email protected] contains a modified version of standard Arduino Firmata that adds several additional features, such as monitoring of the Navigation switch and Status LED control, using the Arduino SMlib. This state machine library provides an extremely lightweight mechanism that allows primitive multitasking.

In principle, an  [email protected] host application can be written in any language that has a Firmata library, including Processing, Python, C++ and many more. However, in a Robotics or IoT environment, services are often distributed over a network whose availability cannot be guaranteed. A responsive system cannot block while waiting for these services to respond.

JavaScript’s support of callbacks, and the availability of non-blocking app frameworks such as node.js, make it the natural choice for a web enabled system, and it is the main supported framework for local programming of  [email protected] . Node.js is built on top of Google’s V8 javascript runtime, and is widely extensible through the use of additional libraries. The addition of Express and AngularJS allow applications to be exposed over the web if required.

Johnny-Five is a great node library that sits on top of Firmata but provides some higher level functions to make use with node even easier. For example,  [email protected] is unit tested using Mocha on Johnny-Five on Firmata.

Application-specific projects can also be created on top of Node using accessible app generators such as Node-Red.

Some of the key libraries are shown below:

Library Function
Johnny-Five JavaScript Robotics programming framework, by Bocoup
Mocha is a JavaScript test framework running on Node.js, making asynchronous testing simple and fun.
Mongodb noSQL database , for example to keep track of authenticated users and nodes
Express minimal node.js web application framework that provides a robust set of features for web and mobile applications
AngularJS front-end application framework toolset for Single Page Applications
Node.js event-driven, non-blocking back-end javascript environment
Firmata node.js library to interact with local Arduino running the firmata protocol (currently limited to serial port only (use of node-i2c TBD))
pi-gpio node.js based library to access the GPIO of the Raspberry Pi real-time bidirectional event-based network communication
mqtt machine-to-machine (M2M)/“Internet of Things” connectivity protocol
mosquitto message broker for mqtt, supports MQTT,MQTT-SN
ponte node.js server bridges MQTT, CoAP
NeDB lightweight MongoDB-like noSQL database for node.js
freeboard local dashboard function
Node-Red A visual tool for wiring the Internet of Things
IoT System Model

The overall system model is as shown below.

image alt text

The Applications and Services may run on the Pi (typically written in javascript), or may in part or whole be provided by a cloud service, subscribed to by the Ardhat software, eg IFTTT. Sensor node topology and authentication info is stored in a NeDB a MongoDB-like noSQL database.

The Arduino RealTime code is responsible for monitoring and handling system power, realtime events, and graceful sleep/shutdown/restart cycles of the Raspberry Pi. It is also responsible for transmitting application information to the Wireless Sensor Nodes, for example using a simplified MQTT (MQTT-SN) over a link layer protocol such as TinyHAN on top of the relevant PHY (eg RFM96).