def initializeSensors(): # Initialize the Altitude/Pressure Sensor (MPL3115A2) global altitudePressureSensor try: altitudePressureSensor = adafruit_mpl3115a2.MPL3115A2(i2c, address=0x60) # You can configure the pressure at sealevel to get better altitude estimates. # This value has to be looked up from your local weather forecast or meteorlogical # reports. It will change day by day and even hour by hour with weather # changes. Remember altitude estimation from barometric pressure is not exact! # Set this to a value in pascals: altitudePressureSensor.sealevel_pressure = 101760 except (OSError, ValueError): print("Altitude sensor not detected") #Initialize the Acceleration Sensor (lsm9ds1) global accelerationSensor try: accelerationSensor = adafruit_lsm9ds1.LSM9DS1_I2C(i2c) except (OSError, ValueError): print("Acceleration sensor not detected") #Initializes the Geiger Counter global radiationSensor try: radiationSensor = RadiationWatch(24, 23) radiationSensor.setup() except (OSError, ValueError): print("Radiation sensor not detected")
def initializeSensors(): #Initialize the Acceleration Sensor (lsm9ds1) #Initializes the Geiger Counter global radiationSensor try: radiationSensor = RadiationWatch(24, 23) radiationSensor.setup() except (OSError, ValueError): print("Radiation sensor not detected")
def initializeSensors(): # Test initializing the I2C try: i2c = busio.I2C(board.SCL, board.SDA) except: print("I2C bus could not be initialized") # Initialize the Altitude/Pressure Sensor (MPL3115A2) # Alternatively you can specify a different I2C address for the device: #sensor = adafruit_mpl3115a2.MPL3115A2(i2c, address=0x10) global altitudePressureSensor try: altitudePressureSensor = adafruit_mpl3115a2.MPL3115A2(i2c, address=0x60) # You can configure the pressure at sealevel to get better altitude estimates. # This value has to be looked up from your local weather forecast or meteorlogical # reports. It will change day by day and even hour by hour with weather # changes. Remember altitude estimation from barometric pressure is not exact! # Set this to a value in pascals: altitudePressureSensor.sealevel_pressure = 101760 except (OSError, ValueError, NameError): print( "Altitude sensor not detected. Please check the connection to the sensor.\n" ) #Initialize the Acceleration Sensor (LSM9DS1) global accelerationSensor try: accelerationSensor = adafruit_lsm9ds1.LSM9DS1_I2C(i2c) except (OSError, ValueError, NameError): print( "Acceleration sensor not detected. Please check the connection to the sensor.\n" ) global radiationSensor try: radiationSensor = RadiationWatch(24, 23) radiationSensor.setup() except (OSError, ValueError, NameError): print( "Radiation sensor not detected. Please check the connection to the sensor.\n" )
Released under MIT License. See LICENSE file. By Yoan Tournade <*****@*****.**> """ from PiPocketGeiger import RadiationWatch import datetime import time import csv FILENAME = "radiation.csv" LOGGING_PERIOD = 30 if __name__ == "__main__": try: radiationWatch = RadiationWatch(24, 23) radiationWatch.setup() print("Logging to the file {0} each {1} seconds.".format( FILENAME, LOGGING_PERIOD)) with open(FILENAME, 'w') as myfile: writer = csv.writer(myfile, quoting=csv.QUOTE_ALL) writer.writerow( ['date', 'duration', 'CPM', 'uSv/h', 'uSv/h error']) while 1: readings = radiationWatch.status() print("Logging... {0}.".format(readings)) writer.writerow([ datetime.datetime.now().strftime('%d/%m/%Y %H:%M:00'), readings['duration'], readings['cpm'], readings['uSvh'], readings['uSvhError'] ]) time.sleep(LOGGING_PERIOD)
class Sensor(object): # sensors should check the configuration flags to see which sensors are # selected and then if active should poll the sensor and append it to the # sensor array. def __init__(self): #set up the necessary info for the sensors that are active. # create a simple reference for the degree symbol since we use it a lot self.deg_sym = '\xB0' self.generators = False # add individual sensor module parameters below. #0 1 2 3 4 #info = (lower range, upper range, unit, symbol) #'value','min','max','dsc','sym','dev','timestamp' # testing: # data fragments (objects that contain the most recent sensor value, # plus its context) are objects called Fragment(). if configure.system_vitals: self.step = 0.0 self.step2 = 0.0 self.steptan = 0.0 totalmem = float(psutil.virtual_memory().total) / 1024 self.cputemp = Fragment(0, 100, "CpuTemp", self.deg_sym + "c", "RaspberryPi") self.cpuperc = Fragment(0, 100, "CpuPercent", "%", "Raspberry Pi") self.virtmem = Fragment(0, totalmem, "VirtualMemory", "b", "RaspberryPi") self.bytsent = Fragment(0, 100000, "BytesSent", "b", "RaspberryPi") self.bytrece = Fragment(0, 100000, "BytesReceived", "b", "RaspberryPi") if self.generators: self.sinewav = Fragment(-100, 100, "SineWave", "", "RaspberryPi") self.tanwave = Fragment(-500, 500, "TangentWave", "", "RaspberryPi") self.coswave = Fragment(-100, 100, "CosWave", "", "RaspberryPi") self.sinwav2 = Fragment(-100, 100, "SineWave2", "", "RaspberryPi") if configure.sensehat: self.ticks = 0 self.onoff = 1 # instantiate a sensehat object, self.sense = SenseHat() # Initially clears the LEDs once loaded self.sense.clear() # Sets the IMU Configuration. self.sense.set_imu_config(True, False, False) # activates low light conditions to not blind the user. self.sense.low_light = True self.sh_temp = Fragment(0, 65, "Thermometer", self.deg_sym + "c", "sensehat") self.sh_humi = Fragment(20, 80, "Hygrometer", "%", "sensehat") self.sh_baro = Fragment(260, 1260, "Barometer", "hPa", "sensehat") self.sh_magx = Fragment(-500, 500, "MagnetX", "G", "sensehat") self.sh_magy = Fragment(-500, 500, "MagnetY", "G", "sensehat") self.sh_magz = Fragment(-500, 500, "MagnetZ", "G", "sensehat") self.sh_accx = Fragment(-500, 500, "AccelX", "g", "sensehat") self.sh_accy = Fragment(-500, 500, "AccelY", "g", "sensehat") self.sh_accz = Fragment(-500, 500, "AccelZ", "g", "sensehat") if configure.ir_thermo: i2c = io.I2C(configure.PIN_SCL, configure.PIN_SDA, frequency=100000) self.mlx = adafruit_mlx90614.MLX90614(i2c) self.irt_ambi = Fragment(0, 80, "IR ambient [mlx]", self.deg_sym + "c") self.irt_obje = Fragment(0, 80, "IR object [mlx]", self.deg_sym + "c") if configure.envirophat: # and not configure.simulate: self.ep_temp = Fragment(0, 65, "Thermometer", self.deg_sym + "c", "Envirophat") self.ep_colo = Fragment(20, 80, "Colour", "RGB", "Envirophat") self.ep_baro = Fragment(260, 1260, "Barometer", "hPa", "Envirophat") self.ep_magx = Fragment(-500, 500, "Magnetomer X", "G", "Envirophat") self.ep_magy = Fragment(-500, 500, "Magnetomer Y", "G", "Envirophat") self.ep_magz = Fragment(-500, 500, "Magnetomer Z", "G", "Envirophat") self.ep_accx = Fragment(-500, 500, "Accelerometer X (EP)", "g", "Envirophat") self.ep_accy = Fragment(-500, 500, "Accelerometer Y (EP)", "g", "Envirophat") self.ep_accz = Fragment(-500, 500, "Accelerometer Z (EP)", "g", "Envirophat") if configure.bme: # Create library object using our Bus I2C port i2c = io.I2C(configure.PIN_SCL, configure.PIN_SDA) self.bme = adafruit_bme680.Adafruit_BME680_I2C(i2c, address=0x76, debug=False) self.bme_temp = Fragment(-40, 85, "Thermometer", self.deg_sym + "c", "BME680") self.bme_humi = Fragment(0, 100, "Hygrometer", "%", "BME680") self.bme_press = Fragment(300, 1100, "Barometer", "hPa", "BME680") self.bme_voc = Fragment(300000, 1100000, "VOC", "KOhm", "BME680") if configure.pocket_geiger: self.radiat = Fragment(0.0, 10000.0, "Radiation", "urem/hr", "pocketgeiger") self.radiation = RadiationWatch(configure.PG_SIG, configure.PG_NS) self.radiation.setup() if configure.amg8833: self.amg_high = Fragment(0.0, 80.0, "IRHigh", self.deg_sym + "c", "amg8833") self.amg_low = Fragment(0.0, 80.0, "IRLow", self.deg_sym + "c", "amg8833") configure.sensor_info = self.get_all_info() def get_all_info(self): info = self.get() allinfo = [] for fragment in info: thisfrag = [ fragment.dsc, fragment.dev, fragment.sym, fragment.mini, fragment.maxi ] allinfo.append(thisfrag) return allinfo def sin_gen(self): wavestep = math.sin(self.step) self.step += .1 return wavestep def tan_gen(self): wavestep = math.tan(self.steptan) self.steptan += .1 return wavestep def sin2_gen(self, offset=0): wavestep = math.sin(self.step2) self.step2 += .05 return wavestep def cos_gen(self, offset=0): wavestep = math.cos(self.step) self.step += .1 return wavestep def get(self): #sensorlist holds all the data fragments to be handed to plars. sensorlist = [] #timestamp for this sensor get. timestamp = time.time() if configure.bme: self.bme_temp.set(self.bme.temperature, timestamp) self.bme_humi.set(self.bme.humidity, timestamp) self.bme_press.set(self.bme.pressure, timestamp) self.bme_voc.set(self.bme.gas / 1000, timestamp) sensorlist.extend( (self.bme_temp, self.bme_humi, self.bme_press, self.bme_voc)) if configure.sensehat: magdata = sense.get_compass_raw() acceldata = sense.get_accelerometer_raw() self.sh_temp.set(sense.get_temperature(), timestamp) self.sh_humi.set(sense.get_humidity(), timestamp) self.sh_baro.set(sense.get_pressure(), timestamp) self.sh_magx.set(magdata["x"], timestamp) self.sh_magy.set(magdata["y"], timestamp) self.sh_magz.set(magdata["z"], timestamp) self.sh_accx.set(acceldata['x'], timestamp) self.sh_accy.set(acceldata['y'], timestamp) self.sh_accz.set(acceldata['z'], timestamp) sensorlist.extend((self.sh_temp, self.sh_baro, self.sh_humi, self.sh_magx, self.sh_magy, self.sh_magz, self.sh_accx, self.sh_accy, self.sh_accz)) if configure.pocket_geiger: data = self.radiation.status() rad_data = float(data["uSvh"]) # times 100 to convert to urem/h self.radiat.set(rad_data * 100, timestamp) sensorlist.append(self.radiat) if configure.amg8833: data = numpy.array(amg.pixels) high = numpy.max(data) low = numpy.min(data) self.amg_high.set(high, timestamp) self.amg_low.set(low, timestamp) sensorlist.extend((self.amg_high, self.amg_low)) if configure.envirophat: self.rgb = light.rgb() self.analog_values = analog.read_all() self.mag_values = motion.magnetometer() self.acc_values = [round(x, 2) for x in motion.accelerometer()] self.ep_temp.set(weather.temperature(), timestamp) self.ep_colo.set(light.light(), timestamp) self.ep_baro.set(weather.pressure(unit='hpa'), timestamp) self.ep_magx.set(self.mag_values[0], timestamp) self.ep_magy.set(self.mag_values[1], timestamp) self.ep_magz.set(self.mag_values[2], timestamp) self.ep_accx.set(self.acc_values[0], timestamp) self.ep_accy.set(self.acc_values[1], timestamp) self.ep_accz.set(self.acc_values[2], timestamp) sensorlist.extend((self.ep_temp, self.ep_baro, self.ep_colo, self.ep_magx, self.ep_magy, self.ep_magz, self.ep_accx, self.ep_accy, self.ep_accz)) # provides the basic definitions for the system vitals sensor readouts if configure.system_vitals: if not configure.pc: f = os.popen( "cat /sys/class/thermal/thermal_zone0/temp").readline() t = float(f[0:2] + "." + f[2:]) else: t = float(47) # update each fragment with new data and mark the time. self.cputemp.set(t, timestamp) self.cpuperc.set(float(psutil.cpu_percent()), timestamp) self.virtmem.set( float(psutil.virtual_memory().available * 0.0000001), timestamp) self.bytsent.set( float(psutil.net_io_counters().bytes_recv * 0.00001), timestamp) self.bytrece.set( float(psutil.net_io_counters().bytes_recv * 0.00001), timestamp) if self.generators: self.sinewav.set(float(self.sin_gen() * 100), timestamp) self.tanwave.set(float(self.tan_gen() * 100), timestamp) self.coswave.set(float(self.cos_gen() * 100), timestamp) self.sinwav2.set(float(self.sin2_gen() * 100), timestamp) # load the fragments into the sensorlist sensorlist.extend((self.cputemp, self.cpuperc, self.virtmem, self.bytsent, self.bytrece)) if self.generators: sensorlist.extend( (self.sinewav, self.tanwave, self.coswave, self.sinwav2)) configure.max_sensors[0] = len(sensorlist) if len(sensorlist) < 1: print("NO SENSORS LOADED") return sensorlist
import time from PiPocketGeiger import RadiationWatch radwat = RadiationWatch(24, 23) radwat.setup() print("Status: " + radwat.status()) def onRadiation(): print("Ray appeared!") def onNoise(): print("Vibration!") radwat.register_radiation_callback(onRadiation) radwat.register_noise_callback(onNoise) while 1: time.sleep(1)