-
Notifications
You must be signed in to change notification settings - Fork 37
/
example.py
52 lines (43 loc) · 1.88 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python
import ms5837
import time
sensor = ms5837.MS5837_30BA() # Default I2C bus is 1 (Raspberry Pi 3)
#sensor = ms5837.MS5837_30BA(0) # Specify I2C bus
#sensor = ms5837.MS5837_02BA()
#sensor = ms5837.MS5837_02BA(0)
#sensor = ms5837.MS5837(model=ms5837.MS5837_MODEL_30BA, bus=0) # Specify model and bus
# We must initialize the sensor before reading it
if not sensor.init():
print("Sensor could not be initialized")
exit(1)
# We have to read values from sensor to update pressure and temperature
if not sensor.read():
print("Sensor read failed!")
exit(1)
print(("Pressure: %.2f atm %.2f Torr %.2f psi") % (
sensor.pressure(ms5837.UNITS_atm),
sensor.pressure(ms5837.UNITS_Torr),
sensor.pressure(ms5837.UNITS_psi)))
print(("Temperature: %.2f C %.2f F %.2f K") % (
sensor.temperature(ms5837.UNITS_Centigrade),
sensor.temperature(ms5837.UNITS_Farenheit),
sensor.temperature(ms5837.UNITS_Kelvin)))
freshwaterDepth = sensor.depth() # default is freshwater
sensor.setFluidDensity(ms5837.DENSITY_SALTWATER)
saltwaterDepth = sensor.depth() # No nead to read() again
sensor.setFluidDensity(1000) # kg/m^3
print(("Depth: %.3f m (freshwater) %.3f m (saltwater)") % (freshwaterDepth, saltwaterDepth))
# fluidDensity doesn't matter for altitude() (always MSL air density)
print(("MSL Relative Altitude: %.2f m") % sensor.altitude()) # relative to Mean Sea Level pressure in air
time.sleep(5)
# Spew readings
while True:
if sensor.read():
print(("P: %0.1f mbar %0.3f psi\tT: %0.2f C %0.2f F") % (
sensor.pressure(), # Default is mbar (no arguments)
sensor.pressure(ms5837.UNITS_psi), # Request psi
sensor.temperature(), # Default is degrees C (no arguments)
sensor.temperature(ms5837.UNITS_Farenheit))) # Request Farenheit
else:
print("Sensor read failed!")
exit(1)