This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MPU-9250 and BME280 to OpenPlotter
#5
PaddyB, amazing, it worked on the first try! :-) Nearly, literally just had to downgrade to ArduinoJson.h V5 library instead of the V6 :-)

I also added a line to get the humidity reading for the BME280, and a correction to have the temperature in Kelvin. For BMP280 just remove this line and replace the library by the BMP one.
I didn't have time to go through the code that now is a mix between Nanobaro and PaddyB codes with some redundancy, but it runs very smooth as it is, SignalK is parsing all the data and exporting to all my devices.

It is a great solution for anyone having an I2C IMU and want to have a BME/BMP280 I2C on a good location running long cable length. Just run an USB cable to the arduino, it will act as power and serial data.

Here is the code if someone wanna use it as it is or try to make it more "pretty":

Code:
#include <Adafruit_BME280.h>
#include <Wire.h>
#include "ArduinoJson.h"

#define NMEA_TALKER_ID "WI" // Weather Instruments
#define NMEA_DELAY 1 // Send data every 1 seconds

// meters above mean sea level
#define ALTITUDE 1
// How many samples to take per iteration
#define PRESS_OVERSAMPLING 3

//create a new Adafruit_BME280 class object called pressure.
Adafruit_BME280 pressure;

const byte buff_len = 90;
char CRCbuffer[buff_len];

byte nmea_crc(String msg) {
 // NMEA CRC: XOR each byte with previous for all chars between '$' and '*'
 char c;
 int i;
 byte crc = 0;
 for (i = 0; i < buff_len; i++) {
   crc ^= msg.charAt(i); // XOR
 }
 return crc;
}

void setup() {
 // put your setup code here, to run once:

 Serial.begin(4800);

 if (!pressure.begin(0x76) ) {
   nmea_txt("BMP280 init fail");
   exit(1);
 }
 nmea_txt("Nanobaro ready.");

}

void nmea_send(String sentence, String params) {
 String msg = String(NMEA_TALKER_ID) + sentence + params;

 msg.toCharArray(CRCbuffer, sizeof(CRCbuffer)); // put complete string into CRCbuffer
 int crc = nmea_crc(CRCbuffer);

 if (crc < 16) msg += "0"; // pad with leading 0
 String hexcrc = String(crc, HEX);
 hexcrc.toUpperCase();
 Serial.println("$" + msg + "*" + hexcrc);
}

void nmea_txt(String text) {
 nmea_send("TXT", ",01,01,01," + text);
}

void sendSigK(String sigKey, float data)

{
  DynamicJsonBuffer jsonBuffer;
  String deltaText;

  //  build delta message
  JsonObject &delta = jsonBuffer.createObject();

  //updated array
  JsonArray &updatesArr = delta.createNestedArray("updates");
  JsonObject &thisUpdate = updatesArr.createNestedObject();   //Json Object nested inside delta [...
  JsonArray &values = thisUpdate.createNestedArray("values"); // Values array nested in delta[ values....
  JsonObject &thisValue = values.createNestedObject();
  thisValue["path"] = sigKey;
  thisValue["value"] = data;

  thisUpdate["Source"] = "ESP32";

  delta.printTo(Serial);
  Serial.println();
}

void loop() {
 
 float temp, press, hum;
   
 temp = pressure.readTemperature();
 press = pressure.readPressure();
 hum = pressure.readHumidity();
 
sendSigK("environment.outside.pressure", press);
sendSigK("environment.outside.temperature", temp + 273.5 );
sendSigK("environment.outside.humidity", hum/100 );

 delay(NMEA_DELAY * 1000);
}
Reply


Messages In This Thread
RE: MPU-9250 and BME280 to OpenPlotter - by Barentsailor - 2020-06-17, 07:56 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)