UDP broadcast packet

edited December 2017 in Arduino

Hello, i've made a sketch in processing to detect digiboard IP on instruments, so, i send UDP query in broadcast mode and instrument return datagram where i can show remote ip and i make connexion with TCP client to this remote on port 502 with specific data to making instrument action. It's work fine in processing but i wanted to make the same in arduino sketch with arduino library...

So please, help me to make the same code for arduino, see my code...

      import hypermedia.net.*;
     import processing.net.*;
     UDP udp;  // define the UDP object
     Client myClient; 

     //START COMMANDE CI_3100:c1920000000601050001ff00
     //STOP COMMANDE CI_3100+CI_X5X:c19200000006010500010000
     //FIND IP COMMANDE CI_3100:304236384432324154454d494c4353444e414c444552303830343365393434363330633962343830343830303030303001707274013630303001636d640130017369640134313830372c373032343532363201
     //FIND IP COMMANDE CI_X5X:323032314441424654454d494c4353444e414c444552303830343365393434363330633962343830343830305845303001707274013630303001636d640130017369640134313831372c373034363132303801
     byte[] datadetect_CI3100 = new byte[]{(byte)0x30, (byte)0x42, (byte)0x36, (byte)0x38, (byte)0x44, (byte)0x32, (byte)0x32, (byte)0x41, (byte)0x54, (byte)0x45, (byte)0x4d, (byte)0x49, (byte)0x4c, (byte)0x43, (byte)0x53, (byte)0x44, (byte)0x4e, (byte)0x41, (byte)0x4c, (byte)0x44, (byte)0x45, (byte)0x52, (byte)0x30, (byte)0x38, (byte)0x30, (byte)0x34, (byte)0x33, (byte)0x65, (byte)0x39, (byte)0x34, (byte)0x34, (byte)0x36, (byte)0x33, (byte)0x30, (byte)0x63, (byte)0x39, (byte)0x62, (byte)0x34, (byte)0x38, (byte)0x30, (byte)0x34, (byte)0x38, (byte)0x30, (byte)0x30, (byte)0x30, (byte)0x30, (byte)0x30, (byte)0x30, (byte)0x01, (byte)0x70, (byte)0x72, (byte)0x74, (byte)0x01, (byte)0x36, (byte)0x30, (byte)0x30, (byte)0x30, (byte)0x01, (byte)0x63, (byte)0x6d, (byte)0x64, (byte)0x01, (byte)0x30, (byte)0x01, (byte)0x73, (byte)0x69, (byte)0x64, (byte)0x01, (byte)0x34, (byte)0x31, (byte)0x38, (byte)0x30, (byte)0x37, (byte)0x2c, (byte)0x37, (byte)0x30, (byte)0x32, (byte)0x34, (byte)0x35, (byte)0x32, (byte)0x36, (byte)0x32, (byte)0x01};
     byte[] datadetect_CIX5X = new byte[]{(byte)0x32, (byte)0x30, (byte)0x32, (byte)0x31, (byte)0x44, (byte)0x41, (byte)0x42, (byte)0x46, (byte)0x54, (byte)0x45, (byte)0x4d, (byte)0x49, (byte)0x4c, (byte)0x43, (byte)0x53, (byte)0x44, (byte)0x4e, (byte)0x41, (byte)0x4c, (byte)0x44, (byte)0x45, (byte)0x52, (byte)0x30, (byte)0x38, (byte)0x30, (byte)0x34, (byte)0x33, (byte)0x65, (byte)0x39, (byte)0x34, (byte)0x34, (byte)0x36, (byte)0x33, (byte)0x30, (byte)0x63, (byte)0x39, (byte)0x62, (byte)0x34, (byte)0x38, (byte)0x30, (byte)0x34, (byte)0x38, (byte)0x30, (byte)0x30, (byte)0x58, (byte)0x45, (byte)0x30, (byte)0x30, (byte)0x01, (byte)0x70, (byte)0x72, (byte)0x74, (byte)0x01, (byte)0x36, (byte)0x30, (byte)0x30, (byte)0x30, (byte)0x01, (byte)0x63, (byte)0x6d, (byte)0x64, (byte)0x01, (byte)0x30, (byte)0x01, (byte)0x73, (byte)0x69, (byte)0x64, (byte)0x01, (byte)0x34, (byte)0x31, (byte)0x38, (byte)0x31, (byte)0x37, (byte)0x2c, (byte)0x37, (byte)0x30, (byte)0x34, (byte)0x36, (byte)0x31, (byte)0x32, (byte)0x30, (byte)0x38, (byte)0x01};
     byte[] datastart = new byte[]{(byte)0xc1, (byte)0x92, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x06, (byte)0x01, (byte)0x05, (byte)0x00, (byte)0x01, (byte)0xff, (byte)0x00};
     byte[] datastop = new byte[]{(byte)0xc1, (byte)0x92, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x06, (byte)0x01, (byte)0x05, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00};

     void setup() {
     udp = new UDP( this, 6000 );  // create a new datagram connection on port 6000
     //udp.log( true );         // <-- printout the connection activity
     udp.listen( true );           // and wait for incoming message  
     }

     void draw()
     {
     }

     void keyPressed() {
     String ip       = "255.255.255.255"; // the destination IP BROADCAST
     int port        = 5048;        // the destination port
     udp.send(datadetect_CI3100, ip, port );   // the message 1 to send
     delay(1000);
      udp.send(datadetect_CIX5X, ip, port );   // the message 2 to send
     }

     void receive( byte[] data ) {          // <-- default handler
    for(int i=0; i < data.length; i++){
       String ipstart       = str(char(data[i])) +""+ str(char(data[i+1]));
     if (ipstart.equals("ip")==true) {
       i=i+1;
        ipstart       =str(char(data[i+2]))+""+ str(char(data[i+3]))+""+ str(char(data[i+4]))+""+ str(char(data[i+5]))+""+ str(char(data[i+6]))+""+ str(char(data[i+7]))+""+ str(char(data[i+8]))+""+ str(char(data[i+9]));
         //println(ipstart);
         String strtest       =str(char(data[i+10]));
         if (strtest.equals("s")==true) {
           ipstart       =unhex("0"+char(data[i+2]))+"."+unhex(char(data[i+3])+""+char(data[i+4]))+"."+unhex(char(data[i+5])+""+char(data[i+6]))+"."+unhex(char(data[i+7])+""+char(data[i+8]));
           println(ipstart);
           }
             else{
           ipstart       =unhex(char(data[i+2])+""+char(data[i+3]))+"."+unhex(char(data[i+4])+""+char(data[i+5]))+"."+unhex(char(data[i+6])+""+char(data[i+7]))+"."+unhex(char(data[i+8])+""+char(data[i+9]));
           println(ipstart);
      }
      i=data.length;
      myClient = new Client(this,ipstart , 502); 
      myClient.write(datastop);
      delay(1000);
      myClient.write(datastart);
      delay(1000);
      myClient.write(datastop);
         }
      }
     }
Tagged:

Answers

  • edited February 2015

    no one can help me ?

  • edited February 2015

    i code the arduino udp part successfully ! my udp part code on arduino

    #include <SPI.h>        
    #include <Ethernet.h>
    #include <EthernetUdp.h> 
    
    EthernetUDP Udp;
    EthernetUDP Udp2;
    IPAddress remote;
    // buffers for receiving and sending data
    char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  //buffer to hold incoming packet,
    byte mac[] = { 
      0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    IPAddress locIP(192, 168, 1, 17);
    IPAddress broadcastIP(255, 255,255, 255);
    unsigned int localPort = 5048;     
    unsigned int remPort = 6000; 
    //START COMMANDE CI_3100:c1920000000601050001ff00
    //STOP COMMANDE CI_3100+CI_X5X:c19200000006010500010000
    //FIND IP COMMANDE CI_3100:304236384432324154454d494c4353444e414c444552303830343365393434363330633962343830343830303030303001707274013630303001636d640130017369640134313830372c373032343532363201
    //FIND IP COMMANDE CI_X5X:323032314441424654454d494c4353444e414c444552303830343365393434363330633962343830343830305845303001707274013630303001636d640130017369640134313831372c373034363132303801
    char datadetect_CI3100[]  = {
      0x30, 0x42, 0x36, 0x38, 0x44, 0x32, 0x32, 0x41, 0x54, 0x45, 0x4d, 0x49, 0x4c, 0x43, 0x53, 0x44, 0x4e, 0x41, 0x4c, 0x44, 0x45, 0x52, 0x30, 0x38, 0x30, 0x34, 0x33, 0x65, 0x39, 0x34, 0x34, 0x36, 0x33, 0x30, 0x63, 0x39, 0x62, 0x34, 0x38, 0x30, 0x34, 0x38, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x01, 0x70, 0x72, 0x74, 0x01, 0x36, 0x30, 0x30, 0x30, 0x01, 0x63, 0x6d, 0x64, 0x01, 0x30, 0x01, 0x73, 0x69, 0x64, 0x01, 0x34, 0x31, 0x38, 0x30, 0x37, 0x2c, 0x37, 0x30, 0x32, 0x34, 0x35, 0x32, 0x36, 0x32, 0x01};
    byte datadetect_CIX5X[]  = {
      0x32, 0x30, 0x32, 0x31, 0x44, 0x41, 0x42, 0x46, 0x54, 0x45, 0x4d, 0x49, 0x4c, 0x43, 0x53, 0x44, 0x4e, 0x41, 0x4c, 0x44, 0x45, 0x52, 0x30, 0x38, 0x30, 0x34, 0x33, 0x65, 0x39, 0x34, 0x34, 0x36, 0x33, 0x30, 0x63, 0x39, 0x62, 0x34, 0x38, 0x30, 0x34, 0x38, 0x30, 0x30, 0x58, 0x45, 0x30, 0x30, 0x01, 0x70, 0x72, 0x74, 0x01, 0x36, 0x30, 0x30, 0x30, 0x01, 0x63, 0x6d, 0x64, 0x01, 0x30, 0x01, 0x73, 0x69, 0x64, 0x01, 0x34, 0x31, 0x38, 0x31, 0x37, 0x2c, 0x37, 0x30, 0x34, 0x36, 0x31, 0x32, 0x30, 0x38, 0x01};
    byte datastart[]  = {
      0xc1, 0x92, 0x00, 0x00, 0x00, 0x06, 0x01, 0x05, 0x00, 0x01, 0xff, 0x00};
    byte datastop[]  = {
      0xc1, 0x92, 0x00, 0x00, 0x00, 0x06, 0x01, 0x05, 0x00, 0x01, 0x00, 0x00};
    
    void setup() {
      Ethernet.begin(mac,locIP);
    
      delay(1000);
      Udp.begin(localPort);
      Udp2.begin(remPort);
      Serial.begin(9600);
    }
    
    void loop() {
      connectclimet();
      delay(1000);
      int packetSize = Udp2.parsePacket();
      if(packetSize){
    //    Serial.print("Received packet of size ");
    //    Serial.println(packetSize);
        Serial.print("Climet IP : ");
        IPAddress remote = Udp2.remoteIP();
        for (int i =0; i < 4; i++){
          Serial.print(remote[i], DEC);
          if (i < 3){
            Serial.print(".");
          }
        }
        Serial.print(", port ");
        Serial.println(Udp2.remotePort());
        Udp2.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
        //Serial.println("Contents:");
        //Serial.println(packetBuffer);
      }
    }
    void connectclimet(){
     //Serial.println("Trying to connect to climet...");
    //  Serial.println(Ethernet.localIP());
    //  Serial.println(localPort);
      Udp.beginPacket(broadcastIP, localPort); 
      //Udp.println(Ethernet.localIP());
      Udp.write(datadetect_CI3100);
      Udp.endPacket();
    }
    
  • edited February 2015

    I'm not able to make the code of the TCP client part i've made in processing... I just want to send packet on TCP with IP and port from the arduino in processing it's look like :

    myClient = new Client(this,ipstart , 502); 
      myClient.write(datastop);
      delay(1000);
      myClient.write(datastart);
      delay(1000);
      myClient.write(datastop);
    
  • I have the same issue, I am struggling to send a broadcast UDP packet to all the devices on the network.

Sign In or Register to comment.