Going to next level of Aquarium Automation...who's with me?

Which python script are you looking to use?

For myself, I have in most cases 2 separate python script for each Temp probe sensor. One that retrieves the temp in Celsius and the next one that does a Fahrenheit conversion.

This is the Celsius script. Note the Calibration adjustment section - if you don’t want to use this, remark it out. But I would eventually recommend doing a very minimum of 5 probe calibration to a single degree set point (whatever you usually keep your water temp at). When you’re ready to do this step tho, just let me know and I’ll help you more with that.

Also, be sure to insert the UUID # of each 1-Wire Temp probe to the respective file for that temp probe. Where you see the ##-############# in each script is where the 1-Wire UUID / Serial number needs to go.

the file name is, temp_1_C:

#!/usr/bin/python
import os
import glob
import time

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '##-##################')[0]
device_file = device_folder + '/w1_slave'

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
# Calibration adjustments next line
        temp_c = temp_c - 0.062
#        temp_c = temp_c - 0.124
# end calibration
        temp_c = round(temp_c, 2)
        return temp_c

print(read_temp())

#print("Temp: " + read_temp() + unichr(223) + "C")

Here’s my Fahrenheit script, which is called ‘temp_1_F’

    #!/usr/bin/python
    import os
    import glob
    import time

    os.system('modprobe w1-gpio')
    os.system('modprobe w1-therm')

    base_dir = '/sys/bus/w1/devices/'
    device_folder = glob.glob(base_dir + '##-##############')[0]
    device_file = device_folder + '/w1_slave'

    def read_temp_raw():
        f = open(device_file, 'r')
        lines = f.readlines()
        f.close()
        return lines

    def read_temp():
        lines = read_temp_raw()
        while lines[0].strip()[-3:] != 'YES':
            time.sleep(0.2)
            lines = read_temp_raw()
        equals_pos = lines[1].find('t=')
        if equals_pos != -1:
            temp_string = lines[1][equals_pos+2:]
            temp_c = float(temp_string) / 1000.0
     # Calibration section
            temp_c = temp_c - 0.062
     # end calibration
            temp_f = temp_c * 9.0 / 5.0 + 32.0
            temp_f = round(temp_f, 2)
            return temp_f

    #while True:
    #       print(read_temp())
    #       time.sleep(5)

    print(read_temp())

    #print("Temp: " + read_temp() + unichr(223) + "C")

Now, what I do is I have another script or crontab execute this script to retrieve the 1-wire temp as often as I might need it for that probe. For instance, an unheated culture or salt water mix tank probably doesn’t need to send Temp data more than every 60 seconds. But… the 25watt heater in my 10 litre Fry tank most definitely needs a temp reading every 15 seconds, so I don’t over shoot my target temp.

And that execution is called like this in one of my setups, which is triggered by another script that executes at startup and just loops at a predetermined delay interval.

mosquitto_pub -u USERNAME -P “PASSWORD” -t homeassistant/sensor/fishroom/water/1/temp_C -m “/usr/local/bin/temp_probe_1_C” -h HASSBIAN

Now… the upside to doing this this way, is it’s great for developing / testing the hardware as you build it because it’s pretty simple, modular and each script is basically specific to each probe. Which, since I’m not the worlds best python programmer (sic) I could imagine any single monolithic dynamically adjusting python script I might write to do this for all sensors might not bork out safely for a single sensor should only one sensor fail (in other words, my python script might could bork out on all of them). This initial approach of a single python script for each individual function was “safe and reliable” despite the overhead CPU cost on my first Gen RaspPi.

But I do eventually plan on replacing all of this. It’s not portable, it’s not user-friendly, it’s not self-adapting and dynamic, and it’s messy it’s like trying to kill a fly with a baseball bat. lol. I also made a mistake, which is instead of using a MQTT topic like ‘home assistant/sensor/fishroom/blah/blah/blah’ I SHOULD just be using something like ‘homeassistant/1wire/##-#################’. where the ###'s are the UUID of the 1-wire device.

Why? Because that allows maximum flexibility with temp probe mobility with minimal changes to HA and underlying configurational dependancies. If say you end up with a few Pi-Zero’s around the house with other temp probes, and you want to re-allocate temp probes later between multiple remote Pi-Zero’s - after physical relocation of the probes - any relabelling of the Temp probe interface name would only need to be done in HA’s configuration.xml (or sensor.xml if you broke that out).

In my current construction, I didn’t make it so easy on myself. And I’ve already had a few instances now, where I wanted to re-position temp probes, both in my house and in my aquarium, onto different remote sensor Pi’s… and I realised then, the error of my ways when I had to go around and change all my local Pi’ scripts to adapt. I should “just work” regardless of where I plug it in, without me needing to reconfigure anything (except for a name label for it’s new role… ie. - from Clownfish tank to Reef tank or Bathtub. :wink:

So my plan is to go back and re-write the python script with multiple goals now:

1.) make it 100% pure python (for way lower cpu overhead and portability)
2.) always watch automatically for new 1-wire devices
3.) enumerate and automatically poll all 1-wire devices & publish to MQTT server
4.) centralized management of individual temp probes calibration settings
5.) centralised management of publish frequency

I imagine someone might have something like this already and shared it, but I’ve not seen it yet. But it’ll probably take me a little bit of time to hack through it and get it right but should be a good python learning exercise. :slight_smile:

Ok I am running a similar script. I was able to somewhat modify it to work with two probes at the same time (I assume it would work with more than two no problem. Your script does have some nice functions though like calibration that would be helpful.

import time

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

temp_sensor1 = '/sys/bus/w1/devices/28-#######/w1_slave'
temp_sensor2 = '/sys/bus/w1/devices/28-#######/w1_slave'

def temp_raw1():
    f = open(temp_sensor1, 'r')
    lines = f.readlines()
    f.close()
    return lines

def temp_raw2():
    f = open(temp_sensor2, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp1():
    lines = temp_raw1()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = temp_raw1()
    temp_output1 = lines[1].find('t=')
    if temp_output1 != -1:
        temp_string1 = lines[1].strip()[temp_output1+2:]
        temp_c = float(temp_string1) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_c, temp_f

def read_temp2():
    lines = temp_raw2()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = temp_raw2()
    temp_output2 = lines[1].find('t=')
    if temp_output2 != -1:
        temp_string2 = lines[1].strip()[temp_output2+2:]
        temp_c = float(temp_string2) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_c, temp_f

while True:
        print(read_temp1())
        print(read_temp2())
        time.sleep(1)

So do you think it would make more sense for me to continue expanding on this script or go the separate script route like you have? Most of this is just temperature monitoring for me right now, not control. My reef angel is still controlling my heater.
Love the sound of the all in one script. I’m a long ways away from being able to help with that though.

Now MQTT is a whole nother topic. I have only been using cloudMQTT so far and I know I need to switch to mosquitto but haven’t figured out what I’m doing in that regard yet.

Thanks for being so helpful. This concept is amazing and hopefully you’ll be helping out more than just me by answering all my questions.

The calibration function is a single line, so you can just copy paste it into yours.

It’s really up to you, but you may as well go with what you’re doing now.

And no worries about not being able to help. It’s not on my priority list at the moment to work on anyways.

Part of the reason I even shared it is maybe someone else comes along and either has already coded it, or know’s how to code it but didn’t have the idea yet, and would be willing to share with us. And the other reason is by putting this project out for others to be inspired by, or to even take and modify - I’m hoping we can all share in the benefits together. :smiley:

mosquitto is super easy. Just read through /etc/mosquitto/mosquitto.conf and set the basics there. Create a username /password combo, you’re basically off and running. Once you create a topic, other devices can subscribe to and monitor it.

This is an earlier pic from when I had my 500 gallon tank. It is 4 feet front to back which is really sweet. Now it has been sitting empty for over a year now. I tried to sell it but gave up as its too big and custom made for me. Just got into the home assistant a few weeks ago. And googled home assistant instead of neptune apex like I have used in the past. Wish i would have known about this stuff then haha. It may get set up again, but I think its going to be fresh water. Looks like I could do everything I needed with HA for a lot cheaper. Just got the garage door automated for when i leave and come home, and the furnace is running off of it as well. Got a nice shipment of a bunch for stuff coming for more projects.

2 Likes

WOW! Sorry to hear you stopped with it. But yeah, if you get back into aquarium hobby and even go freshwater route, I hope what we’re doing here can help you with that.

Yeah…when I looked at all the aquarium controllers on the market & measured them against the kind of technologies we architect-ed and engineered and built in the world of Mobile Telecommunications (and still do)… I found the current crop of aquarium controllers left me wanting so much more. For instance… who in their right mind would throw down 800 euros for a mobile phone, and then accept they have to spend another 100 or 200 for a WiFi Module in 2018? And said company wants me to think their Ardunio like controller is just as / more powerful than a modern mobile phone? Really? Ehhh… no.

And breakout boxes that use SPI and the like but only offer 4, 5, maybe 8 break out ports for other things, but not much else, at a price point of 150-300 euros. Why not offer “redundant” controller inside that remote breakout box instead? One that could offer nearly all the same controlling services as the primary controller, in order to maintain total system control availability and up-time reliability. Advanced aquariaist who keep “remote operations” running in addition to their display tanks, say someone who raises Fry or Food cultures in their Garage would benefit from electrical home circuit redundancy in the event the circuit breaker / fuse went out on their display tanks at 3am. The backup remote unit then fires up / reload a different configuration of HA and proceeds to determine what happened and what to do based on the pre-programmed actions I set.
This seriously saved my & my tanks ass twice now in 2017.

And that’s where I thought if I lay out thousands for something like what’s available today, it should do all this for me already. Unfortunately, they don’t really deliver that. So I thought I’d build it myself & quickly discovered the community behind HA already gave me an awesome framework to build most of that in. :slight_smile:

Nice tank, is it a ReefSavy?

Ok so I haven’t been able to work on this all that much, but I have my script working so it will read all 5 temperature probes. I also have mosquitto installed on the pi zero (which was not an easy task running rasspian stretch!)

Where I am not sure how to handle this next is how to make the pi zero publish this topic and then set up home assistant to receive the data and display it.

I’m guessing I will be entering a line similar to this in my config.yaml:

- platform: mqtt
    name: "Fish Room Temp"
    state_topic: "tele/TempHumidityFishRoom/SENSOR"
    unit_of_measurement: "°C"
    value_template: "{{ value_json.AM2301.Temperature }}"

but I have no idea what to do on the pi zero to make the two talk to each other for lack of a better term. (that code is from my config but it is just reading from a sonoff switch)

This is how I have it config’d on Home Assistant for the AM2305 (slightly diff than yours in that it does both Temp and Humidity). Respectively, I publish both values to differently named state topics as described below, so of course HA is configured to listen (subscribe) to those MQTT sub topics and that’s reflected here:

platform: mqtt
state_topic: “homeassistant/sensor/nemopi/temp/1/temp_C”
name: “Downstairs Celsius”
unit_of_measurement: C

platform: mqtt
state_topic: “homeassistant/sensor/nemopi/humidity/1/humidity”
name: “Downstairs Humidity”
unit_of_measurement: H

Yes that makes sense. My issue is getting the mosquitto topics on the pi zero up and running. I know the server is running, but I am having some issues getting it to work. I can get it to publish a test message, and when I have mosquitto running (by typing “mosquitto” in the console) I can see the connection being made, however, I do not see the message published anywhere?

Then I have tried publishing a topic similar to what you put above for the temperature sensor, but I have no idea if the data is being published properly or if home assistant is even receiving it. My entity in homeassistant simply says unknown for temperature.

If you use chrome on a pc you can install Mqttlens to check whats happening with the mqtt topics.

1 Like

What’s your current MQTT client / submission setup as?

I’m really not sure what that is? I have a conf file set up fairly basic really. I added a username and password. It is set to listen on port 1883. The username and password are the same ones I’ve set up for the mosquitto server on home assistant.

I’m sure this will seem easy once I get it but right now its all very foreign.

if I subscribe to ‘test/topic’ on the broker then type mosquitto_pub -t ‘test/topic’ -m ‘helloWorld’ in another window
I will get:

new connection from ::1 on port 1883
new client connected from ::1 as mosqpub|18272-raspberry (c1, k60)
client mosqpub|18272-raspberry disconnected

I do not receive the helloworld message anywhere though?

Have you checked that a username password is required in your configuration? Try setting one in the mosquitto.conf file and then use that to see if it changes anything.

There’s a few ways to submit and here’s a couple that I have right now that work for me.

Run a few commands of python and take result as input:
mosquitto_pub -u USER -P “PASSWORD” -t homeassistant/sensor/nemopi/temp/1/temp_C -m “python3 -c "from AM2315 import * ; sensor = AM2315() ; c = sensor.temperature(); print(c)"” -h HOSTNAME

Call a much larger script and take result as input:
mosquitto_pub -u USER -P “PASSWORD” -t homeassistant/sensor/nemopi/temp/1/temp_C -m “/usr/local/bin/temp_probe_1_C" -h HOSTNAME

I used the mosquitto_passwd command to set my username and password. It appeared to work properly. What do you mean by take the result as input? Also for HOSTNAME what should I be putting there? Would it be HASSIO? or do I make up what I want it to be?

Finally my python scripts appear to be saved in my root directory on the pi (after I sign in and type ls the files are right there). Is that folder the /usr/local/bin/ folder? Also it is not necessary to put the type of file on this? eg temp1_F.py?

Thanks

take the result as input = take the output of the script and use that as the data to transmit over MQTT.

HOSTNAME is the hostname of your Mosquito server which is probably your Home Assistant system? so whatever hostname or IP address that’s running on. If you’re remote from the Mosquito server, you’ll have to specify this, if you’re on the same computer, it will default to localhost if you don’t specify anything.

You can find out what directory you’re in, by typing the command ‘pwd’ [ENTER]. I suspect if you’re logging in as pi, that’s gonna be in /home/pi

the /usr/local is a pseudo linux standard where locally authored / maintained scripts and programs can often be installed in. It’s not uncommon for Linux / Unix file systems to have more than one “bin” directory. Generally, you can often find all the core base system utilities in /bin & /sbin, where more common applications installed via package management utils will normally be installed in /usr/bin & /usr/sbin. Whereas /usr/local/bin & /usr/local/sbin is left reserved for system owners to install their own scripts without worry / concern that future installs / upgrades of the overall Linux OS will damage / touch these custom / self-authored files.

You can put a .py extension on it if you like. But Unix/Linux reads and interprets if a script is executable differently than Windows, so extensions are not usually necessary. It’s more important to set the mode to ‘executable’ (as in: chmod 755 script-name-here ).

Ok. That is making sense. I will have to try this a bit when I get home from my trip. Really would be nice to be able to work on this while I’m sitting in a hotel! haha

I am remote from the mosquitto server. The temp probes are plugged into a pi zero and my Hass.io is on a separate RP3. Gonna put the pi zero in the fish room and hopefully run all my probes and what not off of it.

I’m leaning towards trying out the tentacle you mentioned a while back at some point. When you start pricing out all the components separately to set up pH monitoring you get very close to the same cost if not more with the tentacle having expand-ability.

Ok progress. I got a communication to HA which is a start. I used this message: mosquitto_pub -u USER -P “PASSWORD” -t homeassistant/sensor/fishroom/temp_F -m “/home/pi/temp1_F" -h HA IP address

In my entities tab on HA the temp sensor shows a state of: /home/pi/temp1_F. So it sent the message and HA received it, just not what I was hoping it would receive lol.

So now how do I create a script on the pi zero to constantly send that message and then how to I turn that message into the temp data that running that python script produces? If I run the python script I can’t do anything else until I stop the script.

I’ve been wanting to post in this thread since I took over a small freshwater aquarium from a neighbour. But now I’m finally ready to start automating some things.

I’ve had to basically buy everything new since the old 60L started losing water.

Current setup: 80x40x40cm, 14fish, 230V AC pump 400L/h with manual water flow restriction valve, 230V 200W Scuba heater, analog temperature sensor. Raspberry Pi3 running MJPEG Streamer.

Here’s a livestream: https://vicious.thomasbrasser.nl

I’m planning on adding the following soon

  • Sonoffs to get relais and sensors on MQTT
  • 2x 15W RGB+CCT LED downlight (also controlled over MQTT)
  • obviously some sensors

and I’m going to hide the air tubes, but I haven’t found a way to get them where I want after the aquarium has already been setup.

I’m planning on using the flux component to simulate a natural day/night cycle, any more suggestions on what to automate on a simple aquarium?

2 Likes

You’ll probably want to hook up your heater to a sonoff and use the temp sensor to control your heater. That way you create a redundancy if the heater fails on. (If you’ve been following the thread you know to not ask me how to get the temp sensors setup! lol)

Also I know auto top off is more of a saltwater thing but keeping your tank always full is also a nice function.

The natural daylight cycle should be neat.

1 Like

Yeah I was planning on going that route with the heater. With the sonoff/mqtt/hass automation at 24C and the heater itself set at 25.

Unfortunately I do not have an easy way to plumb a water topoff system, as I’m in a single dorm room.

I was thinking of adding a ph probe and maybe an automatic feeder.

1 Like