Cannot connect to UDP webserver MKR NB 1500

Hello everybody. I am trying to connect to a UPD Webserver. I used the NBWebClient example project for the MKRNB 1500 with sim and antenna. With this code I had no trouble connecting to the http test website. But for this UPD server I am not sure what is going wrong. So far I know that something with this code part might not be right:

client.connect(server, port)

I saw that the function has a return value which I am saving and printing in the serial monitor. it takes a very long time. Sometimes it does not work and sometimes it gives me back a 0, connects and then disconnects again. My colleague who built the webserver (according to this example UDP - Client and Server example programs in Python | Pythontic.com) added the function if the client send a message Hello UDP Server it should answer Hello UDP Client. But nothing happens. even tough it quickly connects sometimes.

image

.
Unfortunately I have trouble using the debugger in the Arduino IDE and was not able to use it yet (error message Failed to launch OpenOCD GDB Server: Timeout)

/*
  Web client

  This sketch connects to a website through a MKR NB 1500 board. Specifically,
  this example downloads the URL "http://example.org/" and
  prints it to the Serial monitor.

  Circuit:
   - MKR NB 1500 board
   - Antenna
   - SIM card with a data plan

  created 8 Mar 2012
  by Tom Igoe
*/

// libraries
#include <MKRNB.h>
//#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[]     = "xxxx";

// initialize the library instance
NBClient client;
GPRS gprs;
NB nbAccess;

// URL, path and port (for example: example.org)
char server[] = "ec2-63-32-126-88.eu-west-1.compute.amazonaws.com";
//char server[] = "ec2-63-32-126-88.eu-west-1.compute.amazonaws.com";//"example.org";// "63.32.126.88"; // "ec2-63-32-126-88.eu-west-1.compute.amazonaws.com";
//char path[] = "/";
int port = 20001; //20001; // port 80 is the default for HTTP

void setup() {// initialize serial communications and wait for port to open:

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Starting Arduino web client.");
  // connection state
  boolean connected = false;
  // After starting the modem with NB.begin()
  // attach to the GPRS network with the APN, login and password
 
  while (!connected) {
//    Serial.println("TEST: connecting");
    if ((nbAccess.begin(PINNUMBER) == NB_READY) &&
        (gprs.attachGPRS() == GPRS_READY)) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  int connResult = client.connect(server, port);
  Serial.println(connResult);
  if (connResult == 0) {
//  if (client.connect(server, port)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("Hello UDP Server");
    Serial.println("message to server sent");
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {
  // if there are incoming bytes available from the server, read them and print them: 
  if (client.available()) {
    Serial.print((char)client.read());
  }
  // if the server's disconnected, stop the client:
  if (!client.available() && !client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for (;;)
      ;
  }
} 

That's impossible as a webserver is using TCP by definition.

That's not a webserver! That's a UDP server which has nothing to do with HTTP and Web in general.

What are you trying to achieve? Don't tell us how you're trying to achieve it (UDP, webserver, etc.) but what this is used for.
Does your colleague who built the UDP server have a specification of the protocol to run for that server? Is he an expert in server programming or if not, why did he think UDP might be a good choice for your project?

Hello,
I have understand what problem you're facing. I have done a project named as pubg name generator and faced the same problem with it. But after too much struggle I have applied some steps to overcome this problem. You can also done by following these steps:

  1. Create a UDP socket.
  2. Bind the socket to the server address.
  3. Wait until the datagram packet arrives from the client.
  4. Process the datagram packet and send a reply to the client.
  5. Go back to Step 3.

good to know, thank you!

I have a MKR NB 1500 and I want to connect to a UPD Server. Was this not clear?
We use a UDP server because we are communicating with NB-IoT. This is an Project just to test this technology. I don't need suggestions using LTE-M with TCP IP.

Thankyou @evelynesorhene23 for the suggestion. I will try this and get back to you asap :slight_smile:

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