GithubHelp home page GithubHelp logo

bluerobotics / ms5837-python Goto Github PK

View Code? Open in Web Editor NEW
26.0 5.0 36.0 17 KB

A python module to interface with MS5837-30BA and MS5837-02BA waterproof pressure and temperature sensors.

License: MIT License

Python 100.00%
pressure temperature sensor python i2c rov

ms5837-python's Introduction

ms5837-python

A python module to interface with MS5837-30BA and MS5837-02BA waterproof pressure and temperature sensors. Tested on Raspberry Pi 3 with Raspbian.

Installation

The python SMBus library must be installed.

sudo apt-get install python-smbus2

Download this repository by clicking on the download button in this webpage, or using git:

git clone https://github.com/bluerobotics/ms5837-python

If you would like to try the example, move to the directory where you downloaded the repository, and run python example.py. To use the library, copy the ms5837.py file to your project/program directory and use this import statement in your program: import ms5837.

Raspberry Pi

If you are using a Raspberry Pi, the i2c interface must be enabled. Run sudo raspi-config, and choose to enable the i2c interface in the interfacing options.

Usage

import ms5837

ms5837 provides a generic MS5837 class for use with different models

MS5837(model=ms5837.MODEL_30BA, bus=1)

These model-specific classes inherit from MS5837 and don't have any unique members

MS5837_30BA(bus=1)
MS5837_02BA(bus=1)

An MS5837 object can be constructed by specifiying the model and the bus

sensor = ms5837.MS5837() # Use defaults (MS5837-30BA device on I2C bus 1)
sensor = ms5837.MS5837(ms5837.MODEL_02BA, 0) # Specify MS5837-02BA device on I2C bus 0

Or by creating a model-specific object

sensor = ms5837.MS5837_30BA() # Use default I2C bus (1)
sensor = ms5837.MS5837_30BA(0) # Specify I2C bus 0

init()

Initialize the sensor. This needs to be called before using any other methods.

sensor.init()

Returns true if the sensor was successfully initialized, false otherwise.

read(oversampling=OSR_8192)

Read the sensor and update the pressure and temperature. The sensor will be read with the supplied oversampling setting. Greater oversampling increases resolution, but takes longer and increases current consumption.

sensor.read(ms5837.OSR_256)

Valid arguments are:

ms5837.OSR_256
ms5837.OSR_512
ms5837.OSR_1024
ms5837.OSR_2048
ms5837.OSR_4096
ms5837.OSR_8192

Returns True if read was successful, False otherwise.

setFluidDensity(density)

Sets the density in (kg/m^3) of the fluid for depth measurements. The default fluid density is ms5837.DENISTY_FRESHWATER.

sensor.setFluidDensity(1000) # Set fluid density to 1000 kg/m^3
sensor.setFluidDensity(ms5837.DENSITY_SALTWATER) # Use predefined saltwater density

Some convenient constants are:

ms5837.DENSITY_FRESHWATER = 997
ms5837.DENSITY_SALTWATER = 1029

pressure(conversion=UNITS_mbar)

Get the most recent pressure measurement.

sensor.pressure() # Get pressure in default units (millibar)
sensor.pressure(ms5837.UNITS_atm) # Get pressure in atmospheres
sensor.pressure(ms5837.UNITS_kPa) # Get pressure in kilopascal

Some convenient constants are:

ms5837.UNITS_Pa     = 100.0
ms5837.UNITS_hPa    = 1.0
ms5837.UNITS_kPa    = 0.1
ms5837.UNITS_mbar   = 1.0
ms5837.UNITS_bar    = 0.001
ms5837.UNITS_atm    = 0.000986923
ms5837.UNITS_Torr   = 0.750062
ms5837.UNITS_psi    = 0.014503773773022

Returns the most recent pressure in millibar * conversion. Call read() to update.

temperature(conversion=UNITS_Centigrade)

Get the most recent temperature measurement.

sensor.temperature() # Get temperature in default units (Centigrade)
sensor.temperature(ms5837.UNITS_Farenheit) # Get temperature in Farenheit

Valid arguments are:

ms5837.UNITS_Centigrade
ms5837.UNITS_Farenheit
ms5837.UNITS_Kelvin

Returns the most recent temperature in the requested units, or temperature in degrees Centigrade if invalid units specified. Call read() to update.

depth()

Get the most recent depth measurement in meters.

sensor.depth()

Returns the most recent depth in meters using the fluid density (kg/m^3) configured by setFluidDensity(). Call read() to update.

altitude()

Get the most recent altitude measurement relative to Mean Sea Level pressure in meters.

sensor.altitude()

Returns the most recent altitude in meters relative to MSL pressure using the density of air at MSL. Call read() to update.

ms5837-python's People

Contributors

dheideman avatar es-alexander avatar jaxxzer avatar jneines avatar rgov avatar wlkh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ms5837-python's Issues

Library doesn't work with Beaglebone Black

It looks like it is because the BeagleBone does not support SMBus Block reads:

debian@beaglebone:~$ i2cdetect -F 2
Functionalities implemented by /dev/i2c-2:
I2C yes
SMBus Quick Command no
SMBus Send Byte yes
SMBus Receive Byte yes
SMBus Write Byte yes
SMBus Read Byte yes
SMBus Write Word yes
SMBus Read Word yes
SMBus Process Call yes
SMBus Block Write yes
SMBus Block Read no
SMBus Block Process Call no
SMBus PEC yes
I2C Block Write yes
I2C Block Read yes

Another note is that i2cdetect does not see the MS5837 on the BeagleBone. i2cdump does though...

debian@beaglebone:~$ i2cdetect -y -r 2
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- UU UU UU UU -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

debian@beaglebone:~$ i2cdump -y 2 0x76
No size specified (using byte-data access)
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: 00 00 XX XX XX XX XX XX XX XX XX XX XX XX XX XX ..XXXXXXXXXXXXXX
10: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
20: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
30: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
40: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
50: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
60: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
70: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
80: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
90: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
a0: 70 70 86 86 87 87 50 50 53 53 6c 6c 67 67 XX XX pp????PPSSllggXX
b0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
c0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
d0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
e0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
f0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX

check out ian black's work

https://github.com/IanTBlack/br_sensors

  1. Used TEOS-10 pressure to depth conversion to compute depth.
  2. Changed variable/function names to more closely match MS5837-30BA manual.
  3. Combined read and temperature/pressure/depth/altitude functions into one.
  4. temperature/pressure/depth/altitude are called independently of one another. They do rely on one another, but I formatted in a way that doesn't require users to call pressure before calling depth.
  5. absolute_pressure() give the absolute pressure reading in user selected units
  6. pressure() removes atmospheric pressure if given. if not it uses standard atmospheric pressure.

Farenheit conversion

Great library, thank you very much! Just noticed that the Fahrenheit conversion isn't correct. If you replace 9/5 with 1.8 in ms5837.py line 139 it works. 9/5 defaults to 1 rather than 1.8

Pressure reading seems to track temperature

I have been having problems with accurate readings from several BAR02’s. I am measuring the water depth in an outdoor water fountain tank (20cm deep).

I finally plotted absolute pressure (Bar02 - atmospheric pressure measured with a BMP280) vs water temperature, measured with the BAR02. There is an exact correlation.
18FD79BF-E04C-499A-B3CC-1569EFB7F4CE

In the attached, there is rain at midnight, and the water pump switches on at 8:00am and off at 10:00am. The jumps in water pressure is reflected in the changes in water depth of the tank.

I have tried two different BAR02’s with the same result. It’s as if the temperature compensation is not working. The library init’s just fine though, so the CRC is correct. I have checked the math, and it corresponds to the datasheet.

Something is not right though.

Remote I/O error - running example.py

Hi,

I am trying to run example.py on a RPi4, but when I do, I get the following error:

Traceback (most recent call last):
  File "./example.py", line 13, in <module>
    if not sensor.init():
  File "/home/pi/ms5837-python/ms5837/ms5837.py", line 74, in init
    self._bus.write_byte(self._MS5837_ADDR, self._MS5837_RESET)
IOError: [Errno 121] Remote I/O error

It appears that the ms5837 is recognized when I run i2cdetect:

i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- 76 --

Any ideas why I'm getting this error? Any help would be greatly appreciated!

Thanks,
Jonathan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.