NMEA2000 Library  0.1
Library to handle NMEA 2000 Communication written in C++
TemperatureMonitor.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={130310L,130311L,130312L,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.
36
37// *****************************************************************************
38// Call back for NMEA2000 open. This will be called, when library starts bus communication.
39// See NMEA2000.SetOnOpen(OnN2kOpen); on setup()
40void OnN2kOpen() {
41 // Start schedulers now.
45}
46
47// *****************************************************************************
48void setup() {
49 // Set Product information
50 NMEA2000.SetProductInformation("00000001", // Manufacturer's Model serial code
51 100, // Manufacturer's product code
52 "Simple temp monitor", // Manufacturer's Model ID
53 "1.2.0.21 (2022-09-30)", // Manufacturer's Software version code
54 "1.1.0.0 (2022-09-30)" // Manufacturer's Model version
55 );
56 // Set device information
57 NMEA2000.SetDeviceInformation(112233, // Unique number. Use e.g. Serial number.
58 130, // Device function=Temperature. 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
59 75, // Device class=Sensor Communication Interface. 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
60 2040 // Just choosen free from code list on https://web.archive.org/web/20190529161431/http://www.nmea.org/Assets/20121020%20nmea%202000%20registration%20list.pdf
61 );
62 // Uncomment 2 rows below to see, what device will send to bus. Use e.g. OpenSkipper or Actisense NMEA Reader
63 //Serial.begin(115200);
64 //NMEA2000.SetForwardStream(&Serial);
65 // If you want to use simple ascii monitor like Arduino Serial Monitor, uncomment next line
66 //NMEA2000.SetForwardType(tNMEA2000::fwdt_Text); // Show in clear text. Leave uncommented for default Actisense format.
67
68 // If you also want to see all traffic on the bus use N2km_ListenAndNode instead of N2km_NodeOnly below
70 //NMEA2000.SetDebugMode(tNMEA2000::dm_Actisense); // Uncomment this, so you can test code without CAN bus chips on Arduino Mega
71 NMEA2000.EnableForward(false); // Disable all msg forwarding to USB (=Serial)
72 // Here we tell library, which PGNs we transmit
74 // Define OnOpen call back. This will be called, when CAN is open and system starts address claiming.
76 NMEA2000.Open();
77}
78
79
80// *****************************************************************************
81void loop() {
84}
85
86// *****************************************************************************
87double ReadCabinTemp() {
88 return CToKelvin(22.5); // Read here the true temperature e.g. from analog input
89}
90
91// *****************************************************************************
92double ReadWaterTemp() {
93 return CToKelvin(15.5); // Read here the true temperature e.g. from analog input
94}
95
96// *****************************************************************************
98 tN2kMsg N2kMsg;
99
103 NMEA2000.SendMsg(N2kMsg);
104 }
105
109 NMEA2000.SendMsg(N2kMsg);
110 }
111
115 NMEA2000.SendMsg(N2kMsg);
116 // Serial.print(millis()); Serial.println(", Temperature send ready");
117 }
118}
119
#define PROGMEM
Definition: N2kDef.h:45
This File contains all SetXXX functions which will be needed to transfer data with a specific PGN.
double CToKelvin(double v)
Converting a value from Celsius to Kelvin.
Definition: N2kMessages.h:81
@ N2kts_MainCabinTemperature
value is representing a main cabin temperature
Definition: N2kTypes.h:176
Automatic library selection according to the selected board.
tNMEA2000 & NMEA2000
Definition: NMEA2000_CAN.h:350
void SendN2kTemperature()
tN2kSyncScheduler TemperatureScheduler(false, 2000, 500)
void setup()
void OnN2kOpen()
double ReadCabinTemp()
const unsigned long TransmitMessages[]
double ReadWaterTemp()
tN2kSyncScheduler EnvironmentalScheduler(false, 500, 510)
tN2kSyncScheduler OutsideEnvironmentalScheduler(false, 500, 520)
void loop()
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_ListenAndNode
Definition: NMEA2000.h:699
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 SetN2kOutsideEnvironmentalParameters(tN2kMsg &N2kMsg, unsigned char SID, double WaterTemperature, double OutsideAmbientAirTemperature=N2kDoubleNA, double AtmosphericPressure=N2kDoubleNA)
Setting up Message "Environmental Parameters - DEPRECATED" - PGN 130310.
Definition: N2kMessages.h:4793
void SetN2kTemperature(tN2kMsg &N2kMsg, unsigned char SID, unsigned char TempInstance, tN2kTempSource TempSource, double ActualTemperature, double SetTemperature=N2kDoubleNA)
Setting up Message "Temperature - DEPRECATED" - PGN 130312.
Definition: N2kMessages.h:4962
void SetN2kEnvironmentalParameters(tN2kMsg &N2kMsg, unsigned char SID, tN2kTempSource TempSource, double Temperature, tN2kHumiditySource HumiditySource=N2khs_Undef, double Humidity=N2kDoubleNA, double AtmosphericPressure=N2kDoubleNA)
Setting up Message "Environmental Parameters - DEPRECATED" - PGN 130311.
Definition: N2kMessages.h:4876