Interfacing GrovePi with Scratch 1.4 on Pi

Am using scratchpy with GrovePi and am getting the temperature from sensor on A0. Have the following python program but it doesn’t continuously run - see the error message below. I do not yet have firmware 1.1 but wish to know if there are known issues with the original firmware in this area and I should upgrade. I don’t like upgrading for no reason. It does not always crash at the same time (known this since counter i is not always the same value).


import scratch
import smbus
import time
import grovepi
s=scratch.Scratch()
bus=smbus.SMBus(0)
address=0x04
i=0
f=0
while True:
        i=i+1
        if i> 255:
                i=0
        f=grovepi.temp(0)
#       print grovepi.analogRead(0)
        time.sleep(.5)
        s.sensorupdate({'temp' : f})
        print i,grovepi.temp(0)


===============
CTraceback (most recent call last):
File “temp.py”, line 33, in <module>
f=grovepi.temp(0)
File “/home/pi/sensorlab/grove_scratch/grovepi.py”, line 100, in temp
a=analogRead(pin)
File “/home/pi/sensorlab/grove_scratch/grovepi.py”, line 88, in analogRead
time.sleep(.1)
KeyboardInterrupt

Hi,
There should be no issue with the AnalogRead in the old firmware. Can you tell us a bit more in detail about the issue that you are facing, along with the error that comes and the analog value that you are getting in the program.

Thanks,
Karan

Have changed the code slightly since the above but the error is the same - see below. Is there some tracing option I can turn on for GrovePi or is this dependent on tracing available in Python ?

Confirm I do have Firmware 1.1 since when ran GrovePi /Firmware/new_fw_search.sh this returned “You have the latest firmware”


pi@raspberrypi ~ $ 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: -- -- -- -- -- -- -- --
pi@raspberrypi ~ $

Error I get :
Traceback (most recent call last):
File “temp.py”, line 35, in <module>
f=grovepi.temp(0)
File “/home/pi/sensorlab/grove_scratch/grovepi.py”, line 100, in temp
a=analogRead(pin)
File “/home/pi/sensorlab/grove_scratch/grovepi.py”, line 87, in analogRead
bus.write_i2c_block_data(address,1,aRead_cmd+[pin,0,0])
IOError: [Errno 5] Input/output error

Now only have read A0 and temperature sensor on A0 - still same issue - its not reliable and crashes after a few seconds and sometimes a few minutes - can’t use this in a classroom environment - will try a different sensor on A0 and see what happens.

Changed again the python program :


import scratch
import smbus
import time
import grovepi
import datetime
s=scratch.Scratch()
bus=smbus.SMBus(0)
address=0x04
i=0
f=0
#ff=open(&#039;temp&#039;,&#039;a&#039;)
while True:
        i=i+1
        f=grovepi.temp(0)
        time.sleep(0.5)
        s.sensorupdate({&#039;date&#039; : datetime.datetime.today()})
        s.sensorupdate({&#039;temp&#039; : f})
        s.sensorupdate({&#039;i&#039; : i})
        print i,f
        with open (&#039;temp&#039;,&#039;a&#039;) as ff:
                ff.write(str(i)+&#039;,&#039;+str(f)+&#039;n&#039;)

Output in the file :


173,23.9107594119
174,23.9107594119
175,23.8239250554
176,23.8239250554
177,23.7371324894
178,23.6503810198
179,23.6503810198
180,23.6503810198
181,23.6503810198
182,23.5636699536
183,23.5636699536
184,23.5636699536
185,23.5636699536
186,23.5636699536
187,23.4769985989
188,23.4769985989
189,23.4769985989
190,23.4769985989

Counter reached 190 and then this error :


Traceback (most recent call last):
  File &quot;temp.py&quot;, line 33, in &lt;module&gt;
    f=grovepi.temp(0)
  File &quot;/home/pi/sensorlab/grove_scratch/grovepi.py&quot;, line 100, in temp
    a=analogRead(pin)
  File &quot;/home/pi/sensorlab/grove_scratch/grovepi.py&quot;, line 90, in analogRead
    number = bus.read_i2c_block_data(address,1)
IOError: [Errno 5] Input/output error

Changed so no calls to Scratch at all :


import scratch
import smbus
import time
import grovepi
import datetime
s=scratch.Scratch()
bus=smbus.SMBus(0)
address=0x04
i=0
f=0
#ff=open(&#039;temp&#039;,&#039;a&#039;)
while True:
        i=i+1
        f=grovepi.temp(0)
        time.sleep(0.5)
#       s.sensorupdate({&#039;date&#039; : datetime.datetime.today()})
#       s.sensorupdate({&#039;temp&#039; : f})
#       s.sensorupdate({&#039;i&#039; : i})
        print i,f
        with open (&#039;temp&#039;,&#039;a&#039;) as ff:
                ff.write(str(i)+&#039;,&#039;+str(f)+&#039;n&#039;)

Got further but still crashed :


Traceback (most recent call last):
  File &quot;temp.py&quot;, line 33, in &lt;module&gt;
    f=grovepi.temp(0)
  File &quot;/home/pi/sensorlab/grove_scratch/grovepi.py&quot;, line 100, in temp
    a=analogRead(pin)
  File &quot;/home/pi/sensorlab/grove_scratch/grovepi.py&quot;, line 87, in analogRead
    bus.write_i2c_block_data(address,1,aRead_cmd+[pin,0,0])
IOError: [Errno 5] Input/output error

File output :
369 20.7203883998
370 20.6346447236
371 20.7203883998

OK - fixed it by using try blocks :


import scratch
import smbus
import time
import grovepi
import datetime
s=scratch.Scratch()
bus=smbus.SMBus(0)
address=0x04
i=0
f=0
#ff=open(&#039;temp&#039;,&#039;a&#039;)
while True:
        try:
                i=i+1
                f=grovepi.temp(2)
                time.sleep(0.5)
                s.sensorupdate({&#039;date&#039; : datetime.datetime.today()})
                s.sensorupdate({&#039;temp&#039; : f})
                s.sensorupdate({&#039;i&#039; : i})
                print i,f
                with open (&#039;temp&#039;,&#039;a&#039;) as ff:
                        ff.write(str(i)+&#039;,&#039;+str(f)+&#039;n&#039;)
        except IOError:
                print &quot;sjk IO Error&quot;
        except:
                print &quot;error&quot;