ttlappalainen / NMEA2000_esp32

Inherited object for use NMEA2000 with ESP32 boards
58 stars 34 forks source link

ESP32 NMea2000 Sender #24

Open merco opened 1 month ago

merco commented 1 month ago

Hi Timo. thanks for your suggestions. I'm still here to bother you about it: be patient. I used the software simulator, now I would like to have a PGN generated via firmware and inserted into a minimal bus. The example takes the Actisense listener that I modified like this that follow.

Obviously the two devices cannot communicate. It seems to me that the SN65HVD230 have the termination resistor... Is it necessary to remove it also in the case of point-to-point communications like this?

Do you see some problem in this simple source code ?

// DOIT ESP32 DEVKIT V1

//per usare ESP32 websocket il 15/04/24 si deve
//cmodificare il file Websockets.cpp
// da C:\Users\mercanti\Documents\Arduino\libraries\WebSockets\src\WebSockets.cpp
//mettendo #include <esp32/sha.h> invece di hwcrypto 
// qualcouno dice di mettere  #include "sha/sha_parallel_engine.h"
#include <Arduino.h>

//#define N2k_CAN_INT_PIN 2
//#define USE_N2K_CAN 1
//#define N2k_SPI_CS_PIN 10

#define ESP32_CAN_TX_PIN GPIO_NUM_5  
#define ESP32_CAN_RX_PIN GPIO_NUM_4 
#define N2k_CAN_INT_PIN 0xff

#include <NMEA2000_CAN.h>
#include <N2kMsg.h>
#include <NMEA2000.h>
#include <N2kMessages.h>
#include <ActisenseReader.h>
#include <Seasmart.h>

tActisenseReader ActisenseReader;

// Define READ_STREAM to port, where you write data from PC e.g. with NMEA Simulator.
#define READ_STREAM Serial       
// Define ForwardStream to port, what you listen on PC side. On Arduino Due you can use e.g. SerialUSB
#define FORWARD_STREAM Serial    

Stream *ReadStream=&READ_STREAM;
Stream *ForwardStream=&FORWARD_STREAM;
const unsigned long TransmitMessages[] PROGMEM = {130310L, // Outside Environmental parameters
                                                 };
// Send time offsets
#define TempSendOffset 0
#define SlowDataUpdatePeriod 1000  // Time between CAN Messages sent

bool IsTimeToUpdate(unsigned long NextUpdate) {
  return (NextUpdate < millis());
}
unsigned long InitNextUpdate(unsigned long Period, unsigned long Offset = 0) {
  return millis() + Period + Offset;
}

void SetNextUpdate(unsigned long & NextUpdate, unsigned long Period) {
  while ( NextUpdate < millis() ) NextUpdate += Period;
}

void setup() {
  // Define buffers big enough
  NMEA2000.SetN2kCANMsgBufSize(8);
  NMEA2000.SetN2kCANSendFrameBufSize(250);
  NMEA2000.SetN2kCANReceiveFrameBufSize(250);

  if (ReadStream!=ForwardStream) READ_STREAM.begin(115200);
  FORWARD_STREAM.begin(115200);
  NMEA2000.SetForwardStream(ForwardStream); 
  NMEA2000.SetMode(tNMEA2000::N2km_ListenAndSend);
  NMEA2000.SetForwardType(tNMEA2000::fwdt_Text); // Show bus data in clear text
  NMEA2000.ExtendTransmitMessages(TransmitMessages);
  if (ReadStream==ForwardStream) NMEA2000.SetForwardOwnMessages(false); // If streams are same, do not echo own messages.
  NMEA2000.EnableForward(true);
  NMEA2000.Open();
  Serial.println("Hi, SIMULATOR on standard TX RX pin! --> ");
  ActisenseReader.SetReadStream(ReadStream);
  ActisenseReader.SetDefaultSource(75);
  ActisenseReader.SetMsgHandler(HandleStreamN2kMsg);

}
void SendN2kTempPressure(void) {
  static unsigned long SlowDataUpdated = InitNextUpdate(SlowDataUpdatePeriod, TempSendOffset);
  tN2kMsg N2kMsg;

  double Temperature = 0;
  double BarometricPressure = 0;
  if ( IsTimeToUpdate(SlowDataUpdated) ) {
    SetNextUpdate(SlowDataUpdated, SlowDataUpdatePeriod);
    Temperature = 31.5;
    BarometricPressure = 1036;
    Serial.printf("Temperature: %3.1f °C - Barometric Pressure: %6.0f Pa\n", Temperature, BarometricPressure);
    SetN2kPGN130310(N2kMsg, 0, N2kDoubleNA, Temperature, BarometricPressure);
    NMEA2000.SendMsg(N2kMsg);
  }
}
#define MAX_NMEA2000_MESSAGE_SEASMART_SIZE 500 
void HandleStreamN2kMsg(const tN2kMsg &N2kMsg) {
  N2kMsg.Print(&Serial);
  NMEA2000.SendMsg(N2kMsg,-1);
  char buf[MAX_NMEA2000_MESSAGE_SEASMART_SIZE];
  if ( N2kToSeasmart(N2kMsg,millis(),buf,MAX_NMEA2000_MESSAGE_SEASMART_SIZE)==0 ) return;
  String mystring(buf);
  Serial.println(mystring);
}

void loop() {
  SendN2kTempPressure();
  NMEA2000.ParseMessages();
  ActisenseReader.ParseMessages();
}

I'm using "SN65HVD230 " and I've read what you wrote about it.

immagine

ttlappalainen commented 1 month ago