reef-pi :: An opensource reef tank controller based on Raspberry Pi.

robsworld78

Valuable Member
View Badges
Joined
Feb 14, 2020
Messages
1,029
Reaction score
1,293
Location
Edmonton, Canada
Rating - 0%
0   0   0
thank you very much @robsworld78!! It’ll test it and let you know!
regarding the auto run of the script it is better to start at boot and let it run with a sleep command at the end or schedule the script with crontab?
Thank you again!!
No problem but I really messed up the code, that's what I get for rushing, unfortunately I can't edit that post. Still untested but should work. I must get to bed now but tomorrow I'll post a service file so the script runs when the Pi boots.

Python:
import os
import time
import Adafruit_DHT

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

    if temperature is not None:
        f = open('/var/lib/reef-pi/dht22/temperature.txt', 'w')
        f.write(temperature)

    if humidity is not None:
        f = open('/var/lib/reef-pi/dht22/humidity.txt', 'w')
        f.write(humidity)

    time.sleep(30)
 

Simonv92

Active Member
View Badges
Joined
Oct 21, 2014
Messages
161
Reaction score
104
Location
Italy
Rating - 0%
0   0   0
Thank you again @robsworld78!! I've tested the python script, I think that the only missing part is a conversion from float to string to print the right value. I've tryed with:

Python:
import os
import time
import Adafruit_DHT

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

    if temperature is not None:
        temp_string = str(temperature)
        f = open('/var/lib/reef-pi/dht22/temperature.txt', 'w')
        f.write(temp_string)

    if humidity is not None:
        hum_string = str(humidity)
        f = open('/var/lib/reef-pi/dht22/humidity.txt', 'w')
        f.write(hum_string)

    time.sleep(30)

But it doesn't work...

Reef-Pi side I setup the file path and if I write manually any value in the files Reef-Pi show me that value :beaming-face-with-smiling-eyes:
 

Sral

Valuable Member
View Badges
Joined
May 2, 2022
Messages
1,015
Reaction score
943
Location
Germany
Rating - 0%
0   0   0
I believe you might be missing an f.close().

I would advise you to use something like the following:
Python:
import os
import time
import Adafruit_DHT

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
  
    if temperature is not None:
        with f = open('/var/lib/reef-pi/dht22/temperature.txt', 'w'):
            # go to beginning of the file
            f.seek()
            # write temperature as a floating point number(f)
            # with sign(+), 5 characters (5) and 1 digit(.1)
            f.write("{:+5.1f}".format(temperature))

    if humidity is not None:
        with f = open('/var/lib/reef-pi/dht22/humidity.txt', 'w'):
            # go to beginning of the file
            f.seek()
            # write humidity as a floating point number(f)
            # 4 characters (4) and 1 digit(.1)
            f.write("{:4.1f}".format(humidity))
  
    time.sleep(30) # in seconds
The "with" statement takes care of closing the file after writing under all curcumstances (and thus avoids errors)
and the format takes care of formating the numbers to the appropriate string representations, assuming they are numbers to begin with.

Depending on circumstances, one might also add a "f.truncate()" in between "f.seek()" and the "f.write(...)" to make sure the file is realy empty and won't contain any leftovers from old values.
 

Simonv92

Active Member
View Badges
Joined
Oct 21, 2014
Messages
161
Reaction score
104
Location
Italy
Rating - 0%
0   0   0
I believe you might be missing an f.close().

I would advise you to use something like the following:
Python:
import os
import time
import Adafruit_DHT

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
 
    if temperature is not None:
        with f = open('/var/lib/reef-pi/dht22/temperature.txt', 'w'):
            # go to beginning of the file
            f.seek()
            # write temperature as a floating point number(f)
            # with sign(+), 5 characters (5) and 1 digit(.1)
            f.write("{:+5.1f}".format(temperature))

    if humidity is not None:
        with f = open('/var/lib/reef-pi/dht22/humidity.txt', 'w'):
            # go to beginning of the file
            f.seek()
            # write humidity as a floating point number(f)
            # 4 characters (4) and 1 digit(.1)
            f.write("{:4.1f}".format(humidity))
 
    time.sleep(30) # in seconds
The "with" statement takes care of closing the file after writing under all curcumstances (and thus avoids errors)
and the format takes care of formating the numbers to the appropriate string representations, assuming they are numbers to begin with.

Depending on circumstances, one might also add a "f.truncate()" in between "f.seek()" and the "f.write(...)" to make sure the file is realy empty and won't contain any leftovers from old values.
Thank you @Sral, I've tryed the script but I have a SyntaxError in line 12:
with f = open('/var/lib/reef-pi/dht22/temperature.txt', 'w')

What can it be? Thank you!
 

Sral

Valuable Member
View Badges
Joined
May 2, 2022
Messages
1,015
Reaction score
943
Location
Germany
Rating - 0%
0   0   0
Thank you @Sral, I've tryed the script but I have a SyntaxError in line 12:
with f = open('/var/lib/reef-pi/dht22/temperature.txt', 'w')

What can it be? Thank you!
Looks like you have forgotten the ":" at the end of the line.

Ah, no, I think I screwed up the syntax, it should probably be:
Code:
with open('var/lib/reef-pi/dht22/temperature.txt') as f:

The corresponding "humidity.txt" for the humidity, of course.
 

Simonv92

Active Member
View Badges
Joined
Oct 21, 2014
Messages
161
Reaction score
104
Location
Italy
Rating - 0%
0   0   0
Thank you for the reply, I've make it work like that:


Python:
  GNU nano 5.4                                                                                                            DHT22_Read.py
import os
import time
import Adafruit_DHT

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

    if temperature is not None:
        with open('/var/lib/reef-pi/dht22/temperature.txt', 'w') as my_file:
            # write temperature as a floating point number (f)
            # with sign(+), 5 characters (5) and 1 digit(.1)
            my_file.write("{:+5.1f}".format(temperature))

    if humidity is not None:
        with open('/var/lib/reef-pi/dht22/humidity.txt', 'w') as my_file2:
            # write humidity as a floating point number(f)
            # 4 characters (4) and 1 digit(.1)
            my_file2.write("{:4.1f}".format(humidity))

    with open('/var/lib/reef-pi/dht22/temperature.txt') as my_file:
        print(my_file.read())

    time.sleep(10) # in seconds

Don't know if it's the best way but it seems to work..
How can I make it auto start at boot?
 

robsworld78

Valuable Member
View Badges
Joined
Feb 14, 2020
Messages
1,029
Reaction score
1,293
Location
Edmonton, Canada
Rating - 0%
0   0   0
Thank you for the reply, I've make it work like that:


Python:
  GNU nano 5.4                                                                                                            DHT22_Read.py
import os
import time
import Adafruit_DHT

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

    if temperature is not None:
        with open('/var/lib/reef-pi/dht22/temperature.txt', 'w') as my_file:
            # write temperature as a floating point number (f)
            # with sign(+), 5 characters (5) and 1 digit(.1)
            my_file.write("{:+5.1f}".format(temperature))

    if humidity is not None:
        with open('/var/lib/reef-pi/dht22/humidity.txt', 'w') as my_file2:
            # write humidity as a floating point number(f)
            # 4 characters (4) and 1 digit(.1)
            my_file2.write("{:4.1f}".format(humidity))

    with open('/var/lib/reef-pi/dht22/temperature.txt') as my_file:
        print(my_file.read())

    time.sleep(10) # in seconds

Don't know if it's the best way but it seems to work..
How can I make it auto start at boot?
I think next time I better test. :) Glad you guys got it working.

Try this to get it running when the pi starts.

Create a new file using the command below.

sudo nano /etc/systemd/system/dht22.service

Paste in the following to the new file. You may need to change the first two lines under "service" section to point to the location you have the python script and also update the script file name to match.

Code:
[Unit]
Description=reef-pi DHT22
After=network.target

[Service]
ExecStart=/var/lib/reef-pi/dht22/humidity_logger.py
WorkingDirectory=/var/lib/reef-pi/dht22/
StandardOutput=inherit
StandardError=inherit
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

Then run the following two commands and reboot the Pi to see if it starts.

sudo systemctl enable dht22.service

sudo systemctl start dht22.service


If you run the following command you can see if it's running.

sudo systemctl status dht22.service
 

Simonv92

Active Member
View Badges
Joined
Oct 21, 2014
Messages
161
Reaction score
104
Location
Italy
Rating - 0%
0   0   0
Hi @robsworld78 thank you again,
I've tested your instruction, I only have to add the path to Python3 in the ExecStart command:

Python:
ExecStart=/usr/bin/python3 /var/lib/reef-pi/dht22/dht22_read.py

Now everything works fine :beaming-face-with-smiling-eyes:

I ask to both of you, @Sral & @robsworld78 can I write a post as a reference guide if someone else would like to use this type of sensor?
Thank you!
 

Sral

Valuable Member
View Badges
Joined
May 2, 2022
Messages
1,015
Reaction score
943
Location
Germany
Rating - 0%
0   0   0
Absolutely ! Just open a new thread and post the instructions. I did the same with my SCD30 Air Quality sensor code. I also used @robsworld78's pH code and service file as a base and he didn't have a problem with it, so I would guess that he will likely be fine with this as well. In my case I included the same LICENSE that he used for good measure.
 

robsworld78

Valuable Member
View Badges
Joined
Feb 14, 2020
Messages
1,029
Reaction score
1,293
Location
Edmonton, Canada
Rating - 0%
0   0   0
Hi @robsworld78 thank you again,
I've tested your instruction, I only have to add the path to Python3 in the ExecStart command:

Python:
ExecStart=/usr/bin/python3 /var/lib/reef-pi/dht22/dht22_read.py

Now everything works fine :beaming-face-with-smiling-eyes:

I ask to both of you, @Sral & @robsworld78 can I write a post as a reference guide if someone else would like to use this type of sensor?
Thank you!
Great, glad you got it working. I think it's a great idea to start a new thread explaining how to set it up, I'm sure a lot of people would appreciate it.
 

scottsmith

New Member
View Badges
Joined
Jul 21, 2021
Messages
4
Reaction score
7
Location
Australia
Rating - 0%
0   0   0
Yet another reef pi build .
Brain, doser and power board. I’ve got a fuge light on the way to.
Happy to share parts or files if anybody needs. Was tricky finding all the bits for an Australian power board.
 

Attachments

  • 65A513C6-ADE8-4864-A1DF-B42E03CE46E0.jpeg
    65A513C6-ADE8-4864-A1DF-B42E03CE46E0.jpeg
    333 KB · Views: 116
  • 0A91A114-CD95-45CB-BC27-E69167F50615.jpeg
    0A91A114-CD95-45CB-BC27-E69167F50615.jpeg
    375.5 KB · Views: 118
  • D16C0928-1410-48F2-9232-E31989B5FFA5.jpeg
    D16C0928-1410-48F2-9232-E31989B5FFA5.jpeg
    155.4 KB · Views: 106

Sral

Valuable Member
View Badges
Joined
May 2, 2022
Messages
1,015
Reaction score
943
Location
Germany
Rating - 0%
0   0   0
Yet another reef pi build .
Brain, doser and power board. I’ve got a fuge light on the way to.
Happy to share parts or files if anybody needs. Was tricky finding all the bits for an Australian power board.
Looks gorgeous !

Makes me feel even sillier for not going for 3D printed cases and etched PCBs with SMD components ... mine looks much less sophisticated ^^

You can try to make a Build-thread, that way @Ranjib has an easier time linking those sources of information on the Website and it's a bit easier to find.
 
Last edited:

scottsmith

New Member
View Badges
Joined
Jul 21, 2021
Messages
4
Reaction score
7
Location
Australia
Rating - 0%
0   0   0
Looks gorgeous !

Makes me feel even sillier for not going for 3D printed cases and etched PCBs with SMD components ... mine looks much less sophisticated ^^

You can try to make a Build-thread, that way @Ranjib has an easier time linking those sources of information on the Website and it's a bit easier to find.
Thanks!
I’ll put up a post when I get time
 
OP
OP
Ranjib

Ranjib

7500 Club Member
View Badges
Joined
Apr 16, 2016
Messages
9,876
Reaction score
16,680
Location
Pleasant Hill, Concord
Rating - 0%
0   0   0
Yet another reef pi build .
Brain, doser and power board. I’ve got a fuge light on the way to.
Happy to share parts or files if anybody needs. Was tricky finding all the bits for an Australian power board.
This looks awesome :-) thank you for sharing
 

elysics

Valuable Member
View Badges
Joined
Jan 15, 2020
Messages
1,695
Reaction score
1,607
Rating - 0%
0   0   0
Have you played around with the i2c baudrate? Those 0 values (or near zero after calibration) are from failure in i2c communication with the pH board ime. The change in 6.0 is that failed communication, instead of not showing up at all, shows up as a 0. From the graph, does a dosing pump kick on in those pH valleys? Maybe that's electrical interference from the pumps power line onto the i2c lines.

If you can't fix the communication, you can make the spikes smaller visually in the graph by putting a cutoff function in the transform function field that replaces those near zero values with a constant like 8 or whatever. That would clean up the graph, just shouldn't forget that you did that and that those aren't real readings.
 

iamdan

Community Member
View Badges
Joined
Jul 1, 2022
Messages
89
Reaction score
67
Location
Western Australia
Rating - 0%
0   0   0
Have you played around with the i2c baudrate? Those 0 values (or near zero after calibration) are from failure in i2c communication with the pH board ime. The change in 6.0 is that failed communication, instead of not showing up at all, shows up as a 0. From the graph, does a dosing pump kick on in those pH valleys? Maybe that's electrical interference from the pumps power line onto the i2c lines.

If you can't fix the communication, you can make the spikes smaller visually in the graph by putting a cutoff function in the transform function field that replaces those near zero values with a constant like 8 or whatever. That would clean up the graph, just shouldn't forget that you did that and that those aren't real readings.
Hi Elysics

I haven’t been fiddling with the i2c baud rate it’s on 10000 and for the issue to occur on two Robo tank boards on two different tanks with v6.0 and not 5.3 or older tells me it has to be a software glitch as Ranjib had stated in the GitHub post.

No dosing pump or any other functionality runs at the time of the dip. I’ve also run everything with only the ph probe connected and everything else unplugged or off and still the same thing.

When you say fiddling with the i2c value do you mean setting it lower than 10000?
 

Sral

Valuable Member
View Badges
Joined
May 2, 2022
Messages
1,015
Reaction score
943
Location
Germany
Rating - 0%
0   0   0
@iamdan Looks like a strange thing your error:
Line 1645: Nov 28 23:58:36 robopi reef-pi[368]: 2022/11/28 23:58:36 ph sub-system: Probe: pH Reading: 0.16442953020134254
Line 2979: Nov 29 00:59:36 robopi reef-pi[368]: 2022/11/29 00:59:36 ph sub-system: Probe: pH Reading: 0.16442953020134254
Line 4313: Nov 29 02:00:36 robopi reef-pi[368]: 2022/11/29 02:00:36 ph sub-system: Probe: pH Reading: 0.16442953020134254
Line 5647: Nov 29 03:01:36 robopi reef-pi[368]: 2022/11/29 03:01:36 ph sub-system: Probe: pH Reading: 0.16442953020134254
Line 6975: Nov 29 04:03:36 robopi reef-pi[368]: 2022/11/29 04:03:36 ph sub-system: Probe: pH Reading: 0.16442953020134254
Line 8309: Nov 29 05:04:36 robopi reef-pi[368]: 2022/11/29 05:04:36 ph sub-system: Probe: pH Reading: 0.16442953020134254

You basically seem to get the error once per hour and its the same numerical value every time, indicating that it might be an error value of "0" (internal of due to I2C communication) that is shifted by calibration to the 0.16

I remember reading something exactly like this before, e.g. hourly errors on certain measurement values, but I can#t remember what or whom it was ...

But I guess it might already be fixed in the next update, since @Ranjib would "only" need to implement an "error mode" for sensors. Maybe a hardcoded "no Integer value of 0" would even suffice for start.

To be fair though, your pH seems jumpy even without the regular dips to 0.16 ... have you tried using something like a grounding-probe ?
 
Last edited:

Sral

Valuable Member
View Badges
Joined
May 2, 2022
Messages
1,015
Reaction score
943
Location
Germany
Rating - 0%
0   0   0
Hi Sral thanks for the diagnosis :)

Ph is “jumpy” as is a freshwater tank with co2 injection :)
Ah good to know ! I'll hopefully be running something like that as well ^^

Although I was talking more about the spikes in the data, not the oscillating pattern from the CO2 injection:
1683310892167.png

Those are way too sharp to represent a real change in pH, but rather look like an electrically caused anomaly. Incidentally they always coincide with your lower turning point in pH, so that should be more or less when your valve shuts the CO2 supply off, right ? I guess it is actuated by a electromechanical valve with a solenoid inside that can cause a lot of disturbances when switched off.

So it seems possible that you either have a badly designed solenoid driver, or your measurement and/or I2C system is sensitive to these electrical distrubances.
 

TOP 10 Trending Threads

WHAT AMOUNT OF LIVE ROCK AND SAND SHOULD BE PRIORITIZED FOR OPTIMAL BIODIVERSITY/FILTRATION?

  • 100% live rock + bagged sand

    Votes: 37 27.2%
  • 100% dry rock + 100% live sand

    Votes: 46 33.8%
  • 50/50 live/dry rock, 50/50 live/bagged sand

    Votes: 30 22.1%
  • 75% live rock, 25% live sand

    Votes: 13 9.6%
  • 25% live rock, 75% live sand

    Votes: 10 7.4%
Back
Top