1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-13 05:53:06 +01:00

separate boat data, integrate with web server

This commit is contained in:
andreas
2021-10-18 19:20:00 +02:00
parent fb20135718
commit 3bbd9ef965
6 changed files with 190 additions and 178 deletions

View File

@@ -0,0 +1,54 @@
#include "GwBoatData.h"
GwBoatItem *GwBoatData::find(String name,bool doCreate){
GwBoatItemMap::iterator it;
if ((it=values.find(name)) != values.end()){
return it->second;
}
if (! doCreate) return NULL;
GwBoatItem *ni=new GwBoatItem();
values[name]=ni;
return ni;
}
GwBoatData::GwBoatData(GwLog *logger){
}
GwBoatData::~GwBoatData(){
GwBoatItemMap::iterator it;
for (it=values.begin() ; it != values.end();it++){
delete it->second;
}
}
void GwBoatData::update(String name,const char *value){
GwBoatItem *i=find(name);
i->update(value);
}
void GwBoatData::update(String name, String value){
GwBoatItem *i=find(name);
i->update(value);
}
void GwBoatData::update(String name, long value){
GwBoatItem *i=find(name);
i->update(value);
}
void GwBoatData::update(String name, double value){
GwBoatItem *i=find(name);
i->update(value);
}
void GwBoatData::update(String name, bool value){
GwBoatItem *i=find(name);
i->update(value);
}
String GwBoatData::toJson() const {
long minTime=millis() - maxAge;
DynamicJsonDocument json(800);
GwBoatItemMap::const_iterator it;
for (it=values.begin() ; it != values.end();it++){
if (it->second->isValid(minTime)){
json[it->first]=it->second->getValue();
}
}
String buf;
serializeJson(json,buf);
return buf;
}

67
lib/boatData/GwBoatData.h Normal file
View File

@@ -0,0 +1,67 @@
#ifndef _GWBOATDATA_H
#define _GWBOATDATA_H
#include "GwLog.h"
#include <ArduinoJson.h>
#include <Arduino.h>
#include <map>
#define GW_BOAT_VALUE_LEN 32
class GwBoatItem{
private:
char value[GW_BOAT_VALUE_LEN];
long lastSet;
void uls(){
lastSet=millis();
}
public:
const char * getValue() const{return value;}
long getLastSet() const {return lastSet;}
bool isValid(long minTime) const {return lastSet > minTime;}
GwBoatItem(){
value[0]=0;
lastSet=-1;
}
void update(String nv){
strncpy(value,nv.c_str(),GW_BOAT_VALUE_LEN-1);
uls();
}
void update(const char * nv){
strncpy(value,nv,GW_BOAT_VALUE_LEN-1);
}
void update(long nv){
ltoa(nv,value,10);
uls();
}
void update(bool nv){
if (nv) strcpy_P(value,PSTR("true"));
else strcpy_P(value,PSTR("false"));
uls();
}
void update(double nv){
dtostrf(nv,3,7,value);
uls();
}
void invalidate(){
lastSet=0;
}
};
class GwBoatData{
typedef std::map<String,GwBoatItem*> GwBoatItemMap;
private:
const long maxAge=60000; //max age for valid data in ms
GwLog *logger;
GwBoatItemMap values;
GwBoatItem *find(String name, bool doCreate=true);
public:
GwBoatData(GwLog *logger);
~GwBoatData();
void update(String name,const char *value);
void update(String name, String value);
void update(String name, long value);
void update(String name, bool value);
void update(String name, double value);
String toJson() const;
};
#endif

View File

@@ -0,0 +1,352 @@
/*
N2kDataToNMEA0183.cpp
Copyright (c) 2015-2018 Timo Lappalainen, Kave Oy, www.kave.fi
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.
*/
#include <N2kMessages.h>
#include <NMEA0183Messages.h>
#include <math.h>
#include "N2kDataToNMEA0183.h"
const double radToDeg = 180.0 / M_PI;
//*****************************************************************************
void N2kDataToNMEA0183::HandleMsg(const tN2kMsg &N2kMsg) {
switch (N2kMsg.PGN) {
case 127250UL: HandleHeading(N2kMsg);
case 127258UL: HandleVariation(N2kMsg);
case 128259UL: HandleBoatSpeed(N2kMsg);
case 128267UL: HandleDepth(N2kMsg);
case 129025UL: HandlePosition(N2kMsg);
case 129026UL: HandleCOGSOG(N2kMsg);
case 129029UL: HandleGNSS(N2kMsg);
case 130306UL: HandleWind(N2kMsg);
case 128275UL: HandleLog(N2kMsg);
case 127245UL: HandleRudder(N2kMsg);
case 130310UL: HandleWaterTemp(N2kMsg);
}
}
//*****************************************************************************
void N2kDataToNMEA0183::loop() {
unsigned long now=millis();
if ( now < (lastLoopTime + 100)) return;
lastLoopTime=now;
SendRMC();
if ( (LastHeadingTime + 2000) < now ) Heading = N2kDoubleNA;
if ( (LastCOGSOGTime + 2000) < now ) {
COG = N2kDoubleNA;
SOG = N2kDoubleNA;
}
if ( (LastPositionTime + 4000) < now ) {
Latitude = N2kDoubleNA;
Longitude = N2kDoubleNA;
}
if ( ( LastWindTime + 2000) < now ) {
AWS = N2kDoubleNA;
AWA = N2kDoubleNA;
TWS = N2kDoubleNA;
TWA = N2kDoubleNA;
TWD = N2kDoubleNA;
}
boatData->update(F("Latitude"),Latitude);
boatData->update(F("Longitude"),Longitude);
boatData->update(F("Altitude"),Altitude);
boatData->update(F("Heading"),Heading * radToDeg);
boatData->update(F("COG"),COG * radToDeg);
boatData->update(F("SOG"),SOG * 3600.0/1852.0);
boatData->update(F("STW"),STW * 3600.0/1852.0);
boatData->update(F("AWS"),AWS * 3600.0/1852.0);
boatData->update(F("TWS"),TWS * 3600.0/1852.0);
boatData->update(F("MaxAws"),MaxAws * 3600.0/1852.0);
boatData->update(F("MaxTws"),MaxTws * 3600.0/1852.0);
boatData->update(F("AWA"),AWA * radToDeg);
boatData->update(F("TWA"),TWA * radToDeg);
boatData->update(F("TWD"),TWD * radToDeg);
boatData->update(F("TripLog"),TripLog / 1825.0);
boatData->update(F("Log"),Log / 1825.0);
boatData->update(F("RudderPosition"),RudderPosition * radToDeg);
boatData->update(F("WaterTemperature"),KelvinToC(WaterTemperature)) ;
boatData->update(F("WaterDepth"),WaterDepth);
boatData->update(F("Variation"),Variation *radToDeg);
boatData->update(F("GPSTime"),SecondsSinceMidnight);
boatData->update(F("DaysSince1970"),(long)DaysSince1970);
}
//*****************************************************************************
void N2kDataToNMEA0183::SendMessage(const tNMEA0183Msg &NMEA0183Msg) {
if ( pNMEA0183 != 0 ) pNMEA0183->SendMessage(NMEA0183Msg);
if ( SendNMEA0183MessageCallback != 0 ) SendNMEA0183MessageCallback(NMEA0183Msg);
}
//*****************************************************************************
void N2kDataToNMEA0183::HandleHeading(const tN2kMsg &N2kMsg) {
unsigned char SID;
tN2kHeadingReference ref;
double _Deviation = 0;
double _Variation;
tNMEA0183Msg NMEA0183Msg;
if ( ParseN2kHeading(N2kMsg, SID, Heading, _Deviation, _Variation, ref) ) {
if ( ref == N2khr_magnetic ) {
if ( !N2kIsNA(_Variation) ) Variation = _Variation; // Update Variation
if ( !N2kIsNA(Heading) && !N2kIsNA(Variation) ) Heading -= Variation;
}
LastHeadingTime = millis();
if ( NMEA0183SetHDG(NMEA0183Msg, Heading, _Deviation, Variation) ) {
SendMessage(NMEA0183Msg);
}
}
}
//*****************************************************************************
void N2kDataToNMEA0183::HandleVariation(const tN2kMsg &N2kMsg) {
unsigned char SID;
tN2kMagneticVariation Source;
uint16_t DaysSince1970;
ParseN2kMagneticVariation(N2kMsg, SID, Source, DaysSince1970, Variation);
}
//*****************************************************************************
void N2kDataToNMEA0183::HandleBoatSpeed(const tN2kMsg &N2kMsg) {
unsigned char SID;
double WaterReferenced;
double GroundReferenced;
tN2kSpeedWaterReferenceType SWRT;
if ( ParseN2kBoatSpeed(N2kMsg, SID, WaterReferenced, GroundReferenced, SWRT) ) {
tNMEA0183Msg NMEA0183Msg;
STW=WaterReferenced;
double MagneticHeading = ( !N2kIsNA(Heading) && !N2kIsNA(Variation) ? Heading + Variation : NMEA0183DoubleNA);
if ( NMEA0183SetVHW(NMEA0183Msg, Heading, MagneticHeading, WaterReferenced) ) {
SendMessage(NMEA0183Msg);
}
}
}
//*****************************************************************************
void N2kDataToNMEA0183::HandleDepth(const tN2kMsg &N2kMsg) {
unsigned char SID;
double DepthBelowTransducer;
double Offset;
double Range;
if ( ParseN2kWaterDepth(N2kMsg, SID, DepthBelowTransducer, Offset, Range) ) {
WaterDepth=DepthBelowTransducer+Offset;
tNMEA0183Msg NMEA0183Msg;
if ( NMEA0183SetDPT(NMEA0183Msg, DepthBelowTransducer, Offset) ) {
SendMessage(NMEA0183Msg);
}
if ( NMEA0183SetDBx(NMEA0183Msg, DepthBelowTransducer, Offset) ) {
SendMessage(NMEA0183Msg);
}
}
}
//*****************************************************************************
void N2kDataToNMEA0183::HandlePosition(const tN2kMsg &N2kMsg) {
if ( ParseN2kPGN129025(N2kMsg, Latitude, Longitude) ) {
LastPositionTime = millis();
}
}
//*****************************************************************************
void N2kDataToNMEA0183::HandleCOGSOG(const tN2kMsg &N2kMsg) {
unsigned char SID;
tN2kHeadingReference HeadingReference;
tNMEA0183Msg NMEA0183Msg;
if ( ParseN2kCOGSOGRapid(N2kMsg, SID, HeadingReference, COG, SOG) ) {
LastCOGSOGTime = millis();
double MCOG = ( !N2kIsNA(COG) && !N2kIsNA(Variation) ? COG - Variation : NMEA0183DoubleNA );
if ( HeadingReference == N2khr_magnetic ) {
MCOG = COG;
if ( !N2kIsNA(Variation) ) COG -= Variation;
}
if ( NMEA0183SetVTG(NMEA0183Msg, COG, MCOG, SOG) ) {
SendMessage(NMEA0183Msg);
}
}
}
//*****************************************************************************
void N2kDataToNMEA0183::HandleGNSS(const tN2kMsg &N2kMsg) {
unsigned char SID;
tN2kGNSStype GNSStype;
tN2kGNSSmethod GNSSmethod;
unsigned char nSatellites;
double HDOP;
double PDOP;
double GeoidalSeparation;
unsigned char nReferenceStations;
tN2kGNSStype ReferenceStationType;
uint16_t ReferenceSationID;
double AgeOfCorrection;
if ( ParseN2kGNSS(N2kMsg, SID, DaysSince1970, SecondsSinceMidnight, Latitude, Longitude, Altitude, GNSStype, GNSSmethod,
nSatellites, HDOP, PDOP, GeoidalSeparation,
nReferenceStations, ReferenceStationType, ReferenceSationID, AgeOfCorrection) ) {
LastPositionTime = millis();
}
}
//*****************************************************************************
void N2kDataToNMEA0183::HandleWind(const tN2kMsg &N2kMsg) {
unsigned char SID;
tN2kWindReference WindReference;
tNMEA0183WindReference NMEA0183Reference = NMEA0183Wind_True;
double x, y;
double WindAngle, WindSpeed;
// double TWS, TWA, AWD;
if ( ParseN2kWindSpeed(N2kMsg, SID, WindSpeed, WindAngle, WindReference) ) {
tNMEA0183Msg NMEA0183Msg;
LastWindTime = millis();
if ( WindReference == N2kWind_Apparent ) {
NMEA0183Reference = NMEA0183Wind_Apparent;
AWA=WindAngle;
AWS=WindSpeed;
if (AWS > MaxAws) MaxAws=AWS;
}
if ( NMEA0183SetMWV(NMEA0183Msg, WindAngle*radToDeg, NMEA0183Reference , WindSpeed)) SendMessage(NMEA0183Msg);
if (WindReference == N2kWind_Apparent && SOG != N2kDoubleNA) { // Lets calculate and send TWS/TWA if SOG is available
AWD=WindAngle*radToDeg + Heading*radToDeg;
if (AWD>360) AWD=AWD-360;
if (AWD<0) AWD=AWD+360;
x = WindSpeed * cos(WindAngle);
y = WindSpeed * sin(WindAngle);
TWA = atan2(y, -SOG + x);
TWS = sqrt(( y*y) + ((-SOG+x)*(-SOG+x)));
if (TWS > MaxTws) MaxTws=TWS;
TWA = TWA * radToDeg +360;
if (TWA>360) TWA=TWA-360;
if (TWA<0) TWA=TWA+360;
NMEA0183Reference = NMEA0183Wind_True;
if ( NMEA0183SetMWV(NMEA0183Msg, TWA, NMEA0183Reference , TWS)) SendMessage(NMEA0183Msg);
if ( !NMEA0183Msg.Init("MWD", "GP") ) return;
if ( !NMEA0183Msg.AddDoubleField(AWD) ) return;
if ( !NMEA0183Msg.AddStrField("T") ) return;
if ( !NMEA0183Msg.AddDoubleField(AWD) ) return;
if ( !NMEA0183Msg.AddStrField("M") ) return;
if ( !NMEA0183Msg.AddDoubleField(TWS/0.514444) ) return;
if ( !NMEA0183Msg.AddStrField("N") ) return;
if ( !NMEA0183Msg.AddDoubleField(TWS) ) return;
if ( !NMEA0183Msg.AddStrField("M") ) return;
SendMessage(NMEA0183Msg);
TWA=TWA/radToDeg;
}
}
}
//*****************************************************************************
void N2kDataToNMEA0183::SendRMC() {
if ( NextRMCSend <= millis() && !N2kIsNA(Latitude) ) {
tNMEA0183Msg NMEA0183Msg;
if ( NMEA0183SetRMC(NMEA0183Msg, SecondsSinceMidnight, Latitude, Longitude, COG, SOG, DaysSince1970, Variation) ) {
SendMessage(NMEA0183Msg);
}
SetNextRMCSend();
}
}
//*****************************************************************************
void N2kDataToNMEA0183::HandleLog(const tN2kMsg & N2kMsg) {
uint16_t DaysSince1970;
double SecondsSinceMidnight;
if ( ParseN2kDistanceLog(N2kMsg, DaysSince1970, SecondsSinceMidnight, Log, TripLog) ) {
tNMEA0183Msg NMEA0183Msg;
if ( !NMEA0183Msg.Init("VLW", "GP") ) return;
if ( !NMEA0183Msg.AddDoubleField(Log / 1852.0) ) return;
if ( !NMEA0183Msg.AddStrField("N") ) return;
if ( !NMEA0183Msg.AddDoubleField(TripLog / 1852.0) ) return;
if ( !NMEA0183Msg.AddStrField("N") ) return;
SendMessage(NMEA0183Msg);
}
}
//*****************************************************************************
void N2kDataToNMEA0183::HandleRudder(const tN2kMsg & N2kMsg) {
unsigned char Instance;
tN2kRudderDirectionOrder RudderDirectionOrder;
double AngleOrder;
if ( ParseN2kRudder(N2kMsg, RudderPosition, Instance, RudderDirectionOrder, AngleOrder) ) {
if(Instance!=0) return;
tNMEA0183Msg NMEA0183Msg;
if ( !NMEA0183Msg.Init("RSA", "GP") ) return;
if ( !NMEA0183Msg.AddDoubleField(RudderPosition * radToDeg) ) return;
if ( !NMEA0183Msg.AddStrField("A") ) return;
if ( !NMEA0183Msg.AddDoubleField(0.0) ) return;
if ( !NMEA0183Msg.AddStrField("A") ) return;
SendMessage(NMEA0183Msg);
}
}
//*****************************************************************************
void N2kDataToNMEA0183::HandleWaterTemp(const tN2kMsg & N2kMsg) {
unsigned char SID;
double OutsideAmbientAirTemperature;
double AtmosphericPressure;
if ( ParseN2kPGN130310(N2kMsg, SID, WaterTemperature, OutsideAmbientAirTemperature, AtmosphericPressure) ) {
tNMEA0183Msg NMEA0183Msg;
if ( !NMEA0183Msg.Init("MTW", "GP") ) return;
if ( !NMEA0183Msg.AddDoubleField(KelvinToC(WaterTemperature))) return;
if ( !NMEA0183Msg.AddStrField("C") ) return;
SendMessage(NMEA0183Msg);
}
}

View File

@@ -0,0 +1,123 @@
/*
N2kDataToNMEA0183.h
Copyright (c) 2015-2018 Timo Lappalainen, Kave Oy, www.kave.fi
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.
*/
#include <NMEA0183.h>
#include <NMEA2000.h>
#include <GwLog.h>
#include <GwBoatData.h>
//------------------------------------------------------------------------------
class N2kDataToNMEA0183 : public tNMEA2000::tMsgHandler {
public:
using tSendNMEA0183MessageCallback=void (*)(const tNMEA0183Msg &NMEA0183Msg);
protected:
static const unsigned long RMCPeriod=500;
double Latitude;
double Longitude;
double Altitude;
double Variation;
double Heading;
double COG;
double SOG;
double STW;
double TWS;
double TWA;
double TWD;
double AWS;
double AWA;
double AWD;
double MaxAws;
double MaxTws;
double RudderPosition;
double WaterTemperature;
double WaterDepth;
uint32_t TripLog;
uint32_t Log;
uint16_t DaysSince1970;
double SecondsSinceMidnight;
unsigned long LastHeadingTime;
unsigned long LastCOGSOGTime;
unsigned long LastPositionTime;
unsigned long LastPosSend;
unsigned long LastWindTime;
unsigned long NextRMCSend;
unsigned long lastLoopTime;
GwLog *logger;
GwBoatData *boatData;
tNMEA0183 *pNMEA0183;
tSendNMEA0183MessageCallback SendNMEA0183MessageCallback;
protected:
void HandleHeading(const tN2kMsg &N2kMsg); // 127250
void HandleVariation(const tN2kMsg &N2kMsg); // 127258
void HandleBoatSpeed(const tN2kMsg &N2kMsg); // 128259
void HandleDepth(const tN2kMsg &N2kMsg); // 128267
void HandlePosition(const tN2kMsg &N2kMsg); // 129025
void HandleCOGSOG(const tN2kMsg &N2kMsg); // 129026
void HandleGNSS(const tN2kMsg &N2kMsg); // 129029
void HandleWind(const tN2kMsg &N2kMsg); // 130306
void HandleLog(const tN2kMsg &N2kMsg); // 128275
void HandleRudder(const tN2kMsg &N2kMsg); // 127245
void HandleWaterTemp(const tN2kMsg &N2kMsg); // 130310
void SetNextRMCSend() { NextRMCSend=millis()+RMCPeriod; }
void SendRMC();
void SendMessage(const tNMEA0183Msg &NMEA0183Msg);
public:
N2kDataToNMEA0183(GwLog * logger, GwBoatData *boatData, tNMEA2000 *NMEA2000, tNMEA0183 *NMEA0183) : tNMEA2000::tMsgHandler(0,NMEA2000) {
SendNMEA0183MessageCallback=0;
pNMEA0183=NMEA0183;
Latitude=N2kDoubleNA; Longitude=N2kDoubleNA; Altitude=N2kDoubleNA;
Variation=N2kDoubleNA; Heading=N2kDoubleNA; COG=N2kDoubleNA; SOG=N2kDoubleNA;
SecondsSinceMidnight=N2kDoubleNA; DaysSince1970=N2kUInt16NA;
LastPosSend=0;
lastLoopTime=0;
NextRMCSend=millis()+RMCPeriod;
LastHeadingTime=0;
LastCOGSOGTime=0;
LastPositionTime=0;
LastWindTime=0;
this->logger=logger;
this->boatData=boatData;
}
void HandleMsg(const tN2kMsg &N2kMsg);
void SetSendNMEA0183MessageCallback(tSendNMEA0183MessageCallback _SendNMEA0183MessageCallback) {
SendNMEA0183MessageCallback=_SendNMEA0183MessageCallback;
}
void loop();
};