Need help with a Arduino project

so this is my code

(#include <SPI.h>
#include <WiFiNINA.h>

char ssid[] = "MEML";    // your network SSID (name)
char pass[] = "nano33iot"; // your network password
int status = WL_IDLE_STATUS; // the WiFi radio's status

WiFiUDP udpServer;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the WiFi shield:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true)
      ;
  }

  String fv = WiFi.firmwareVersion();
  if (fv < "1.0.0") {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  // Start UDP server on port 8888
  udpServer.begin(8888);
  
  // you're connected now, so print out the status:
  printWiFiStatus();
}

void loop() {
  int packetSize = udpServer.parsePacket();
  if (packetSize) {
    // Allocate a buffer for the incoming packet
    byte buffer[packetSize];
    // Read the packet into buffer
    udpServer.read(buffer, packetSize);
    // Process the received data directly
    Serial.print("Received data: ");
    for (int i = 0; i < packetSize; i++) {
      Serial.print((char)buffer[i]);
    }
    Serial.println();
    
    // Print Arduino's IP address
    Serial.print("Arduino's IP address: ");
    Serial.println(WiFi.localIP());
    
    // Print sender's IP address (Raspberry Pi)
    Serial.print("Sender's IP address (Raspberry Pi): ");
    Serial.println(udpServer.remoteIP());

    // Print destination port
    Serial.print("Destination port: ");
    Serial.println(udpServer.remotePort());
    
    // Decode the received data (assuming it's text)
    decodeData(buffer, packetSize);
  }
}

void printWiFiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void decodeData(byte* data, int length) {
  Serial.println("Decoded data:");
  for (int i = 0; i < length; i++) {
    // Perform any decoding operations here
    // For example, if the data is in ASCII format, you can simply print it
    Serial.write(data[i]);
  }
  Serial.println();
}

and can you guys give me comment on my code. the main question is i need to extract ipv4 data and i have tried and didnt succeed. i would like to extract the data like ipv4 datagram format. the link to the ipv4 datagram is here
https://www.researchgate.net/figure/IPv4-Datagram-header-with-checksum_fig5_224242081

have a read of how-to-get-the-best-out-of-this-forum

in particular

  1. what microcontroller are you using, e.g. ESP32?
  2. Please edit your post, select all code and click the <CODE/> button; next save your post. This will make the code easier to read, easier to copy and the forum software will display it correctly.

hi again i am using Nano 33 IoT

not sure what you are attempting to do
your code reads the datagram contents and prints it, e.g.

   udpServer.read(buffer, packetSize);
    // Process the received data directly
    Serial.print("Received data: ");
    for (int i = 0; i < packetSize; i++) {
      Serial.print((char)buffer[i]);
    }

it is up to you to know what the datagram, which is just an array of bytes, actually contains

for example, the following ESP32 code receives a datagram which contains a structure which it then displays

// ESP32 WiFi AP UDP server - receive UDP datagrams from client

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com
*********/

// Load Wi-Fi library
#include <WiFi.h>
#include <AsyncUDP.h>

// Replace with your network credentials
const char* ssid = "ESP32-Access-Point";
const char* password = "123456789";

#define UDP_PORT 4210
//create UDP instance
WiFiUDP udp;

// UDP Datagram
struct UDPDatagram {
  unsigned long int seq;  // sequence number
  char data[256];         // data
  unsigned short crc;     // crc check
} udpDatagram;

void setup() {
  Serial.begin(115200);
  while (!Serial)
    ;
  delay(1000);
  Serial.println();
  Serial.println("ESP32 WiFi AP UDP server - receive UDP datagrams from client");
  Serial.print("Setting AP (Access Point)…");
  // Remove the password parameter, if you want the AP (Access Point) to be open
  WiFi.softAP(ssid, password);
  IPAddress IP = WiFi.softAPIP();
  Serial.print("\nAP IP address: ");
  Serial.println(IP);
  udp.begin(UDP_PORT);  // Begin listening to UDP port
  Serial.print("Listening on UDP port ");
  Serial.println(UDP_PORT);
}

// receive datagram and check and display contents
void loop() {
  static unsigned long int seq = 0;  // to force seq error check set this none zero
  if (udp.parsePacket()) {           // Receive packet and display it
    int len = udp.read((unsigned char*)&udpDatagram, sizeof(udpDatagram));
    Serial.print("UDP datagram received length ");
    Serial.print(len);
    Serial.print(" From ");
    IPAddress remote = udp.remoteIP();
    Serial.print(remote);
    Serial.print(" seq ");
    Serial.print((int)udpDatagram.seq);
    if (seq != udpDatagram.seq) {
      Serial.print(" Sequence ERROR! expected ");
      Serial.print(seq);
    }
    seq = ++udpDatagram.seq;
    Serial.print("\nData ");
    unsigned short crc = 0;  // to force crc error check set this none zero
    for (int i = 0; i < sizeof(udpDatagram.data); i++) {
      Serial.print((int)udpDatagram.data[i]);
      Serial.print(' ');
      crc += udpDatagram.data[i];
    }
    Serial.print("\nreceived crc = ");
    Serial.print(udpDatagram.crc);
    if (crc != udpDatagram.crc) {
      Serial.print(" CRC ERROR! calculated ");
      Serial.println(crc);
    }
    Serial.println();
  }
}

the client which transmits the datagram

// Arduino TCP server listen for connection and display messages

//  from https://github.com/evothings/evothings-examples/blob/master/examples/arduino-led-onoff-tcp/arduinowifi/arduinowifi/arduinowifi.ino

// ESP32 WiFi UDP client - send UDP datagrams to server

#include <WiFi.h>
#include <AsyncUDP.h>

// Remove this line once you've entered WiFi SSID and password below.
//#error "WiFi SSID and password required!"

// network SSID (network name). and network password.
char ssid[] = "ESP32-Access-Point";
char pass[] = "123456789";

// network key Index number (needed only for WEP).
int keyIndex = 0;

// UDP information
#define UDP_PORT 4210
WiFiUDP udp;

// UDP Datagram
struct UDPDatagram {
  long int seq;        // sequence number
  char data[256];      // data
  unsigned short crc;  // crc check
} udpDatagram;

void setup() {
  Serial.begin(115200);
  // Wait for serial port to connect. Needed for Leonardo only
  while (!Serial)
    ;
  delay(1000);
  Serial.println();
  Serial.println("ESP32 WiFi UDP client - send UDP datagrams to server");
  WiFi.mode(WIFI_STA);  // Connect to Wifi network.
  WiFi.begin(ssid, pass);
  Serial.println("");
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.print("\nGateway IP address: ");
  Serial.println(WiFi.gatewayIP());
  Serial.print("local IP address: ");
  Serial.println(WiFi.localIP());
  // Begin udp port
  udp.begin(UDP_PORT);
  Serial.print("Opening udp port ");
  Serial.println(UDP_PORT);
}

void loop() {
  // generate crc check
  Serial.print("Datagram data ");
  udpDatagram.crc = 0;
  for (int i = 0; i < sizeof(udpDatagram.data); i++) {
    udpDatagram.data[i] = rand() % 255;
    Serial.print((int)udpDatagram.data[i]);
    Serial.print(' ');
    udpDatagram.crc += udpDatagram.data[i];
  }
  // Multicast UDP -  IPAddress broadcast;
  IPAddress broadcast = IPAddress(224, 0, 0, 1);
  udp.beginPacket(broadcast.toString().c_str(), UDP_PORT);
  // Send Packet to UDP server running on Gateway
  // udp.beginPacket(WiFi.gatewayIP().toString().c_str(), UDP_PORT);
  int len = udp.write((const uint8_t *)&udpDatagram, sizeof(udpDatagram));
  udp.endPacket();
  Serial.print("\nudp datagram transmitted length ");
  Serial.print(len);
  Serial.print(" seq ");
  Serial.println(udpDatagram.seq++);
  Serial.print("\ncrc = ");
  Serial.println(udpDatagram.crc);
  delay(5000);
}

client serial monitor output

Gateway IP address: 192.168.4.1
local IP address: 192.168.4.2
Opening udp port 4210
Datagram data 203 78 62 164 0 48 157 171 101 109 141 191 228 185 63 53 75 201 224 212 232 107 77 248 130 43 24 102 24 244 246 242 250 204 30 80 74 170 37 89 198 156 90 36 61 156 129 157 130 36 140 174 129 100 144 60 72 221 78 19 174 40 164 185 106 32 127 8 10 89 164 135 240 224 27 34 152 224 66 249 131 254 18 180 7 56 119 44 142 80 233 198 211 115 60 217 70 46 113 56 72 22 11 158 157 6 20 140 193 171 201 89 187 154 57 120 228 186 63 117 156 52 108 254 165 45 140 113 127 254 238 152 76 225 125 177 253 46 153 152 78 108 148 248 130 3 48 120 11 221 252 155 4 247 88 18 248 45 134 170 188 52 100 217 187 235 210 240 240 191 162 175 15 192 119 86 93 26 104 60 235 225 172 34 61 206 217 173 25 195 38 63 14 101 164 35 235 147 143 24 80 250 203 160 190 100 0 45 199 3 218 123 214 50 221 46 99 63 175 237 254 10 4 254 140 42 102 0 172 73 216 125 147 122 0 48 208 3 87 80 217 216 2 86 224 89 8 242 1 150 195 51 139 236 45 210 
udp datagram transmitted length 264 seq 0

crc = 32517
Datagram data 139 111 19 72 169 29 247 75 163 157 106 237 153 7 162 38 150 217 26 31 94 213 176 127 251 115 148 193 189 120 2 18 75 154 139 200 84 240 93 111 191 34 41 82 167 227 114 5 211 103 65 193 21 72 242 3 142 37 24 73 155 14 72 236 92 247 105 173 110 238 181 96 237 192 217 123 120 64 90 175 1 81 183 169 113 238 185 24 233 204 8 36 177 71 69 169 118 130 119 150 47 218 161 173 93 29 180 24 74 98 139 168 231 201 81 39 252 222 235 241 72 107 77 223 36 14 44 172 7 119 163 191 153 60 251 159 251 41 74 68 78 192 24 22 151 168 120 254 139 54 245 143 183 239 232 17 47 201 174 247 243 219 193 228 147 119 222 193 151 116 195 117 63 113 148 193 200 244 184 78 241 149 121 126 50 112 30 154 55 51 41 89 246 190 151 153 63 65 198 181 10 169 72 12 201 52 72 22 204 131 222 24 10 221 236 87 113 65 20 141 62 36 164 225 207 7 155 128 203 25 12 154 209 236 22 100 15 226 124 92 21 165 151 200 65 146 192 88 248 141 78 206 163 14 233 133 
udp datagram transmitted length 264 seq 1

crc = 33192
Datagram data 77 251 48 118 42 121 129 7 67 162 230 53 213 59 37 197 193 141 52 182 213 227 199 46 75 85 150 223 234 241 65 135 39 219 58 83 239 156 159 52 64 66 239 191 122 98 60 199 129 185 108 152 250 218 96 27 50 214 58 247 41 119 207 176 186 52 56 62 79 76 197 17 88 190 13 135 198 70 177 124 143 241 122 176 198 99 220 29 182 13 31 131 76 173 165 245 129 217 66 208 6 178 196 192 57 13 157 152 163 234 140 73 79 19 194 41 54 136 181 253 115 33 57 178 136 234 159 188 203 14 205 21 13 254 6 202 28 179 30 250 87 106 45 159 70 243 18 67 224 127 190 231 11 180 143 232 163 135 141 77 119 220 191 167 37 237 163 111 135 134 186 0 37 69 40 247 152 45 147 208 57 167 229 190 212 82 173 178 23 78 10 225 34 24 103 125 82 89 180 138 83 43 108 204 1 180 182 78 50 62 39 230 49 230 4 79 142 155 222 75 177 10 103 83 245 43 104 205 159 246 51 225 75 181 8 122 18 101 135 52 119 226 181 205 174 133 65 90 149 229 183 84 117 102 44 121 
udp datagram transmitted length 264 seq 2

crc = 32674

server serial monitor output

ESP32 WiFi AP UDP server - receive UDP datagrams from client
Setting AP (Access Point)…
AP IP address: 192.168.4.1
Listening on UDP port 4210
UDP datagram received length 264 From 192.168.4.2 seq 0
Data 203 78 62 164 0 48 157 171 101 109 141 191 228 185 63 53 75 201 224 212 232 107 77 248 130 43 24 102 24 244 246 242 250 204 30 80 74 170 37 89 198 156 90 36 61 156 129 157 130 36 140 174 129 100 144 60 72 221 78 19 174 40 164 185 106 32 127 8 10 89 164 135 240 224 27 34 152 224 66 249 131 254 18 180 7 56 119 44 142 80 233 198 211 115 60 217 70 46 113 56 72 22 11 158 157 6 20 140 193 171 201 89 187 154 57 120 228 186 63 117 156 52 108 254 165 45 140 113 127 254 238 152 76 225 125 177 253 46 153 152 78 108 148 248 130 3 48 120 11 221 252 155 4 247 88 18 248 45 134 170 188 52 100 217 187 235 210 240 240 191 162 175 15 192 119 86 93 26 104 60 235 225 172 34 61 206 217 173 25 195 38 63 14 101 164 35 235 147 143 24 80 250 203 160 190 100 0 45 199 3 218 123 214 50 221 46 99 63 175 237 254 10 4 254 140 42 102 0 172 73 216 125 147 122 0 48 208 3 87 80 217 216 2 86 224 89 8 242 1 150 195 51 139 236 45 210 
received crc = 32517
UDP datagram received length 264 From 192.168.4.2 seq 1
Data 139 111 19 72 169 29 247 75 163 157 106 237 153 7 162 38 150 217 26 31 94 213 176 127 251 115 148 193 189 120 2 18 75 154 139 200 84 240 93 111 191 34 41 82 167 227 114 5 211 103 65 193 21 72 242 3 142 37 24 73 155 14 72 236 92 247 105 173 110 238 181 96 237 192 217 123 120 64 90 175 1 81 183 169 113 238 185 24 233 204 8 36 177 71 69 169 118 130 119 150 47 218 161 173 93 29 180 24 74 98 139 168 231 201 81 39 252 222 235 241 72 107 77 223 36 14 44 172 7 119 163 191 153 60 251 159 251 41 74 68 78 192 24 22 151 168 120 254 139 54 245 143 183 239 232 17 47 201 174 247 243 219 193 228 147 119 222 193 151 116 195 117 63 113 148 193 200 244 184 78 241 149 121 126 50 112 30 154 55 51 41 89 246 190 151 153 63 65 198 181 10 169 72 12 201 52 72 22 204 131 222 24 10 221 236 87 113 65 20 141 62 36 164 225 207 7 155 128 203 25 12 154 209 236 22 100 15 226 124 92 21 165 151 200 65 146 192 88 248 141 78 206 163 14 233 133 
received crc = 33192
UDP datagram received length 264 From 192.168.4.2 seq 2
Data 77 251 48 118 42 121 129 7 67 162 230 53 213 59 37 197 193 141 52 182 213 227 199 46 75 85 150 223 234 241 65 135 39 219 58 83 239 156 159 52 64 66 239 191 122 98 60 199 129 185 108 152 250 218 96 27 50 214 58 247 41 119 207 176 186 52 56 62 79 76 197 17 88 190 13 135 198 70 177 124 143 241 122 176 198 99 220 29 182 13 31 131 76 173 165 245 129 217 66 208 6 178 196 192 57 13 157 152 163 234 140 73 79 19 194 41 54 136 181 253 115 33 57 178 136 234 159 188 203 14 205 21 13 254 6 202 28 179 30 250 87 106 45 159 70 243 18 67 224 127 190 231 11 180 143 232 163 135 141 77 119 220 191 167 37 237 163 111 135 134 186 0 37 69 40 247 152 45 147 208 57 167 229 190 212 82 173 178 23 78 10 225 34 24 103 125 82 89 180 138 83 43 108 204 1 180 182 78 50 62 39 230 49 230 4 79 142 155 222 75 177 10 103 83 245 43 104 205 159 246 51 225 75 181 8 122 18 101 135 52 119 226 181 205 174 133 65 90 149 229 183 84 117 102 44 121 
received crc = 32674

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.