Temperature Monitoring

How to connect a DS18B20 temperature sensor? Just google how to do this, and you’ll see a lot of pictures like this one:

In my case I had Black, Yellow and Red wires. The black is ground, goes to Ground pin, the red is power — goes to 3.3v pin, and the yellow is data — should go to GPIO4 pin, with 4.7 kOm resistor connected between data and power.

Note, you can connect several sensors in parallel (they are digital, and has different addresses), you need only one resistor.

After connecting your sensor, you should enable 1Wire in the raspi-config:

sudo raspi-config

Go to 5 Interfacing options, enable P7 1-Wire and reboot.

Then you can test if you can see the sensor:

sudo modprobe w1-gpio
sudo modprobe w1-therm
ls /sys/bus/w1/devices/

You should see something like this:

pi@vcontrol:~ $ ls /sys/bus/w1/devices/
2800044eae2dff w1_bus_master1

28–00044eae2dff is our temperature sensor.

Hardware is ready. Now I need to set up the monitoring part. I need something that would show me the data and notify me if the device is disconnected for a while or there is no power, or the temperature is low. Obviously this cannot be raspberry pi itself, there should be some server or service in the internet that monitors my device.

I can create a simple server, get a hosting and set everything up, but honestly, I don’t want to. Luckily, somebody has thought about this already and created cloud4rpi.io — a cloud control panel for your device.


Setting Up Cloud4RPi.io

Cloud4RPi provides a service that let your device send and receive data using MQTT or HTTP protocols. They have a client library for Python, so I’ll use Python.

Python examples that comes with Cloud4RPi service already contains code for DS18B20 temp sensor.

So I went to https://cloud4rpi.io, created an account and added a new device there. The device page has a token — a string that identifies the device, and which should be specified in the program that sends data.

To begin with, it is always a good idea to update a package manager and upgrade packages (note: it can take hours if you haven’t upgraded for a while)

sudo apt-get update && sudo apt-get upgrade

Then, install git, Python and its package manager Pip:

sudo apt-get install git python python-pip

Then, install cloud4rpi Python library:

sudo pip install cloud4rpi

Finally, i’m ready to write my control program. I start from example available at https://github.com/cloud4rpi/cloud4rpi-raspberrypi-python

git clone https://github.com/cloud4rpi/cloud4rpi-raspberrypi-python.git cloud4rpi && cd cloud4rpi

 The main program file is control.py —  I need to modify it for my needs. First, edit the program and paste a token:

sudo nano control.py

Find a line DEVICE_TOKEN='...' and specify a device token there. After that I can simply run the program:

sudo python control.py

It works and reports a temperature in RoomTemp variable:

Note that it discovers all onewire ds18b20 sensors

ds_sensors = ds18b20.DS18B20.find_all() 

 and uses the first found sensor:

'RoomTemp': {
    'type': 'numeric',
    'bind': ds_sensors[0] if ds_sensors else None
}

Ok, that was easy, because the sample program has everything that is required to work with ds18b20 sensor on Raspberry Pi. Now I need to find the way to report the power status.

UPS Monitoring

Next thing I want to monitor is UPS status, so if there is power outage, I will know about it before everything disconnects.

I have an APC UPS with USB control, so I quickly googled and found that I need apcupsd. http://www.anites.com/2013/09/monitoring-ups.html....

Read more »