At the first part of the code(Initializing relays) the GPIO output works as expected, but at the second part it doesn’t works, I have break my head for the last four hours trying to find out what is happening here…
The updateGPIOstatuses method is executed as a thread:
_thread.start_new_thread(updateGPIOstatuses,(gpioCheckerInterval,pins,))
The actual behaviour is: The second part of this code doesn’t turn on the relay from the relay board… But at the first part it does the job.
Any idea?
def updateGPIOstatuses(gpioCheckerInterval,gpioConfig):
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
gpioConfig=[19]
#First part
print('Initializing relays')
for i in gpioConfig:
print('Relay '+str(i)+' on..')
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(i, GPIO.LOW)
time.sleep(0.5)
print('Relay '+str(i)+' off..')
print('Initialization done')
#Second part
while True:
#statuses=dbQuery('2','')
statuses={'gpio8': 0, 'gpio7': 0, 'gpio10': 0, 'gpio4': 0, 'gpio2': 0, 'gpio1': 1, 'device': '1', 'gpio5': 0, 'gpio3': 0, 'gpio9': 0, 'gpio6': 0}
time.sleep(0.5)
after=''
print(' ')
j=1
for i in gpioConfig:
newState=statuses['gpio'+str(j)]
j+=1
if newState==1:
print(1)
GPIO.output(i, GPIO.HIGH)
time.sleep(0.5)
else:
print(0)
GPIO.output(i, GPIO.LOW)
time.sleep(0.5)
time.sleep(gpioCheckerInterval)
EXTRA: I forget to mention, the relay phisical state changes in the first part, but not in the second one, if I check the pin state at the second part after changing the pin value I get the correct value, but it doesn’t correspond to the phisical state of the current relay.
In resume, seems like the pin is correctly settled but the relay doesn’t, and as I said, only in the second part.
-
1Your code is really confusing. You are passing in gpioConfig then setting it to [19] is that your intention? – CoderMike 3 hours ago
-
1Does HIGH or LOW turn your relay on? – CoderMike 3 hours ago
-
1setting it to 19 just for this example, normally it is passed from outside, I just want to test with one pin – Martin Ocando Corleone 2 hours ago
-
1Yes, HIGH or LOW toggles the relay in the first part but not in the second one – Martin Ocando Corleone 2 hours ago
-
1I added an extra info – Martin Ocando Corleone 2 hours ago
-
Does HIGH turn the relay on or does LOW turn the relay on? – CoderMike 1 hour ago
-
You should really only call setwarnings, setmode and setup once at the start of your code. – CoderMike 1 hour ago
-
Your second section only ever sets the relay HIGH because gpio1 returns 1. – CoderMike 1 hour ago
-
I knew it was a stupid thing lol. Years working with relays, code and stuff and making this kind of mistakes, yes you are right the relay won’t change its state to LOW because it is always is HIGH.. I fixed it now – Martin Ocando Corleone 43 mins ago
-
Thank you @CoderMike for help me to see it was inverted.. – Martin Ocando Corleone 39 mins ago
Question
I have been breaking my head for the last 4 hours, …
Answer
Ah, let me see, I think I can very likely find the bug in less than 40 minutes,
(1) Get my Occam’s razor (KISS),
(2) Morning coffee time, ..
Appendices
Appendix A – The code
Appendix B – KISS
The principle most likely finds its origins in similar minimalist concepts, such as Occam’s razor, Leonardo da Vinci’s “Simplicity is the ultimate sophistication”, Shakespeare’s “Brevity is the soul of wit“, Mies Van Der Rohe’s “Less is more“, Bjarne Stroustrup‘s “Make Simple Tasks Simple!“, or Antoine de Saint Exupéry’s “It seems that perfection is reached not when there is nothing left to add, but when there is nothing left to take away“. Colin Chapman, the founder of Lotus Cars, urged his designers to “Simplify, then add lightness“. Heath Robinson machines and Rube Goldberg’s machines, intentionally overly-complex solutions to simple tasks or problems, are humorous examples of “non-KISS” solutions.
A variant – “Make everything as simple as possible, but not simpler” – is attributed to Albert Einstein, although this may be an editor’s paraphrase of a lecture he gave.
Appendix C – Occam’s Razor
Occam’s razor is the problem-solving principle that states that “Entities should not be multiplied without necessity.”
The idea is attributed to English Franciscan friar William of Ockham (1287–1347), a scholastic philosopher and theologian who used a preference for simplicity to defend the idea of divine miracles.
It is sometimes paraphrased by a statement like “the simplest solution is most likely the right one”, but is the same as the Razor only if results match.
Occam’s razor says that when presented with competing hypotheses that make the same predictions, one should select the solution with the fewest assumptions, and it is not meant to be a way of choosing between hypotheses that make different predictions.
Similarly, in science, Occam’s razor is used as an abductive heuristic in the development of theoretical models rather than as a rigorous arbiter between candidate models.
In the scientific method, Occam’s razor is not considered an irrefutable principle of logic or a scientific result; the preference for simplicity in the scientific method is based on the falsifiability criterion.
For each accepted explanation of a phenomenon, there may be an extremely large, perhaps even incomprehensible, number of possible and more complex alternatives.
Since one can always burden failing explanations with ad hoc hypotheses to prevent them from being falsified, simpler theories are preferable to more complex ones because they are more testable.
Appendix D – William Occam
William of Ockham (Occam, c. 1280—c. 1349) – IEP
In logic, Ockham presents a version of supposition theory to support his commitment to mental language. Supposition theory had various purposes in medieval logic, one of which was to explain how words bear meaning.
Theologically, Ockham is a fideist, maintaining that belief in God is a matter of faith rather than knowledge. Against the mainstream, he insists that theology is not a science and rejects all the alleged proofs of the existence of God. Ockham’s ethics is a divine command theory. In the Euthyphro dialogue, Plato (437-347 B.C.E.) poses the following question: Is something good because God wills it or does God will something because it is good? Although most philosophers affirm the latter, divine command theorists affirm the former.
Ockham’s divine command theory can be seen as a consequence of his metaphysical libertarianism. In political theory, Ockham advances the notion of rights, separation of church and state, and freedom of speech.
Categories: Uncategorized