loading...
Hasura

Experiments with Arduino and Hasura

hasurahq_staff profile image Hasura Originally published at hasura.io on ・4 min read

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 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 installed on your computer
  7. Switch to the project directory and install the johnny-five library:

cd hello-blinky-project

npm install johnny-five

The traditional “Hello World!” program becomes the “Hello Blinky!” here

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. 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(!)

Testing out Hasura Cloud-Apollo Client-Arduino setup

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 Temperature Sensor
  3. 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 on(“data”) and 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:

Room Temperature Data over a 12 hour period

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:

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

Let us know what you’ve been building!

Posted on by:

hasurahq_staff profile

Hasura

@hasurahq_staff

⚡️ Instant GraphQL APIs to build realtime apps & APIs. Connect Hasura to your database & other data sources (REST & GraphQL servers, 3rd party APIs) and get a unified data access layer instantly.

Hasura

Hasura GraphQL Engine is an opensource service that gives you instant realtime GraphQL APIs on Postgres with fine grained access control.

Discussion

markdown guide
 

I'm really glad that more articles are written about how to implement IoT along with awesome BaaS technologies like Hasura. Nice post guys!.

The only feedback I need to give is that Johnny-Five is ok to start getting into IoT but has a huge weak point for real world telemetry applications, you need to have the board connected to the computer for Johnny-Five to work.

99% of the time, boards goes to the wild with just a li-po battery and an antenna (only the ones with Wifi or any other wireless communication protocol).

Is a shame because it will be super awesome to use javascript for this, as in the example of this post, integrates just beautifully with Apollo code.

So just to give people an alternative to Johnny-Five to be able to connect to Hasura, you can use the HttpClient library for Arduino, and do a http.POST to your api with the mutation query string on the body data.

I was planning to write and article about using Hasura + Particle Boards using webhooks, but I could write another one with Arduino if you guys are interested.

Happy coding.

 

Awesome... I was in search of just somethin like this😍