Experiments with Arduino and Hasura
Tags: arduino, hasura, graphql
Published: 2020-08-05 15:46:36

---
title: Experiments with Arduino and Hasura
published: true
date: 2020-08-05 13:34:31 UTC
tags: Arduino, Hasura, GraphQL
canonical_url: https://hasura.io/blog/experiments-with-arduino-and-hasura/
---

![](https://hasura.io/blog/content/images/2020/08/Twitter-banners--29-July--2020-1.png)

**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!**

Johnny-five is a Javascript robotics & IoT platform. It is an easy way for web developers to get up-and-running with IoT, without any worries about learning a new language. Let’s get started!

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:

1. Download the Arduino IDE for your machine from [here](https://www.arduino.cc/en/main/software) and install it
2. Connect your Arduino board to your machine and open up the IDE
3. Navigate to File -> Examples -> Firmata -> StandardFirmataPlus
4. This would open a “sketch” in the IDE. Load this on to the board.
5. Next, create a new project directory from your machine’s Terminal / command line: `mkdir hello-blinky-project`
6. Make sure you have [nodejs](https://nodejs.org/en/) installed on your computer
7. Switch to the project directory and install the johnny-five library:

`cd hello-blinky-project`

`npm install johnny-five`

![](https://lh5.googleusercontent.com/St-dy6yKPH5-F8qMeRdtYojBlOYUREtu-NOT-72PmBcA7ll47c_v3BN9r6wqoXlR58YS7RyP9c7zHT7fdftUGCCGTdebSqDuPfjEohSRE7rF2hWu3a--OAY4Kf67djL9h9fxl_Y)<figcaption>The traditional “Hello World!” program becomes the “Hello Blinky!” here</figcaption>

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.

**Hasura-controlled LEDs**

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](https://hasura.io/docs/cloud/1.0/manual/getting-started/index.html). The schema is super simple, and has a couple of tables: `authors` and `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(!)

![](https://lh3.googleusercontent.com/swwJqrys3FF8iXfCFDKw5FLpJBke9yPEVx9nSV2lGn-gUkwkWfE_hDdayOAbAk8vuAi7olbC6Sl-Uv8mKd7fVz64KspOtACEA6pMnhrBNC-FmNOulhh8D1rDZcXwHXUGNINkj9E)<figcaption>Testing out Hasura Cloud-Apollo Client-Arduino setup</figcaption>

<iframe width="480" height="270" src="https://www.youtube.com/embed/zfWQIGnMwWI?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

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-

1. Arduino Uno
2. [LM35](https://www.ti.com/product/LM35) Temperature Sensor
3. Breadboard and jumper wires

The circuit diagram and sensor related code can be found [here](http://johnny-five.io/examples/temperature-lm35/)

![](https://lh3.googleusercontent.com/EjBntcxvdNr34nlyii42foxi61-0rmVvy5DkMwp-t_IU22O60DcRrsld5BhE5fhhZdLnLuRx_jQ2NoIG4PI2Kp70ljlzgG_FpB9P734dWBCIsfBhY4XERjgpoZbc5bUPe3NAdek)

We again have an [instance setup on Hasura Cloud](https://hasura.io/docs/cloud/1.0/manual/getting-started/index.html). Our schema has a single `temperature` table with these fields:

![](https://lh5.googleusercontent.com/3pNvdOrsJ7H9UZ7DMUit__DpjHt3kxSLTKfKaRM0-CjIZkU3Uqp7yOfJdotV7w120RXBXH9YdxWK7CyjQ8CnFXnlA_61DK76PSlwjv5dkZX-IIm85kt1xPioZlpw7uV6EVZ_nlQ)

We continue using Apollo Client and Johnny-Five in our code.

![](https://lh3.googleusercontent.com/1jVRCP2mvgAOLxIHGekM8Pak4k6fx_1IL3msVMAeYkVyP68u1iYO7KvA6LJiWm2jrnU0mkWO_tXhJnJUG4BF5MAgqo6MSAPU-ZD77cUuMZj6o3WZmkxSnRJZG1Rn-aQAHslXEaI)

Notice that we configure the thermometer sensor at the beginning. The frequency is set to 60000 milliseconds ( = 1 minute). We use the `on(“data”)` and `off` parameters from the [Thermometer API](http://johnny-five.io/api/thermometer/)

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:

![](https://lh5.googleusercontent.com/SddwvPDhe4_SXEGLzrr3PDM-wQYPF-ABiK6mTkLxI1gXqJwi2oFuN5Hkr-lUa7z3n8y4MPqeirHpkEcor_R1ikKGCwpXH7_JrkH_zP1fFDWmFSTyGbWSmerAPLcKJJHFlkSYfwk)<figcaption>Room Temperature Data over a 12 hour period</figcaption>

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](https://github.com/meerasndr/temperature-sensor-johnnyfive-hasura).

There are a ton of cool ways to integrate the Arduino with GraphQL and Hasura. Here are a couple of ideas:

1. Flame sensor + Hasura + Event Triggers
2. DIY GPS + Hasura + PostGIS

Let us know what you’ve been building!