How to control ESP8266/ESP32 with GraphQL
Experimenting connecting ESP8266/ESP32 to a GraphQL API
Some time in 2018-2019 I experimented with Parse Server (sort of open-source BaaS/“Backend as a Service”) and how to hook an ESP device into it . This idea is quite similar.
My interest is to have an open source, self-hosted backend service with a database, and make cheap, simple IoT devices (like based on ESP8266) being controlled by it.
One web technology that has been popular lately is GraphQL. GraphQL has a concept of ‘subscriptions’ that provide real-time indications from server to clients when some data object changes. This is exactly what I want. And I wanted to learn something new.
GraphQL is quite complicated beast. So I wanted to start with ready made server side solution.
Hasura.io looked to be what want. It provides GraphQL backend (using PostgreSQL database) and out-of-the-box support for real-time data via GraphQL subscriptions. And there is also one click deployment to Heroku for experimentation. Similar alternative to Hasura would be Graphile, also later versions of Parse server should support GraphQL, which I will experiment later.
So, I set up the server with similar ‘device’ data object I used in my Parse experiment. Then I played with Graphiql (GraphQL frontend) and created very ugly (I cannot CSS) web app UI with Svelte. I have wanted to learn Svelte and got inspired by a Youtube video (its code) about Svelte, Hasura and GraphQL.
Using those under Firefox developer tools to spy GraphQL and related websocket messages, plus some googling, I got an idea what sort of messages ESP would minimally need to support.
Turns out the implementation complexity was the same order of magnitude as with Parse platform, and in fact rather similar. So, I managed to implement a demo where ESP connects to Hasura backend and a web UI can be used to toggle LED in the ESP.
One big missing feature is access control (authentication and authorization). Hasura does not include integrated system for it like Parse, but requires external webhooks or OAuth to handle part of it. I left this for later exercise.
The source code of my implementation for ESP8266 and ESP32, and a simple (and ugly) web interface is available at https://github.com/jukkas/esp-graphql-experiment . It uses ESP-Arduino libraries and can be build with PlatformIO.
Implementation is very rough, completely insecure and not suitable for anything else than playing around.