Bonjour Timo,
I am still waiting for the Mega board I ordered, so I continue my test with Arduino Boards.
Anyway, parts of my latest sketch work, but not together.
There are 2 main parts:
- Acquiring datas from the serial port of a Victron BMV700 battery Monitor: Current (A), Voltage (V) and capacity (%)
- Sending those datas to the SeaTalkNG network (NMEA2000).
I can send datas to a Raymarine MFD or B&G display if I write test values directly in the
SetN2kDCBatStatus(N2kMsg,0,13.87,5.12,35.12,1)
command. That is ok.
I can get data from the BMV700 and print them on the serial Monitor. So I gess this part works.
But for that, I must comment (//) all the lines who send the N2K messages, especially this command
// tN2kMsg N2kMsg;
As soon as I uncomment these lines, I only get null values for Current, Voltage and Capacity
I get :
"0.00 0.00 0.00"
, instead of, for example:
"-3.45 12.90 95.45"
// Print the values on the Serial Monitor
Serial.print (Current);
Serial.print (" ");
Serial.print (Voltage);
Serial.print (" ");
Serial.println (SOC);
During the test I have modified the structure of your Battery Monitor Example, copying the SendN2kBattery subroutine in the main loop.
Why the tN2kMsg N2kMsg; command seems to RAZ my datas ?
Regards, thanks for your great job.
François-Xavier.
// From http://www.jw5zla.com/?p=7 Interfacing the Victron BMV-700
// and Timo Lappalainen'NMEA2000 libraries
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <SPI.h>
SoftwareSerial Victron(0,3); // RX= Pin 0, TX= Pin 3(Not used)
char p_buffer[80];
#define P(str) (strcpy_P(p_buffer, PSTR(str)), p_buffer)
#define N2k_SPI_CS_PIN 9 // Valeur 10 pour le shield www.elecrow.com, 9 pour le Seeedunio
#include <NMEA2000_CAN.h> // This will automatically choose right CAN library and create suitable NMEA2000 object
#include <N2kMessages.h>
char c;
String V_buffer; // Buffer to hold data from the Victron monitor
String E_buffer; // Buffer to hold data from the ethernet shield
float Current;
float Voltage;
float SOC;
// List here messages your device will transmit.
const unsigned long TransmitMessages[] PROGMEM={127506L,127508L,0};
// --- Example of using PROGMEM to hold Product ID. However, doing this will prevent any updating of
// these details outside of recompiling the program.
const tProductInformation BatteryMonitorProductInformation PROGMEM={
1300, // N2kVersion
100, // Manufacturer's product code
"Moniteur Batterie Arduino", // Manufacturer's Model ID C'est la ligne qui est détectée comme appareil par le Triton
"1.0.0.0 (2017-06)", // Manufacturer's Software version code
"1.0.0.0 (2017-06)", // Manufacturer's Model version
"00000001", // Manufacturer's Model serial code
0, // SertificationLevel
1 // LoadEquivalency
};
// --- Example of using PROGMEM to hold Configuration information. However, doing this will prevent any updating of
// these details outside of recompiling the program.
const char BatteryMonitorManufacturerInformation [] PROGMEM = "FX VAN THUAN";
const char BatteryMonitorInstallationDescription1 [] PROGMEM = "BMV700 Victron & Arduino";
const char BatteryMonitorInstallationDescription2 [] PROGMEM = "Send Informations to the N2K bus";
void setup()
{
Serial.begin(19200);
Victron.begin(19200);
// Set Product information
NMEA2000.SetProductInformation(&BatteryMonitorProductInformation );
// Set Configuration information
NMEA2000.SetProgmemConfigurationInformation(BatteryMonitorManufacturerInformation,BatteryMonitorInstallationDescription1,BatteryMonitorInstallationDescription2);
// Set device information
NMEA2000.SetDeviceInformation(1, // Unique number. Use e.g. Serial number.
170, // Device function=Battery. See codes on http://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf
35, // Device class=Electrical Generation. See codes on http://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf
999 // Just choosen free from code list on http://www.nmea.org/Assets/20121020%20nmea%202000%20registration%20list.pdf
);
// Uncomment 3 rows below to see, what device will send to bus
// Serial.begin(115200);
NMEA2000.SetForwardStream(&Serial);
NMEA2000.SetForwardType(tNMEA2000::fwdt_Text); // Show in clear text. Leave uncommented for default Actisense format.
Serial.println ("ETAPE 1");
// If you also want to see all traffic on the bus use N2km_ListenAndNode instead of N2km_NodeOnly below
NMEA2000.SetMode(tNMEA2000::N2km_NodeOnly,22);
// NMEA2000.SetDebugMode(tNMEA2000::dm_ClearText); // Uncomment this, so you can test code without CAN bus chips on Arduino Mega
// NMEA2000.EnableForward(false); // Disable all msg forwarding to USB (=Serial)
NMEA2000.SetN2kCANMsgBufSize(2); // For this simple example, limit buffer size to 2, since we are only sending data
NMEA2000.SetN2kCANSendFrameBufSize(30); // essai suite lecture forum
Serial.println ("ETAPE 2");
NMEA2000.Open();
Serial.println ("ETAPE 3");
}
void loop() {
// Victron
if (Victron.available()) {
c = Victron.read();
if (V_buffer.length() <80) {
V_buffer += c;
}
if (c == '\n') { // New line.
if (V_buffer.startsWith("I")) {
String temp_string = V_buffer.substring(V_buffer.indexOf("\t")+1);
double temp_int = temp_string.toInt();
Current = (float) temp_int/1000;
}
if (V_buffer.startsWith("V")) {
String temp_string = V_buffer.substring(V_buffer.indexOf("\t")+1);
int temp_int = temp_string.toInt();
Voltage = (float) temp_int/1000;
}
if (V_buffer.startsWith("SOC")) {
String temp_string = V_buffer.substring(V_buffer.indexOf("\t")+1);
int temp_int = temp_string.toInt();
SOC = (float) temp_int/10;
}
V_buffer="";
}
}
// Print the values on the Serial Monitor
Serial.print (Current);
Serial.print (" ");
Serial.print (Voltage);
Serial.print (" ");
Serial.println (SOC);
//---------------------- Section N2K
#define BatUpdatePeriod 1000
static unsigned long TempUpdated=millis();
// tN2kMsg N2kMsg;
if ( TempUpdated+BatUpdatePeriod<millis() )
{
TempUpdated=millis();
/*
//SetN2kDCBatStatus(N2kMsg,0,13.87,5.12,35.12,1); // test
SetN2kDCBatStatus(N2kMsg,0,Voltage,Current,22.22,1);
NMEA2000.SendMsg(N2kMsg);
//SetN2kDCStatus(N2kMsg,1,0,0,56,92,38500,0.012); // test
SetN2kDCStatus(N2kMsg,1,0,N2kDCt_Battery,SOC,0,0,0);
NMEA2000.SendMsg(N2kMsg);
*/
Serial.print(millis()); Serial.println(", Battery send ready");
} // Fin de la boucle conditionnelle de temps
NMEA2000.ParseMessages();
}