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---------------------------
rev = GPIO.RPI_REVISION
if rev == 2 or rev == 3:
bus = smbus.SMBus(1)
else:
bus = smbus.SMBus(0)
GPIO.setwarnings(False)
#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
grovepi.pinMode(sensor0,"INPUT")
grovepi.pinMode(sensor1,"INPUT")
grovepi.pinMode(sensor2,"INPUT")
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:
try:
analog_read()
except (TypeError, IOError) as e:
unsuccessful_readings += 1
print(str(unsuccessful_readings) + " unsuccessfull readings so far : error -> " + str(e))
except KeyboardInterrupt as e:
print()
print(str(float(unsuccessful_readings) * 100 / (unsuccessful_readings + successful_readings)) + "% error rate")
exit(0)
else:
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!