Writing Content to Zesty.io using a Raspberry Pi
This past week at our monthly Meetup, we talked about automating content submission and publishing to a Zesty.io instance.
To demonstrate this, we used a Raspberry Pi - a $35 computer the size of a business of a business card that is powered using a smartphone charger. The Raspberry Pi also runs a fully-featured Linux distribution, which includes a variety of intuitive and easy-to-use graphical user interfaces.
For the Meetup, I wrote an app using Node.js that will automatically write data to a headless content model periodically (code can be found here), which is then graphed using Chart.js. The finished product can be found here: https://meetupdemo.zesty.dev/.
I wrote the app using Node.js because Zesty provides an SDK written in Node.js (npm package can be found here). However, you can easily accomplish the same task using languages like Python, Ruby, Java or even a Bash script - using our REST API.
For process management goes, I used pm2 to run our Node.js application in the background.
Let’s dive into some code:
Let’s start with this.
Here we are loading our environment variables from .env file and initializing our SDK.
I have also implemented a function to perform scheduled publishing of our data points, using our REST API.
In my example, I set the publishAt field to now, telling the API I wish to publish the content item now. However - the publishAt value can be set to a specified time value. This also demonstrates how one would go about implementing a call to our REST API using an HTTP client library.
Finally, we are using setInterval() to create and publish a new headless data entry every 5 seconds. setInterval() takes in two parameters: a callback function and an interval value in milliseconds. The data entry contains a numeric field called moisture, which our graph is using to plot data points.
And that’s it! Easy as pie… Raspberry Pi, that is.
By Wei-En Wong
I'm a software engineer who likes to tinker. Backend engineer at Zesty.io. Graduated from University of Waterloo with a degree in Computer Science in 2016.