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)