Udp.beginPacket(URL, port) not possible?

Hi,
I have just got UDP logging to my InfluxDB working but only when I use the server ip address. The IP of my server changes every 24 hours so I am using a dynamic DNS service. My software runs at the Moment on a Photon but as soon as all features are ready I would like to move to my Electron.

Udp.beginPacket(dyndns_address, port)

does not work so I am using:

IPAddress ip = WiFi.resolve(INFLUXDB_HOST);

But this is not possible on an Electron and I could not find any similar function in the docs.
The TCP protocol supports client.connect(URL, port); but it consumes to much data on an Electron.
I would be very grateful if you could tell me if there is a way to resolve an URL to an IP address on an Electron?
Will there be support for Udp.beginPacket(URL, port) in the future?

Kind regards

1 Like

The workaround that I use is simple electronResolve function below. It works like WiFi.resolve() except on the Electron, using the DNS functions built into the cellular modem.

#include "Particle.h"

boolean checked = false;

IPAddress electronResolve(const char *hostname); // forward declaration

const char *testHostname = "www.google.com";

void setup() {
	Serial.begin(9600);
}

void loop() {
	if (!checked) {
		checked = true;

		IPAddress result = electronResolve(testHostname);
		Serial.printlnf("looking up %s got %s", testHostname, result.toString().c_str());
	}
}


static int electronResolveCallback(int type, const char* buf, int len, IPAddress *pResult) {

	// AT+UDNSRN=0,"www.google.com"
	// +UDNSRN: "216.239.59.147"
	// OK
	if (type == TYPE_PLUS && pResult != NULL) {
		char *mutableCopy = (char *) malloc(len + 1);
		if (mutableCopy != NULL) {
			strncpy(mutableCopy, buf, len);
			mutableCopy[len] = 0;

			char *quoted = strtok(mutableCopy, "\"");
			if (quoted != NULL) {
				quoted = strtok(NULL, "\"");
				if (quoted != NULL) {
					int addr[4];
					if (sscanf(quoted, "%u.%u.%u.%u", &addr[0], &addr[1], &addr[2], &addr[3]) == 4) {
						*pResult = IPAddress(addr[0], addr[1], addr[2], addr[3]);
					}
				}
			}

			free(mutableCopy);
		}
	}

	return WAIT;
}

IPAddress electronResolve(const char *hostname) {
	IPAddress result;

	Cellular.command(electronResolveCallback, &result, 10000, "AT+UDNSRN=0,\"%s\"\r\n", hostname);

	return result;
}

4 Likes