0

If it's not obvious, I'm trying to learn python, and am having a go at it. Although, I'm running into some issues. Any help would be appreciated.
I'm trying to run this on a Rpi3b+ & with a relay

Edit: What I'm trying to do is have the script check the water level first before the it activates the pump relay, but I can't even get the script to pass a debug run through. Or is there a better way to write this script that will work with a Rpi?

# Hydroponic Watering v.0.0.1
import RPi.GPIO as GPIO
import time
import sys
import signal
signal.signal(signal.sigint, GPIO)
# Setting GPIO pins on Rpi
GPIO.setmode(GPIO.BCM)
signal, resv_level_pin, GPIO.in, pull_up_down = GPIO.PUD_up = 18
pump_relay, GPIO.OUT = 9
def check_water_level
 if resv_level_pin == true
 pump_relay(on)
 elif resv_level_pin == false
 pump_relay(false)
 print("Water level too low!")
exit(sys.exit)

Debug output as follows:

Traceback (most recent call last):
 File "/usr/share/mu-editor/mu/mu-debug.py", line 4, in <module>
 from mu.app import debug
ModuleNotFoundError: No module named 'mu'
---------- FINISHED ----------
exit code: 1 status: 0
goldilocks
60.4k17 gold badges117 silver badges236 bronze badges
asked May 10, 2019 at 14:38
4
  • 1
    I still wonder that any of this works. signal.signal(signal.sigint, GPIO) is certainly not what you intend it to be. What is signal, resv_level_pin, GPIO.in, pull_up_down = GPIO.PUD_up = 18 supposed to mean? Your function definition lacks the parentheses, the parameters (if any) and the colon. It is furthermore never called. if and elif lack the colon too and the indentation is off after if. Where there is the elif an else should suffice. True and False keywords should start uppercase. Commented May 10, 2019 at 23:30
  • The final exit(sys.exit) is not only superfluous but also a strange combination of two things. See here. It should be exit() or sys.exit() with the parameter being the error code if you intend to send one to the caller. Commented May 10, 2019 at 23:30
  • The answer may have indicated your immediate problem, but the code listed still has code errors, logic errors and will actually do nothing. I suggest you look at GPIO Zero which has helpful tutorials. Commented May 11, 2019 at 0:13
  • @hanlok Please understand that literally asking other people to write code for you ("is there a better way to write this script that will work") is kind of a non-starter. Based on the errors other people have pointed out, you need to get down to basics; if you don't understand those you will just have one error after another. Commented May 12, 2019 at 18:03

2 Answers 2

0

You're using Mu to run that python file, and it (not your script) is returning an error:

 File "/usr/share/mu-editor/mu/mu-debug.py", line 4, in <module>
 from mu.app import debug
ModuleNotFoundError: No module named 'mu'

I reccomend trying that code with Python IDLE, or Geany -- which should have come with Raspbian.

answered May 10, 2019 at 16:52
1
  • @hanlok, if you have any further problems, don't hesitate to post 'em here. Commented May 10, 2019 at 18:01
0

After using a different editor, I learned that I had all kinds of problems, like not understanding what I was trying to accomplish, but after getting away from the screen and coming back with fresh eyes, I ended up with the follow script which works.

#!/usr/bin/env python3
# Hydroponic Watering v.0.0.1
import RPi.GPIO as GPIO
import time
import sys
import datetime
sys.stdout=open("test.txt","a")
timestamp = time.strftime("Time: %H:%M, Date: %Y%m%d, ")
# Setting GPIO pins on Rpi
GPIO.setwarnings(False)
switch1=18
pump_relay=26
refill_relay=21
GPIO.setmode(GPIO.BCM)
GPIO.setup(switch1,GPIO.OUT)
GPIO.setup(pump_relay, GPIO.OUT)
GPIO.setup(refill_relay, GPIO.OUT)
GPIO.output(switch1, GPIO.LOW)
# Check Water Level
switch1_is_on = GPIO.input(switch1)
# Activate pump_relay
if switch1_is_on:
 GPIO.output(26,1)
 time.sleep(5)
 print(timestamp,"Water level OK.")
 GPIO.output(26,0)
else:
 GPIO.output(21,1)
 time.sleep(5)
 GPIO.output(21,0)
 print(timestamp,"Water level is too low!")
answered May 13, 2019 at 0:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.