Home / Tutorials / ESP32 Tutorial / Get Any Reddit Post using ESP32

Get Any Reddit Post using ESP32

Reddit's API has been piquing my curiosity for some time. However, my past experiences with APIs, especially with Twitter and Facebook have left a bad taste in my mouth. Then one day I had the courage to try it again and to my surprise, it's far easier than it looks!

You can acquire data from subreddits without needing to authenticate. A simple GET request, for example, to get the top posts from r/jokes would only require this URL:

https://www.reddit.com/r/jokes/top.json?

Copy-pasting that URL to your web browser would show JSON data that looks like this:

With this, you can then use ArduinoJSON to extract any data you want from a specific subreddit!

For a full rundown of Reddit's API, see Reddit API Docs

For this project, I will be extracting the top post for today from a subreddit and have that displayed on a 20x4 LCD. This means I'm limited to subreddits with text posts like r/jokes, r/showerthoughts, r/askreddit, etc.

Factoring in the average post length and usefulness, I decided on r/showerthoughts.

Step 1: Getting the Target Data

Since I only need the top post in a day, I use this URL to filter the results:

https://www.reddit.com/r/showerthoughts/top.json?limit=1&t=day

This will give me filtered results, but it's still a lot of data. Thankfully, the ArduinoJSON assistant can help in extracting the only data I need, which is in this case, the post title.

Go to https://arduinojson.org/v6/assistant/. The first step requires you to select the processor, mode, and input type. I am using an ESP32, I need to decode JSON and the data is coming from a stream, hence the field values below:


The next step is to paste the JSON data:


You can also click the "Prettify" button to have a clearer view of the nodes.

The next step is about the size needed to hold the JSON data:

Since the ESP32 has memory enough to hold the JSON data, there's no warning here. But if your JSON data exceeds that of what your processor is capable of, you'll see something like this:

This is something to keep in mind when handling large JSON data.

Finally, the assistant will give you an example program for processing the data. The program is quite long so I will just point out the things that relate to my project requirement. If you look back at the JSON data, the title of the Reddit post is under a couple of nodes:

The root objects are "kind" and "data". Under "data", there's an object called "children" and under that is another "data" field.  You'll then see the "title" under that second "data".

Step 2. Writing the ESP32 Code

Link to source code

External libraries:

Built-in libraries:

  • HTTPClient
  • WiFi
  • Wire

Obviously, the ESP32 needs to be connected to the Internet first. The following lines of code do that:

Next, an instance of the HTTPClient library and the API URL is declared:

To acquire the JSON data, a GET request function is used:

The raw JSON data will then be inside the string variable reddit_json.

Before calling setup(), a JSON document object is declared with size as suggested by the ArduinoJSON assistant:

This object will house the deserialized data from the raw JSON:

Next is to filter our data. To get only the items under the first "data" object, we use this filter:

Then, we acquire only the "data" object under the "children" object. Note that the "children" is an array. To get only the first post, we specify the index as 0:

Finally, to get the title of the post, we use this filter:

The full code found in this repository allows you to choose your own subreddit and filter type. Just look for the #defines's near the top of the code and change that:

Step 3: Wire the LCD

For this project, I am using a 20 x 4 LCD (I2C) to fit more characters. Sometimes, however, this is still not enough as some r/showerthoughts posts can be more than 80 characters long. You could opt for a graphic LCD if your target is to handle more characters.

A non-I2C LCD wiring is provided as the featured image.

Step 4: Upload the Code

With the circuit in place, the final step is to upload the code.

Now, the code in the repository should be able to get the top post for the day from /r/showerthoughts. There's no method yet to acquire new data: to do that, you need to reset the ESP32. If you want to make your project get a post, say every 24 hours, you can add this inside the loop function:

This will restart the ESP32 after 86400000 milliseconds, equivalent to 24 hours.

Oh, and don't forget to add this before setup():

I hope you find this tutorial helpful!

Check Also

ESP32 MAX7219 WiFi Message Board

ESP32 MAX7219 WiFi Message Board

This ESP32-powered project allows you to write messages to a MAX7219 LED matrix display using …

Leave a Reply

Your email address will not be published. Required fields are marked *