Everything seems ok yet Arduino Mega is still not in network

Hello guys,
I am trying to connect Arduino Mega 2560 over USB using standart Serial.

Everything seems correctly set, compiles and a uploads

When i open script it will write:

Connecting device at COM4 to cloud.blynk.cc:8442...
OpenC0C("\\.\COM4", baud=115200, data=8, parity=no, stop=1) - OK
Connect("cloud.blynk.cc", "8442") - OK
InOut() START
DSR is OFF

and nothing more happens, applications keeps saying: Your Arduino Mega is not in network.
I see aproximately once per second some data send over Arduino TX:

02 00 01 00 20 <AUTH_CODE>
  • probably to keep connection open.

I have tried to update auth key several times without succes. When i keep pressed reset on Arduino in some time i get probably some timeout from server:

Received EOF
EVENT_CLOSE
InOut() - STOP
Disconnect() - OK
Connect("cloud.blynk.cc", "8442") - OK
InOut() START
DSR is OFF

My code:

    #include <BlynkSimpleSerial.h>

    #define BLYNK_PRINT Serial

char auth[ ] = "e130f9caba0e4a8f8e46996620c47893";

// the setup function runs once when you press reset or power the board
void setup() {
    Serial.begin( 115200 );
    Blynk.begin( auth,115200);  
}

BLYNK_READ( V1 ) {
    Blynk.virtualWrite( V1, millis( ) / 1000 );
}

// the loop function runs over and over again until power down or reset
void loop() {
    Blynk.run();
}

I’ve also tried software serial and different speeds settings - result is always the same. Also tried exactly same sketch as in tutorial for Arduino with USB. I use Arduino 1.6 and Blynk 0.3.1

Thanks for any reply as i am running out of ideas.
Martin

Ok after some more trying today i’ve managed to make it work. Here is the conlusion:

  • there are probably some timeouts and so on and establishing of connection can sometimes work in mysterious ways, so when u are relying on connection trough USB be sure that your sketch looks something like this:

    void setup() {

    Blynk.begin( auth, 115200);

    /*

    • OTHER INITIALIZATIONS
      */

    Blynk.connect();
    while (Blynk.connected() == false) {
    Blynk.connect();
    }
    }
    void loop() {
    while (Blynk.connected() == false) {
    Blynk.connect();
    }
    Blynk.run();
    /*
    *YOUR OTHER CODE
    */
    }
    Because there is sometimes mismatch between what blynk thinks on the side of Arduino and Android.
    I’ve seen situation that Blynk in Arduino thought that it is not connected yet app was kept updated and i’ve also seen opposite. e.g. app was not updates but Blynk on Arduino side thought that it is connected. It seems that main reason for those problem is probably in order in which you start program in Arduino, scrip on your PC ans application in android - I’ve did not analyzed exact problematic combination but checking whether Blynk is really connected resolved problem.

Hopefully this post will help someone. See you guys and good luck!