Sunday, July 24, 2016

Open Source Reef Controller Update #1

I decided to begin this project some time ago, but shelved the idea until recently. Why? The time, effort, and cost of developing a system with the capabilities I wanted were prohibitive. I wanted more than just an aquarium data logger; I wanted a controller capable of handling lighting, dosing, and a number of additional safety measures. As time wore on, I decided to simplify things by completing one section at a time. First up, temperature data logging!

Temperature Data Logging

Hardware Setup

A Raspberry Pi 1 B+ is the foundation of the controller. I've added a wifi dongle to connect it to my home network and allow for easy access through SSH. Temperature measurement will be handled by a waterproof DS18B20 sensor. This sensor can be run at 3.3v and communicates with the Pi using the one-wire interface. The digital interface is a must, since the Pi does not have an analog to digital converter. A 4.7k pull up resistor is needed on the data line.

Software Setup

Luckily, the heavy lifting on the software side was already complete. On my Pi 1 B+ running Raspbian, I installed the w1thermsensor Python module. This can be done with the below commands or by downloading the source from the link.

sudo apt-get install python-w1thermsensor
python --command-packages=stdeb.command bdist_deb
Before the sensor can be queried, onewire must be enabled in the device tree overlay. This is done by modifying the boot config file.

To modfiy the boot config file:
sudo nano /boot/config.txt 

Add the line dtoverlay=w1-gpio to the bottom of the file and save. This enables onewire on pin 4 of the pi. On my setup, I'm using pin 6 which requires dtoverlay=w1-gpio,gpiopin=6. The chosen pin is generally arbitrary.

With the hardware and software installed, it's simple to query the sensor by running the example script in the w1thermsensor documentation. From a python terminal:

from w1thermsensor import W1ThermSensor 
sensor = W1ThermSensor()
temperature_in_celsius = sensor.get_temperature() 


In lieu of a functional GUI, I decided to create the controller as an IoT device. As such, recorded data and alerts will be pushed to the cloud service ThingSpeak. ThingSpeak is a free service with a number of graphing and data analysis tools that's great for this kind of project. My controller will log data here for viewing. As features are added, additional charts and alerts can be appended to the site.

I created a simple python script to measure the temperature with the sensor and push the result to ThingSpeak every 15 minutes by executing the below as a crontab job.
# Developed by cmarzano
# 5/26/2016

import httplib, urllib
from w1thermsensor import W1ThermSensor

sensor = W1ThermSensor()
temp = sensor.get_temperature(W1ThermSensor.DEGREES_F)

# Input your thingspeak channel API key below
apikey = 'yourkeygoeshere'

params = urllib.urlencode({'field1': temp, 'key':apikey})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = httplib.HTTPConnection("")
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print response.status, response.reason
data =

To run the script every 15 minutes in crontab, you'll need to add the below line to your crontab list after entering crontab -e with the path correctly filled in.

*/15 * * * * /usr/bin/python /absolute/path/to/ 

Some additional tweaking on the ThingSpeak side completes the temperature logger. Embedded below is the LIVE data from my tank. I have the temperature sensor slightly downstream of the heater, which causes some observable fluctuations when the heater activates. As I make hardware and software changes to the setup, automatic updates may periodically stop.

No comments:

Post a Comment