E
E
Evgeny Oleinikov2018-05-23 22:39:58
Java
Evgeny Oleinikov, 2018-05-23 22:39:58

Data transfer via udp from arduino to server in java?

Good day to all!
Tell me how to solve the following problem, I've been fighting for a couple of days.
For the test, I wrote a simple udp server in java (I took an example):

import java.io.*;
import java.net.*;

class UDPClient
{
   public static void main(String args[]) throws Exception
   {
      BufferedReader inFromUser =
         new BufferedReader(new InputStreamReader(System.in));
      DatagramSocket clientSocket = new DatagramSocket();
      InetAddress IPAddress = InetAddress.getByName("192.168.31.157");
      byte[] sendData = new byte[1024];
      byte[] receiveData = new byte[1024];
      String sentence = inFromUser.readLine();
      sendData = sentence.getBytes();
      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
      clientSocket.send(sendPacket);
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
      clientSocket.receive(receivePacket);
      String modifiedSentence = new String(receivePacket.getData());
     System.out.println("FROM SERVER:" + modifiedSentence);
        clientSocket.close();
   }
}

A string is sent from the server to arduino, arduino in turn responds by sending temperature sensor readings:
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);
    char ch = temperatureC;
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ch); 
    Udp.endPacket();

Everything leaves the server, arduino receives and sends everything, but the server output is either empty or the ?
5b05c313e2327765626518.png
How can this problem be solved?
Thanks in advance for your replies

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Oleinikov, 2018-05-24
@ruJumi

I solved the problem: instead of Udp.write (ch) I use Udp.print (ch)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question