Experiments with Arduino and Hasura
What is Arduino?
Arduino is an open-source electronic prototyping platform, usually consisting of a single-board microcontroller. There are a bunch of different variants at the entry-level: Uno, Nano, Leonardo, Due, Mega etc. In this post, we’ll use the Arduino Uno. The Arduino and the Arduino-IDE come with native support for C & C++ languages.
Say hello to Johnny-five!
There is a bit of initial board setup that is required. Johnny-Five uses something called the StandardFirmataPlus firmware to communicate with the Arduino board. We need to set this up before we start executing our nodejs code:
- Download the Arduino IDE for your machine from here and install it
- Connect your Arduino board to your machine and open up the IDE
- Navigate to File -> Examples -> Firmata -> StandardFirmataPlus
- This would open a “sketch” in the IDE. Load this on to the board.
- Next, create a new project directory from your machine’s Terminal / command line:
- Make sure you have nodejs installed on your computer
- Switch to the project directory and install the johnny-five library:
npm install johnny-five
Run this program in the Terminal:
node blinky.js.This should make the on-board LED(or an external LED pin connected to port #13) to blink, once every 500 milliseconds.
That was pretty simple. Let’s move on to cooler stuff now.
Let’s do more LED blinking, but this time based on the result of a GraphQL query. We have an instance setup on Hasura Cloud. The schema is super simple, and has a couple of tables:
books. We use Apollo-Client to send out a query for a particular author in our table. If the author row is present, the LED blinks every 1000 milliseconds. If the author row is not in our database, the LED blinks every 3000 milliseconds(!)
This is a very basic way to test out our Hasura-Apollo-Arduino setup. Enough with the LEDs now, let’s move on to more serious stuff!
Room Temperature Sensor + Hasura
The goal of this project is to measure the room temperature and keep pushing it to a Hasura Cloud instance every one minute, over a 12-hour period.
This is our setup-
- Arduino Uno
- LM35 Temperature Sensor
- Breadboard and jumper wires
The circuit diagram and sensor related code can be found here
We again have an instance setup on Hasura Cloud. Our schema has a single
temperature table with these fields:
We continue using Apollo Client and Johnny-Five in our code.
Notice that we configure the thermometer sensor at the beginning. The frequency is set to 60000 milliseconds ( = 1 minute). We use the
off parameters from the Thermometer API
Every 60 seconds, the temperature object’s on(“data”) function is called. Within this is our GraphQL mutation, which sends out the current room temperature in celsius, fahrenheit and kelvin. This is a snapshot of the data pushed, as seen from the Hasura console:
As one might expect, there isn’t much variation in the temperature values that are measured every minute. This can be fixed by reducing the thermometer object’s frequency and running this for 24+ hours.
That’s it! We now have a homegrown weather service! The full code is available here.
There are a ton of cool ways to integrate the Arduino with GraphQL and Hasura. Here are a couple of ideas:
- Flame sensor + Hasura + Event Triggers
- DIY GPS + Hasura + PostGIS
Let us know what you’ve been building!