Blazing-fast IoT data pipeline for infrared monitoring
Real-time IoT devices need real-time action, and wrangling hog loads of data from these devices requires a simple and fast streaming pipeline. Make the data durable, and you can easily have multiple consumers that don’t skip a beat.
POV: You have a shared space with your housemates and want to know if it's empty, but in the least privacy-invasive way.
The Recipe:
- AMG8833 an infrared thermal imaging sensor – 8x8 array of infrared sensors, totaling
64 pixels
measuring temperatures ranging from0°C to 80°C (32°F to 176°F) ±2.5°C (4.5°F)
up to7 meters (23 feet)
away at a max frame rate of10 Hz
. Cheap, and best for our use case as it covers a large area with a relatively high accuracy. - Raspberry Pi 4B or just any low-cost, single-board computer.
- An S2 account as our secret sauce! Follow along here.
For schematics, or to purchase and assemble the electronic components, I recommend following this MakerPortal article, or this Adafruit page.
Once you have everything set up, it should look something like this:
The S2 API will allow us to model our raw sensor output as a Stream
, which is just an unbounded sequence of records – in other words, our data in motion! The number of streams you can create is unlimited, all namespaced in a globally unique Basin
.
Let's use the S2 CLI to create a basin called monitors
, with a stream named amg8833
.
For building our backend I will use the S2 Python SDK and the adafruit-circuitpython-amg88xx library. Python will help us reduce friction and iterate faster.
Each frame from the sensor reshaped into a 2D 8x8 array
can be written as a record at the tail of the stream. Since we want to keep pushing data as we read from the sensor, we will use the streaming AppendSession
, which gives us acknowledgments in the same order that records were sent.
The S2 REST API supports SSE through which it can push real-time updates to a client over a persistent HTTP connection. SSE is perfectly encapsulated by the S2 Typescript SDK generated using Speakeasy. This will power our little Next.js app, where I can view the "live footage" and know if someone is in the shared space 👀.
I prompted v0 to help me plot this temperature grid as real pixels converting temperature to hsl, and I was able to just ship it!
I added an extra field to the records (see the small bar at the top of the thermal image) to determine whether someone is really in the frame or not by doing some naïve connected-component labeling to notify me in a text message.
A sweet spot temperature threshold was 28-29°C, and positioning the sensor at a height around 7-8 feet in a cooler area tends to work quite accurately!
The obvious next step that comes to my mind is to train a machine learning model, using supervised learning, for determining the number of people in the frame. Here, S2 stands out since we can not only consume data at the time it is published by the device, but have it available as a durable stream of records to train my model.
So how much does this project cost us in a month? S2 is free for now, but we can refer to the intended pricing and figure out a monthly estimate.
Each record is ~470 bytes and at 10 Hz over the course of a month this would be ~12 GiB of data.
- Initial operations to
CreateBasin
andCreateStream
are fractions-of-a-cent. - Each stream costs $0.00001 in a minute it is written to, and here we are writing continously to it, which adds up to $0.432.
AppendSession
andReadSession
have a per-minute cost of $0.0000001, adding up to $0.00864. So total operation cost of $0.44. - If we also keep record retention at a month, at $0.04/GiB-month that will cost us $0.48.
- Data transfer into S2 depends on the storage class. We'll go for the faster
Express
at $0.05 per GiB, which works out to $0.60. - Data transfer out from S2 depends on where we are accessing it from, with the most cost-efficient being same cloud region. We'll be accessing the stream from home so at $0.07 per GiB for internet egress – assuming we kept the footage up at all times! – this works out to $0.84.
All together, around $2 a month. Not having to worry about high costs, and pushing that side project with a simple API for your data pipeline? Win!
S2 is currently in preview. Come join us on Discord and get hacking!