Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Help) UDP by esp32 #415

Open
peppeve opened this issue Jul 20, 2024 · 12 comments
Open

(Help) UDP by esp32 #415

peppeve opened this issue Jul 20, 2024 · 12 comments

Comments

@peppeve
Copy link

peppeve commented Jul 20, 2024

Is possible implement the UDP stream by esp32 of received messages? some software are able to read this UDP stream and use N2K messages.

@ttlappalainen
Copy link
Owner

You may mix things a bit. NMEA2000 library core takes care of NMEA2000 (CAN) bus communication and necessary required tasks. On UDP stream you have just data coming, which means that you need some kind of format converter instead of full library. UDP stream format is not specified as standard. So you may have:

  1. Actisense format. Then you can use ActisenseReader module to generate tN2kMsg and then use N2kMessages for parsing.
  2. SeaSmart format. Then you ca use SeaSmart module to generate tN2kMsg and then use N2kMessages for parsing.
  3. Bare frame format. Needs some logic to build messages to tN2kMsg and then use N2kMessages for parsing.
  4. Some other format. Needs some logic to build messages to tN2kMsg and then use N2kMessages for parsing.

Note that UDP data can be in what ever format sender has decided to use. Even bare frame format can be ascii or binary.

@peppeve
Copy link
Author

peppeve commented Jul 21, 2024

Sorry Timo, i try to expain better.
Some professianal marine software are able to read actisense format nmea data from the UDP and not only from serial.
My doubt is how use. Something like this will be fine?

NMEA2000.SetForwardStream(&Udp);
....
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
NMEA2000.ParseMessages();
Udp.endPacket();

@ttlappalainen
Copy link
Owner

You can just open stream and set it as forward stream. You do not need to use beginPacket/endPacket on loop.

That method sends all messages to the stream, which is a bit wasting of resources, if you do not want to analyze all messages. Other method is just handle each message and on message handler send only selected messages with tN2kMsg::SendInActisenseFormat

void HandleNMEA2000Msg(const tN2kMsg &N2kMsg) {
  switch ( N2kMsg.PGN ) {
    case 126992:
    case 127250:
    case 127488:
    case 127489:
    case 129026:
    case 129029:
      N2kMsg.SendInActisenseFormat(&udp);
      break;
  }
}

Remember to use AsyncUDP library to avoid blocking.

@peppeve
Copy link
Author

peppeve commented Jul 21, 2024

Thanks Timo. But how I can define in this case the IP address and port where send the udp packets?

@ttlappalainen
Copy link
Owner

When you open UDP stream. I have tested only broadcast and just kept udp open all the time. If you need to use addressed, you have to test that can it be open all the time as far and if connection closes, then try to reopen. If you open/close it all the time, you have to test that it does not block loop and possibly move sending to an other thread. If you need to use other thread, you have to put received messages to some list and take care of data locking with mutexes.

@peppeve
Copy link
Author

peppeve commented Jul 21, 2024

My IP esp32 is 192.168.0.15 and I want to send the udp packages in all my internal network 192.168.0.255 at the 5000 port.

@ttlappalainen
Copy link
Owner

Then you simply use tUDPBroadcastStream, open it at setup and either use forward stream or on message handler as I wrote above.
UDPBroadcastStream.zip

@peppeve
Copy link
Author

peppeve commented Jul 22, 2024

Hi Timo, it work but seem to be some problems. the stream (text) seem to be wrong in the udp side, serial side it is fine. I attach two filese with log of both serial and udp.
Serial.txt
UDP 60001.log

@ttlappalainen
Copy link
Owner

You have set: NMEA2000.SetForwardType(tNMEA2000::fwdt_Text);
Remove or comment that line.

@peppeve
Copy link
Author

peppeve commented Jul 22, 2024

Yes i Know, but i suspect the Actisense format is wrong too on UDP stream, if you check the two files in text format, serial stream is ok UDP stream is wrong

@ttlappalainen
Copy link
Owner

Both are not in actisense format. They are both in text format, which is only for testing. If you use forwarding, then you have to use fwdt_Actisense format or remove line I mentioned. If you use messagehandler, then use N2kMsg.SendInActisenseFormat(&outputStream);

@peppeve
Copy link
Author

peppeve commented Jul 22, 2024

The UDP stream in actisense format seem to be very fine (respect to Text stream). I attach a log file.
Thank's for yours help
UDP 60001.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants