You may have heard from Particle and their range of microcontroller boards such as the Core, Photon and Electron. Their platform, the Particle Cloud, offers many interesting features for IoT projects: remote firmware upgrade and code upload, secure and encrypted communication to the web and much more. And now, the Particle Cloud supports the Raspberry Pi, allowing you to run Arduino code on your Pi!
The initial beta has launched, and a first batch of people have been given access. Should you be interested in trying this with your own Pi, you can sign up for the beta here to gain access in the near future.
In this post, I cover the setup of the particle agent on the Raspberry Pi, including some examples.
Contents
Preparation
You will require the following before getting started:
- a Raspberry Pi 3 with latest Raspbian Jessie image and internet access
- a Particle account with access to the beta
I’ve set up my Raspberry Pi completely headless. After flashing the latest Raspbian image to my SD card, I copied my “wpa_supplicant.conf” file to the SD card. When the Pi first boots, this config is copied to the right location and the Pi should connect to the network automatically. You can then retrieve the Pi’s IP address from your router in order to access it via SSH.
wpa_supplicant.conf:
1 2 3 4 5 6 7 |
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="your_ssid" psk="your_pass" } |
To restore SSH access, add an empty text file called “ssh” to the SD card.
Particle Agent
The Particle Agent is required on the Pi so it can connect to the Particle Cloud. The installation couldn’t be easier, as it requires you to execute only a single command:
1 |
pi@particlepi:~ $ bash <( curl -sL https://particle.io/install-pi ) |
At the end of the installation you will be prompted with some questions so the agent can be linked to your own Particle account.
If at any point, you would like to change the values you entered, such as the Pi’s name for example, simply re-run the setup of the agent:
1 |
pi@particlepi:~ $ sudo particle-agent setup |
That’s it! Your Pi should now appear in the Particle IDE and be ready for programming with Arduino code.
Examples
I’ve created two examples to show some of the capabilities, a basic one, and a more advanced one.
Blink
The “Hello World” of many electronics projects: blinking an LED. More specifically, the Pi’s green status LED.
The status LED is connected to the Pi’s GPIO19, which according to Particle’s datasheet, maps to pin “D7”. So by constantly toggling D7, the status LED blinks.
The code:
Temperature Warning
This more advanced project involves running commands on the Raspberry Pi from Particle, retrieve the output and use it to trigger different actions.This project can be split into following parts:
- retrieve the Pi’s temperature via command line
- if temperature exceeds certain value (e.g. 60°C), trigger:
- a local warning by flashing LEDs
- a remote warning by sending a notification
Retrieving the temperature
The temperature of the CPU can be retrieved from the command line of the Pi:
1 2 |
pi@particlepi:~ $ cat /sys/class/thermal/thermal_zone0/temp 56920 |
This same command can be run from the Particle program using the “Process::run()” call. The returned value can then be used to make decisions within the program.
1 2 3 4 5 6 7 8 |
// Measure the CPU temperature proc = Process::run("cat /sys/class/thermal/thermal_zone0/temp"); // Wait for process to finish proc.wait(); // Parse command response cpuTemp = proc.out().parseFloat(); |
Local Warning
The local warning is created by blinking a Pimoroni Blinkt in red. This can be achieved by using the “rgb.py” example included in the Blinkt installation and passing the RGB values for red and black in an alternating fashion.
1 2 3 4 5 6 7 8 9 10 11 |
void blink() { // Set Blinkt to RED for 500ms proc = Process::run("/home/pi/Pimoroni/blinkt/examples/rgb.py 255 0 0"); proc.wait(); delay(500); // Set Blinkt to OFF for 500ms proc = Process::run("/home/pi/Pimoroni/blinkt/examples/rgb.py 0 0 0"); proc.wait(); delay(500); } |
Remote Warning
The remote warning relies on an IFTTT (“If This, Then That”) applet which listens for Particle messages. These messages are sent from the code using the following command:
1 2 |
// Particle.publish("event_name", "event_value"); Particle.publish("cpu_temp", String(int(cpuTemp/1000))); |
So if an event is published on theParticle channel, for device “fvanPiOne”, with event name “cpu_temp”, then trigger the Notification channel to send a message containing the value of the event (the temperature).
Code
Assembling all the pieces, the full code for this project becomes:
To see this code in action, check the demo below.
Demo
In this demo, the “stress” application is launched to heat the CPU. When it reaches 60°C, the LEDs start blinking and a notification is triggered. After some time, the notification appears on the phone.
Conclusion
The software is easy to install and I haven’t encountered any real problems yet myself. Since this is still in beta, updates are being pushed frequently, so it’s important to update your installation on a regular basis! Make use of the community forums to read about issues other users have experienced, or to submit your own. This beta has a lot of potential, though a lot of work is still required to make the online libraries compatible with the Pi (MQTT, NeoPixel, …). Let’s see how things evolve.
Is it something you would consider using? What would you program with it? Let me know in the comments!