Python Programming Language LogoIn case you haven’t read any of my other home automation articles, I use a lot of Raspberry Pis in my home automation system. Keeping track of them used to be a burden, but not any more.¬†As mentioned in yesterday’s article, I have Raspberry Pis installed in some pretty crazy places. Even though all of the Pis in my house have dedicated functions, some Pis aren’t used as extensively as others. For example, I have Raspberry Pis installed inside the walls of several rooms in my house which act as built-in media centers. But, when those Pis aren’t being used for providing media, I don’t want them sitting there doing nothing. So, I make them contribute their unused resources to the rest of the Pis in my house to get a little extra horsepower for some of the Pis that have more heavy lifting to do (such as the Pis that do face & speech recognition).

In order for me to keep up with which Pis are online and how much free diskspace, memory, and CPU utilization they have available to share with the rest of the house, I wrote a small Python app that runs every time the Raspberry Pi starts up and continues to broadcast its existence and system vitals every 10 seconds (configurable) where I detect those broadcasts and display the results on a touchscreen TFT monitor (which I will explain in my next article). For now, I want to share with you the Python script that broadcasts its system vitals out to any listening system on the network.

Before we get started, the commands in this article are intended to be used with Arch Linux. If you are using another Linux distro such as Raspbian, you will need to refer to that OS’s documentation for the corresponding commands.

To begin with, the following script requires the “psutil” module. To install it, you will need to run the following command:

pacman -S psutil-python2

If you run into any issues when installing that module, you will probably need to first update your Raspberry Pi. You can do that by running the following:

pacman -Syu

After your system has updated, re-run the command above to install the psutil module.

Next, type the following command on your Raspberry Pi to create a new file called “”. This is the same name that I used in yesterday’s article. So, if you want to follow the instructions in that article for setting this script to run every time your Pi starts, make sure you name this file the same as you used in yesterday’s tutorial.


You should now be looking at a blank-black screen. If so, copy the code below and paste it into your script. Then, type Ctrl+X, press the “Y” key, and press enter. This will save your changes and close the file.

To verify that your script is working, you will need another script that listens for these broadcasts and prints their results. I will describe how to create that script in my next article. Until the, run the following command to start this script and to begin broadcasting its system vitals.


If you followed yesterday’s tutorial, you can start this script and run it in the background by running the following:

systemctl start pybroadcast

Keep in mind, though, that even without a script to listen for these broadcasts, the broadcasts are still happening. So, if you are familiar with network monitoring tools such as Wireshark, you can use those tools to view these broadcasts.

That’s it. Your Raspberry Pi is now telling all the other systems on your network how much diskspace, memory, & CPU it has total and available for the others to use. In another article I will show you how to use these available resources for distributed computing. Until then, here is the code for the broadcast client.

import socket, sys, json, os, psutil, collections, time

#diskdir = "C:\\" # Windows
diskdir = "/" # Linux
dest = ('<broadcast>', 15000)

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.settimeout(10) # seconds

def send():
    diskspace = psutil.disk_usage(diskdir)
    diskspace_total = diskspace[0]
    diskspace_used = diskspace[1]
    diskspace_free = diskspace[2]
    diskspace_percent = diskspace[3]
    memory = psutil.virtual_memory()
    memory_total = memory[0]
    memory_available = memory[1]
    memory_percent = memory[2]
    memory_used = memory[3]
    memory_free = memory[4]
    cpu = psutil.cpu_percent()

    payload = {u"cpu": (cpu),
               u"memory_total":      (memory_total),
               u"memory_available":  (memory_available),
               u"memory_used":       (memory_used),
               u"memory_free":       (memory_free),
               u"memory_percent":    (memory_percent),
               u"diskspace_total":   (diskspace_total),
               u"diskspace_used":    (diskspace_used),
               u"diskspace_free":    (diskspace_free),
               u"diskspace_percent": (diskspace_percent)}
    s.sendto("%s" % json.dumps(payload), dest)

while 1:

P.S. If you want to view your results on the same system that’s running this code, you can add “print” commands after line 37. For example, you can add the following 3 lines to print all of the system vitals.

print diskspace
print memory
print cpu

Also, if you’ve setup this script to run every time your Raspberry Pi boots up as mentioned in yesterday’s article, you will need to add the following line to your “/etc/systemd/system/pybroadcast.service” file so that it will wait until your network card is ready before it starts broadcasting its information. Otherwise, the script will fail with a socket exception since the network interface isn’t ready.

As a refresher, here is what my pybroadcast.service file looks like for this script:

Description=Broadcast system vitals on port 15000

ExecStart=/usr/bin/env python2 /root/pidev/


Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.

Related Posts

Leave a Reply