Udp send/receive errors

Hi,

I’m developing a system where 3 Arduinos are controlled over Wifi from a visual basic program running on my laptop. The Arduinos control a set of relays to power an A/C motor forwards or backwards, and read an encoder to determine the position of the motor.

Some specifics on the project:
1.) Arduino Uno Wifi Rev 2
2.) Dedicated Wifi router with just the PC and the 3 Arduinos connected to it.
3.) Commands from PC are 4 bytes and all start with a ‘$’
4.) PC sends an “alive” message every 500ms on top of any other commands.
5.) Arduinos send their current location every 1s
6.) Arduinos have static IP addresses reserved by the router

The main loop basically checks for a new command, processes it, makes sure PC is still alive, sends location update, and handles an initial calibration:

void loop() 
{
  // if there's data available of the right size, process the command
    
  Packet_Size = Udp.parsePacket();
  if (Packet_Size >= PACKETSIZE) {
    Process_Command();
  }
  
  // Check if Valid command has been received within 1s
  if (millis() >= lastUpdate + COMMAND_TIMEOUT){ 
    All_Stop();                     // If PC is no longer talking to us, stop all motion
  }
    
  // Update current location if moving, and handle no movement timer for calibration mode
  Update_Location();
  
  // If in calibration mode AND time out has occured (no movement detected for TIME_OUT period)
  if (Being_Calibrated && (Timer > Time_Out)){
    
    Cal_Zero();                 // Set "zero" location of tower and exit calibration mode.
  }
  
}   // End of loop

The Process command function reads the message, and a switch statement handles each command:

void Process_Command(){
  
  Udp.read(ReceiveBuffer, PACKETSIZE);

Serial.print("Received Something");
  
  if(ReceiveBuffer[0] == '

The update location function takes the current location and sends to back to the PC:

void Update_Location (){

  if (millis() >= update_timer + UPDATE_TIMEOUT){
    if (Udp.beginPacket(Udp.remoteIP(), Udp.remotePort())){ // Send current location back to PC controller every UPDATE_TIMEOUT ms
      ReplyBuffer[0] = '

PROBLEM:

Every now and then, the Arduino appears to stop communicating. I’ve got serial output scattered around the code to try and debug what is going on. I’ve double check the visual basic code, and I have exception handling there to deal with any drop out of the Arduinos. So, I’m pretty sure the Arduinos are the problem. Resetting the Arduino recovers the communications.

When the issue happens, the relays turn off (since there is no alive message from the PC), but no new commands are received, and the PC shows that it is not receiving the periodic location messages (but the serial output is still showing the location and the encoder is still being read).

I tried adding some code to “stop” the Udp and “begin” it again to try and recover, but it did not help. I’m going to add some more error checking to the code, but I’m looking for the proper way to recover from a loss of communications. Should I stop the Udp, then Stop the Wifi, then begin the Wifi, and the begin the Udp?

I’ve been running the current code for a couple of hours, and have not seen the issues, so I’m wondering if the serial.prints are affecting the timing of the code or Wifi reception. I was planning on removing all of the serial communications from the final code, should I just leave them?

Thanks for any help or suggestions. (full code attached)

Tower_Controller.ino (17.6 KB)){                // check for proper command start ’


The update location function takes the current location and sends to back to the PC:

§DISCOURSE_HOISTED_CODE_2§


PROBLEM:

Every now and then, the Arduino appears to stop communicating. I've got serial output scattered around the code to try and debug what is going on. I've double check the visual basic code, and I have exception handling there to deal with any drop out of the Arduinos. So, I'm pretty sure the Arduinos are the problem. Resetting the Arduino recovers the communications. 

When the issue happens, the relays turn off (since there is no alive message from the PC), but no new commands are received, and the PC shows that it is not receiving the periodic location messages (but the serial output is still showing the location and the encoder is still being read). 

I tried adding some code to "stop" the Udp and "begin" it again to try and recover, but it did not help. I'm going to add some more error checking to the code, but I'm looking for the proper way to recover from a loss of communications. Should I stop the Udp, then Stop the Wifi, then begin the Wifi, and the begin the Udp?

I've been running the current code for a couple of hours, and have not seen the issues, so I'm wondering if the serial.prints are affecting the timing of the code or Wifi reception. I was planning on removing all of the serial communications from the final code, should I just leave them?

Thanks for any help or suggestions. (full code attached)

[Tower_Controller.ino|attachment](upload://jhpMC807POqC6MT3C67nF1LIOHg.ino) (17.6 KB)
    
    switch (ReceiveBuffer[1]){
      case 'E':
        if (ReceiveBuffer[2] == 'S' && ReceiveBuffer[3] == 'T'){

Serial.println("\n Received Emergency Stop");

The update location function takes the current location and sends to back to the PC:

§_DISCOURSE_HOISTED_CODE_2_§

PROBLEM:

Every now and then, the Arduino appears to stop communicating. I’ve got serial output scattered around the code to try and debug what is going on. I’ve double check the visual basic code, and I have exception handling there to deal with any drop out of the Arduinos. So, I’m pretty sure the Arduinos are the problem. Resetting the Arduino recovers the communications.

When the issue happens, the relays turn off (since there is no alive message from the PC), but no new commands are received, and the PC shows that it is not receiving the periodic location messages (but the serial output is still showing the location and the encoder is still being read).

I tried adding some code to “stop” the Udp and “begin” it again to try and recover, but it did not help. I’m going to add some more error checking to the code, but I’m looking for the proper way to recover from a loss of communications. Should I stop the Udp, then Stop the Wifi, then begin the Wifi, and the begin the Udp?

I’ve been running the current code for a couple of hours, and have not seen the issues, so I’m wondering if the serial.prints are affecting the timing of the code or Wifi reception. I was planning on removing all of the serial communications from the final code, should I just leave them?

Thanks for any help or suggestions. (full code attached)

Tower_Controller.ino (17.6 KB);                                // Setup reply buffer
      ReplyBuffer[1] = controller_ID;                      // Set ID in Udp reply buffer
      ReplyBuffer[2] = char(Current_Location / 256);      // convert unsigned int to pair of chars
      ReplyBuffer[3] = char(Current_Location & 0x00FF);
      Udp.write(ReplyBuffer, PACKETSIZE);
      Udp.endPacket();

Serial.print("$");
Serial.write(controller_ID);
Serial.print(String(Current_Location));
Serial.print("\n");

}

update_timer = millis();   
  }


PROBLEM:

Every now and then, the Arduino appears to stop communicating. I've got serial output scattered around the code to try and debug what is going on. I've double check the visual basic code, and I have exception handling there to deal with any drop out of the Arduinos. So, I'm pretty sure the Arduinos are the problem. Resetting the Arduino recovers the communications. 

When the issue happens, the relays turn off (since there is no alive message from the PC), but no new commands are received, and the PC shows that it is not receiving the periodic location messages (but the serial output is still showing the location and the encoder is still being read). 

I tried adding some code to "stop" the Udp and "begin" it again to try and recover, but it did not help. I'm going to add some more error checking to the code, but I'm looking for the proper way to recover from a loss of communications. Should I stop the Udp, then Stop the Wifi, then begin the Wifi, and the begin the Udp?

I've been running the current code for a couple of hours, and have not seen the issues, so I'm wondering if the serial.prints are affecting the timing of the code or Wifi reception. I was planning on removing all of the serial communications from the final code, should I just leave them?

Thanks for any help or suggestions. (full code attached)

[Tower_Controller.ino|attachment](upload://jhpMC807POqC6MT3C67nF1LIOHg.ino) (17.6 KB)){                // check for proper command start '

The update location function takes the current location and sends to back to the PC:

§_DISCOURSE_HOISTED_CODE_2_§

PROBLEM:

Every now and then, the Arduino appears to stop communicating. I’ve got serial output scattered around the code to try and debug what is going on. I’ve double check the visual basic code, and I have exception handling there to deal with any drop out of the Arduinos. So, I’m pretty sure the Arduinos are the problem. Resetting the Arduino recovers the communications.

When the issue happens, the relays turn off (since there is no alive message from the PC), but no new commands are received, and the PC shows that it is not receiving the periodic location messages (but the serial output is still showing the location and the encoder is still being read).

I tried adding some code to “stop” the Udp and “begin” it again to try and recover, but it did not help. I’m going to add some more error checking to the code, but I’m looking for the proper way to recover from a loss of communications. Should I stop the Udp, then Stop the Wifi, then begin the Wifi, and the begin the Udp?

I’ve been running the current code for a couple of hours, and have not seen the issues, so I’m wondering if the serial.prints are affecting the timing of the code or Wifi reception. I was planning on removing all of the serial communications from the final code, should I just leave them?

Thanks for any help or suggestions. (full code attached)

Tower_Controller.ino (17.6 KB)
   
    switch (ReceiveBuffer[1]){
      case ‘E’:
        if (ReceiveBuffer[2] == ‘S’ && ReceiveBuffer[3] == ‘T’){

Serial.println("\n Received Emergency Stop");


The update location function takes the current location and sends to back to the PC:

§DISCOURSE_HOISTED_CODE_2§


PROBLEM:

Every now and then, the Arduino appears to stop communicating. I've got serial output scattered around the code to try and debug what is going on. I've double check the visual basic code, and I have exception handling there to deal with any drop out of the Arduinos. So, I'm pretty sure the Arduinos are the problem. Resetting the Arduino recovers the communications. 

When the issue happens, the relays turn off (since there is no alive message from the PC), but no new commands are received, and the PC shows that it is not receiving the periodic location messages (but the serial output is still showing the location and the encoder is still being read). 

I tried adding some code to "stop" the Udp and "begin" it again to try and recover, but it did not help. I'm going to add some more error checking to the code, but I'm looking for the proper way to recover from a loss of communications. Should I stop the Udp, then Stop the Wifi, then begin the Wifi, and the begin the Udp?

I've been running the current code for a couple of hours, and have not seen the issues, so I'm wondering if the serial.prints are affecting the timing of the code or Wifi reception. I was planning on removing all of the serial communications from the final code, should I just leave them?

Thanks for any help or suggestions. (full code attached)

[Tower_Controller.ino|attachment](upload://jhpMC807POqC6MT3C67nF1LIOHg.ino) (17.6 KB)

Update:

I added a “WiFi Recovery” function that stops the Udp and Wifi, and then reinitializes the Wifi and Udp. I ran the software for a couple of hours and got one instance where this function was called and everything recovered properly. So, at least I have a solution to keep the system from crashing, but there is about a 6 second recovery time.

I would really like to know why the Arduino is losing connection with the Wifi. I think the next thing I’ll try is monitoring the Wifi.status() to see if that gives me any indication of what is going wrong.

The only other thing I can think of is my Wifi router. It is a Netgear WGR614v9 that is about 10 years old. It could be that it is getting tired or glitchy. Everything is within a few feet of each other, so signal strength should be fine.

I’m using this router since I will be running this system away from my home, and didn’t want to pack up my current router. Also, I was able to assign static IP addresses with this old router since the Wifi.config(IP) function didn’t appear to work on my current or old router. I needed static IPs, since I wasn’t sure how to detect them in visual basic. I’ll look into this more to see if I can handle random IPs from the 3 Arduinos in VB.

2nd Update:

Using the WiFi.status() function, I'm getting a WL_NO_SHIELD error periodically, and then the recovery function is called and the WiFi reconnects and everything is fine for a while.

I'm not sure what a WL_NO_SHIELD means for a Uno Wifi Rev 2 with built in Wifi? I guess it could mean the Wifi chip is being reset or losing connection?