FRANK333 Posted May 12, 2019 Posted May 12, 2019 I'm going to use an orange Pi zero to make a thermostat adjustable via lan; do you know how to point me to some easy to apply project? I tried several projects, with ds1820b sensors and relais card for raspberry, but I have problems for example with the libraries RPi.GPIO because they do not match those OPi.GPIO. I can currently turn the relays on and off with an android app or via terminal command.
martinayotte Posted May 12, 2019 Posted May 12, 2019 46 minutes ago, FRANK333 said: but I have problems for example with the libraries RPi.GPIO because they do not match those OPi.GPIO. Provide details about errors/discrepancies you found between those 2 libs.
FRANK333 Posted May 12, 2019 Author Posted May 12, 2019 hello martin , let me explain myself better , using this tutorial ( thank you the author ) http://davetsao.com/blog/2015-07-11-raspberry-pi-thermostat.html there is this command import RPi.GPIO as GPIO , which I replaced with OPi.GPIO (of course I loaded the libraries Opi) it seems that the commands setmode setup mode are not recognized
martinayotte Posted May 12, 2019 Posted May 12, 2019 6 minutes ago, FRANK333 said: let me explain myself better I'm using this GPIO python library instead, much more easier to use : https://github.com/duxingkei33/orangepi_PC_gpio_pyH3 In the thermostat code, simply comment out the setmode/setup lines, it should be easy to translate the rest of the code.
FRANK333 Posted May 12, 2019 Author Posted May 12, 2019 All right, now I do a clean installation and I start again, then I let you know. but I had decommented a few lines but the software did not start.Do you have any simple project to start from?
martinayotte Posted May 12, 2019 Posted May 12, 2019 10 minutes ago, FRANK333 said: Do you have any simple project to start from? What do you mean ? Simple project to test this GPIO library ? Simple blinking LED on PA10 for example : #!/usr/bin/python import sys, time from pyA20.gpio import gpio from pyA20.gpio import port gpio.init() gpio.setcfg(port.PA10, gpio.OUTPUT) while True: gpio.output(port.PA10, gpio.LOW) time.sleep(0.25) gpio.output(port.PA10, gpio.HIGH) time.sleep(0.25)
FRANK333 Posted May 12, 2019 Author Posted May 12, 2019 I'm looking for a software that through a web page (or an android app) makes the Opi 0 work as a thermostat or at least a software sketch. As for the test program I'll try, but that library is for A20 I have an H2 will work ?
martinayotte Posted May 12, 2019 Posted May 12, 2019 11 minutes ago, FRANK333 said: but that library is for A20 I have an H2 will work ? The link I've provided is a port of the pyA20 library to H3, and H2 is equivalent to H3. 12 minutes ago, FRANK333 said: I'm looking for a software that through a web page There is the HTTPServer library part of standard python, but you will have to learn a lot about it. I'm using it to produce Web interfaces.
FRANK333 Posted May 19, 2019 Author Posted May 19, 2019 Martin, I installed the library and your test works. When I try to insert an if else line, the script doesn't work. Spoiler #!/usr/bin/env python import os import sys if not os.getegid() == 0: sys.exit('Script must be run as root') from time import sleep from pyA20.gpio import gpio from pyA20.gpio import port from pyA20.gpio import connector REL = port.PA0 maxTemp = 22 gpio.init() gpio.setcfg(REL, gpio.OUTPUT) while 1: #open the file for the sensor and read contents tempfile = open("/sys/bus/w1/devices/28-8000001f9605/w1_slave") thetext = tempfile.read() tempfile.close() #get the temperature tempdata = thetext.split("\n")[1].split(" ")[9] temperature = float(tempdata[2:]) temperature = temperature / 1000 #print out the temperature print temperature #switch the relays if temperature above maxTemp while True: if temperature > maxTemp: gpio.output(REL, 0) else: gpio.output(REL, 1) What's wrong? I don't know anything about Python ,I'm sorry.
martinayotte Posted May 19, 2019 Posted May 19, 2019 1 hour ago, FRANK333 said: What's wrong? I don't know anything about Python ,I'm sorry. Indentation of line of code is important in Python ! Seeing your code above, the second "while" is not indented, so it never been executed since outside of the first one. Lets rewrite your code here without second "while", since it is useless, but adding a delay of 30 seconds : bad spoiler ... #!/usr/bin/env python import os import sys if not os.getegid() == 0: sys.exit('Script must be run as root') from time import sleep from pyA20.gpio import gpio from pyA20.gpio import port from pyA20.gpio import connector REL = port.PA0 maxTemp = 22 gpio.init() gpio.setcfg(REL, gpio.OUTPUT) while 1: #open the file for the sensor and read contents tempfile = open("/sys/bus/w1/devices/28-8000001f9605/w1_slave") thetext = tempfile.read() tempfile.close() #get the temperature tempdata = thetext.split("\n")[1].split(" ")[9] temperature = float(tempdata[2:]) temperature = temperature / 1000 #print out the temperature print temperature #switch the relays if temperature above maxTemp if temperature > maxTemp: gpio.output(REL, 0) else: gpio.output(REL, 1) sleep(30) [/spoiler] I've not tested it, but it should now work ...
FRANK333 Posted May 19, 2019 Author Posted May 19, 2019 here is the error it returns, it seems that I can't find the variable maxTemp, and yet I defined it with maxTemp = 22 root@orangepizero:~# python test.py File "test.py", line 32 if temperature > maxTemp: ^ IndentationError: unindent does not match any outer indentation level root@orangepizero:~# python -m tabnanny test.py 'test.py': Indentation Error: unindent does not match any outer indentation level (<tokenize>, line 32) root@orangepizero:~#
NicoD Posted May 19, 2019 Posted May 19, 2019 if temperature > maxTemp: Remove that colon Not that. I don't know python. I've got no idea. My programming must have a semicolon on the end. I can't get used to seeing Python,
martinayotte Posted May 20, 2019 Posted May 20, 2019 14 hours ago, FRANK333 said: IndentationError: unindent does not match any outer indentation level That is what I said earlier, indentation is critical ... Did you take back my version ? It doesn't seems to, because the "if" in my version is located at line 29 ...
FRANK333 Posted May 20, 2019 Author Posted May 20, 2019 Okay Martin I solved was just a tabulation problem as I could tell from your example. Now the rudimentary thermostat works, I would like to make sure that it remains in operation as a daemon, insert different temperatures at different times of the day, and finally make a decent GUI. But the road is uphill.... Spoiler #!/usr/bin/env python import os import sys if not os.getegid() == 0: sys.exit('Script must be run as root') from time import sleep from pyA20.gpio import gpio from pyA20.gpio import port from pyA20.gpio import connector REL = port.PA0 maxTemp = 22 gpio.init() gpio.setcfg(REL, gpio.OUTPUT) #gpio.output(REL, 1) while 1: #open the file for the sensor and read contents tempfile = open("/sys/bus/w1/devices/28-8000001f9605/w1_slave") thetext = tempfile.read() tempfile.close() #get the temperature tempdata = thetext.split("\n")[1].split(" ")[9] temperature = float(tempdata[2:]) temperature = temperature / 1000 #print out the temperature print temperature #switch the relays if temperature above maxTemp if temperature > maxTemp: gpio.output(REL, 0) else: gpio.output(REL, 1) sleep(1)
martinayotte Posted May 20, 2019 Posted May 20, 2019 32 minutes ago, FRANK333 said: sleep(1) This sleep(1) isn't indented properly, so not part of the while loop ...
FRANK333 Posted May 20, 2019 Author Posted May 20, 2019 ok I corrected and added the lines to make the script a daemon, I noticed however that if I kill the script (kill 1234) or if I block the OPI0 the relays card remains in the last state in which it is, how could you do to reset it to off? Spoiler #!/usr/bin/env python import os import sys if not os.getegid() == 0: sys.exit('Script must be run as root') from time import sleep from daemonize import Daemonize from pyA20.gpio import gpio from pyA20.gpio import port from pyA20.gpio import connector REL = port.PA0 maxTemp = 22 gpio.init() gpio.setcfg(REL, gpio.OUTPUT) pid = "/tmp/test.pid" def main(): while 1: #open the file for the sensor and read contents tempfile = open("/sys/bus/w1/devices/28-8000001f9605/w1_slave") thetext = tempfile.read() tempfile.close() #get the temperature tempdata = thetext.split("\n")[1].split(" ")[9] temperature = float(tempdata[2:]) temperature = temperature / 1000 #print out the temperature print temperature #switch the relays if temperature above maxTemp if temperature > maxTemp: gpio.output(REL, 0) else: gpio.output(REL, 1) sleep(1) daemon = Daemonize(app="test_app", pid=pid, action=main) daemon.start()
martinayotte Posted May 20, 2019 Posted May 20, 2019 48 minutes ago, FRANK333 said: how could you do to reset it to off? Although I didn't try it out, you will need some signal handler where you can turn off GPIO before "kill" been completed. Here is some example : https://nattster.wordpress.com/2013/06/05/catch-kill-signal-in-python/
FRANK333 Posted May 20, 2019 Author Posted May 20, 2019 I tried to add at the end of the script, these 4 lines : def signal_term_handler(signal, frame): print 'got SIGTERM' sys.exit(0) signal.signal(signal.SIGTERM, signal_term_handler) but the relays maintain the status, even after the kill command.
martinayotte Posted May 20, 2019 Posted May 20, 2019 5 minutes ago, FRANK333 said: but the relays maintain the status, even after the kill command. While doing the kill, did you got the "got SIGTERM" ? If yes, you should add "gpio.output(REL, 0)" just after the print and before the "sys.exit(0)" ...
Recommended Posts