NMEA2000 Library  0.1
Library to handle NMEA 2000 Communication written in C++
WindMonitor.ino
Go to the documentation of this file.
1/***********************************************************************/
13#include <Arduino.h>
14//#define N2k_SPI_CS_PIN 53 // If you use mcp_can and CS pin is not 53, uncomment this and modify definition to match your CS pin.
15//#define N2k_CAN_INT_PIN 21 // If you use mcp_can and interrupt pin is not 21, uncomment this and modify definition to match your interrupt pin.
16//#define USE_MCP_CAN_CLOCK_SET 8 // If you use mcp_can and your mcp_can shield has 8MHz chrystal, uncomment this.
17//#define ESP32_CAN_TX_PIN GPIO_NUM_16 // If you use ESP32 and do not have TX on default IO 16, uncomment this and and modify definition to match your CAN TX pin.
18//#define ESP32_CAN_RX_PIN GPIO_NUM_17 // If you use ESP32 and do not have RX on default IO 4, uncomment this and and modify definition to match your CAN TX pin.
19//#define NMEA2000_ARDUINO_DUE_CAN_BUS tNMEA2000_due::CANDevice1 // If you use Arduino DUE and want to use CAN bus 1 instead of 0, uncomment this.
20//#define NMEA2000_TEENSY_CAN_BUS 1 // If you use Teensy 3.5 or 3.6 and want to use second CAN bus, uncomment this.
21#include <NMEA2000_CAN.h> // This will automatically choose right CAN library and create suitable NMEA2000 object
22#include <N2kMessages.h>
23
24// List here messages your device will transmit.
25const unsigned long TransmitMessages[] PROGMEM={130306L,0};
26
27// Define schedulers for messages. Define schedulers here disabled. Schedulers will be enabled
28// on OnN2kOpen so they will be synchronized with system.
29// We use own scheduler for each message so that each can have different offset and period.
30// Setup periods according PGN definition (see comments on IsDefaultSingleFrameMessage and
31// IsDefaultFastPacketMessage) and message first start offsets. Use a bit different offset for
32// each message so they will not be sent at same time.
34
35// *****************************************************************************
36// Call back for NMEA2000 open. This will be called, when library starts bus communication.
37// See NMEA2000.SetOnOpen(OnN2kOpen); on setup()
38void OnN2kOpen() {
39 // Start schedulers now.
41}
42
43void setup() {
44 // Set Product information
45 NMEA2000.SetProductInformation("00000002", // Manufacturer's Model serial code
46 100, // Manufacturer's product code
47 "Simple wind monitor", // Manufacturer's Model ID
48 "1.2.0.24 (2022-10-01)", // Manufacturer's Software version code
49 "1.2.0.0 (2022-10-01)" // Manufacturer's Model version
50 );
51 // Set device information
52 NMEA2000.SetDeviceInformation(1, // Unique number. Use e.g. Serial number.
53 130, // Device function=Atmospheric. See codes on https://web.archive.org/web/20190531120557/https://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf
54 85, // Device class=External Environment. See codes on https://web.archive.org/web/20190531120557/https://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf
55 2046 // Just choosen free from code list on https://web.archive.org/web/20190529161431/http://www.nmea.org/Assets/20121020%20nmea%202000%20registration%20list.pdf
56 );
57 // Uncomment 2 rows below to see, what device will send to bus. Use e.g. OpenSkipper or Actisense NMEA Reader
58 //Serial.begin(115200);
59 //NMEA2000.SetForwardStream(&Serial);
60 // If you want to use simple ascii monitor like Arduino Serial Monitor, uncomment next line
61 //NMEA2000.SetForwardType(tNMEA2000::fwdt_Text); // Show in clear text. Leave uncommented for default Actisense format.
62
63 // If you also want to see all traffic on the bus use N2km_ListenAndNode instead of N2km_NodeOnly below
65 // NMEA2000.SetDebugMode(tNMEA2000::dm_Actisense); // Uncomment this, so you can test code without CAN bus chips on Arduino Mega
67 // Here we tell library, which PGNs we transmit
69 // Define OnOpen call back. This will be called, when CAN is open and system starts address claiming.
71 NMEA2000.Open();
72}
73
74
75// *****************************************************************************
76void loop() {
79}
80
81// *****************************************************************************
82double ReadWindAngle() {
83 return DegToRad(50); // Read here the measured wind angle e.g. from analog input
84}
85
86// *****************************************************************************
87double ReadWindSpeed() {
88 return 10.3; // Read here the wind speed e.g. from analog input
89}
90
91// *****************************************************************************
93 tN2kMsg N2kMsg;
94
95 if ( WindScheduler.IsTime() ) {
98 NMEA2000.SendMsg(N2kMsg);
99 }
100}
101
#define PROGMEM
Definition: N2kDef.h:45
This File contains all SetXXX functions which will be needed to transfer data with a specific PGN.
double DegToRad(double v)
Converting a value from Deg to Rad.
Definition: N2kMessages.h:74
@ N2kWind_Apprent
Deprecated - We had the typo in older version of the library.
Definition: N2kTypes.h:256
Automatic library selection according to the selected board.
tNMEA2000 & NMEA2000
Definition: NMEA2000_CAN.h:350
double ReadWindAngle()
Definition: WindMonitor.ino:82
double ReadWindSpeed()
Definition: WindMonitor.ino:87
void setup()
Definition: WindMonitor.ino:43
tN2kSyncScheduler WindScheduler(false, 100, 500)
void OnN2kOpen()
Definition: WindMonitor.ino:38
const unsigned long TransmitMessages[]
Definition: WindMonitor.ino:25
void SendN2kWind()
Definition: WindMonitor.ino:92
void loop()
Definition: WindMonitor.ino:76
This class contains all the data of an NMEA2000 message.
Definition: N2kMsg.h:656
Synchronized Scheduler for timed message sending.
Definition: N2kTimer.h:124
bool IsTime()
Check if ít is time.
Definition: N2kTimer.h:181
void UpdateNextTime()
Update the timestamp for NextTime.
Definition: N2kTimer.h:242
void SetProductInformation(const char *_ModelSerialCode, unsigned short _ProductCode=0xffff, const char *_ModelID=0, const char *_SwCode=0, const char *_ModelVersion=0, unsigned char _LoadEquivalency=0xff, unsigned short _N2kVersion=0xffff, unsigned char _CertificationLevel=0xff, int iDev=0)
Set the Product Information of this device.
Definition: NMEA2000.cpp:745
void EnableForward(bool v=true)
Enable message forwarding to stream.
Definition: NMEA2000.h:2900
void ParseMessages()
Parse all incoming Messages.
Definition: NMEA2000.cpp:2577
void SetDeviceInformation(unsigned long _UniqueNumber, unsigned char _DeviceFunction=0xff, unsigned char _DeviceClass=0xff, uint16_t _ManufacturerCode=0xffff, unsigned char _IndustryGroup=4, int iDev=0)
Set the Device Information. See also NAME.
Definition: NMEA2000.cpp:1093
bool Open()
Open the CAN device.
Definition: NMEA2000.cpp:1200
@ N2km_NodeOnly
Definition: NMEA2000.h:695
bool SendMsg(const tN2kMsg &N2kMsg, int DeviceIndex=0)
Send message to the NMEA2000 bus.
Definition: NMEA2000.cpp:1450
void ExtendTransmitMessages(const unsigned long *_TransmitMessages, int iDev=0)
Extend the list of Transmitted Messages.
Definition: NMEA2000.cpp:1163
void SetOnOpen(void(*_OnOpen)())
Set OnOpen callback function.
Definition: NMEA2000.cpp:2633
void SetMode(tN2kMode _N2kMode, uint8_t _N2kSource=15)
Set the library mode and start source address.
Definition: NMEA2000.cpp:1177
void SetN2kWindSpeed(tN2kMsg &N2kMsg, unsigned char SID, double WindSpeed, double WindAngle, tN2kWindReference WindReference)
Setting up Message "Wind Data" - PGN 130306.
Definition: N2kMessages.h:4717