r/Arduino Reddit Subscribers Monitor

Contents

Code

The code that you need to install is split into two parts:

  • A Python script.
  • Arduino code.

You can download all of the code (in a zip file) by clicking on the disk icon.

When you unzip it, you will find an Arduino project in the SubRedditStatsMonitor directory. Additionally, there is a directory named "Support". In this directory, you will find a Python script.

If you have the either of the display devices that I have used, you can simply upload the Arduino code to your Arduino (with one minor modification if using the LCD panel). Refer to the instructions in the wiring instructions.

If you have both types of display, then you can run both versions at the same time if you wish.
To do this, you will need two Arduinos. Additionally, you will need to run two seperate copies of the Python script. One Python script will drive one Arduino, the other copy will drive the second.

Sha256 hash of downloads:

293ea05d05e2ff2b35fdfdfbaae2298642018ccc7f7595c77d7791294df00420 *SubredditStatsMonitor-v1.00.00.00.zip
e7ad03230350c050cbbb53e1308264529908535a2f8a775c458477e2818beb86 *SubredditStatsMonitor-v1.01.00.01.zip
a22dbb51813d72e0442a828068ce7c39904b7d591b2387edd5a531e89af0855f *SubredditStatsMonitor-v1.02.00.00.zip

Version history:
VersionDesription
v1.00.00.00.00Initial version.
v1.01.00.00.00Bug fixes, improved commentary and minor enhancements.
v1.02.00.00.00Added support for TFT display.

Setting Up

To use this project, you will need a copy of Python 3.
I have tested Python 3.7.3 and Python 3.9.10, both of which worked just fine. You will also need to install pySerial.
There isn't anything terribly fancy in the Python scripts, so any recent Python 3 version should work.

Arduino setup.

After unzipping the download, simply open up the Arduino project in the SubRedditStatsMonitor folder. You should see the project open with five tabs.

If you are using a LED module that is similar to mine, you can upload the project in the usual way.

If you are using the 2 line LCD module, then you will need to make a minor change to the code as described in the important note on the wiring page.

If you want to test the Arduino code after uploading (which is a good idea):

  1. Open the Serial monitor
  2. Set the speed (bottom right corner of the monitor) to 115200
  3. Copy and paste the following lines (one at a time) into the serial monitor:
    1111,2222222,333,--,--,--,600000,2024-01-16
    234,418558,335,123,456,789,500000,2022-10-14
                    
  4. Observe the display cycle through the values.
Make sure there are no trailing spaces at either end of the line you paste into Serial monitor.

Python setup

The Python script obtains data from a server and passes it to the Arduino for display. If the Python script is not running, then (this version of the Arduino code) has no way to get up to date data. The Arduino will simply continue to display the last set of values that was given.

Note that this is a one time setup. You may need to vary this to suit your existing environment. For example, if you have Python 3, but not pySerial, then you will likely only need to do step 3.

To prepare to run the Python script, you will need to do the following:

  1. Download and install Python 3 for your PC from: https://www.python.org/downloads/
  2. If your download does not include pip, install pip. Since Python 3.4 pip should be included by default, so this step might not be needed.
  3. Use pip to install pySerial.
    pip install pySerial
Note that different operating systems have different ways to install Python. Check the documentation on the download page or the instructions for your operating system.

Run the Python script

Once you have the Python environment setup, you can run the script as follows:

  1. Open a command prompt.
  2. Navigate (change directory) to the location of the redditGetSubredditStats.py script.
  3. If you opened the Arduino Serial monitor, close it.
  4. Identify the communications port (Serial port) that your Arduino has been assigned.
  5. Enter the following command:
    python redditGetSubredditStats.py <PORT> 115200
    Where <PORT> is the port assigned to your Arduino (as reported in the IDE).
Note that you must leave the Python script running in order to receive up to date data for your Arduino.

If you are running on Windows, then the command might look like this (assuming the Arduino's port is COM8):

python redditGetSubredditStats.py COM8 115200
If you are running on Linux or Mac, then the command might look like this (assuming the Arduino's port is /dev/ttyS7):
python redditGetSubredditStats.py /dev/ttyS7 115200

The default is to collect statistics from r/Arduino. You can nominate an alternative by providing the subreddit name on the command line as the third parameter.
At the time of publishing only r/ArduinoProjects has data being collected. Specifying any other subreddit name will cause the python script to exit.
To display data for r/ArduinoProjects, use a command of the form:

python redditGetSubredditStats.py <PORT> 115200 ArduinoProjects
Where <PORT> is the port assigned to your Arduino (as reported in the IDE).
Do not specify the "r/" as part of the name.

Note if running under cygwin, you may encounter an error such as either of both of the following:

termios.error: (22, 'Invalid argument')
serial.serialutil.SerialException: Could not configure port: (22, 'Invalid argument')
If you experience this, try openning up a DOS command prompt and enter:
mode COMn 115200
where n is the port number corresponding to the cygwin Serial port name. This is usually one higher than the cygwin number as cygwin starts counting ports from 0, whereas windows starts at 1.

For example, if your arduino is assigned to /dev/ttyS7, then the correct command would be: mode COM8 115200.

Once you have done this, try running the python script again.

Adding a new display type

If you need to add a new display type or want to add Ethernet support, review the rest of this topic then check out one or more of the following instructions:

Currently these are Works in Progress, so not all of them are in place. If any of these are of potential interest, send me a message on Reddit - u/gm310509.

The following diagram shows the software architecture of the project.

Arduino Program Class Diagram

What the diagram shows is that the Monitor (the main Arduino program) defines a Subreddit Stats Object. This is named subredditStats in the program.

The Subreddit Stats class defines a set of functions that do not have any code behind them. These functions relate to displaying data on a display that is yet to be defined. You can find these functions listed in the class definition. They are named:

  • initDisplay() - Initialse the display,
  • two variants of output() - output a message and
  • updateDisplay() - output or update the display of the stats.

The Subreddit Stats class is then subclassed (a.k.a. refined) by the various display drivers such as SubredditStatsLCD(). These subclasses implement (provide code for) the above functions. The code in these functions outputs the data in a way that works for that particular type of device.

For example, the updateDisplay is called frequently to support rolling displays like the LED and 2 line LCD displays. It is also called once when new data is received. This is intended for displays such as the TFT panel which can display all of the information on a single page. An argument is provided to indicate whether the invocation is the regular update or new data update.

As a result of this design, it is easy to add different display types to the program. To do this, we simply define a new class that refines the SubredditStats class and provide code for the above functions.

Finally, the main program simply needs to define the subredditStats variable and the rest of the program should work without any change. For example, if we called our new class SubredditStatsTFT, we would add the following variable declaration in the main program:

SubredditStatsTFT subredditStats = SubredditStatsTFT();

Another interesting point is that because there is a close relationship between the display and the base statistics, it is easy to access the underlying data from within the display classes. The actual statistics sort of form part of the display code, but are also kept isolated from it. What this means is that the maintenance of the statistics can be done in isolation and without affecting the display (or main program) and the display code can do its own thing irrespective of how the metrics are maintained.
This usually makes the maintenance of the program much easier and less risky if things are compartmentalised in this way.

Please support me if you can

Please help support these projects by: Buy me a coffee - gm310509 Patreon - gm310509