Connecting Raspberry Pi to Parse Server with NodeJS

In our previous tutorial on IoT, we explained how to setup your Raspberry Pi and to write values to the GPIO pins. In this part, we will take a step towards a real IoT application, by opening a connection between Raspberry Pi and Parse Server and letting events on each side trigger actions on the other.

Here you will learn how to connect your Raspberry Pi to Parse Server using Back4App and to perform actions such as Queries, Live Queries and writing objects, and how the events on Parse Server may trigger real-world events!

We also provide our codes as a first step for you to develop your desired IoT Apps.


Of course, you need some electronic components. Three LEDs, three resistors (1k works fine), a breadboard and jumpers are sufficient.

You have to set up a Raspberry Pi environment. We strongly recommend you to follow our tutorial on Raspberry Pi, in the following link:

You must have a working app on Back4App. Follow Step 3 on the following tutorial if you still do not have.

You will also need to enable Live Query on your app. You may follow step 1 on the following tutorial if you have not enabled Live Query.

This code was designed to interact with the Android App described in this series of tutorials. However, you may choose to develop it independently and interact directly with Parse Dashboard to create/inspect objects.

Section 1: Responding to Real-Time events on Parse Server

We will use Live Query functionality, made easy in our Back4App dashboard, to listen to real-time events. The real-world response is an interaction with the pins on Raspberry using the onoff package. Make sure you have installed it using npm.

Import Gpio class to initialize each pin.

ledPower is a LED that will always be on to indicate the application is running. output1 is the LED that will be turned on/off as a response to Live Query.

Please note our Raspberry Pi has the following the pin diagram, extracted from here.

There is a physical numbering corresponding to the numbers inside the circles and a GPIO specific numbering. For example, pin number 7 is GPIO4 and pin number 11 is GPIO17.

The integer argument on the constructor in the code above corresponds to the GPIO specific numbering! The second argument just sets it as an output pin. Please note that Raspberry Pi 2B outputs 3.3 V!

To turn on the power LED for the rest of the application, just add the following code.

We want the second LED to respond whenever a new object from “CommandGPIO1” class is created on Parse Server. It will turn on/off depending on its “content” attribute. Since we want it to respond in real-time, we will have to use LiveQuery.

Add the following code to start LiveQuery on NodeJS.

The App ID can be found on “Core Settings” -> “Features” in your app dashboard. The serverURL is the same subdomain that you defined on LiveQuery in the app dashboard.

Now we have to define the query to be used with LiveQuery, and add the code to run when the event happens. Add the code below.

Object query1 corresponds to a new query for objects from “CommandGPIO1” class. Make sure you allowed Live Query for this class on your app dashboard!

The following lines create the subscription and activate it for the creation of a new object of the corresponding class. The function called whenever this event happens sets the variable count1 as 0 or 1 for off or on contents, and it writes this logic voltage value to output1.

We are almost done with this part of the tutorial! Add the following code to turn off our application properly when the process is interrupted.

Section 2: Watching for Real-World Events and Acting on Parse Server


We will define a pin on Raspberry Pi as an input. When the voltage in the input becomes high or low, we will turn on/off a LED and write an object on Parse Server with content as “high” or “low”.

Note, for the input pin, we have to define a second argument to specify on which edge of the signal the pin will operate.

Now we have to initialize Parse Server. Add the code below

We want to create “InputGPIO” class objects. The following code has a key role.

We start by extending Parse Object to the desired class, “InputGPIO”. Whenever a reading comes from the pin corresponding to input1, if it has a value different from the previous one:

  • This value is written to the pin corresponding to output2
  • An “inputGPIO” object with a proper content and type fields is created
  • The object is saved to Parse Server.

The logic involving value and valuePrev is useful because even when the voltage in the input pin is kept constant, multiple readings with the same state may continue to arrive.

We have almost finished! As in Section 1, we have to update the code to manage interruptions to the process.

To test this process, connect and later disconnect the pin on Raspberry that supplies 3.3 V to the pin defined as input.

To have a real IoT device, these node processes need to run automatically when the device boots. Please refer to the pm2 package, already mentioned in our Setting up Raspberry Pi tutorial.

We provide a working code for this tutorial. If you prefer, you may write separated codes for each task and run both simultaneously with pm2. Here is the link for our GitHub repository.

Our next tutorial will cover how to write an Android App that works with the code described here. The code is also available in the repository.


Setting up Raspberry Pi

Onoff library documentation on npm

Using onoff package

Store and query relational data on the cloud. Make it accessible over GraphQL and REST with a scalable, open-source backend, based on the Parse Platform.

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