Fishtank Controller (part I)

My goal for this project was to be able to simply turn the lights on my fishtank on and off from anywhere in the world. This project was very successful and over time and grew into so much more.

Initially I ran implemented this on a virtual machine running Ubuntu Linux utilizing a MySQL database that contained one table and one record in this table. By clicking a button in a simple Android app, I was able to flip a field from “0” to “1” and vice versa. I then used a Raspberry Pi that was programmed to check the contents of the database field on a regular basis (every 5 seconds or so). The field value would determine whether the Raspberry Pi should turn on or off the lights on my fish tank.

This project grew over time to include not only lights, but also the ability to control filter pumps and heaters as well. The technology driving this system was also upgraded to use a software utility called MQTT which is a more standard messaging software developed for applications such as this. The system now consists of two Raspberry Pi devices; one acting as the MQTT broker and another acting as the MQTT subscriber. Openhab2 is the home automation software that is acting as the MQTT publisher.  The MQTT broker setup is documented in another post .  The diagram below shows the basic design of how MQTT operates.

The MQTT subscriber is a Raspberry Pi device loaded with MQTT but also contains a program that detects certain MQTT messages targeted towards the fishtank and will flip GPIO pin voltages from high to low based on the message received. A message to turn off the lights (or pump, heater, etc.) would change the voltage from low to high. There are 3 GPIO pins connected to an electronic relay which would convert the low RPi voltage to 110 volts in order to control the power flowing to the electric receptacles.

The python script that controls the GPIO pins based on the MQTT messages is shown below:


import paho.mqtt.client as mqtt
 import RPi.GPIO as GPIO
 import smtplib

sender = ''
 receivers = ['']
 password = '***********'

message = """From: From Person <>
 To: To Person <> Subject: SMTP e-mail test

This is a test e-mail message.

def on_connect(client, userdata, flags, rc):
 print("Connected with result code "+str(rc))

 smtpObj = smtplib.SMTP("",25)
 smtpObj.login(sender, password)
 smtpObj.sendmail(sender, receivers, message)
 print "Successfully sent email"
 except SMTPException:
 print "Error: unable to send email"

def process_message(longmsg):

 if ( longmsg == "/trentwood/basement/fishtank/light/ 1" ):
 elif ( longmsg == "/trentwood/basement/fishtank/light/ 0" ):
 elif ( longmsg == "/trentwood/basement/fishtank/pump/ 1" ):
 elif ( longmsg == "/trentwood/basement/fishtank/pump/ 0" ):
 elif ( longmsg == "/trentwood/basement/fishtank/heater/ 1" ):
 elif ( longmsg == "/trentwood/basement/fishtank/heater/ 0" ):

def on_message(client, userdata, msg):
 message = str(msg.payload)
 longmsg = (msg.topic+" "+message)

client = mqtt.Client()
 client.username_pw_set('usrmosquitto', password='**********')
 client.on_connect = on_connect
 client.on_message = on_message

client.connect("", 1883, 60)


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.