NMEA2000 And serial communication

hi hope everyone will be fine and good i try to communicate the arduino with pi through serial communication and i successfully done this but after 1st message i get garbage and when i open serial monitor as attached in the picture shows garbage i comment the line NMEA2000.ParseMessages(); then garbage is gone anyone has any idea how i can handle and get rid from this garbage values.
Screenshot 2024-07-10 215848
Screenshot 2024-07-10 204744


#include <Arduino.h>
#include <NMEA2000_CAN.h>
#include <N2kMessages.h>

// Define CAN bus pins if needed
//#define MCP2515_CS_PIN 10
//MCP_CAN CAN(MCP2515_CS_PIN);
#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.
#define N2k_CAN_INT_PIN 21
// Define NMEA2000 messages to transmit
const unsigned long TransmitMessages[] PROGMEM = {
  130311L,  // Temperature PGN
  127489L,  // Flow rate PGN
  0         // End of array
};

// NMEA2000 message scheduler for temperature and flow rate messages
tN2kSyncScheduler TemperatureScheduler(false, 2000, 500);
tN2kSyncScheduler FlowRateScheduler(false, 500, 510);

const int flowSensorPin = 5; //pwm pin of arduino
volatile unsigned long pulseCount = 0; // Pulse counter
unsigned long startTime = 0; // Start time of accumulation period
const unsigned long accumulationInterval = 10000; // Accumulation interval in milliseconds (10 seconds)
float flowRate = 0.0; // Flow rate in liters per minute
float frequency = 0.0;

const int flowSensorbPin = 3; //pwm pin of arduino
unsigned long startTimeb = 0; // Start time of accumulation period
const unsigned long accumulationIntervalb = 10000; // Accumulation interval in milliseconds (10 seconds)
float flowRateb = 0.0; // Flow rate in liters per minute
float frequencyb = 0.0;
volatile unsigned long pulseCountb = 0;

const int ntcPin = A0;                       // For Main Cabin Temperature
const float Beta = 3950.0;
const float thermistorNominal = 10000.0;
const float temperatureNominal = 25.0;
const int referenceResistor = 10000;

const int ntc2Pin = A1;                     // For sea temperature
const float Beta1 = 3950.0;
const float thermistorNominal_1 = 10000.0;
const float temperatureNominal_1 = 25.0;
const int referenceResistor_1 = 10000;

const int ntc3Pin = A2;                    // For outside temperature
const float Beta2 = 3950.0; 
const float thermistorNominal_2 = 10000.0;
const float temperatureNominal_2 = 25.0;
const int referenceResistor_2 = 10000;

void OnN2kOpen() {
  TemperatureScheduler.UpdateNextTime();
  FlowRateScheduler.UpdateNextTime();
}
void setup() {
  Serial.begin(9600);
  pinMode(flowSensorPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(flowSensorPin), pulseCounter, FALLING);
  startTime = millis();
  
  pinMode(flowSensorbPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(flowSensorbPin), pulseCounterb, FALLING);
  startTimeb = millis();


  Serial.println("MCP2515 Initialized Successfully!");

  NMEA2000.SetProductInformation("00000001", 100, "Arduino NMEA2000", "1.0", "1.0");
  NMEA2000.SetDeviceInformation(123456, 130, 75, 2040);
  NMEA2000.SetDeviceInformation(123457, 160, 75, 2040);
  NMEA2000.SetMode(tNMEA2000::N2km_ListenAndNode, 0);  // Node only mode
  NMEA2000.SetForwardStream(&Serial);
  NMEA2000.SetForwardType(tNMEA2000::fwdt_Actisense); // Forward all messages in Actisense format
  NMEA2000.SetDebugMode(tNMEA2000::dm_Actisense); // Enable debug mode

  NMEA2000.SetOnOpen(OnN2kOpen);
  NMEA2000.ExtendTransmitMessages(TransmitMessages);
  NMEA2000.Open();
}

void loop() {
  delay(1000);
  int analogValue = analogRead(ntcPin);
  float resistance = calculateResistance(analogValue);
  float temperatureC = calculateTemperatureC(resistance);
  float temperatureF = temperatureC * 9.0 / 5.0 + 32.0;
  
  Serial.print("Analog Value: ");
  Serial.print(analogValue);
  Serial.print(" Resistance: ");
  Serial.print(resistance);
  Serial.print(" ohms Temperature: ");
  Serial.print(temperatureC);
  Serial.print(" °C / ");
  Serial.print(temperatureF);
  Serial.println(F(" °F"));
  
  delay(1000);
  int analogValue_1 = analogRead(ntc2Pin);
  float resistance_1 = calculateResistance_1(analogValue_1);
  float temperatureC_1 = calculateTemperatureC_1(resistance_1);
  float temperatureF_1 = temperatureC_1 * 9.0 / 5.0 + 32.0;

  Serial.print("Analog Value_1: ");
  Serial.print(analogValue_1);
  Serial.print(" Resistance_1: ");
  Serial.print(resistance_1);
  Serial.print(" ohms Temperature_1: ");
  Serial.print(temperatureC_1);
  Serial.print(" °C / ");
  Serial.print(temperatureF_1);
  Serial.println(F(" °F"));
  delay(1000);


  int analogValue_2 = analogRead(ntc3Pin);
  float resistance_2 = calculateResistance_2(analogValue_2);
  float temperatureC_2 = calculateTemperatureC_2(resistance_2);
  float temperatureF_2 = temperatureC_2 * 9.0 / 5.0 + 32.0;

  Serial.print("Analog Value_2: ");
  Serial.print(analogValue_2);
  Serial.print(" Resistance_2: ");
  Serial.print(resistance_2);
  Serial.print(" ohms Temperature_2: ");
  Serial.print(temperatureC_2);
  Serial.print(" °C / ");
  Serial.print(temperatureF_2);
  Serial.println(F(" °F"));
  delay(1000);

 updateTemperatureMessage(temperatureC);
 updateTemperatureMessage_1(temperatureC_1);
 updateTemperatureMessage_2(temperatureC_2);
 updateFlowRateMessage();
 NMEA2000.ParseMessages();
}
void pulseCounter() {
  pulseCount++;
}

void pulseCounterb() {
  pulseCountb++;
}

float calculateResistance(int analogValue) {
  float voltage = analogValue * (5.0 / 1023.0);
  return referenceResistor * (5.0 / voltage - 1.0);
}

float calculateTemperatureC(float resistance) {
  float steinhart;
  steinhart = resistance / thermistorNominal;  // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= Beta;                           // 1/B * ln(R/Ro)
  steinhart += 1.0 / (temperatureNominal + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // Convert to Celsius
  return steinhart;
}

float calculateResistance_1(int analogValue_1) {
  float voltage = analogValue_1 * (5.0 / 1023.0);
  return referenceResistor_1 * (5.0 / voltage - 1.0);
}

float calculateTemperatureC_1(float resistance_1) {
  float steinhart;
  steinhart = resistance_1 / thermistorNominal_1;  // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= Beta1;                           // 1/B * ln(R/Ro)
  steinhart += 1.0 / (temperatureNominal_1 + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // Convert to Celsius
  return steinhart;
}

float calculateResistance_2(int analogValue_2) {
  float voltage = analogValue_2 * (5.0 / 1023.0);
  return referenceResistor_2 * (5.0 / voltage - 1.0);
}

float calculateTemperatureC_2(float resistance_2) {
  float steinhart;
  steinhart = resistance_2 / thermistorNominal_2;  // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= Beta2;                           // 1/B * ln(R/Ro)
  steinhart += 1.0 / (temperatureNominal_2 + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // Convert to Celsius
  return steinhart;
}

void updateFlowRateMessage() {
  tN2kMsg N2kMsg;
  
  noInterrupts();
  unsigned long currentPulseCount = pulseCount;
  pulseCount = 0;
  interrupts();

  frequency = float(currentPulseCount) / float(accumulationInterval) * 1000.0; 
  flowRate = frequency * 60.0;

  float netFlowRate = flowRate - flowRateb;
  if (FlowRateScheduler.IsTime()) {
    FlowRateScheduler.UpdateNextTime();
    SetN2kEngineTripParameters(N2kMsg, 1, N2kps_Fuel, netFlowRate);
    NMEA2000.SendMsg(N2kMsg);
  }
}

void updateFlowRatebMessage() {
  tN2kMsg N2kMsg;
  
  noInterrupts();
  unsigned long currentPulseCountb = pulseCountb;
  pulseCountb = 0;
  interrupts();

  frequencyb = float(currentPulseCountb) / float(accumulationIntervalb) * 1000.0; 
  flowRateb = frequencyb * 60.0;


}

void updateTemperatureMessage(float temperatureC) {
  tN2kMsg N2kMsg;
  if (TemperatureScheduler.IsTime()) {
    TemperatureScheduler.UpdateNextTime();
    SetN2kTemperature(N2kMsg, 1, 1, N2kts_MainCabinTemperature, CToKelvin(temperatureC));
    NMEA2000.SendMsg(N2kMsg);
  }
}

void updateTemperatureMessage_1(float temperatureC_1) {
  tN2kMsg N2kMsg;
  if (TemperatureScheduler.IsTime()) {
    TemperatureScheduler.UpdateNextTime();
    SetN2kTemperature(N2kMsg, 1, 1, N2kts_SeaTemperature, CToKelvin(temperatureC_1));
    NMEA2000.SendMsg(N2kMsg);
  }
}

void updateTemperatureMessage_2(float temperatureC_2) {
  tN2kMsg N2kMsg;
  if (TemperatureScheduler.IsTime()) {
    TemperatureScheduler.UpdateNextTime();
    SetN2kTemperature(N2kMsg, 1, 1, N2kts_OutsideTemperature, CToKelvin(temperatureC_2));
    NMEA2000.SendMsg(N2kMsg);
  }
}

It appears you did not read the forum guidelines. I cannot read what you posted so I cannot help.

Posting an annotated schematic showing exactly how you have wired it helps us help you. By showing all connections, power, ground and power sources you will save a lot of clarifying questions and time for all of us. Be sure to note any logic wires over 10/25 inches/cm in length. Post links to technical information on the hardware items including the motors, shield, and Arduino. This should include all component values or model numbers and details of all power supplies used (which could be USB power for example).

Posting the code following forum guidelines using code tags will also help. With this information we should be able to answer your question accurately. "How to get the best from this forum". How to get the best out of this forum

If you don't understand what a schematic is, please Google to find out. It is not the same thing as a wiring diagram. Hand-drawn is OK. Some bright, sharp photos of the circuit may also be useful.

now maybe this will help you

Your picture with lines is not much help, most of the information is missing as are the links.

i dont understand what kind of info you need .

I moved your topic to an appropriate forum category @asad_khan_2807.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

Those lines are telling the NMEA2000 library to send debug messages to the serial port.

The hardest part is finding the parts, there are a thousands of different ones available with the more popular included in the install. It has a 3D viewer which is handy. If I am making a schematic and I cannot find the part configuration, I take something close to it and edit it to what I want. Starting with a box and adding pins is not that hard. Before jumping into it look at the following links: What is a schematic What Is a Schematic Diagram?
https://learn.sparkfun.com/tutorials/how-to-read-a-schematic/all
HOW to READ a Schematic: https://www.youtube.com/watch?v=9cps7Q_IrX0&t=15s

KiCad: There are many out there and you will find just like color different people like different colors, some blue, red, ..etc. CAD (Computer Aided Design) programs are like that. You will need to learn a schematic capture program that will work for you.

I work from schematic capture through a set of Gerber files that the PCB will be made from. I have a limited budget and I use KiCad but there are many others available. It is not a program you will learn in an evening or so, nor or the others. There are a lot of resources available, on line, all free same as KiCad. If you download KiCad they will ask for a non required donation. It works great on Linux, I have had it run on the last 5 versions of mint. As I understand it it also works on a Mac and Win-doze
machines as well.

Does this help?

1 Like

I comment out these lines and then check the results.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.