-->
Page 1 of 1

Help with - creating an IP address from variables.

PostPosted: Wed May 18, 2016 5:18 pm
by Barnabybear
Hi, I’m using the Arduino IDE 2.0 & WiFiUdp.h to send sCAN packets. With the way I need it to work, it has to send to various multicast addresses that are defined by variables. If I could pre-set these it would be easy, but I can’t and there are 65,535 (0xffff) possible options,
Code: Select all239.255.0.1 through to 239.255.255.255

so a look up is not going to work unless I have to.
Does anyone know how to construct an IP address from variables?
I'm currently using:
Code: Select allIPAddress IP_Multicast_Out(239, 255, 0, 1);
unsigned int Port_Multicast_Out = 5568;

followed by
Code: Select allUdp.beginPacketMulticast(IP_Multicast_Out, Port_Multicast_Out, WiFi.localIP());

which works fine for Udp.writes to a single IP. If I try to introduce variables at any point (that I've tried - to many to list) it doesn't compile or sends to 0.0.0.0 .
I'm guessing the 'IPAddress' format is something I'm missing, any help appreciated.

Re: Help with - creating an IP address from variables.

PostPosted: Thu May 19, 2016 4:04 am
by bbx10node
IP address is a uint32_t but can be accessed as an array of 4 uint8_t.

Code: Select all#include <ESP8266WiFi.h>

void setup() {
  Serial.begin(115200);
  Serial.println();

  IPAddress ipa;
  ipa[0] = 239;
  ipa[1] = 255;
  ipa[2] =   0;
  for (int i = 0; i < 256; i++) {
    ipa[3] = i;
    Serial.println(ipa);
  }
}

void loop() {
}

Re: Help with - creating an IP address from variables.

PostPosted: Thu May 19, 2016 6:07 am
by Barnabybear
Hi, thanks bbx. That is just what was needed, it works a treat :)