[TUTORIAL] Monitor ...
Clear all

[TUTORIAL] Monitor health of your Raspberry Pi  


Active Member
Joined: 9 months ago
Posts: 10
08/03/2020 3:48 pm  

This tutorial will show you how to use IoT Guru to monitor the health of your Raspberry Pi and alert you when something is wrong.

We will run a python script every hour using a crontab to send the Raspberry Pi's temperature, free disk space and memory usage to IoT Guru using the web API.


An IoT Guru account and a Raspberry Pi with Python version 3

Step 1 : Creating Device, node and fields

Select "Devices" from the menu and click on the "Add New Device" tab. Enter "Raspberry Pi" as the device name and description. Click the "Add" button. Click on the "List of Devices" tab you should see "Raspberry Pi" in the list. Click on "Raspberry Pi" this will take you to the nodes screen for your Raspberry Pi device.

Click the "Add new Node" tab enter "health" as the node name and description. Click the "Add" button. Click on the "List of Nodes" tab and click on your newly added "health" node.

We are now going to add three fields to store our health data. Click on the "Add new Field" tab, in the "Field Name" enter "cputemp", enter "CPU Temperature" as the field description. Since we will be monitoring temperature on the this field enter "℃" as the field unit. Click the "Add" button. Click the "Add new Field" again and enter "memusage" as the field name, "Memory Usage" as the field description and "%" as the field unit. Click the "Add" button. This field will be storing the free memory of your Pi as a percentage. Click the "Add" button. Click the "Add new Field" again and enter "diskfree" as the field name, "Disk Free Space" as the field description and "MB" as the field unit.

Under "List of Fields" you should now have 3 fields listed namely cputemp, diskfree and memusage.

Step 2 : Raspberry Pi python script

We will now create a python script on your Raspberry Pi to get the health values and send them to IoT Guru. Download the attachment "raspi_health.py.zip" to your computer, extract it and open the file raspi_health.py using your text editor. You will need to replace the following fields in the file with the API endpoint for your fields. First find the following text in file "{cpu_temp_api_address}", go back to your IoT Guru web page. Click on the "cputemp" field and click on the "Help" tab. Under "Generic Rest API" you will find the address endpoint for your field e.g. https://api.iotguru.cloud/measurement/create/xyxyxyxyxyxyxyxyxy123/cputemp/

Copy the address WITHOUT the value at end, the script will append the value to the end for you. Now replace the text "{cpu_temp_api_address}" with the address you just copied. The line should look like this now :-

cpu_temp_endpoint = "https://api.iotguru.cloud/measurement/create/xyxyxyxyxyxyxyxyxy123/cputemp/"

(Note the forward slash at the end and that there is no value)

Replace {disk_free_endpoint} with the same address. Edit the end of the address to be "https://api.iotguru.cloud/measurement/create/xyxyxyxyxyxyxyxyxy123/diskfree/". This is the endpoint for your Disk free space value. The line should look like this now :-

disk_free_endpoint = "https://api.iotguru.cloud/measurement/create/xyxyxyxyxyxyxyxyxy123/diskfree/"

Replace {mem_usage_endpoint} with the same address. Edit the end of the address to be "https://api.iotguru.cloud/measurement/create/xyxyxyxyxyxyxyxyxy123/memusage/". This is the endpoint for your memory usage value. The line should look like this now :-

mem_usage_endpoint = "https://api.iotguru.cloud/measurement/create/xyxyxyxyxyxyxyxyxy123/memusage/"

SSH into your Raspberry Pi. At the prompt type

nano raspi_health.py

(creates a new file on your Pi called raspi_health.py). Copy the contents from the open text file and paste them into the ssh session (right click -> paste, ctrl+v will not work). Type ctrl+o (writes out the file), then ctrl+x (exits the nano editor).

Step 3: Testing

Let's manually execute the script and confirm that everything is working. At the prompt on your Pi type

python3 raspi_health.py

(This command will use python3 to run the script). You should see 3 responses being returned for each of the values submitted. If you see anything about errors or traceback there is something wrong in your script. Make sure you have followed all the instructions.

Assuming no errors you should be able to view the data on IoT guru. The graphs will need more data to be able to display correct but you should be able to your data under the "Last Values". If you are seeing data under "Last Values" congratulations your script is working correctly. In the next step we will schedule the script to run automatically every hour using cron.

Step 4: Creating a Crontab Job

At the prompt type

crontab -e

If this is the first time you are using cron you might be prompted to pick a default editor. Nano is the easiest editor to use. Enter the number that corresponds to the Nano editor (usually number 1). Go right to the bottom of the file and enter the following :-

0 * * * * python3 /home/pi/raspi_health.py > /dev/null 2>&1

What we are telling cron to do is execute "python3 /home/pi/raspi_health.py" and discard any output ( > /dev/null) at minute 0 i.e. every hour.

Type ctrl+o (output the text) and then ctrl+x (exit editor). Your Raspberry Pi should now start submitting on the hour to IoT Guru.

Step 5: Creating the Alert

You can set a range alert and the IoT Guru Cloud will alert you whenever the measurement is less than the minimum or greater than the maximum.

For CPU temperature we are not really concerned with low temperature only excessively high temperatures. We are going to create an alert that has a range of 0 - 80. The official operating temperature limit is 85°C, and as a result the Raspberry Pi should start to thermally throttle performance around 82°C. If we are exceeding 80°C the Pi is running hot and we should get an alert.

Click on your field "cputemp" and click on the "Range Alert" tab. Tick the "Alert Enabled" tickbox. Enter 0 as the minimum value and 80 as the maximum value, clear the "Alert webhook Url" field. Click the "Save" button.

Check out this instructable for creating an alert https://www.instructables.com/id/Soil-Moisture-Sensor-With-Watering-Warning/

Step 6: Triggering the Alert

You can submit values to the field endpoint by simply copying the api endpoint address and pasting the address into your browser. e.g.

"https://api.iotguru.cloud/measurement/create/xyxyxyxyxyxyxyxyxy123/cputemp/" append the value 100 to the end of the address and hit enter. You should see some values returned to you in the browser window. You have now submitted the value 100 to the field which is outside the alert range. You should receive an email notifying you of the out of range exception.

This topic was modified 4 weeks ago 2 times by admin

New Member
Joined: 6 months ago
Posts: 1
09/07/2020 6:29 pm  

Hi Jasonb,

I don't have that much knowledge in REST API and just wanted to ask you since you posted how to send values to the cloud can you tell me how can I get a value from the cloud to the raspberry pi?