False readings from Digital ports

grovepi
faq
#1

Hi all,

I have two Raspberry pi3’s with Grovepis attatched.

One one of them I’m getting radom connection readings using this script.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
import grovepi
from grovepi import *

rain_tipper = 3     # socket D3, read 0-1
grovepi.pinMode(rain_tipper, "INPUT")       # Rain Tipper

count = 0
while True:
	if grovepi.digitalRead(rain_tipper) != 0:
		print("Rain_Tipper Pulse Detected!!", "port  no ", rain_tipper, count)
		count += 1
	time.sleep(0.05)

The other does not seem to have this problem.

Do I need to repace it?

Or is there something I can do to correct the error?

Many Thanks
Michael

#2

Can you switch the GrovePis between the 2 Raspberry Pi’s and see which one is having the problem?

Maybe one doesn’t have the firmware updated.

Thank you!

#3

Hi RobertLucian,

Thanks for replying.

I have done as you said and the test appear to be non-conlusive.

So I carried out two Troubleshooting tests

cd ~/Dexter/GrovePi/Troubleshooting
sudo bash all_tests.sh

On each raspberry pi with the Grove pi their boards fitted without any thing connected to them.

I then ran the first test After boot

followed by a second test a few seconds later

and have posed the resulting logs, which I hope help.

Regards Michael

RPI-1 first_log.txt (6.3 KB)
RPI-1 second_log.txt (6.3 KB)
RPI-2 First log.txt (6.1 KB)
RPI-2 second.txt (6.1 KB)

#4

I have done as you said and the test appear to be non-conlusive.

What do you mean inconclusive? Did you also try switching the micro SD cards without switching the GrovePis?

Thank you!

#5

Hi RobertLucian,

I did as you suggest and the fault could not be repeated, and no false possitives could be be repeat .

Interesting I tried the test on both pi’s that had been switch off over night and again no fault was detected.

How ever after updating one of the software Pi’s I found that the Grovepi was not able to work, and showed IO errors.

I also notice that Adafruit CircuitPython may now be added to Raspberry update severs, so get added to the system.

I not sure if they may be causing problems with Grovepi drivers

I am now writing a fresh Raspbian image to an sdcard, and load it up with the Grove drivers, to see what happens.

I will see what happens and get back to you.

Michael

#6

Hi RobertLucian,

During the weekend I ran serval tests with my Grovepi’s (note both have the latest 1.3.0 Firmware).

This is what I did.
I fomated a fresh 8gb sb card and installed Raspbian Stretch with desktop.
expanded the file system, and activated 12c, spi, serial,etc, and rebooted.

I then installed sudo curl -kL dexterindustries.com/update_grovepi | bash, and rebooted.
I connect a grove led to D4 and Grove bmp180 to on of the 12c ports

I ran sudo i2cdetect -y 1

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- 04 -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- 77

All normal

I then ran grove_led_blink.py which worked, followed by sudo i2cdetect -y 1
again.

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Not normal.

I then rebooted and sudo i2cdetect -y 1

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- 04 -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- 77

I then ran /home/pi/Dexter/GrovePi/Software/Python/grove_barometer_sensors/barometric_sensor_bmp180/grove_i2c_barometic_sensor_example.py

sudo python grove_i2c_barometic_sensor_example.py
Temperature: 17.00 C
Pressure: 1031.42 hPa
Altitude: -152.23 m

All OK

Followed by grove_led_blink.py, that worked OK.
then sudo i2cdetect -y 1

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Again not normal.

I the then ran grove_i2c_barometic_sensor_example.py again, with these results.

Error accessing 0x77: Check your I2C address
Error accessing 0x77: Check your I2C address
Error accessing 0x77: Check your I2C address
Error accessing 0x77: Check your I2C address
Error accessing 0x77: Check your I2C address
Error accessing 0x77: Check your I2C address
Error accessing 0x77: Check your I2C address
Temperature: 12.80 C
Pressure:    127.51 hPa
Altitude:    20114.89 m

As you can see something is wrong, I am not sure what may be wrong, but was again when, update_grovepi is ran it downloads, could some of these have changed.
Note:
As far as I can see Adafruit CircuitPython was not downloaded during the update

I hope this info will help solve the problem.

Sorry for the long email
Many thanks Michael

【Help】I2C becomes invalid
#7

Yeah, I think I know why you’re getting these problems. It’s all because before I pushed the latest firmware to master, I also changed the I2C bus that the GrovePi uses.

Check my comment here:

And if you want to use the classic HW I2C, go here:
https://dexterind.github.io/GrovePi/quickstart/#what-i2c-bus-to-use

The advantage of using the software implemented I2C is obvious - it’s almost just as fast as the HW I2C, it doesn’t use much CPU time and it doesn’t have the inherent bugs of the HW I2C. Therefore, using this implementation makes the GrovePi more stable.

There’s only one downside to this: you cannot use the HW I2C at the same time as the software implemented one. And especially about our SW I2C, once you use it, you either have to restore the I2C functionality on the appropriate GPIO pins or just reboot the Raspberry Pi.
If you have wiringpi installed on the Raspberry Pi, you can run gpio -g mode 2 ALT0 && gpio -g mode 3 ALT0 to revert the changes done by the SW I2C.

Since we knew this was going to be a problem, we have worked on making the SW I2C revert to the default functionality after every transfer that it does. Basically, you communicate with the GrovePi over this SW I2C bus and then you’ll still be able to interface with other I2C devices using the HW I2C bus.
This means you can still use both buses (the HW & SW I2C) in the same program just as long as their operations/transfers don’t overlap - like when you do multi-processing. One way to avoid that when multi-processing is by using mutexes, which is still the same thing you’d do if you were only using a single bus (either the SW I2C or the HW one).

If you want to test out our new implementation that allows the above, check out this PR (it’s not yet merged):

To use it on yours, you have to clone this repo (use my branch), go to miscellaneous directory and run python setup.py install (or python3). Then go on and try out the scripts you’ve shown us again and see if it works.

Thank you!

#8

Hi RobertLucian,

Thank very much for the fix.
I have one minor problem though, and that is when you say.

To use it on yours, you have to clone this repo (use my branch), go to miscellaneous directory and run python setup.py install (or python3 ). Then go on and try out the scripts you’ve shown us again and see if it works.

Having looked at the links you suggested,
I have not been able to find the site to clone to, or the miscellaneous directory with the installer script.

Please can you post the link.
Many Thanks
Michael

#9

This is where you can find it:

Thank you!

#10

Hi RobertLucian,

Thank you for your recent link.
I cloned it into the Dexter folder.
Then ran sudo python setup.py install, in the ~/Dexter/RFR_Tools/miscellaneous directory.

I ran

sudo python  /home/pi/Dexter/GrovePi/Software/Python/grove_barometer_sensors/barometric_sensor_bmp180/grove_i2c_barometic_sensor_example.py
Temperature: 31.00 C
Pressure:    1010.34 hPa
Altitude:    52.52 m

followed by sudo python /home/pi/Dexter/GrovePi/Software/Python/grove_led_blink.py

with this result

Traceback (most recent call last):
  File "/home/pi/Dexter/GrovePi/Software/Python/grove_led_blink.py", line 37, in <module>
    from grovepi import *
  File "/home/pi/Dexter/GrovePi/Software/Python/grovepi.py", line 52, in <module>
    import di_i2c

I done some searches to try and fix it without any luck.
What should I do next?

Example should I have used

curl -kL dexterindustries.com/update_grovepi | bash
or
curl -kL https://raw.githubusercontent.com/RobertLucian/GrovePi/fix/firmware-bugs/Script/update_grovepi.sh | bash
from the firmware 1.3.0 fork

Regards
Michael

#11

That traceback you’ve put up there doesn’t seem to be complete. Anyhow, when updating the package in RFR_Tools/miscellaneous, did you make sure you were on my specific branch? That is the hotfix/sw_i2c_on_hw branch.

And just a couple of minutes ago, the patch for this issue has been merged into the master branch of the official repo, so running curl -kL dexterindustries.com/update_grovepi | bash again should fix all problems. Can you try that and see if it fixes it?

And also, other I2C devices will also work side-by-side with the GrovePi. You only have to make sure they don’t attempt to communicate over I2C at the same time.

Thank you!

#12

Hi, RobertLucian,

Thanks for the hard work. I installed curl -kL dexterindustries.com/update_grovepi | bash as recommended,

The update works to a point but sadly it does appear that some of my 12c devices/sensors together without causing errors for example

/Dexter/GrovePi/Software/Python/grove_i2c_temp_hum_hdc1000 $ sudo python3 grove_hdc_1000_example.py

Temp : 13.06 C

Humidity: 100.00 %

-----------------

Temp : 14.35 C

Humidity: 99.61 %

-----------------

No ACK!

Traceback (most recent call last):

File "grove_hdc_1000_example.py", line 44, in <module>

print('Temp : %.2f C' % hdc.Temperature())

TypeError: a float is required

Since I am running two scripts at the same time both use 12c with switchdoclabs/Adafruit drivers and devices.

These do not use CircuitPython.

The drivers are

Adafruit_Python_ADS1x15

Adafruit_Python_BMP280

Adafruit_Python_GPIO

Adafruit_Python_PureIO

And these devices

SDL_DS3231

I2C FRAM.

All of these worked with the GrovePi until the recent changes.

You state in your email ‘make sure they don’t attempt to communicate over I2C at the same time’, is the way of doing this, as both scripts could be calling devices at the similar times.

Have you any advice as to how this could be fixed.

Regards Michael

#13

You state in your email ‘make sure they don’t attempt to communicate over I2C at the same time’, is the way of doing this, as both scripts could be calling devices at the similar times.

Yes, exactly. You cannot have 2 processes running at the same time and trying to access the same device. I’m pretty sure this didn’t work even before.

Can you try using mutexes around each sensor that you are using?

We have a library that we wrote specifically for this type of problems here, although I’m pretty certain you can find others / (other tutorials) on the web for it:

The benefit of our module called dexter_mutex.py is that it already gets installed with the GrovePi, so that’s one less thing to think about. And it’s also reliable - there aren’t any bugs with it.

Thank you!

#14

Hi RobertLucian,

Thankyou for your suggestion regarding python mutex, I will look in tro it and have a play.

How ever I may havr been a little misleading it my last email.

The two pythom3 scripts may bay be running at the together each with a 5 second while loop.
The first script runs my weather station and has a 5 second while loop that reads a number of 12c devices on after each other.
and include a BMP280, HDC1000, SI1145, TSL2561 all of which are 12c.

The second script reads which is runs the weather vane monitors a anemometer socket connected to D4. and rain bucket on D3 both are magnetic switch these are in a continious while loop and reads the switchs with a delay of 'time.sleep (0.4), for each cycle. if a connection is made a led lights connected to D8.
After 5 seconds an ADC ADS1115 measures wind direction.

I am sending the two scripts hoping they help.

Regards Michael

weather_vane_v1.4e.py (12.0 KB)
weather_station_v1.57a.py (28.3 KB)

#15

Hi RobertLucian,

Unfortuntatly I have not been able to figue out how to use mutex.py, despite trying to readup about it on the net. Sadly it is very much above coding skills.

Did you take a look at the two scripts I sent you and have they given you any clue, what may be going wrong. I notice that you use Adafruit_I2C.py, could this be one cause?

Sadly I clutching t straws for ideas !!!

Regards Michael

#16

I think you might be better off running everything from the same Python process.

You could get away with making threads for each of your sensor or application and then use the mutexes that come with Python to synchronize them properly. Here’s what you have to use:
https://docs.python.org/3/library/threading.html#threading.Lock

And we don’t use the Adafruit_I2C.py. This is an old module that we keep it in there for historical reasons. We are now only using the di_i2c.py and di_mutex.py modules.

Let me know if you get to somewhere.

Thank you!

#17

I took a look at you suggestion and did some research.

How would this work since I am running two seprate python3 scripts at the same time, the weather_vane feeding the weather-station via a json file.

Michael

#18

That’s what I’m saying: merge the 2 applications together and use locks around the I2C devices. Every application would probably go in a thread of its own. This is how I’m imagining it.

This is the alternative to using the di_mutex.py module for mutexing the devices while having different processes. Personally, I’d have used the di_mutex module - I find it more versatile. If you still want to give that a shot, I’d suggest you look at our source code - primarily the source code in di-sensors repo: https://github.com/DexterInd/DI_Sensors.

Cheers! :slight_smile:

#19

Thank s the advice, one of the reasons for having th two scripts was because of timing issues, say thay I will try to merge parts of the scripts to see what happens. I’m not sue I fully understand yet how dl-mutex works, but I will give it a try.

I notice that at pressent there is not a grove python2/3 version of BMP280 sensor. Do you have one any were you can send me?

Many Thanks Michael

#20

I have spent many happy hours trying to solve the problem(s).

Firstly many of the Grove/ Seeed studio do use the Adafruit 12c libraries, sbus etc. Adrafruit has updated them to updated to use CircuitPython.

And since your last updates have caused these to create errors when using multiple scripts. I have tried to merge the two scripts and that caused even more problems, not least with timing.

Looking at mutex I not been able to findout easesly how to us it, and need a large a rewriteof my scripts and grove12c drivers to work. Both of which beyond my programming skills.

I sure that others may have or soon have simular problems.

I would be grateful for any solution you may have.

Michael