UDP Packet Data Interpretation?

Hello world!

So here’s where I’m at. I have an SHT20 sensor connected to my Arduino Uno with an added Ethernet shield.

My goal is to send the data collected for the SHT20 over UDP to predetermined web server listening on a specific port.

I haven’t set the port my will listen on yet. At the moment my Uno is listening on port 8888 for anything from my server’s IP, and then return the temp and humidity data to the same IP and Port. If another IP sends a UDP packet by chance or maliciously, the arduino ignores it.

So far it seems to do everything up to that point and I caught the packet manually on my “server” (currently just my laptop) with wireshark.

Here’s snippet of my UDP packet code:

void loop() {
  //if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    if (Udp.remoteIP() == listeningFor) {
      Serial.print("Received packet of size ");
      Serial.println(packetSize);
      Serial.print("From ");
      IPAddress remote = Udp.remoteIP();
      for (int i=0; i < 4; i++) {
        Serial.print(remote[i], DEC);
        if (i < 3) {
          Serial.print(".");
        }
      }
      Serial.print(", port ");
      Serial.println(Udp.remotePort());
  
      // read the packet into packetBufffer
      Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
      Serial.println("Contents:");
      Serial.println(packetBuffer);

      byte humd = sht20.readHumidity();                  // Read Humidity
      byte temp = sht20.readTemperature();               // Read Temperature
      
      Serial.println();
      Serial.println("Temperature: ");
      Serial.print(temp);
      Serial.println();
      Serial.println("Humidity: ");
      Serial.print(humd);
      byte replyBuffer[] = {temp, humd};
  
      // send a reply to the IP address and port that sent us the packet we received
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.write("SHT20");
      for (int i=0; i<2; i++){
        Udp.write(replyBuffer[i]);  
      };
      Udp.endPacket();
      }
    else {
      Serial.print("Received Packet From Unknown Source ");
      Serial.print(Udp.remoteIP());
      } 
    }
  delay(10);
}

Wireshark says the whole packet is 60bytes and the “Data” portion is 7bytes.

Given that I told the Uno to write “SHT20” as the packet message and the tmp & hum variable as individual bytes to the buffer array a total of 7 bytes make sense. 1 for each character in the string message and 1 for each data point.

So what I’m wondering is that the data reads: 53485432301426

Or: 0x53, 0x48, 0x54, 0x32, 0x30, 0x14, 0x26?

I’ve been all over ASCII tables: ASCII Table

And can’t make heads or tails of the result I caught.

If any of you could help shed some light on this I would really appreciate it. The following step after this is to build something on the server end to collect these datagrams and serve it to other, permissioned, clients.

Thanks so much! These forums have been a great help!

The first five hex codes are "SHT 0".
0x14 is the control code DC4 (Device Control).
0x26 is &

Pete

If those last two characters are temperature and humidity then the temperature is 20 degrees (0x14) and the humidity is 38 % (0x26).

Pete

0x53, 0x48, 0x54, 0x32, 0x30, 0x14, 0x26 = "SHT20", 20, 38

Most of the characters in the 0x4X and 0x5X range are uppercase letters.
0x30 through 0x39 are the characters '0' through '9'.

Thanks for confirming guys.

I found a much more useful table than the one I originally linked and also confirmed this morning: http://www.asciitable.com/

I have a follow up question though if that's okay.

So Hexadecimals are the representation of single bytes that contain 8 bits. Correct?

Above that, if I know a byte/hx represents a character it needs to be parsed out of the byte array by index number and translated using utf-8, for standard english output?

If I know a byte represents a uint8 (an unsigned integer in 8bits/1byte) value, I then parse it out of the array by index as above with characters, and translate it as that. u/int8's have values between 0-255 correct?

To represent integers higher than that I would need to use uint16?

Is that a proper understanding of how to translate byte arrays manually? Obviously I would need to know the order of each type of byte but in this case I will every time as each packet will have the same data length.

AustinMorris:
Obviously I would need to know the order of each type of byte but in this case I will every time as each packet will have the same data length.

You either have to know what is in each byte or that information has to be part of the message. Each kind of data could begin with a header byte and variable-length data would either have a length value before the data or a special value to indicate the end of the data. Certainly having fixed format packets is most compact, but you then can't easily update the protocol to include more or different data.