My arduinos only connect when i open the serial monitor

Hello

I am newish to Arduino. I am working on a project that has 3 Arduinos Uno connect to my main vb.net project via Ethernet. However the problem is that i need those Arduinos to connect as soon as i run my main vb code but for some reason they only connect when i open each arduinos serial monitor. Each arduino has a unique ip address and all three connect perfectly when connected.

If anyone could help i would be greatful. Have a nice day.

Here is one of my arduinos code They are all much the same minus the ip address of course.

#include <Ethernet.h>
#include <SPI.h>
#include "Wire.h"

 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 55, 2 };
byte server[] = { 192, 168, 55, 4 };

 

int IC[] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27};

EthernetClient client;

 
String Msg;
String wholeMsg;
String array[13];

 

byte cardAB[2];
int carAdr;
int j;

 

void setup() {

  // put your setup code here, to run once:

  {

    Ethernet.begin(mac, ip, 10000);
    Serial.begin(9600);
    client.connect(server, 8080);

  

    if (client.connect(server, 8080)) {

     

      client.println("Client connected.");

    } else {

      client.connect(server, 8080);

    }

}

  for (int i = 0; i < 8; i++)

  {

    //Initialise ICs as outputs

    Wire.begin(); // wake up I2C bus

    Wire.beginTransmission(IC[i]);

    Wire.write(0x00); // A register

    Wire.write(0x00); // set all of port A to outputs

    Wire.endTransmission();

 

    Wire.begin(); // wake up I2C bus

    Wire.beginTransmission(IC[i]);

    Wire.write(0x1); // B register

    Wire.write(0x00); // set all of port B to outputs

    Wire.endTransmission();

  }

  for (int i = 0; i < 8; i++)

  {

    //Set all relays to LOW

    Wire.beginTransmission(IC[i]);

    Wire.write(0x12);      // address bank A

    Wire.write((byte)~(0x00));  // value to send - all LOW

    Wire.endTransmission();

    Wire.beginTransmission(IC[i]);

    Wire.write(0x13);      // address bank B

    Wire.write((byte)~(0x00));  // value to send - all LOW

    Wire.endTransmission();

    delay (500);

  }

}

 

void loop() {

  // put your main code here, to run repeatedly:

  if (!client.connected()) {

   

    client.connect(server, 8080);

   

  }

  if (client.available()) {

    char serverMsg = client.read();

    Serial.println(serverMsg);

    Serial.println("String " + Msg);

    Msg = String(serverMsg);

    wholeMsg = wholeMsg + Msg;  //FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7F

    Serial.println("Full Message " + wholeMsg);

    //client.println("3218");

  }

  if (wholeMsg.length() == 32)

  {

    for (int i = 0; i <= 8; i++)

    {

      array[i] = wholeMsg.substring(0, 4);

      wholeMsg = wholeMsg.substring(4);

      Serial.println("wholeMsg four " + wholeMsg);

    }

    wholeMsg = "";

    for (int i = 0; i <= 7; i++)

    {

      Serial.println("Card For Loop " + String(i));

      j = i;

      Serial.println("Value of j " + String(j));

      char *charPtr = array[i].c_str();

      unsigned long number = strtoul( charPtr, nullptr, 16);

     

      for (int x = 1; x >= 0; x--)

      {

        Serial.println(long (number));

        Serial.println("AB Loop " + String(i));

        cardAB[x] = number & 0xFF;

        number >>= 8;

      }

      switch (j) {

      case 7:

        carAdr = 0x20;

        break;

      case 6:

        carAdr = 0x21;

        break;

      case 5:

        carAdr = 0x22;

        break;

      case 4:

        carAdr = 0x23;

        break;

      case 3:

        carAdr = 0x24;

        break;

      case 2:

        carAdr = 0x25;

        break;

      case 1:

        carAdr = 0x26;

        break;

      case 0:

        carAdr = 0x27;

        break;

      }

      Serial.println("Card addr " + String(carAdr));

      Serial.println("1A Value " + String(cardAB[0]));

      Serial.println("0A Value " + String(cardAB[1]));

     

      Wire.beginTransmission(carAdr);

      Wire.write(0x12);      // address bank

      Wire.write(cardAB[1]);  // value to send

      Wire.endTransmission();

 

      Wire.beginTransmission(carAdr);

      Wire.write(0x13);      // address bank

      Wire.write(cardAB[0]);  // value to send

      Wire.endTransmission();

      Serial.println("End Transmission");


    }

    wholeMsg = "";

    Serial.println("Empty String " + wholeMsg);

}

when you open the serial monitor, your arduino UNO reboots, hence executing again the setup() (within which you have an extra pair of {} that are useless).

that gives a chance to your arduino to connect to the server. it might have failed before if your server (I guess your vb code listening on port 8080?) was not answering.

➜ you need to make the arduino code try to connect multiple times to the vb code and not just once when you power it

2 Likes

I moved your topic to an appropriate forum category @paulmurph.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

So i made a while loop and got rid of the useless {}. But the problem still consists

#include <Ethernet.h>

#include <SPI.h>

#include "Wire.h"

 

 

 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

byte ip[] = { 192, 168, 55, 2 };

byte server[] = { 192, 168, 55, 4 };

 
int IC[] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27};

 
EthernetClient client;

String Msg;

String wholeMsg;

String array[13];


byte cardAB[2];

int carAdr;

int j;

int test = 0;


void setup() {

  // put your setup code here, to run once:

    Ethernet.begin(mac, ip, 10000);

    Serial.begin(9600);

    client.connect(server, 8080);

    while(!client.connect(server, 8080)){

     

      client.connect(server, 8080);

     

    }


  for (int i = 0; i < 8; i++)

  {

    //Initialise ICs as outputs

    Wire.begin(); // wake up I2C bus

    Wire.beginTransmission(IC[i]);

    Wire.write(0x00); // A register

    Wire.write(0x00); // set all of port A to outputs

    Wire.endTransmission();

 

    Wire.begin(); // wake up I2C bus

    Wire.beginTransmission(IC[i]);

    Wire.write(0x1); // B register

    Wire.write(0x00); // set all of port B to outputs

    Wire.endTransmission();

  }

  for (int i = 0; i < 8; i++)

  {

    //Set all relays to LOW

    Wire.beginTransmission(IC[i]);

    Wire.write(0x12);      // address bank A

    Wire.write((byte)~(0x00));  // value to send - all LOW

    Wire.endTransmission();

    Wire.beginTransmission(IC[i]);

    Wire.write(0x13);      // address bank B

    Wire.write((byte)~(0x00));  // value to send - all LOW

    Wire.endTransmission();

    delay (500);

}

}

void loop() {

  // put your main code here, to run repeatedly:

 

  if (!client.connected()) {

   

    client.connect(server, 8080);

   

  }

  if (client.available()) {

    char serverMsg = client.read();

    Serial.println(serverMsg);

    Serial.println("String " + Msg);

    Msg = String(serverMsg);

    wholeMsg = wholeMsg + Msg;  //FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7F

    Serial.println("Full Message " + wholeMsg);

    //client.println("3218");

  }

 

  if (wholeMsg.length() == 32)

  {

    for (int i = 0; i <= 8; i++)

    {

      array[i] = wholeMsg.substring(0, 4);

      wholeMsg = wholeMsg.substring(4);

      Serial.println("wholeMsg four " + wholeMsg);

    }

    wholeMsg = "";

    for (int i = 0; i <= 7; i++)

    {

      Serial.println("Card For Loop " + String(i));

      j = i;

      Serial.println("Value of j " + String(j));

      char *charPtr = array[i].c_str();

      unsigned long number = strtoul( charPtr, nullptr, 16);

    
      for (int x = 1; x >= 0; x--)

      {

        Serial.println(long (number));

        Serial.println("AB Loop " + String(i));

        cardAB[x] = number & 0xFF;

        number >>= 8;

      }

      switch (j) {

      case 7:

        carAdr = 0x20;

        break;

      case 6:

        carAdr = 0x21;

        break;

      case 5:

        carAdr = 0x22;

        break;

      case 4:

        carAdr = 0x23;

        break;

      case 3:

        carAdr = 0x24;

        break;

      case 2:

        carAdr = 0x25;

        break;

      case 1:

        carAdr = 0x26;

        break;

      case 0:

        carAdr = 0x27;

        break;

      }

      Serial.println("Card addr " + String(carAdr));

      Serial.println("1A Value " + String(cardAB[0]));

      Serial.println("0A Value " + String(cardAB[1]));

     

      Wire.beginTransmission(carAdr);

      Wire.write(0x12);      // address bank

      Wire.write(cardAB[1]);  // value to send

      Wire.endTransmission();

 

      Wire.beginTransmission(carAdr);

      Wire.write(0x13);      // address bank

      Wire.write(cardAB[0]);  // value to send

      Wire.endTransmission();

      Serial.println("End Transmission");

     

    }

    wholeMsg = "";

    Serial.println("Empty String " + wholeMsg);

  }

}

I see the connect code in both the setup and loop portion of your code. Do you need the while loop in both?

For the Ethernet device, how long does it take to startup and be ready for the setup from the Arduino? Is the connect function in startup getting stuck in a loop because of the Ethernet startup delay? This makes sense to me because the Arduino reset without resetting the Ethernet port gets it running.

1 Like

Thanks for your reply

I think i understand what you are saying however there is little delay in the start up of the ethernet device but even if there was a delay, from what i understand the arduino will stay in the while loop constantly trying to connect until it is able to . So it will therefore connect as soon as the ethernet device becomes available.

Am i wrong in saying this?.

try something like this (typed here from your code, did not check it at all)

#include <Ethernet.h>
#include <SPI.h>
#include "Wire.h"

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 55, 2 };
byte server[] = { 192, 168, 55, 4 };
const byte IC[] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27};


EthernetClient client;

String wholeMsg;
String array[13];
byte cardAB[2];
int carAdr;
int j;
int test = 0;

void waitUntilConnected() {
  while (!client.connect(server, 8080)) Serial.write('.');
  Serial.println();
}

void setup() {
  wholeMsg.reserve(32);
  Serial.begin(115200);
  Ethernet.begin(mac, ip, 10000);

  for (int i = 0; i < 8; i++) {
    Wire.begin(); // wake up I2C bus
    Wire.beginTransmission(IC[i]);
    Wire.write(0x00);             // A register
    Wire.write(0x00);             // set all of port A to outputs
    Wire.endTransmission();

    Wire.begin(); // wake up I2C bus
    Wire.beginTransmission(IC[i]);
    Wire.write(0x1);            // B register
    Wire.write(0x00);           // set all of port B to outputs
    Wire.endTransmission();
  }

  for (int i = 0; i < 8; i++) {
    //Set all relays to LOW
    Wire.beginTransmission(IC[i]);
    Wire.write(0x12);           // address bank A
    Wire.write((byte)~(0x00));  // value to send - all LOW
    Wire.endTransmission();

    Wire.beginTransmission(IC[i]);
    Wire.write(0x13);           // address bank B
    Wire.write((byte)~(0x00));  // value to send - all LOW
    Wire.endTransmission();
    delay (500);
  }
  waitUntilConnected();
}

void loop() {

  if (!client.connected()) {
    client.stop();
    waitUntilConnected();
  }


  if (client.available()) {
    wholeMsg = "";
    while (client.available()) wholeMsg += (char) client.read();
    Serial.println("Full Message " + wholeMsg);

    if (wholeMsg.length() == 32) {
      for (int i = 0; i <= 8; i++) {
        array[i] = wholeMsg.substring(0, 4);
        wholeMsg = wholeMsg.substring(4);
        Serial.println("wholeMsg four " + wholeMsg);
      }

      for (int i = 0; i <= 7; i++) {
        Serial.println("Card For Loop " + String(i));
        j = i;
        Serial.println("Value of j " + String(j));
        const char *charPtr = array[i].c_str();
        unsigned long number = strtoul( charPtr, nullptr, 16);
        for (int x = 1; x >= 0; x--) {
          Serial.println(long (number));
          Serial.println("AB Loop " + String(i));
          cardAB[x] = number & 0xFF;
          number >>= 8;
        }

        // big switch for a simple subtraction...
        switch (j) {
          case 7: carAdr = 0x20; break;
          case 6: carAdr = 0x21; break;
          case 5: carAdr = 0x22; break;
          case 4: carAdr = 0x23; break;
          case 3: carAdr = 0x24; break;
          case 2: carAdr = 0x25; break;
          case 1: carAdr = 0x26; break;
          case 0: carAdr = 0x27; break;
        }

        Serial.println("Card addr " + String(carAdr));
        Serial.println("1A Value " + String(cardAB[0]));
        Serial.println("0A Value " + String(cardAB[1]));

        Wire.beginTransmission(carAdr);
        Wire.write(0x12);      // address bank
        Wire.write(cardAB[1]);  // value to send
        Wire.endTransmission();

        Wire.beginTransmission(carAdr);
        Wire.write(0x13);      // address bank
        Wire.write(cardAB[0]);  // value to send
        Wire.endTransmission();

        Serial.println("End Transmission");
      }
    } else {
      Serial.println(F("Message was not 32 byte long"));
    }
  }
}

Note that your switch/case is lots of work for a simple subtraction... (and does not have a default case).
➜ just do

carAdr = 0x27 - j;
1 Like

Dear J-M-L

Thank you very much for helping me solve this problem. You have added a few years onto my life now that the stress is gone.

Kind regard
Paul

:wink:

glad if that was helpful

have fun

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.