Instable port reading

Hi,

The GrovePi + that I received last week has some problems when reading from a digital port.
It often reads value 255 while it should just read 0.
The opposite is also true, it reads a 0 instead of 255.

I solved the problem by writing a script that checks n consecutive reads that have the same value.

I have the problem with a simple Grove button or a self made switch connected via a screw terminal an a pull down resistor of 10K.

Anyone else with the same problem and/or alternative solution?

Kind regards,
Freddy

Hey Freddy,
Can you post the program that you are using and the output that you get.

-Karan

Hi Kara,

Many thanks for your reply.
I re posted my request yesterday because I was not sure that it was send.
It did not showed up in my list.

The problems I have is that the reading of the digital ports.
They are not stable. This means that I have from time to time a reading that the button was down while nothing was touched.
The opposite is also true. When the button is pushed, the reading is randomly wrong.

I included the program but unfortunately some comments are in Dutch (I am from Belgium).

What you should know is that the function ‘LeesSwitch(button,n)’ is in there in order to solve that problem.
I read the switch status for n times and if they are all the same, i accept the value.

I hope you could give me some advise.
By the way, is there a way to force an key interrupt for the Grove digital ports like 'GPIO.wait_for_edge(23, GPIO.FALLING)'for the normal ports?

Kind regards,
Freddy

Hi Karan

I receive sending errors, so here is the listing (still under development):

Weer_display.py

Copyright 2015 Freddy <freddy.cluckers@gmail.com>

Note the dht_sensor_type below may need to be changed depending on which DHT sensor you have:

0 - DHT11 - blue one

1 - DHT22 - white one, aka DHT Pro or AM2302 - comes with the GrovePi+ Starter Kit

2 - DHT21 - black one, aka AM2301

from grovepi import *
from grove_rgb_lcd import *
import datetime
import grovepi

relay = 2
switch = 3
led = 4
water = 5
dht_sensor_port = 7 # DHt sensor - port 7
dht_sensor_type = 0

grovepi.pinMode(relay,“OUTPUT”)
grovepi.pinMode(switch,“INPUT”)
grovepi.pinMode(led,“OUTPUT”)
grovepi.pinMode(water,“INPUT”)

Definier de switch toestanden

import itertools
mySW = itertools.cycle([0,1]).next

LED aanzetten

digitalWrite(led,1)

#Startminuut bepalen
beg_min = datetime.datetime.now().strftime(’%M’) #Minuten van de klok
nu_min = beg_min

Om erzeker van te zijn dat er bij opstart onmiddellijk een melding is

Datum en tijd

nu = datetime.datetime.now().strftime(’%d-%m-%y %H:%M’)

Declaratie van menu_val

menu_val = 0
beg_mail_val = 0
#NuVal = beg_mail_val

#Weer
[temp,hum ] = dht(dht_sensor_port,dht_sensor_type) #Get the temperature and Humidity from the DHT sensor
t = str(temp)
h = str(hum)
setRGB(40,200,200)

Mess = “T:” + t + “C H:” + h + "%"
Mess = Mess + “\n” + nu

Meetfunctie bepalen

def Meet(waarde):
#print "Meting minuut: " + datetime.datetime.now().strftime(’%M’) + " minuten"
global Mess
#Datum en tijd
nu = datetime.datetime.now().strftime(’%d-%m-%y %H:%M’)

#Weer
[temp,hum ] = dht(dht_sensor_port,dht_sensor_type)		#Get the temperature and Humidity from the DHT sensor
t = str(temp)
h = str(hum)
if waarde == 1:
	nu = datetime.datetime.now().strftime('%d-%m-%y * %H:%M')
else:
	nu = datetime.datetime.now().strftime('%d-%m-%y   %H:%M')
	
Mess = "T:" + t + "C  H:" + h + "%"	
Mess = Mess + "\n" + nu

print "temp =", temp, "C\thumidity =", hum,"%" + "  -  " + nu

setText(Mess)

Gmail zendfunctie

Zendmail = 1
def ZendMail(body):

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

fromaddr = "freddy.cluckers@gmail.com"
toaddr = "freddy.cluckers@gmail.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['SUbject'] = "Waarschuwing van Freddy's Raspberry"
msg.attach(MIMEText(body,'plain'))

server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(fromaddr,"prutsatir")
text=msg.as_string()
server.sendmail(fromaddr,toaddr,text)
server.quit()

Eerste maal bij opstarten

Meet(1)

while True:
try:
# Menu
if grovepi.digitalRead(switch) == 1:
menu_val = mySW()
print "Start menu: " + str(menu_val)

	# Tijd op dit ogenblik bepalen
	nu_min = datetime.datetime.now().strftime('%M') #Minuten van de klok
	
	# Water en relay bediening
	pomp = grovepi.digitalRead(water)
	print 'Pomp: ' + str(pomp)
	grovepi.digitalWrite(relay,pomp)
	Meet(pomp)	
	if pomp == 1:
		Zendmail = 1
	else:			
		if Zendmail == 1:
			boodschap = 'Geen water meer in de regenton.\nGelieve zo snel mogelijk bij te vullen.\nVerzonden op: '
			ZendMail(boodschap + nu)
			print 'E-mail: ' + boodschap + nu
			Zendmail = 0
			#stuur e-mail en stop de pomp
	
	# Uitlezen van de display om de minuut			
	if str(nu_min) &lt;&gt; str(beg_min):
		print 'Uitlezen naar display - ' + Mess
		Meet(pomp)
		setText(Mess)
		beg_min = nu_min
		
	time.sleep(0.5)
	
except KeyboardInterrupt:
	setText('')
	setRGB(0,0,0)
	digitalWrite(led,0)
	time.sleep(0.5)
	grovepi.digitalWrite(relay,0)
	break
	
except (IOError,TypeError) as e:
	print "Error"

Hey Freddy,
Can you try running the program without the temp and humidity sensor and see if it is any better. A few people had reported problems with the Temperature and huidity sensor too. Can you also the grove_firmware_version_check.py script and check that you are running v1.2.2.

-Karan

Hi Karan,

The problem remains.
I used the following program to generate a menu choice with 3 buttons and the lcd display.
Maybe you can try it.

In my case, when I start the program with a time.sleep delay of 0.5 and without touching anything, I have a read out of m_level1 (button menu) of 255.
This looks a bit wrong to me since it can either be 1 or 0.
Correct?

I included the program.

Kind regards and many thanks for your support,

Freddy

Hi Karan,

I did some testing on the temp/hum sensor.
It indeed has some instability.
Once a while the reading is ‘nan’ or it generates an Error as reading

Kind regards,
Freddy

Hi Karan,

Again me. Sorry.
I have tested all analog and digital ports with temp/hum sensor, moisture sensor, switch on analog port.
The analog ports all give an error after a while and keep continuing working.
However the error that occurs occasionally are very annoying.
I tested all of this using the simple python scripts delivered by Grove.

Kind regards,
Freddy

Hi Karan,

I forgot to mention…
I am running v 1.2.2

Kind regards,
Freddy

Hey freddy,
For a lot of the users dacing the problem with analog reads and DHT sensor, the newer firmware solved the problem. You can update your GrovePi repository with a Git pull and then update the firmware to v1.2.5: https://github.com/DexterInd/GrovePi/tree/master/Firmware/Source/v1.2/grove_pi_v1_2_5 using: avrdude -c gpio -p m328p -U flash:w:grove_pi_v1_2_5.cpp.hex. Then update the GrovePi python library here: https://github.com/DexterInd/GrovePi/tree/master/Software/Python by running: sudo python setup.py install. After this first try the firmware version script then try the DHT sensor and then try running the DHT sensor along with multiple sensors to see if it’s any better.

-Karan

Hi Karan,

When I am in the GrovePi folder of my Raspberry and when I issue 'git pull, the following message appears:

'Error: Your local changes to the following files would be overwritten by merge: Script/install.sh’
Please, commit your changes or stash them before you can merge.

Because I am not very familiar with Linux, I do not know what to do next.
Can you please advise me a bit more in detail?
How do I get the necessary files on my Raspberry and in what folder should they reside.

Many thanks for your effort.

Kind regards,
Freddy

Hi Karan,

After trying to do things, the DHT sensor stopped working completely and was no longer accessible.
Therefore I issued a new firmware update as indicated by the instructions from the web site.
Firmware version is now again v 1.2.2

So, I will need all the help I can get to install the 1.2.5 version

Kind regards,
Freddy

Hey Freddy,
An easier way to test the new firmware and software would be to clone the GrovePi repository in another directory and try it out. If it works, then merge it with the main folder. Make a temporary folder on Desktop and clone the GrovePi repository: git clone https://github.com/DexterInd/GrovePi.git. Once this is done, open the GrovePi folder and follow the steps here: http://www.dexterindustries.com/topic/instable-port-reading/#post-56207.

Do let us know if this helps.

-Karan

Hi Karan,

Following your instructions I was able to upgrade to the new version and it now reads v 1.2.5.
However, the bad news is that it did not solved the problem…
I still have read errors (‘nan’) when I use the temp/hum sensor and even when I simply use a switch on the analog port with a pull down resistor of 10 K.
I tried this with only a very small script to be sure that it is not due to my amateurish programming…

Also when I run my program I receive an error when reading ports.

Kind regards,

freddy

Hey Freddy,
Just wanted to make sure that you have run the setup.py script to install the new lGrovePi python library.

If you have done that already, can you try: https://github.com/DexterInd/GrovePi/blob/master/Software/Python/grove_dht_pro.py and https://github.com/DexterInd/GrovePi/blob/master/Software/Python/grove_analog_read.py to see if these work properly. If these do work properly, can you try running the program attached here.

-Karan

Hi Karan,

I did all that and have run the grove_dht_pro.py.
The only thing I changed is the sensor port -> 7 and the sensor type to ‘0’ since my package came with the less professional sensor…

Once a while it produces ‘nan’ readings for both temperature and humidity.
I can trap these readings in my program.

However the following error is produced at the end:

Traceback (most recent call last):
File “grove grove_dht_pro.py”, line 44, in <module>
[temp,humidity] = grovepi.dht(sensor,0)
TypeError: ínt’object is not iterable

I hope this helps to solve the problem.
Is this reproducable in your lab?

Kind regards,
Freddy

Hey Freddy,
We have seen the type errors a few times too. Using an try block and catching the TypeError usually makes it work. Can you try something like except TypeError: pass and see if it works. Have a look here on how to handle exceptions: https://docs.python.org/2/tutorial/errors.html .

-Karan

Hi Karan,

Thanks for your reply.
It is comfortable for me to know that the errors are not only with me and that they occur also in the lab.
Of course now I am trapping the errors but normally, this should only be the exceptions and not to make a program work.
However, I can live with it.
I noticed that I get less errors if the system is up and running for a longer time.
One would expect the opposite, but it can only be an impression of me…
Thanks anyway for your continuous support.

Freddy

So what is the root cause and solution since I got exact the same issue? A simple digitalRead of an input button gives unreliable results. For no reason it’s get high. I use the latest image and firmware.

Hey budy,
The IOErrors basically comes when the Pi misses a byte or reads a bad value from the GrovePi. It is pretty easy to handle. What you are describing is probably a bad sensor. Do you have any other sensors to try out.

-Karan