Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MS5837 functionality with BMP280 optional dependency #22376

Merged
merged 19 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
3266e13
Finalized gain/integration adjustment trees
vtHydroponics Nov 16, 2023
0ef3791
Fixed the bugs
vtHydroponics Nov 30, 2023
517590b
works but polishing code
vtHydroponics Mar 12, 2024
80cc8be
need to debug pressure in bmp
vtHydroponics Mar 12, 2024
0e20bde
updated temp to change via setoption8 command from tasmota
vtHydroponics May 6, 2024
8893648
sensor table working, value reporting working, need to update depende…
vtHydroponics Oct 18, 2024
4650d94
working
vtHydroponics Oct 28, 2024
1a1cc48
updated file name for ms5837 xsns file
vtHydroponics Oct 28, 2024
f3c9d1f
final working with renamed for current updates (128->116)
vtHydroponics Oct 28, 2024
d67c932
resolved PR comments for extra spaces, xi2c_96
vtHydroponics Oct 28, 2024
29a0322
removed extra spaces, added unit for inches across languages
vtHydroponics Oct 30, 2024
2b2b28c
added "Water depth" for languages
vtHydroponics Oct 30, 2024
c58164f
removed inches as a unit from language files
vtHydroponics Nov 1, 2024
557f49a
switched to centimeter units for SI consistency in Tasmota
vtHydroponics Nov 1, 2024
827646b
all variables showing in console and table; need to adjust offsets
vtHydroponics Nov 1, 2024
eec8108
cm conversion properly reporting
vtHydroponics Nov 4, 2024
f18d4c4
Sensor116 in console calibrates the sensor's pressure_offset variable
vtHydroponics Nov 4, 2024
3f33616
removed pressure offset debugging lines
vtHydroponics Nov 4, 2024
cbf9f1c
removed unecessary commented items from old code
vtHydroponics Nov 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/lib_i2c/Adafruit_TSL2591_Library/Adafruit_TSL2591.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**************************************************************************/
/*!
@file Adafruit_TSL2591.h
/*!
@file Adafruit_TSL2591.h
@author KT0WN (adafruit.com)
vtHydroponics marked this conversation as resolved.
Show resolved Hide resolved

This is a library for the Adafruit TSL2591 breakout board
Expand Down
258 changes: 258 additions & 0 deletions lib/lib_i2c/BlueRobotics_MS5837_Library/MS5837.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
#include "MS5837.h"
#include <Wire.h>

const uint8_t MS5837_ADDR = 0x76;
const uint8_t MS5837_RESET = 0x1E;
const uint8_t MS5837_ADC_READ = 0x00;
const uint8_t MS5837_PROM_READ = 0xA0;
const uint8_t MS5837_CONVERT_D1_8192 = 0x4A;
const uint8_t MS5837_CONVERT_D2_8192 = 0x5A;

const float MS5837::Pa = 100.0f;
const float MS5837::bar = 0.001f;
const float MS5837::mbar = 1.0f;

const uint8_t MS5837::MS5837_30BA = 0;
const uint8_t MS5837::MS5837_02BA = 1;
const uint8_t MS5837::MS5837_UNRECOGNISED = 255;

const uint8_t MS5837_02BA01 = 0x00; // Sensor version: From MS5837_02BA datasheet Version PROM Word 0
const uint8_t MS5837_02BA21 = 0x15; // Sensor version: From MS5837_02BA datasheet Version PROM Word 0
const uint8_t MS5837_30BA26 = 0x1A; // Sensor version: From MS5837_30BA datasheet Version PROM Word 0

MS5837::MS5837() {
fluidDensity = 1029;
}

bool MS5837::begin(TwoWire &wirePort) {
return (init(wirePort));
}

bool MS5837::init(TwoWire &wirePort) {
_i2cPort = &wirePort; //Grab which port the user wants us to use

// Reset the MS5837, per datasheet
_i2cPort->beginTransmission(MS5837_ADDR);
_i2cPort->write(MS5837_RESET);
_i2cPort->endTransmission();

// Wait for reset to complete
delay(10);

// Read calibration values and CRC
for ( uint8_t i = 0 ; i < 7 ; i++ ) {
_i2cPort->beginTransmission(MS5837_ADDR);
_i2cPort->write(MS5837_PROM_READ+i*2);
_i2cPort->endTransmission();

_i2cPort->requestFrom(MS5837_ADDR,2);
C[i] = (_i2cPort->read() << 8) | _i2cPort->read();
}

// Verify that data is correct with CRC
uint8_t crcRead = C[0] >> 12;
uint8_t crcCalculated = crc4(C);

if ( crcCalculated != crcRead ) {
return false; // CRC fail
}

uint8_t version = (C[0] >> 5) & 0x7F; // Extract the sensor version from PROM Word 0

// Set _model according to the sensor version
if (version == MS5837_02BA01)
{
_model = MS5837_02BA;
}
else if (version == MS5837_02BA21)
{
_model = MS5837_02BA;
}
else if (version == MS5837_30BA26)
{
_model = MS5837_30BA;
}
else
{
_model = MS5837_UNRECOGNISED;
}
// The sensor has passed the CRC check, so we should return true even if
// the sensor version is unrecognised.
// (The MS5637 has the same address as the MS5837 and will also pass the CRC check)
// (but will hopefully be unrecognised.)
return true;
}

void MS5837::setModel(uint8_t model) {
_model = model;
}

uint8_t MS5837::getModel() {
return (_model);
}

void MS5837::setFluidDensity(float density) {
fluidDensity = density;
}

void MS5837::read() {
//Check that _i2cPort is not NULL (i.e. has the user forgoten to call .init or .begin?)
if (_i2cPort == NULL)
{
return;
}

// Request D1 conversion
_i2cPort->beginTransmission(MS5837_ADDR);
_i2cPort->write(MS5837_CONVERT_D1_8192);
_i2cPort->endTransmission();

delay(20); // Max conversion time per datasheet

_i2cPort->beginTransmission(MS5837_ADDR);
_i2cPort->write(MS5837_ADC_READ);
_i2cPort->endTransmission();

_i2cPort->requestFrom(MS5837_ADDR,3);
D1_pres = 0;
D1_pres = _i2cPort->read();
D1_pres = (D1_pres << 8) | _i2cPort->read();
D1_pres = (D1_pres << 8) | _i2cPort->read();

// Request D2 conversion
_i2cPort->beginTransmission(MS5837_ADDR);
_i2cPort->write(MS5837_CONVERT_D2_8192);
_i2cPort->endTransmission();

delay(20); // Max conversion time per datasheet

_i2cPort->beginTransmission(MS5837_ADDR);
_i2cPort->write(MS5837_ADC_READ);
_i2cPort->endTransmission();

_i2cPort->requestFrom(MS5837_ADDR,3);
D2_temp = 0;
D2_temp = _i2cPort->read();
D2_temp = (D2_temp << 8) | _i2cPort->read();
D2_temp = (D2_temp << 8) | _i2cPort->read();

calculate();
}

void MS5837::calculate() {
// Given C1-C6 and D1, D2, calculated TEMP and P
// Do conversion first and then second order temp compensation

int32_t dT = 0;
int64_t SENS = 0;
int64_t OFF = 0;
int32_t SENSi = 0;
int32_t OFFi = 0;
int32_t Ti = 0;
int64_t OFF2 = 0;
int64_t SENS2 = 0;

// Terms called
dT = D2_temp-uint32_t(C[5])*256l;
if ( _model == MS5837_02BA ) {
SENS = int64_t(C[1])*65536l+(int64_t(C[3])*dT)/128l;
OFF = int64_t(C[2])*131072l+(int64_t(C[4])*dT)/64l;
P = (D1_pres*SENS/(2097152l)-OFF)/(32768l);
} else {
SENS = int64_t(C[1])*32768l+(int64_t(C[3])*dT)/256l;
OFF = int64_t(C[2])*65536l+(int64_t(C[4])*dT)/128l;
P = (D1_pres*SENS/(2097152l)-OFF)/(8192l);
}

// Temp conversion
TEMP = 2000l+int64_t(dT)*C[6]/8388608LL;

//Second order compensation
if ( _model == MS5837_02BA ) {
if((TEMP/100)<20){ //Low temp
Ti = (11*int64_t(dT)*int64_t(dT))/(34359738368LL);
OFFi = (31*(TEMP-2000)*(TEMP-2000))/8;
SENSi = (63*(TEMP-2000)*(TEMP-2000))/32;
}
} else {
if((TEMP/100)<20){ //Low temp
Ti = (3*int64_t(dT)*int64_t(dT))/(8589934592LL);
OFFi = (3*(TEMP-2000)*(TEMP-2000))/2;
SENSi = (5*(TEMP-2000)*(TEMP-2000))/8;
if((TEMP/100)<-15){ //Very low temp
OFFi = OFFi+7*(TEMP+1500l)*(TEMP+1500l);
SENSi = SENSi+4*(TEMP+1500l)*(TEMP+1500l);
}
}
else if((TEMP/100)>=20){ //High temp
Ti = 2*(dT*dT)/(137438953472LL);
OFFi = (1*(TEMP-2000)*(TEMP-2000))/16;
SENSi = 0;
}
}

OFF2 = OFF-OFFi; //Calculate pressure and temp second order
SENS2 = SENS-SENSi;

TEMP = (TEMP-Ti);

if ( _model == MS5837_02BA ) {
P = (((D1_pres*SENS2)/2097152l-OFF2)/32768l);
} else {
P = (((D1_pres*SENS2)/2097152l-OFF2)/8192l);
}
}

float MS5837::pressure(float conversion) {
if ( _model == MS5837_02BA ) {
return P*conversion/100.0f;
}
else {
return P*conversion/10.0f;
}
}

float MS5837::temperature() {
return TEMP/100.0f;
}

// The pressure sensor measures absolute pressure, so it will measure the atmospheric pressure + water pressure
// We subtract the atmospheric pressure to calculate the depth with only the water pressure
// The average atmospheric pressure of 101300 pascal is used for the calcuation, but atmospheric pressure varies
// If the atmospheric pressure is not 101300 at the time of reading, the depth reported will be offset
// In order to calculate the correct depth, the actual atmospheric pressure should be measured once in air, and
// that value should subtracted for subsequent depth calculations.
float MS5837::depth() {
return (pressure(MS5837::Pa)-101300)/(fluidDensity*9.80665f);
}

float MS5837::altitude() {
return (1-pow((pressure()/1013.25f),.190284f))*145366.45f*.3048f;
}


uint8_t MS5837::crc4(uint16_t n_prom[]) {
uint16_t n_rem = 0;

n_prom[0] = ((n_prom[0]) & 0x0FFF);
n_prom[7] = 0;

for ( uint8_t i = 0 ; i < 16; i++ ) {
if ( i%2 == 1 ) {
n_rem ^= (uint16_t)((n_prom[i>>1]) & 0x00FF);
} else {
n_rem ^= (uint16_t)(n_prom[i>>1] >> 8);
}
for ( uint8_t n_bit = 8 ; n_bit > 0 ; n_bit-- ) {
if ( n_rem & 0x8000 ) {
n_rem = (n_rem << 1) ^ 0x3000;
} else {
n_rem = (n_rem << 1);
}
}
}

n_rem = ((n_rem >> 12) & 0x000F);

return n_rem ^ 0x00;
}
113 changes: 113 additions & 0 deletions lib/lib_i2c/BlueRobotics_MS5837_Library/MS5837.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* Blue Robotics Arduino MS5837-30BA Pressure/Temperature Sensor Library
------------------------------------------------------------

Title: Blue Robotics Arduino MS5837-30BA Pressure/Temperature Sensor Library

Description: This library provides utilities to communicate with and to
read data from the Measurement Specialties MS5837-30BA pressure/temperature
sensor.

Authors: Rustom Jehangir, Blue Robotics Inc.
Adam Šimko, Blue Robotics Inc.

-------------------------------
The MIT License (MIT)

Copyright (c) 2015 Blue Robotics Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-------------------------------*/

#ifndef MS5837_H_BLUEROBOTICS
#define MS5837_H_BLUEROBOTICS

#include "Arduino.h"
#include <Wire.h>

class MS5837 {
public:
static const float Pa;
static const float bar;
static const float mbar;

static const uint8_t MS5837_30BA;
static const uint8_t MS5837_02BA;
static const uint8_t MS5837_UNRECOGNISED;

MS5837();

bool init(TwoWire &wirePort = Wire);
bool begin(TwoWire &wirePort = Wire); // Calls init()

/** Set model of MS5837 sensor. Valid options are MS5837::MS5837_30BA (default)
* and MS5837::MS5837_02BA.
*/
void setModel(uint8_t model);
uint8_t getModel();

/** Provide the density of the working fluid in kg/m^3. Default is for
* seawater. Should be 997 for freshwater.
*/
void setFluidDensity(float density);

/** The read from I2C takes up to 40 ms, so use sparingly is possible.
*/
void read();

/** Pressure returned in mbar or mbar*conversion rate.
*/
float pressure(float conversion = 1.0f);

/** Temperature returned in deg C.
*/
float temperature();

/** Depth returned in meters (valid for operation in incompressible
* liquids only. Uses density that is set for fresh or seawater.
*/
float depth();

/** Altitude returned in meters (valid for operation in air only).
*/
float altitude();

uint8_t crc4(uint16_t n_prom[]);

private:

//This stores the requested i2c port
TwoWire * _i2cPort;

uint16_t C[8];
uint32_t D1_pres, D2_temp;
int32_t TEMP;
int32_t P;
uint8_t _model;

float fluidDensity;

/** Performs calculations per the sensor data sheet for conversion and
* second order compensation.
*/
void calculate();

//uint8_t crc4(uint16_t n_prom[]);
};

#endif
2 changes: 2 additions & 0 deletions tasmota/include/i18n.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@
#define D_JSON_VERSION "Version"
#define D_JSON_VOLTAGE "Voltage"
#define D_JSON_VOLUME "Volume"
#define D_JSON_WATER_DEPTH "Water Depth"
#define D_JSON_WEIGHT "Weight"
#define D_JSON_WIFI "Wifi"
#define D_JSON_WIFI_MODE "Mode"
Expand Down Expand Up @@ -1001,6 +1002,7 @@ const char HTTP_SNS_MILLILITERS[] PROGMEM = "{s}%s " D_VOLUME "{
const char HTTP_SNS_GAS[] PROGMEM = "{s}%s " D_GAS "{m}%d " D_UNIT_PERCENT "LEL{e}";
const char HTTP_SNS_SOC[] PROGMEM = "{s}%s " D_SOC "{m}%d " D_UNIT_PERCENT "{e}";
const char HTTP_SNS_SOH[] PROGMEM = "{s}%s " D_SOH "{m}%d " D_UNIT_PERCENT "{e}";
const char HTTP_SNS_WATER_DEPTH[] PROGMEM = "{s}%s " D_WATER_DEPTH "{m}%s " D_UNIT_CENTIMETER "{e}";

const char HTTP_SNS_STANDARD_CONCENTRATION[] PROGMEM = "{s}%s " D_STANDARD_CONCENTRATION " %s " D_UNIT_MICROMETER "{m}%d " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}";
const char HTTP_SNS_ENVIRONMENTAL_CONCENTRATION[] PROGMEM = "{s}%s " D_ENVIRONMENTAL_CONCENTRATION " %s " D_UNIT_MICROMETER "{m}%d " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}";
Expand Down
Loading