Losing IP Addresses in UDP sketch...

I’m getting a headache. I’ve called the MIA department (Missing IP Address), but no love.

My test sketch is modelled after the UDPSendReceiveString example. I want to add simple UDP comms to an existing program so that it can communicate to other instances of the same program.

I have attached the code. I’ve defined two variables globally, set them as IPAddress() type. [Note: they were byte arrays at one point in time]

They appear to bet set correctly in setup(). But they are empty in loop(). At least by the time the Udp.beginPacket() is called.

Why???

Here is some test output:

10, 1, 1, 10 <-local

10, 1, 1, 20 <-remote

Setup: Found Ethernet Shield
Setup: Cable connected
Setup: UDP port connection successful to 8888

Loop: _UDP packet begin failed. Host not found
0, 0, 0, 0 <-local
0, 0, 0, 0 <-remote
1 characters sent
Error occurred during write

And here is the code…

// sends a message first

#include <SPI.h>
#include <Ethernet.h>
// #include <EthernetUdp2.h>

#define localID 10 // local
#define remoteID 20 // remote
#define localPORT 8888 // Port
#define EthMegPin 53
#define debugDelay 2500
#define Halt while(true)
#define Forever while(true)
boolean start = true;

// define UDP object
EthernetUDP sendUdp;

char UDPRecvBuffer[UDP_TX_PACKET_MAX_SIZE];
char UDPSendBuffer[] = "ack";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xFE, 0x00 };
IPAddress local_IP;
IPAddress remote_IP;
int packetSize;
byte localScriptToken;
int remote_message = 0;



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

  pinMode(EthMegPin, OUTPUT);
  digitalWrite(EthMegPin, HIGH);
  // Ethernet.init(10);  // Most Arduino shields

  // localID read in setup
  mac[4] = localID;
  IPAddress local_IP( 10, 1, 1, localID );
  for (int i = 0; i < 4; i++) {
    Serial.print(local_IP[i]);
    if (i < 3) Serial.print(", ");
  }; Serial.println(" <-local");

  IPAddress remote_IP(10, 1, 1, remoteID);
  for (int i = 0; i < 4; i++) {
    Serial.print(remote_IP[i]);
    if (i < 3) Serial.print(", ");
  }; Serial.println(" <-remote");

  Ethernet.begin(mac, local_IP);

  //print out the IP address
  Serial.print("Setup: Local IP = ");
  Serial.println(Ethernet.localIP());
  Serial.print("Setup: Rem't IP = 10, 1, 1, ");
  Serial.println(remoteID);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Setup: Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    Halt;
  } else {
    Serial.println("Setup: Found Ethernet Shield");
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Setup: Ethernet cable is not connected.");
    Halt;
  } else {
    Serial.println("Setup: Cable connected");
  }
  Serial.println("...");

  int Udp_Conn = sendUdp.begin(localPORT);
  if (Udp_Conn) {
    Serial.print("Setup: UDP port connection successful to ");
    Serial.println(localPORT);
  }
  else {
    Serial.print("Setup: UDP port connection failed to ");
    Serial.println(localPORT);
    Halt;
  }
  Serial.println();
}


void loop() {
  // int UdpConn = sendUdp.beginPacket(remote_IP, sendUdp.remotePort());
  int UdpConn = sendUdp.beginPacket(remote_IP, 8888);
  if (UdpConn) {
    Serial.println("Loop: Begin packet successful");
  }
  else {
    Serial.println("Loop: _UDP packet begin failed. Host not found");
    Serial.print("...Host device IP address: ");

    for (int i = 0; i < 4; i++) {
      Serial.print(local_IP[i]);
      if (i < 3) Serial.print(", ");
    }; Serial.println(" <-local");

    for (int i = 0; i < 4; i++) {
      Serial.print(remote_IP[i]);
      if (i < 3) Serial.print(", ");
    }; Serial.println(" <-remote");
  }

  remote_message ++;
  itoa(remote_message, UDPSendBuffer, 10);
  int charsSent = sendUdp.write(UDPSendBuffer);
  delay(debugDelay);
  Serial.print(charsSent);
  Serial.println(" characters sent");
  int UdpSent = sendUdp.endPacket();
  if (UdpSent) {
    Serial.println("Message sent via UDP");
  }
  else {
    Serial.println("Error occurred during write");
  }
  //  delay(3000);
  Halt;
}

TII-Interact_commsTest_Send.ino (3.14 KB)

You declare them as globals. Then you redeclare them as local in setup and initialize them. The globals are untouched, as you see in loop.

A MAC address has 6 octets.

 mac[4] = localID;
  IPAddress local_IP( 10, 1, 1, localID );

This is a new variable, with the same name as the uninitialized global variable.

 IPAddress remote_IP(10, 1, 1, remoteID);

Same here.

This code, in loop(),

   for (int i = 0; i < 4; i++) {
      Serial.print(local_IP[i]);

is printing the global variable that has never been initialized.

As far as the MAC address, I miscounted. Oops.

I think I finally get it. Since the Ip addresses get set in setup() and used in loop(), the two definitions are in conflict. The code before this one defined each address as a global 5 element byte array. Then IPAddress was used as a function, not a type, to initialize their values.

That’s wrong.

You all have given me the mental kick I needed. Thanks.