UDP Packet Send and Received in Java Script Code Required

Hi everyone
I need UDP Packet Send & Receive complete code in Java Script, if anyone have please share with me. Thanks in advance.

What for? What device or gateway? There’s code around for Node.js if that’s what you need.

I have a gateway and I need to send UDP packet to send and receive, for this testing I need code in java script.

Which gateway?

Why do you need JS to send UDP a packet? Gateways using the legacy packet forwarder use UDP built in to communicate with the network server.

2 Likes

Presumably the intent is to interact with the gateway in place of a network server. It shouldn’t matter which gateway as long as it implements either the original or later version of the Semtech UDP protocol (differences are quite slight - documentation is in the official packet forwarder repo, and most non-drastic forks)

Keep in mind that the Semtech packet forwarder and LoRa HAL code also come with some test programs for exercising the concentrator.

It’s a little tempting to say that “if this is something you should be doing you should have no trouble figuring it out” - you can probably use netcat in UDP mode. The actual UDP part of the python implementation I did for a client is pretty literally based on an generic UDP example found online - the interesting part is the packet contents (binary header and printable JSON) not the UDP.

Though it’s dubious if that is on topic for the TTN forum.

Related(/duplicate) thread: https://forum.chirpstack.io/t/demo-fully-automated-decoding-of-lora-messages/10112

Hi @descartes I have nFuse PoE enabled Rpi 3B + LoraWAN Gateway (https://www.n-fuse.co/devices/LoRaWAN-Gateway-RaspberryPi-PoE.html)

I have UDP Packet Send/Received complete code in Java as you can see below;

package udp_demo;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UDP_Receive
{
final static boolean VERBOSE = false;
final static int RX_PORT = 1700;
final static String IP_STRING = “192.168.0.107”;
public UDP_Receive()
{

}
public void rxLoop() 
{
    int count=0;  
    InetAddress IP_Adr = null;
    try 
    {
        IP_Adr = InetAddress.getByName(IP_STRING);
    } 
    catch (UnknownHostException ex) 
    {
        System.out.println("no IP: " + ex);
    }
    boolean doLoop=true;
    try 
    {
        DatagramSocket serverSocket = new DatagramSocket(RX_PORT, IP_Adr);
        serverSocket.setSoTimeout(10000);
        System.out.println("Waiting on Port: " + RX_PORT + " on IP: " + IP_Adr);
    while(doLoop)
    {
        try
        {
            byte[] receiveData = new byte[1024];
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length)
            serverSocket.receive(receivePacket);
            int rxLen = receivePacket.getLength();
            System.out.println("RECEIVED " + rxLen + " bytes from " + IP_Adr + " : " + RX_PORT);
            System.out.print("." + rxLen); // + " "
            if(rxLen>0) // 65, 350
            {
                byte[] rxData = receivePacket.getData();
                System.out.print("We got Data: ");
                for(int i=0; i<rxLen; i++)
                {
                    System.out.print(Integer.toHexString(rxData[i]) + " ");
                }
                String rxString = new String(rxData);
                System.out.println( " = <" + rxString + ">");
                if(rxString.equalsIgnoreCase("Stop"))
                {
                    doLoop = false;
                    System.out.println("Stopped by command");
                }
                if(rxString.contains("Stop"))
                {
                    doLoop = false;
                    System.out.println("Stop in command");
                }
               count=0;
            }
            count++;
            if(count%30==0)
            {
                System.out.println("loop= " + count);
            }
        }        
        catch(IOException ex) 
        {
            System.out.println("IO Problem: \n" + ex);        
        }
    }
    serverSocket.close();
    } 
    catch (SocketException ex) 
    {
        System.out.println("Socket Problem: \n" + ex);
    }
}
public static void main(String[] args)
{
    UDP_Receive me = new UDP_Receive();
    me.rxLoop();
}

}

And for decoding code, I am using this one (https://www.skypack.dev/view/lora-packet)

My both code is working fine now I want to combine both, I think we cannot combine Java with JavaScript code so that is why I need UDP Packet code in JavaScript. Thanks for understanding,

Hi @mfalkvidd I posted over there but I did not get proper response and also I do not want to use Chirpstack I want to decode data in my Rpi which is already installed in my Gateway.

You could redo the Java in JavaScript using Node that has various network interfaces (unlike a browser) or call the Skypack code from the command line as it suggests in the second paragraph on their home page.

As noted on the ChirpStack website thread you have created, you are trying to warp time & space with this project and as it’s not actually TTN related, so not something for this forum.

1 Like

Basically you are saying you did not get the response you are looking for. Tha5 is different to a proper response. The answer you were given is correct, if you do not want to use chirpstack and do not want to use TTN you will have to write your own back end stack for LoRaWAN which is a lot more than just receiving a packet from the packet forwarder and calling a decoder. Anyway, if you do not want to use TTN your question is out of scope for this forum.

1 Like