-3

I'm hoping that someone can help me out. I've made a box that contains a raspberry pi and some relays. It takes in readings from pH and ORP sensors and I'm almost at the stage where it's running the pool for me. The last thing that I'm trying to get working is a python app to show me which of the relays (there are 4 of them and they control the power to the pump, the chlorinator, the sensors, plus a spare outlet) are "on" and which are "off". What I've written so far contains 4 images, with each image being for one of the relays. When the python script is executed and the app opens, it interrogates the GPIO pins and then displays an image for each of the relays. The image is of either a red led or a green led, plus the name (eg Pump, Chlorinator, Sensors, Spare). The idea is that the app will start as a cron job when the raspberry pi boots, and then the app stays on at all times will give the up-to-date status. The raspberry pi runs headless and I connect to it via ssh and vnc.

I've spent ages searching on the internet for how to make the app update. The existing code is something that I've cobbled together (with help from others).

The current code is:

import RPi.GPIO as GPIO
import sys
import time
from time import sleep
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class window:

   def __init__(self):
      app = QApplication(sys.argv)
      win = QWidget()
      # setup vars for Qlabel and set to initial state = off
      self.img1 = QLabel()
      self.img1.setPixmap(QPixmap("/home/pi/Pictures/PumpOff.png").scaled(250, 50))
      self.img2 = QLabel()
      self.img2.setPixmap(QPixmap("/home/pi/Pictures/ChlorinatorOff.png").scaled(250, 50))
      self.img3 = QLabel()
      self.img3.setPixmap(QPixmap("/home/pi/Pictures/SensorsOff.png").scaled(250, 50))
      self.img4 = QLabel()
      self.img4.setPixmap(QPixmap("/home/pi/Pictures/SpareOff.png").scaled(250, 50))

      # setup window
      self.vbox = QVBoxLayout()
      self.vbox.addWidget(self.img1)
      self.vbox.addWidget(self.img2)
      self.vbox.addWidget(self.img3)
      self.vbox.addWidget(self.img4)
      win.setLayout(self.vbox)
      win.setWindowTitle("Pool Status")
      win.setGeometry(300,300,200,200)
      win.show()
      #sys.exit(app.exec_())

   def update_status(self):
      GPIO.setmode(GPIO.BCM)
      GPIO.setwarnings(False)
      GPIO.setup(22, GPIO.OUT)
      # To read the Pump outlet state
      state = GPIO.input(22)
      if state:
         self.img1.setPixmap(QPixmap("/home/pi/Pictures/PumpOn.png").scaled(250, 50))
      else:
         self.img1.setPixmap(QPixmap("/home/pi/Pictures/PumpOff.png").scaled(250, 50))

      GPIO.setup(23, GPIO.OUT)
      # To read the Chlorinator outlet state
      state = GPIO.input(23)
      if state:
         self.img2.setPixmap(QPixmap("/home/pi/Pictures/ChlorinatorOn.png").scaled(250, 50))
      else:
         self.img2.setPixmap(QPixmap("/home/pi/Pictures/ChlorinatorOff.png").scaled(250, 50))

      GPIO.setup(24, GPIO.OUT)
      # To read the Sensors outlet state
      state = GPIO.input(24)
      if state:
         self.img3.setPixmap(QPixmap("/home/pi/Pictures/SensorsOn.png").scaled(250, 50))
      else:
         self.img3.setPixmap(QPixmap("/home/pi/Pictures/SensorsOff.png").scaled(250, 50))

      GPIO.setup(25, GPIO.OUT)
      # To read the Spare outlet state
      state = GPIO.input(25)
      if state:
         self.img4.setPixmap(QPixmap("/home/pi/Pictures/SpareOn.png").scaled(250, 50))
      else:
         self.img4.setPixmap(QPixmap("/home/pi/Pictures/SpareOff.png").scaled(250, 50))

      # Re-draw screen
      #self.win.show()
      self.win.update()
      #self.win.show()

if __name__ == '__main__':

   # Create an instance of window
   new_window = window()
   #new_window.show()

   # Now loop and update the status
   while True:
      # Update the initial off status with current status every X seconds
      new_window.update_status()
      #new_window.show()
      sleep(5)

If I uncomment the "sys.exit(app.exec_())" at the bottom of the def init(self): section, the app shows the images at the initial stage (with all images showing as "off") and the app image is persistent (it doesn't just flash on the screen very briefly), but it is obviously not updating. But as soon as I comment that line out (in order to try and get the updating working), I get an error of one sort or another. Running the code as shown gives the following error: "AttributeError: 'window' object has no attribute 'win'"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.