[SOLVED] SwitchDoc Labs WeatherRack - TypeError Exception



Hi Robert,
Just thought I would let you know I restarted my weather station script and it immediately through up the errors, as before.
So I went to bed.
In the afternoon I decided I would have a play again, and changed the Wind_Rain code to use different GPIO’s.
Changing them to use

GPIO Numbering Mode GPIO.BCM

anenometerPin = 18 # Pin 12 = GPIO 18 Yellow Wire
rainPin = 17 # Pin 11 = GPIO 17

Ground is Black

And behold at this present moment in time it is working.
I would be interested on your thoughts about changing the GPIO’s.
Thanks for your help.


Hi @whimickrh,

I’m linking the previously related post here: http://forum.dexterindustries.com/t/solved-gpio-to-grovepi-digitalread/3041/3

Going on GPIO 17 & GPIO 18 should be fine.

What I can suggest you if the problem persists, is to make a very simple program and test the GrovePi without the anemometer connected.
Just let the Grove analog sensors run and nothing else.
Then start adding sensors gradually and see where it’s heading.

Please keep me updated on how your situation is evolving.

Thank you!


Hi Robert,

I ran the program over the weekend and sadly the dreaded
TypeError: ‘int’ object has no attribute ‘getitem’ poped up.

Your suggestion about disableling the GPIO had occured to me and I tried it and the anemometer connected and rain connected and it worked.

I have alway thought the mixing GPIO and Grove commands may cause problems.

So today I removed the analog sensors from the main and made an import module to see if that works any better.

And have attach copies of my scripts.

I would realy like to re write the Switchdoc Labs SDL_Pi_WeatherRack scripts so to make use of Grove grovepi.analogRead and digitalread.

Do you think this could be done, if so can you suggest how.
I will have ago myself though I think it may be above my programming skills, I will do some research.

Raspi_Met.py (18.9 KB)
grove_analog.py (3.3 KB)
Wind_Rain.py (7.9 KB)

Thankyou for you help


Hi @whimickrh,

First of all, I don’t understand why the TypeError exception isn’t caught yet.
As far as I remember, when I tried the code, it worked even when I was purposefully inserting raises inside the try block.
Can you try running your scripts with Python 3 instead of Python 2, if that’s the case?

I’m not sure which scripts have failed. Was it the Raspi_Met.py, was it the Wind_Rain.py or are there any other scripts you have ran and failed?

Also, I’m not getting this phrase:

So today I removed the analog sensors from the main and made an import module to see if that works any better.

Why remove the algorithm for reading the sensor values off of the GrovePi, since the target is to debug these sensors and see how they can be fixed?
I was thinking that it’s better to start with reading off of the GrovePi and then start adding gradually.

Since the anemometer is an I2C device, I don’t see how you can use the GrovePi's library on the Raspberry Pi. There’s no benefit in doing this way, so your current setup should do it.

For now, I’m suggesting to stick with the following script (which is dealing only with analog sensor) and see how it works. It’s a slightly modified version of your code which allows to be ran indefinitely.
When the user presses CTRL-C, the user is shown an error rate which was calculated based on the amount of errors it encountered.

import smbus
import RPi.GPIO as GPIO
import grovepi
import math
import logging

# --------------------------SETTINGS---------------------------

if rev == 2 or rev == 3:
	bus = smbus.SMBus(1)
	bus = smbus.SMBus(0)


#Sensor connected to A0 Port
sensor0 = 14		# Pin 14 is A0 Port. Light Sensor
sensor1 = 15		# Pin 15 is A1 Port. Grove - Water Sensor
sensor2 = 16		# Pin 16 is A2 Port. Grove - Moisture Sensor

light_reading = 0
solar_wm2 = 0
Light_lux = 0
Light_resistance = 0
Light_temperature = 0
leaf_wetness = 0
soil_moisture = 0

unsuccessful_readings = 0
successful_readings = 0

# -------------------------ANALOG SENSORS----------------------------

def analog_read():
	global light_reading, solar_wm2, Light_lux, Light_resistance, Light_temperature, leaf_wetness, soil_moisture
	lux_max = 1200.0
	light_reading = float(grovepi.analogRead(sensor0) / 2.0)
	soil_moisture = float(grovepi.analogRead(sensor1) / 100)
	leaf_wetness = float(grovepi.analogRead(sensor2) / 100)
	Light_lux = round(math.exp(float(light_reading) / 100.0), 2)
	solar_wm2 = int((float(Light_lux * 0.0079)) * 100)
	#Light_resistance = int((float)(1023 - light_reading) * 10000 / light_reading)
	# Actual_solar_reading = round(Light_lux) * 100 / int(lux_max)
	#Light_temperature = int(1 / (math.log(Light_resistance / 10000) / 3975 + 1 / 298.15) - 273.15)

while True:

	except (TypeError, IOError) as e:
		unsuccessful_readings += 1
		print(str(unsuccessful_readings) + " unsuccessfull readings so far : error -> " + str(e))

	except KeyboardInterrupt as e:
		print(str(float(unsuccessful_readings) * 100 / (unsuccessful_readings + successful_readings)) + "% error rate")

		successful_readings += 1
		print ("light_reading = " + str(light_reading) + " | leaf_wetness = " + str(leaf_wetness) + " | soil_moisture = " + str(soil_moisture))

On my Raspberry Pi it works even when I disconnect the GrovePi while running the script.
Please let me know how it goes on your Raspberry Pi.

Thank you!


Hi Robert,

Thank you for your script, and time.

I am running your script on its own, with the sensors attached, and see no errors for now. I will leave it running over night and see if any errors pop up.

One thing of note is the light and leaf sensor are giving a reading, but the soil is showing 0.
There an occasional odd light reading, but no error messages

light_reading = 370.0 | leaf_wetness = 2.0 | soil_moisture = 0.0
light_reading = 370.5 | leaf_wetness = 2.0 | soil_moisture = 0.0
light_reading = 32767.5 | leaf_wetness = 1.0 | soil_moisture = 0.0
light_reading = 369.0 | leaf_wetness = 6.0 | soil_moisture = 0.0
light_reading = 369.5 | leaf_wetness = 1.0 | soil_moisture = 0.0




Before I went to bed last night, I stopped your script briefly and this was produced.

light_reading = 66.0 | leaf_wetness = 0.0 | soil_moisture = 0.0
light_reading = 66.0 | leaf_wetness = 0.0 | soil_moisture = 0.0
light_reading = 64.5 | leaf_wetness = 0.0 | soil_moisture = 0.0
light_reading = 63.5 | leaf_wetness = 0.0 | soil_moisture = 0.0
0.0% error rate

Both the leaf and soil sensor are showing 0.0 (no reading), and no errors were reported

I ran grove_analog_read_v2.py

sensor_value0 (light_reading) 35.0
sensor_value1 (leaf_wetness) 20.0
sensor_value2 (soil_moisture) 125.0

and sensor were read.
I will now run your script over night with python 3.

and got this
Traceback (most recent call last):
File “grove_analogv2.py”, line 3, in
import grovepi
ImportError: No module named ‘grovepi’

Note that I have never used python3, and have only learned Python2
Back to Python 2 for now



Hi @whimickrh,

It’s great to hear the grove_analog.py didn’t throw any reading errors.

Also, can you show me how does grove_analog_read_v2.py look like?
If it’s going to be an error with it, I think it’d be better to share it here so I can give it some thought.

Other than that, I’m waiting to see how the test goes.

Thank you!


The test ran OK over night without errors.

I am now running read_analog.py, which is used to import and read the data from grove_analog_read_v1_1.py. It loops and print data, I have also ran your error trap and include a method of withing a error log file.
Both these have been running for some hours now without errors.

I will also attach all the python Scripts I use with my weather station, I hope this will give us some more clues. I have disablebel parts of the Raspi_Met.py.

Sadly it relies on Weewx to work, would to construct the Script so it can be used with out Weewx. I have not been able to workout how to do that.

I have included all my scripts.

I hope thisd helps

dust_sensor_lib.py (1.2 KB)
grove_i2c_temp_hum_hdc1000.py (3.3 KB)
Raspi_Met.py (18.9 KB)
<a class=“attachment” href="/uploagrove_analog.py (3.3 KB)
grove_analog_read_v1_1.py (1.9 KB)
read_analog.py (610 Bytes)
ds/dexterindustries/original/1X/0c266a5c8e9df02938484da17376c1d6ef3f1577.py">Wind_Rain.py (7.9 KB)
Wind_Rain_Test.py (1.8 KB)
BMP280.py (9.6 KB)


Wind_Rain.py (7.9 KB)


Hi Robert,

I have been playing and today I cobled together this script that does now use weewx.
Its has been running to some short time and it not showing any errors.

I found windspeed examples on the net and adapted it so to use grovepi.digitalRead to get pulses from a anemometer that uses a magnetic switch, that is simular to the Grove Magnetic switch.

Since there has been no wind today I have not been able to fully test it, hense the printing to screen
I will work on the wind direction over the next fewdays.

My aim is to simpilfy the script, which may have become to complex, I have used most of the orginal code, and cut out chunks.

An I on the right track, and is the script less likley to throw any futher errors, my aim is to make a weather station that makes full use of the GroverPi.

I hope my effort will bennifit other GrovePi uses

Raspi_MetV2_2.py (10.7 KB)



Hi @whimickrh,

Thank you for sharing all this code.
It must have taken quite some time to work on it.
It’s definitely going to be useful if another error occurs.

Just as you said, I think testing small blocks of code is much better than testing all of it at once.
If I were you, I’d just include the basic functionality and let each sensor run at once - not along many other.

Please keep me updated with any issues that may arise.

Thank you!


Hi Robert,

Unfortunatly I have not been able to send as much time on it today as I hoped, and tomorrow I have to attend a sent you has worked without errors so far. As I thought though the wind side of things needs some playing with. Forunatly the connections on the SwithDocs GroveWeather board alow to connect using Govecable and I am able to make use of grovepi.digitalRead.

As far as testing the code I have been testing it in chunks and when they work I add the function to the main script, to see what happens.

If you get a few minutes could you look at the dust-sensor code I sent you, that I have tweaked, so it can be imported as a module into the main code. Although it works at my end, it may have some errors that could cause errors with the main script.

Many Thanks


3 posts were split to a new topic: Parallel lines of execution for Python script with GrovePi sensors