Home › Forums › Environmental Sensors › MayFly Capability with Northern Widget Walrus Sensor › Reply To: MayFly Capability with Northern Widget Walrus Sensor
2020-10-29 at 11:28 AM
#14739
Alright, here’s something. I can’t promise it *works*, but I’d like to see the results.
C++
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
#include <Arduino.h> #include <Wire.h> #define DEVICE_I2C_ADDRESS \ 0x4D // I *think* this is the address it's using for itself // https://github.com/NorthernWidget-Skunkworks/Project-Walrus/blob/b5d9cc1fdfe2cf16fb5048a1f9ff0b27253c3a94/Firmware/Walrus_I2C_5BA/Walrus_I2C_5BA.ino#L155 // A helpful union for flipping between bytes to longs typedef union leFrame { byte Byte[4]; // 4 bytes will occupy 4 bytes uint32_t uInt32; // a single float occupies 4 bytes } leFrame; void setup() { Wire.begin(); // join i2c bus (address optional for master) Serial.begin(9600); // start serial for output // NOTE: Something may have to happen here to "start the thing up" // I can't tell if they intend that or not } void loop() { // It looks like the control register (0x00) is being set to 0x7F // (0b1111111) when values are being written (ie, values aren't ready) and // to 0x80 (0b10000000) when it's ready. // https://github.com/NorthernWidget-Skunkworks/Project-Walrus/blob/b5d9cc1fdfe2cf16fb5048a1f9ff0b27253c3a94/Firmware/Walrus_I2C_5BA/Walrus_I2C_5BA.ino#L242 byte control_flag = 0x7f; while (control_flag == 0x7f) { Wire.beginTransmission( DEVICE_I2C_ADDRESS); // select device with "beginTransmission()" Wire.write(0x00); // select starting register with "write()" Wire.endTransmission(); // end write operation, as we just wanted to // select the starting register Wire.requestFrom(DEVICE_I2C_ADDRESS, 1); // select number of bytes to get from the device // (1 byte in this case) control_flag = Wire.read(); // read from the starting register } // We'll try reading all the registers at once sequentially instead of // reading one after the other // the registers are loaded starting on line 242: // https://github.com/NorthernWidget-Skunkworks/Project-Walrus/blob/master/Firmware/Walrus_I2C_5BA/Walrus_I2C_5BA.ino#L242 Wire.beginTransmission( DEVICE_I2C_ADDRESS); // select device with "beginTransmission()" Wire.write(0x02); // select starting register with "write()" // the first data register is pressure starting in register 0x02 Wire.endTransmission(); // end write operation, as we just wanted to select // the starting register // select number of bytes to get from the device // + 4 for pressure in microbars (ie mbar*1000) // + 4 for temp0 - This is the temperature from the MCP9808 (*10000.0) // + 4 for temp1 - This is the temperature from the MS5803 (*10000.0) Wire.requestFrom(DEVICE_I2C_ADDRESS, 12); // We won't bother to read the next 8 bytes of unchanging sensor // information: // + 2 for model (uint16_t) // + 2 for group id (uint16_t) // + 2 for "INDID" ?dummy? (uint16_t) // + 2 for firmware id (uint16_t)) // my helpful union leFrame fram; // NOTE: may need to flop endinaness for the values. // if getting garbage, try switching all for loops to // 'for (int8_t i = 0; i<4; i++)' // get pressure from the first 4 bytes, reversing order/endianness for (int8_t i = 4; i; i--) { fram.Byte[i] = Wire.read(); } float pressure = fram.uInt32 / 1000; // get MCP9808 temp from the next 4 bytes, reversing order/endianness for (int8_t i = 4; i; i--) { fram.Byte[i] = Wire.read(); } float temp_mcp9808 = fram.uInt32 / 10000; // get MS5803 temp from the last 4 bytes, reversing order/endianness for (int8_t i = 4; i; i--) { fram.Byte[i] = Wire.read(); } float temp_ms5803 = fram.uInt32 / 10000; // print the results Serial.print("MS5803 Pressure: "); Serial.print(pressure); Serial.println(" mbar"); Serial.print("MCP9808 Temperature: "); Serial.print(temp_mcp9808); Serial.println(" °C"); Serial.print("MS5803 Temperature: "); Serial.print(temp_ms5803); Serial.println(" °C"); // wait 5s and then repeat Serial.println("---"); delay(5000); } |