Skip to content

WiFi Shield UDP send and receive from same port on Arduino not working well #47

@Sudeshna19

Description

@Sudeshna19

I am trying to send and receive UDP data using WiFi Shield connected to Arduino Mega ADK with IDE 1.8.1. I modified the UDPSendReceive example as below:

#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>

int status = WL_IDLE_STATUS;
char ssid[] = "**************"; //  your network SSID (name)
char pass[] = "**************";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)
unsigned int localPort[1] = {5900};      // local port to listen on

WiFiUDP Udp[1];

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
  }
  // 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. Change this line if using open or WEP network:
    status = WiFi.begin(ssid,pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  Udp[0].begin(localPort[0]);
}

void loop() {
    // Send UDP data to host
    uint8_t data[3] = {97, 98, 99};
    IPAddress remoteIPaddr(192,168,0,102);
    Udp[0].beginPacket(remoteIPaddr, 8200);
    Udp[0].write(data, 3);
    Udp[0].endPacket();

    // Receive UDP data from host
    char  databuf[5];
    int packetSize = Udp[0].parsePacket();
    if (packetSize > 0) {
      int sizercvd =0;
      sizercvd = Udp[0].read(databuf,5);
      Serial.println("Contents:");
      Serial.println(databuf);
    }

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

  // print your WiFi shield'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");
}

I have an application running on host to send and receive UDP messages. I send 'abc' from Arduino to host and 'apple' from host to Arduino. In the screenshot
udphost
you can see the host utility receives 'abc' correctly.

The Serial Monitor of IDE shows the below output:
udpserialout

Along with 'apple' it receives some junk value and the transmitted 'abc'. Does Arduino get confused in what it is sending and what it is receiving?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions