Skip to content

Latest commit

 

History

History
999 lines (636 loc) · 35.5 KB

README.adoc

File metadata and controls

999 lines (636 loc) · 35.5 KB

NMEA2000 library for C++

Object oriented NMEA2000 library for Teensy, Arduino, MBED and Rasberry type Boards. These board types has been tested, but library may work also in other systems.

Library gives you easy way to make different kind of NMEA2000 bus devices like NMEA2000→PC interface (like Actisense NGT1), NMEA0183→NMEA2000 converter, sensor (e.g. temperature, wind, engine) node.

Library fulfills NMEA 2000 mandatory functions and behaviour, so it looks a bit complex inside. With default settings it requires about 23 kB rom and 3.3kB RAM in normal operation. So I prefer to use Teensy 3.2, 3.5, 3.6, 4.0, 4.1 or ESP32 board. Arduino Due board works fine, but is physically bigger and eats more power. Arduino Mega board is ok for simple projects. Arduino Uno board has too little memory for full functionality. See "Memory requirements" later.

If you are not sure what board would be best, I personally prefer Teensy boards. They are smaller size, more powerfull and uses less power than traditional Arduino Mega or Due boards. Also Arduino Mega double is only 4 byte, while some NMEA2000 data uses 8 byte which means that with Mega you will loose accuracy.

To use Teensy 4.0/4.1 boards internal CAN you need also NMEA2000_Teensyx library. CAN library is included to the code so you do not need any extra CAN library with this. NMEA2000_Teensyx library will replace NMEA2000_teensy library in future. You can already start to use it with all Teensy boards by forcing it with define (see NMEA2000_CAN.h comment). For critical projects I prefer to use old library until this has been running under tests for a while.

To use a Teensy 3.1/3.2 or 3.5/3.6 board internal CAN with a MCP2562 or MCP2551 CAN-bus transeiver (or similar, see FlexCAN documentation), you will need the NMEA2000_teensy library contributed by (Thomas Sarlandie) and the FlexCAN library. FlexCan library is already included in Teensyduino extension. In order to use my fork, you have to remove FlexCAN library from C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\FlexCAN

To use ESP32 boards you need also NMEA2000_esp32 library. ESP32 CAN driver by Thomas Barth has been implemented inside NMEA2000_esp32 so you do not need other drivers for that.

To use Arduino Due board internal CAN you will also need NMEA2000_due library and due_can library, which you can download from https://github.com/ttlappalainen/due_can

To use Arduino boards with MCP2515 CAN bus controller, you will also need NMEA2000_mcp library and mcp_can library. Use mcp_can library found on https://github.com/ttlappalainen/CAN_BUS_Shield, which is developed version of original mcp_can library. This allows also to use 8MHz clock and has been tested with Maple Mini. mcp_can library can be originally found on https://github.com/Seeed-Studio/CAN_BUS_Shield, but check library status has all improvements been implemented to Seeed-Studio. At least 18.09.2017 it was up to date.

To use avr processors internal CAN you need also NMEA2000_avr library and avr_can library, which you can download from https://github.com/thomasonw/avr_can

To use MBED boards you need also NMEA2000_mbed library and other related libraries. See origin for MBED port on https://github.com/thomasonw/NMEA2000_mbed

To use Raspberry boards you need also NMEA2000_socketCAN library. NMEA2000_socketCAN has been originally forked from https://github.com/thomasonw/NMEA2000_socketCAN

Thanks

I thank Kees Verruijt for his excellent CanBoat project and Dr Andrew Mason for OpenSkipper project. I originally started to learn NMEA 2000 via OpenSkipper project, which development I continued. PGN library on OpenSkipper and so on this library is mostly based on Kees work. Also it appeared that some code parts are probably originally from CanBoat even I have found them from other sources. Actisense message format handling I have learned from OpenSkipper project.

I also thank for anybody who has extended library with new PGNs or processor support.

Changes

04.03.2021

  • Fix for engine discrete status 2.

10.02.2021

  • Added definition to NMEA2000_CAN.h to select CAN device for Arduino DUE

06.02.2021

  • Added handlers for PGN 129540 GNSS satellites in view

  • Added new necessary types

  • Added SetByte for tN2kMsg class.

  • Added PGN 129540 handler for test example DataDisplay2

  • Redesigned example MessageSender. Now it is more usefull for testing, since each message can be individually enabled/disabled.

  • Added PGN 129540 handler for test example MessageSender

15.01.2021

  • Added better inline ParseN2kEngineDynamicParam to N2kMessages

  • Copyright update

05.01.2021

  • Added better engine discrete status handling to N2kMessages

08.10.2020

  • Fix for 'memcpy' overflow for float reading on N2kMsg.cpp

23.08.2020

  • Add support for Teensy 4.x

05.08.2020

  • Add support for Windlass Network Messages PGN 128776, 128777 & 128778 by Paul Reeve

16.07.2020

  • Fixed setting buffered frame length to min instead of max length on NMEA2000.cpp SendFrame

  • Added NMEA2000::IsProprietaryMessage

  • Fixed AddGroupFunctionHandler to allow to add handlers after Open

  • Added RemoveGroupFunctionHandler to allow dynamically remove handlers on runtime.

  • Added proprietary message handling to N2kGroupFunction.cpp

  • Improved N2kDeviceList HandleMsg to see devices, which has been off.

  • Added FindDeviceByProduct to N2kDeviceList

  • Extented list of fast packet messages to all known fast packet messages. This also leads to situation that all fast packet messages are known messages and may effect to setting SetHandleOnlyKnownMessages behaviour. To limit known messages, one should have provided list with SetFastPacketMessages.

27.06.2020

  • Fixed PGN 130314 and PGN 130315 pressure type to signed.

  • Added pressure enums.

28.01.2019

  • NOTE! Compatibility change! PGN 127506 TimeRemaining should have been in seconds as it is SI unit. If you have used that on your code, provide value in seconds. Parse function also return in seconds now.

  • Added Capacity parameter to PGN 127506. I did not made overwrite function without due to above change. So it hopefully wakes you up about the changes.

  • Fixed MaretronProprietary test on N2kMaretron.cpp

  • Some comment fixes.

20.10.2019

  • Added support for Maretron proprietary PGNs 130823,65286, 65287. See N2kMaretron.h. Thanks to Vassilis Bourdakis.

  • Added proprietary fast packet message test as default. So now proprietary fast packet messages will be automatically parsed right without need to use ExtendFastPacketMessages

  • Fixed strings on product information. Unused characters will be filled with 0xff.

17.10.2019

  • Fixed Rate of turn (PGN 127251) and AIS position report ROT (PGN 129038) value multiplier.

  • Separated N2k enums to own file N2kTypes.h

  • Fixed PGNs 127251, 127258, 130576 length for sending by padding with reserved. Some devices refuces to listen message, if length is wrong.

07.07.2019

  • Fixed: PGN 127513 was accidently defined also as single frame message.

07.07.2019

  • Added: Trip fuel consumption, engine PGN 127497.

  • Added: More PGN:s to default fast packet list.

  • Updated: Examples MessageSender and DataDisplay2, which can be used for testing messages.

03.03.2019

  • Added: Charger status PGN 127507.

  • Added: Possibility to delay ISO address claim. Due to some devices CAN priority, it was sent too fast.

  • Added: Count for tDeviceList

14.01.2019

  • Fix: Device list handler fix. Some tools may use source 254 and that was checked wrong.

  • Fix: Message priorities.

04.08.2018

  • Fix: NMEA2000.h/NMEA2000.cpp, just in parameter N2kSource type change by mrbubble62

18.07.2018

  • Added: N2kMessagesEnumToStr.h "exhaust gas" string for temperature sources by mrbubble62.

09.05.2018

  • Fix: NMEA2000_CAN.h, make ESP32 work with ESP-IDF framework, by Sarfata

10.04.2018

  • Fix: Handling of humidity PGN 130313.

06.04.2018

  • Added: Support for ESP32. See also NMEA2000_esp32

  • Fix: Changed some names to avoid conflicts with some stupid define macros on some environments.

04.04.2018

  • Fix: NextHeartbeatSentTime initial value.

  • Added: New example NMEA2000ToNMEA0183. This has been tested on RPi3B, Arduino DUE, Arduino Mega, Teensy.

26.03.2018

  • Added: Strings for Magnetic Variation enum by mrbubble62

13.03.2018

  • Added: PGN129033 Local offset

23.02.2018

  • Port config ability for SockeCAN by Al Thomason

16.02.2018

  • SetN2kSource for other devices by jpilet

29.01.2018

  • Added: PGN130576 Small Craft Status / Trim Tab Position definition by Nicholas Agro

15.01.2018

  • Fix: ParseN2kPGN129284, Index was not initialized to 0, which caused unpredictable read.

07.01.2018

  • Fix: Fast packet sequence counter must be related to PGN.

  • Fix: TP message priorities.

  • Fix: On SetDeviceInformationInstances we need to send ISO address claim, not start adress claim.

  • Fix: Do not respond any queries during address claim.

  • Fix: Fixed some messages default priority.

  • Fix: Responce to Complex Group Function requests.

  • Fix: Now compiles with different compiler definitions defined on NMEA2000_CompilerDefns.h

  • Added: Support for sending messages by using Transport Protocol. This is mandatory for NMEA 2000

  • Updated: License to 2018

02.12.2017

  • Changes effect only use of tN2kDeviceList and AttachMsgHandler callbacks.

  • Added FindDeviceByIDs to tN2kDeviceList

  • Fix: Devicelist did not handle right, if device changed its address higher.

  • Fix: PNG message handler order problem on tNMEA2000::AttachMsgHandler(tMsgHandler *_MsgHandler);

  • Fix: If there was NMEA2000 library device with same name, they handshaked both addresses to null. The problem still appears, if both devices will be started at same time. I need to add some random start delay

  • Cleaned spaces from end of lines on updated code.

29.11.2017

  • Added PGN 128000 Leeway to N2kMessages.h/.cpp

  • Fix: Spelling WaterRefereced → WaterReferenced on N2kMessages.h/.cpp

27.11.2017

  • Fix some build errors due to missing include statements (did not affect Arduino, only other platforms)

14.11.2017

  • Fix: Default responces to Complex Group Function.

09.11.2017

  • Added library.json, thanks for ronzeiller.

31.10.2017

  • Fix: fill unused chars on fastpacket messages with 0xff

  • Fix: fill unused chars on message fixed sized strings with 0xff. One sample is e.g. PGN 126996, Product Information.

14.10.2017

  • Changed tActisenseReader class to handle also Actisense N2k request message types. This type will be used by applications, which sends data through NGT-1.

  • Updated related examples ActisenseListener and ActisenseListenerSender.

  • Renamed Min/Max to N2kMin/N2kMax, since Due code had definitions for Min/Max

12.10.2017

  • Added PGN validity check to the SendMsg()

  • Changed max/min → own Max/Min. max/min defines does not exist on other systems and Arduino does not have std <Algorithm> for all boards.

10.10.2017

  • Fix: PGN 126992 TimeSource handling.

  • Fix: Added missing wind reference type.

02.10.2017

  • Added range parameter for depth PGN 128267.

  • Added millis() time stamp to N2kMsg clear text Print.

  • Now responces also Group Function request for PGN lists PGN 126464

  • Now responces also Group Function request for Product Information PGN 126996

  • Now responces also Group Function request for Configuration Information PGN 126998

  • Fix: spelling Sertification → Certification

  • Fix: on changing SystemInstance reseted DeviceInstance

  • Fix: "Group function" responces according to tests with certified Airmar DST800

  • Fix: Address claiming could go up to 253 and did not went to "cannot claim". Now, if address cannot be claimed, goes to "cannot claim state" and prevents all message output except ISO address claim.

  • Fix: fast packet response for less than 7 data bytes caused two frames.

26.09.2017

  • Fixed PGN 128259 parser SOG data type.

  • DataDisplay2 example update.

31.08.2017

  • Support for changing configuration information fields InstallationDescription1 and InstallationDescription2 on runtime e.g. with NMEA Reader. Meaning of those fields is define their "installation description". So if you have two engine monitor devices, you can set e.g. InstallationDescription1 field to "Port engine" for one and "Starboard engine" for other. So it is not necessary to hardcode those setting. Of coarse your code must support parameter saving to e.g. EEPROM as with other parameters (see ReadResetInstallationDescriptionChanged, ReadResetAddressChanged and ReadResetDeviceInformationChanged). I have example under construction for handling parameter changes.

  • Changed some indexes to size_t. This may effect compatibility, if you have used those functions.

31.07.2017

29.07.2017

  • Fixed setting device instances on N2kGroupFunctionDefaultHandlers

  • Fixed wind PGN 130306 output with reserved field.

  • New abstract class tNEMA2000:tMsgHandler and functions AttachMsgHandler/DetachMsgHandler. With these you can have multiple handlers. It also allow PGN specific handlers. See how it has been used on example DeviceAnalyzer. Other simple example is under construction.

  • New class tN2kDeviceList. See more on library reference and on example DeviceAnalyzer.

  • Improved message type checking. This will be done for every message, so speed in important. For Arduino Mega average test time was dropped from about 90 us to 9 us and for Teensy from 3.5 us to 0.9 us.

26.06.2017 Example updates

  • ActisenseListenerSender can be used to listen and send data to NMEA 2000 bus. This is almost same as TeensyActisenseListenerSender, but read and forward ports can be chosen with #define.

  • ActisenseListener uses now SetN2kCANReceiveFrameBufSize.

  • Removed FromPCToN2k. ActisenseListenerSender replaces this.

25.06.2017 Fix and cosmetic changes

  • ForwardStream initialization was accidentaly deleted

  • Clean code and more debug options.

22.06.2017 Fixes and cosmetic changes

  • Crashed, if ForwardStream was not defined. I accidentaly forgot to comment some debug code.

  • Definition of tDeviceInformation changed to fixed sized data so that compiler can not mix them.

  • Added debug definitions to avoid first bug.

  • Some cosmetic changes and tests.

19.06.2017 Changes due to different revisions of FlexCAN library for Teeansy boards. NOTE! You must update NMEA2000_Teensy library. I also forked and developed FlexCAN library from collin80 and also send pull request for him. Until updated there my fork has more features for use with NMEA2000 library.

13.06.2017 NOTE! Some compatibility changes.

  • !NOTE compatibility change. tProductInformation has been moved inside tNMEA2000 class. If you have defined tProductInformation to PROGMEM as in example BatteryMonitor, you need to change definition const tProductInformation…​ to const tNMEA2000::tProductInformation…​ See example BatteryMonitor.

  • Multi device support should work now. So you can show several devices on bus with single hw. See example MultiDevice.

  • !NOTE compatibility change. tDeviceInformation has been moved inside tNMEA2000 class. This was used only internally until 11.06.2017 release.

11.06.2017 Added NMEA 2000 mandatory features. Some bug fixes.

  • !NOTE compatibility change. PROGMEM configuration information did not work and actually wasted RAM. You should define each configuration information string alone as PROGMEM and call changed SetProgmemConfigurationInformation. See sample BatteryMonitor

  • Due to new mandatory features library requires more RAM and program memory. It is possible to squeeze requirements with compiler options. See more info on NMEA2000_CompilerDefns.h.

  • Added new class tN2kGroupFunctionHandler (N2kGroupFunction.h/.cpp) for NMEA 2000 group function (PGN 126208) handling. Group function can be used to e.g. to set "temperature instance" or "set temperature" fields on PGN 130316.

  • Added automatic Heartbeat, which is mandatory for certified NMEA 2000 devices. If you do not want it to be sent, you have to set heartbeat interval to 0. Added also function SetHeartbeatInterval, GetHeartbeatInterval and SendHeartbeat.

  • Added group function handling for PGN 60928 (ISO Address) and PGN 126993 (Heartbeat). Handlers can be found on N2kGroupFunctionDefaultHandlers module.

  • Added functions ReadResetDeviceInformationChanged, SetDeviceInformationInstances, GetDeviceInformation for checking, setting and reading device instance changes. See more info on document.

  • Added ISO Multi-packet handling. Changed logic on SetN2kCANBufMsg due this.

05.06.2017

  • Added PGN 130314 by sarfata.

  • Added PGN 127245 rudder parser

  • Fixed Device Information, last bit must be set to 1

  • Fixed response to ISO Address Claim request. Seems that all new devices respond allways with broadcast instead of caller address.

28.05.2017

  • Changed default NMEA2000 variable definition in NMEA2000_CAN.h to reference. So now it is possible to refer it in other modules with definition: extern tNMEA2000 &NMEA2000;

08.04.2017

  • Added Binary status report (PGN 127501) handling. See updated examples MessageSender and DataDisplay2.

09.03.2017

  • Added PGN 129539 support and PGN 129283, 129284 parsers by denravonska.

07.03.2017

  • Debug mode check for DeviceReady and ParseMessages.

05.03.2017

  • RPi socketCAN auto selection and MBED compiler portability fix by thomasonw.

08.02.2017

  • Fixed Heading PGN 127250 parsing

22.01.2017

  • Replace pointer casting with memcpy to avoid unaligned access, and add endian support. Thanks to denravonska.

  • Handle for PGN 65240 "Commanded address". E.g. diagnostic device may command your device to change address.

01.01.2017

  • Document and some example fixes to match library portability changes.

20.12.2016

  • Added support for PGN 126464L, PGN List (Transmit and Receive). Library will automatically respond to this message. You need only add message lists and call to methods ExtendTransmitMessages and/or ExtendReceiveMessages. See e.g. example TemperatureMonitor.

17.12.2016

  • Fixes to avoid compiler warnings

16.12.2016

  • Portability fixes. Thanks to denravonska and thomasonw!

  • NOTE! compatibility issue! There is no more default stream set on library constuctor. So in case you are using forwarding, you need to setup it (like in examples) NMEA2000.SetForwardStream(&Serial);

  • This reduces the Arduino dependency, allowing the library to more easily be used on other platforms. Check all changes under ttlappalainen#35

01.12.2016

  • License change to MIT for more permissive

  • Also some started to remove platform dependent code.

12.11.2016

  • PGN129025 parser added and some fixes by KimBP

11.11.2016

  • Added support for PGN 127258 - magnetic variation by adwuk.

18.10.2016

  • Added parsing for PGN 130311 by adwuk. Typo fix for system date comment by sarfata.

19.09.2016

  • Lot of testing behind - hopefully works now better.

  • NOTE! New method SetN2kCANSendFrameBufSize. Added buffer for frames to be sent. This takes more RAM and may be critical for low RAM systems.

  • If frame sending fails, system now buffers frames to be sent automatically and tries to resend them on next call for ParseMessages. With this feature it solved my problem that time to time my MFD could not receive important GNSS or SOG/GOG messages and informed error.

  • System now also has more reliable response to the Product Information ISO request (PGN 126998). Unfortunately if your system does not poll often enough incoming messages (ParseMessages), you still may loose the request itself. This is specially the case if you system spends some time reading sensors like 1-wire system. Even with 1-wire asynchronous read, it may spend 10 ms interrupts disabled. Within 10 ms there may be about 30 messages on bus.

  • New methods SetConfigurationInformation and SetProgmemConfigurationInformation. System can now also handle Configuration Information ISO request to (PGN 126998). Default configuration information is saved to PROGMEM.

  • NOTE! Reload also NMEA2000_due!

17.09.2016

  • Temporary fix for problem to respond product information ISO request.

12.09.2016

  • Thanks for people (usauerbrey, OzOns), who noted below problems

  • NOTE! If you are using NMEA2000_can, remember to update that too!

  • Fix for ISORequest handling. Now responds allways also for broadcasts.

  • Some fixes to avoid compiler warnings.

  • Fix for parsing PGN 127257/Attitude

09.08.2016

  • NOTE! Fixed PGN 130310, PGN 130311 and added SetHandleOnlyKnownMessages(), which effects backward compatibility. See below.

  • NOTE! On PGN 130310 and PGN 130311 description says that "Atmospheric pressure in Pascals. Use function mBarToPascal". There was scaling error and now they works like description. After update you have to provide value on Pascals and really use mBarToPascal, if you have your value in mBar.

  • NOTE! Added SetHandleOnlyKnownMessages(). If you have called SetForwardOnlyKnownMessages(true), library did not handle unknown messages. After update, this effects only message forwarding - as it should have been. So call also SetHandleOnlyKnownMessages(true), if you want to disable any handling for unknown messages.

  • NMEA 2000 Library reference update.

  • Added ExtendSingleFrameMessages and ExtendFastPacketMessages. With these one can own list of known messages so that it is not necessary to duplicate message list as, if used only SetSingleFrameMessages and SetFastPacketMessages.

  • Added discrete status flags for transmission parameters (PGN 127493), thanks for testing Jason.

06.08.2016

  • Added SetISORqstHandler for setting handler for ISO requests. Thanks thomasonw.

30.07.2016

  • NMEA 2000 Library reference update.

  • Added example TeensyActisenseListenerSender. Example contains code, schematics and document.

19.07.2016

  • Fixed discrete status on engine dynamic parameters (PGN 127489), thanks Jason.

  • Added new PGN 127257, vessel attitude. Only sending has been tested with NMEA Reader

12.07.2016

  • Added to API — Optional message lists by thomasonw

25.06.2016

  • Corrected Battery Current in ParseN2kPGN127508 by thomasonw.

23.03.2016

  • Additional PGN 129038, PGN 129039, PGN 129285, PGN 130074 support by adwuk.

13.03.2016

  • Fix of using PROGMEM. Now also product information defined to PROGMEM works right.

13.03.2016

  • Fix of using PROGMEM. Still does not work right with product information in PROGMEM. So all changes after 09.03 are still under validation.

13.03.2016

  • More memory optimization - thanks for thomasonw. Constant message strings has been marked with F(…​) moving them to flash instead of RAM.

Note also that there is new function void tNMEA2000::SetProductInformation(const tProductInformation *_ProductInformation); So one can save memory by defining product information to flash by using syntax:

const tProductInformation BatteryMonitorProductInformation PROGMEM={
1300,               // N2kVersion
...

See example BatteryMonitor.ino

12.03.2016

  • Memory tuning. Currently multi device and user definable message filters has not been implemented, so I changed buffer sizes to minimum.

  • There is also new function void tNMEA2000::SetN2kCANMsgBufSize(const unsigned char _MaxN2kCANMsgs); to define buffer size for received N2k messages. Note that library has to collect fast packet frames, which may arrive fragmented from different devices, so as default this buffer size has been set to 5. If your device is only sending some data (mode is tNMEA2000::N2km_NodeOnly), you do not need to catch all fast packet messages (if any), so you can set buffer size smaller.

09.03.2016

  • Additional PGN 127250, PGN 128275 Support by adwuk.

08.03.2016

  • AVR CAN support by thomasonw.

02.02.2016

  • NOTE! Updates, which effects backward compatibility. See list below.

  • PGN 127489, SetN2kPGN127489 EngineOilTemp and EngineCoolantTemp is in Kelvins as in other temperature functions. So add for call to this function CToKelvin(…​)

  • Some function names withing N2kMessages have been changed. Change function names listed below!
    SetN2kPGNSystemTime → SetN2kSystemTime
    ParseN2kPGNSystemTime → ParseN2kSystemTime
    SetN2kPGNTrueHeading → SetN2kTrueHeading
    SetN2kPGNMagneticHeading → SetN2kMagneticHeading

  • Variable types has been changed on some functions in N2kMessages. So when you get an compiler error about functions in N2kMessages, check carefully all parameter definitions for function from N2kMessages.h.

  • If you do not have value for some parameter for functions in N2kMessages, use related N2kxxxxNA constant defined in N2kMsg.h. So e.g. if you only have wind speed, call
    SetN2kWindSpeed(N2kMsg, 1, ReadWindSpeed(),N2kDoubleNA,N2kWind_Apprent);

  • If you are reading values from N2k bus, you can now check does some value exist by using function N2kIsNA. So if you e.g. call
    ParseN2kOutsideEnvironmentalParameters(N2kMsg,SID,WaterTemperature,OutsideAmbientAirTemperature,AtmosphericPressure);
    then check pressure value with
    if ( !N2kIsNA(AtmosphericPressure) ) { // It is available, so we can show it!

  • Added reference document to the documents, which hopefully helps to get started.

23.01.2016

  • Added PGN 127493 support. NMEA2000_mcp has now interrupt support. Some other fixes.

23.01.2016

  • Added some comments to samples and several new message readers. Also added support for 130316 extended temperature. Added new include N2kMessagesEnumToStr.h for translating library enums to clear text. This is now just for preliminary so I may changes texts in coming future. Added also new examples DataDisplay2.ini and MessageSender.ino. They are extended versions of DataDisplay.ino and TemperatureMonitor.ino.

05.12.2015

  • Added NMEA2000_CAN.h and some fixes. Library has been originally developed with Arduino Software 1.6.5 On Arduino Software 1.6.6 it is possible to include libraries within included files, so now it is possible to just include one file NMEA2000_CAN.h, which automatically selects right CAN library according. So you can have same code for different hw. Currently supported CAN libraries are mcp_can, due_can and teensy. Note! NMEA2000_CAN.h is now used on examples TemperatureMonitor and WindMonitor!

Memory requirements

I have tried to measure memory used by library, but it is not so simple, since there are some automated operations. With version 11.06.2017 I got results:

  • Approximate ROM 26.9 kB

  • Approximate RAM 3.4 kB

This is with simple TemperatureMonitor example. This can be squeezed by setting:

  • Add below to setup() before NMEA2000.Open();

  NMEA2000.SetN2kCANMsgBufSize(2);
  NMEA2000.SetN2kCANSendFrameBufSize(15);
  • Defining ProductInformation to PROGMEM as in BatteryMonitor example.

  • Disabling all extra features. See NMEA2000_CompilerDefns.h

  • Disable interrupt receiving.

With those setting you can go down to appr. 19 kB ROM and 1.9 kB RAM. So for 2 kB devices like Arduino Uno, there is not much for your own code.

Hardware setup

NMEA2000 is inherited from CAN. Many MCUs like Teensy >3.1, ESP32, Arduino Due has already CAN controller inside. If your MCU does not have CAN controller inside or you need second external CAN controller, you can use e.g. MCP2515 CAN controller, which is supported by library.

For final connection to the bus you need CAN bus_transceiver chip. Devices on NMEA2000 bus should be isolated to avoid ground loops. So if you take power from NMEA2000 bus and your device is not connected to ground enywhere else, you can use unisolated tranceiver like MCP2551, MCP2562 or SN65HVD234. If you instead feed power to your device directly or e.g. use engine own sensors for measuring, you have to use isolated tranceivers like ISO1050. Remember also use isolated power supply, if you take power from bus and have any unisolated connection to anywhere on your whole system.

Easiest for connecting to NMEA2000 bus is to use some ready shield.

In case you build your tranceiver connection by yourself there are some connection examples under documents.

Library has been also used with Maple Mini board.

Software setup

You need at least Arduino Software 1.6.6 for this sample. I’ll expect you are familiar with Arduino and using libraries. When your Arduino environment is ready,

  • Download NMEA2000 library zip.

  • Depending of your board download libraries as zip:

  • Install all libraries to your Arduino IDE (Sketch-Include Library-Add .ZIP library).

  • Open NMEA2000\Examples\TemperatureMonitor.

  • Connect you board to USB and NMEA2000 bus.

  • Send sketch to your board.

  • If you have Multi Function Display (e.g. Garmin GMI-20) on your NMEA2000 bus, you should see on it’s NMEA2000 bus devices new device "Simple temp monitor" on the list.

Now you are ready to play with your own device. Check also the NMEA2000\Examples\ActisenseListener, which reads all data from NMEA2000 bus and sends it to PC. NMEA2000/Examples/ArduinoGateway allows you to mimic Actisense NGT-1 and connect e.g. a Raspberry Pi running Signal-K to the NMEA2000 bus with an Arduino or Teensy.

Forcing CAN "driver" (was using Arduino Software older than 1.6.6)

In examples there are simple includes:
#include <Arduino.h>
#include <NMEA2000_CAN.h> // This will automatically choose right CAN library and create suitable NMEA2000 object

If that can not be used (like with Arduino IDE older than 1.6.6) or you would like to control naming and used "driver", you can manually include necessary files. Specially if you want to use secondary CAN bus on your system.

For use with Teensy 4.x (also with 3.1/3.2/3.5/3.6)

#include <N2kMsg.h>
#include <NMEA2000.h>
#include <NMEA2000_Teensyx.h> // https://github.com/ttlappalainen/NMEA2000_Teensyx
//
tNMEA2000_Teensyx NMEA2000;

For use with Teensy 3.1/3.2/3.5/3.6 board and FlexCan

#include <N2kMsg.h>
#include <NMEA2000.h>
#include <FlexCAN.h>
#include <NMEA2000_teensy.h> // https://github.com/sarfata/NMEA2000_teensy
//
tNMEA2000_teensy NMEA2000;

For use with ESP32

#include <N2kMsg.h>
#include <NMEA2000.h>
#include <NMEA2000_esp32.h> // https://github.com/ttlappalainen/NMEA2000_esp32
//
tNMEA2000_esp32 NMEA2000;

For use board with MCP2515 SPI can bus tranceiver and mcp_can library

#include <N2kMsg.h>
#include <NMEA2000.h>
#include <SPI.h>
#include <mcp_can.h> // https://github.com/ttlappalainen/CAN_BUS_Shield
#include <NMEA2000_mcp.h>
#define N2k_CAN_INT_PIN 21 // Pin, where interrupt line has been connected
#define N2k_SPI_CS_PIN 53  // Pin for SPI Can Select
//
tNMEA2000_mcp NMEA2000(N2k_SPI_CS_PIN,MCP_16MHz,N2k_CAN_INT_PIN);

For use with Arduino due and due_can library

#include <N2kMsg.h>
#include <NMEA2000.h>
#include <due_can.h>  // https://github.com/ttlappalainen/due_can
#include <NMEA2000_due.h>
//
tNMEA2000_due NMEA2000;

For use with Atmel AVR processors internal CAN controller

#include <N2kMsg.h>
#include <NMEA2000.h>
#include <avr_can.h>            // https://github.com/thomasonw/avr_can
#include <NMEA2000_avr.h>       // https://github.com/thomasonw/NMEA2000_avr
//
tNMEA2000_avr NMEA2000;

License

MIT license

Copyright (c) 2015-2021 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.