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:

pi@fish_controller_pi:~/Downloads/newtest3.py

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

sender = 'pbosker@hotmail.com'
 receivers = ['pbosker@hotmail.com']
 password = '***********'

message = """From: From Person <foo@hotmail.com>
 To: To Person <pbosker@hotmail.com> 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))
 client.subscribe("/trentwood/basement/fishtank/#")

try:
 smtpObj = smtplib.SMTP("smtp.live.com",25)
 smtpObj.ehlo()
 smtpObj.starttls()
 smtpObj.ehlo()
 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):

print(longmsg)
 if ( longmsg == "/trentwood/basement/fishtank/light/ 1" ):
 GPIO.output(17,GPIO.HIGH)
 elif ( longmsg == "/trentwood/basement/fishtank/light/ 0" ):
 GPIO.output(17,GPIO.LOW)
 elif ( longmsg == "/trentwood/basement/fishtank/pump/ 1" ):
 GPIO.output(27,GPIO.HIGH)
 elif ( longmsg == "/trentwood/basement/fishtank/pump/ 0" ):
 GPIO.output(27,GPIO.LOW)
 elif ( longmsg == "/trentwood/basement/fishtank/heater/ 1" ):
 GPIO.output(22,GPIO.HIGH)
 elif ( longmsg == "/trentwood/basement/fishtank/heater/ 0" ):
 GPIO.output(22,GPIO.LOW)

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

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

client.connect("192.168.1.201", 1883, 60)

GPIO.setmode(GPIO.BCM)
 GPIO.cleanup()
 GPIO.setwarnings(False)
 GPIO.setup(17,GPIO.OUT)
 GPIO.setup(22,GPIO.OUT)
 GPIO.setup(27,GPIO.OUT)
 client.loop_forever()

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.