I am going to show you how to make a small RaspberryPI server to monitor a temperature using a sensor. I will be using a RaspberryPI, DS18B20 Waterproof Digital temperature sensor. I'll start with how to set up the hardware first and jump to the server side code later.
Here is what we need for this project:
- Hardware: RaspberryPI, breadboard, jumper cables and a resistor, and DS18B20 Waterproof Digital temperature sensor
The DS18B20 Waterproof Digital temperature sensor has three wires: Yellow - data(GPIO), Red - power(3v) and Blue - ground. Now use the breadboard to make a simple circuit. User the 4.7k resistor that between the power supply and the data. Using the jumper cables provide power to the sensor from the PI using the first pin. Then use any ground pin on the PI and connect to the board.
Now let's test the sensor
Before you run the command to test the sensor, edit the /boot/config.txt file. Here I am using nano to edit the file
Then, add the following line:
Here, we are enabling the wire interface with the raspi-config tool. If you open the manual: /boot/overlays/README, you can see the full documentation of the "dtoverlay" command.
Now save the file and reboot the PI.
To test the sensor, we need to communicate with the operating system and load the kernel modules:
Then locate the device by running the following command:
When you list the files, you should see 28-xxxxxxxxxxxx device directory. This is the ROM of DS18B20. If more than one DS18B20 are connected, you will find more directories. Cd into the directory and, type:
The interface is not that great so look for crc=da YES. The YES means it’s working, and the t=n is the celsius*1000. This verifies the sensor is functioning. Now that we're sure the hardware is working, let's collect some data and store it.
Now let's gather some data. The code is fairly simple. As the operating system to load the kernel modules, locate the device's location, read the file and parse the data and finally do the appropriate calculation to get the temperature data. Here is a code I lifted from the Adafruit website:
This code a good starting point and when you execute the Python script, the application should display the temperature in Celsius and Fahrenheit. But Our goal is to persist this data to a Firebase real-time database so we can use the data to build an app. So we have to change a couple of things:
First, we need to format this data as a JSON format and to send it to Firebase.
To be able to do this, we need to add time and calendar to the python script and remove the while loop.
Now add the following to the python script:
At this point, we are almost done with the server side of the application but there is one more thing we need to do since we remove the loop from the python script we need some kind of mechanism to automate the data record process. Some might think why not add the loop back to the script and run the code. This is not a good idea for many reasons but the main reason is we need to add additional service to run this small script which adds more overhead, and we don't want that but rather we can use Cron Jobs. Cron is used for scheduling tasks that run on the server. It is a daemon process that runs on the system. To do this scheduled task, type in:
If it's your first time running the application it will prompt you to select an editor, i use nano edit.
This command is telling Cron to get the temperature every 10 minutes but you can change it whatever setting you desire to. Now the schedule is all set and we should be getting new data to our database. On the next article, we will use this data and create our Ionic application.