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.
#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);
}
}