BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

[SOLVED] Blynk v0.4.8 resets Wemos D1 mini hardware on reconnection to Blynk.connect()


#5

Opp,But I think I got hardware reset too, I can connect to my nodeMCU and then it connect to Blynk server then get reset and I can see new ssid appeared and i can can connect to it again and it keep repeating this process.


#6

@Phyruos just use the ESP8266_Standalone sketch and you should see you have no general reset problems with 0.4.8 and NODEMCU.


#7

@Dmitriy @vhymanskyy
hi guys - I appreciate you are very busy but wondering if you were able to advise why I may be experiencing issues with V0.4.8 as described in this thread ? [I would assume other would be also experiencing same issues given my basic example on wemos d1 mini]


#8

@mars which version of the Arduino core are you using? If you are on 2.4.0-rc1 roll back to 2.3.0.

A lot of the WDT stuff wasn’t originally in 2.3.0 and was added to the master as issues were raised. So it’s been in the master for up to 12 months but only just found it’s way into 2.4.0-rc1.

That applies to a massive amount of ESP stuff as Espressif “dropped” 8266 for 32. Now that 2.4.0-rc1 has been released with 12+ months of bug fixes it appears the bug fixes have bugs. Might not relate to WDT specifically but I would roll back to 2.3.0 if you are not already using it.


#9

thanks costas
according to arduino ide, it tells me in board manager menu that I’m using 2.3.0. As a matter of interest were you able to replicate the issue I reported ?


#10

Yes I have just flashed my WeMos with your sketch and got the same results. I am actually using the master Arduino core and can’t easily roll back to 2.3.0 on this machine.

Just let me check something.


#11

Are you sure?

Are you seeing “reconnectBlynk(): Before Blynk.connect” before the ESP reset from the Serial.println() line above the Blynk.connect(4333) call?

Rather than mess around with any servers I am just powering down the router and back on again. This is what my Serial Monitor shows.

95 : All good and connected to Blynk!
96 : All good and connected to Blynk!
97 : All good and connected to Blynk!
98 : All good and connected to Blynk!
******************************* In reconnect
*WM: 
*WM: AutoConnect

 ets Jan  8 2013,rst cause:4, boot mode:(1,6)

wdt reset
*WM: AutoConnect
*WM: After waiting 
*WM: 3.00
*WM:  secs local ip: 
*WM: 192.168.10.171
[3062] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.8 on ESP8266

On reflection router reset is perhaps not quite the same simulation as server not accessible but WiFi still up.


#12

OK I switched routers and I’m now using a slave router with Ethernet connection to master. This is a better simulation of a server going down than powering down the router. Below is the Serial Monitor from just before removing the Ethernet cable to plugging it back in.

6795 : All good and connected to Blynk!
6796 : All good and connected to Blynk!
6797 : All good and connected to Blynk!
6798 : All good and connected to Blynk!
6799 : All good and connected to Blynk!
6800 : All good and connected to Blynk!
6801 : All good and connected to Blynk!
6802 : All good and connected to Blynk!
6803 : All good and connected to Blynk!
6804 : All good and connected to Blynk!
6805 : All good and connected to Blynk!
[6811981] Heartbeat timeout
6806 : Seemed to have lost connected to Blynk :-(
6807 : Seemed to have lost connected to Blynk :-(
6808 : Seemed to have lost connected to Blynk :-(
6809 : Seemed to have lost connected to Blynk :-(
6810 : Seemed to have lost connected to Blynk :-(
6811 : Seemed to have lost connected to Blynk :-(
6812 : Seemed to have lost connected to Blynk :-(
6813 : Seemed to have lost connected to Blynk :-(
6814 : Seemed to have lost connected to Blynk :-(
6815 : Seemed to have lost connected to Blynk :-(
6816 : Seemed to have lost connected to Blynk :-(
6817 : Seemed to have lost connected to Blynk :-(
6818 : Seemed to have lost connected to Blynk :-(
6819 : Seemed to have lost connected to Blynk :-(
6820 : Seemed to have lost connected to Blynk :-(
6821 : Seemed to have lost connected to Blynk :-(
6822 : Seemed to have lost connected to Blynk :-(
6823 : Seemed to have lost connected to Blynk :-(
******************************* In reconnect
reconnectBlynk(): WDT disabled
reconnectBlynk(): Before Blynk.connect
[6830174] Connecting to abc.xyz:8442
[6835175] Connecting to abc.xyz:8442
reconnectBlynk(): After Blynk.connect UN-successfull
reconnectBlynk(): WDT enabled
reconnectBlynk(): restarting timer and exiting function
6824 : Seemed to have lost connected to Blynk :-(
6825 : Seemed to have lost connected to Blynk :-(
6826 : Seemed to have lost connected to Blynk :-(
6827 : Seemed to have lost connected to Blynk :-(
6828 : Seemed to have lost connected to Blynk :-(
6829 : Seemed to have lost connected to Blynk :-(
6830 : Seemed to have lost connected to Blynk :-(
6831 : Seemed to have lost connected to Blynk :-(
6832 : Seemed to have lost connected to Blynk :-(
6833 : Seemed to have lost connected to Blynk :-(
6834 : Seemed to have lost connected to Blynk :-(
6835 : Seemed to have lost connected to Blynk :-(
6836 : Seemed to have lost connected to Blynk :-(
6837 : Seemed to have lost connected to Blynk :-(
6838 : Seemed to have lost connected to Blynk :-(
6839 : Seemed to have lost connected to Blynk :-(
6840 : Seemed to have lost connected to Blynk :-(
6841 : Seemed to have lost connected to Blynk :-(
6842 : Seemed to have lost connected to Blynk :-(
6843 : Seemed to have lost connected to Blynk :-(
6844 : Seemed to have lost connected to Blynk :-(
6845 : Seemed to have lost connected to Blynk :-(
6846 : Seemed to have lost connected to Blynk :-(
6847 : Seemed to have lost connected to Blynk :-(
6848 : Seemed to have lost connected to Blynk :-(
6849 : Seemed to have lost connected to Blynk :-(
6850 : Seemed to have lost connected to Blynk :-(
6851 : Seemed to have lost connected to Blynk :-(
6852 : Seemed to have lost connected to Blynk :-(
6853 : Seemed to have lost connected to Blynk :-(
6854 : Seemed to have lost connected to Blynk :-(
6855 : Seemed to have lost connected to Blynk :-(
******************************* In reconnect
reconnectBlynk(): WDT disabled
reconnectBlynk(): Before Blynk.connect
[6861349] Connecting to abc.xyz:8442
[6866350] Connecting to abc.xyz:8442
reconnectBlynk(): After Blynk.connect UN-successfull
reconnectBlynk(): WDT enabled
reconnectBlynk(): restarting timer and exiting function
6856 : Seemed to have lost connected to Blynk :-(
6857 : Seemed to have lost connected to Blynk :-(
6858 : Seemed to have lost connected to Blynk :-(
6859 : Seemed to have lost connected to Blynk :-(
6860 : Seemed to have lost connected to Blynk :-(
6861 : Seemed to have lost connected to Blynk :-(
6862 : Seemed to have lost connected to Blynk :-(
6863 : Seemed to have lost connected to Blynk :-(
6864 : Seemed to have lost connected to Blynk :-(
6865 : Seemed to have lost connected to Blynk :-(
6866 : Seemed to have lost connected to Blynk :-(
6867 : Seemed to have lost connected to Blynk :-(
6868 : Seemed to have lost connected to Blynk :-(
6869 : Seemed to have lost connected to Blynk :-(
6870 : Seemed to have lost connected to Blynk :-(
6871 : Seemed to have lost connected to Blynk :-(
6872 : Seemed to have lost connected to Blynk :-(
6873 : Seemed to have lost connected to Blynk :-(
6874 : Seemed to have lost connected to Blynk :-(
6875 : Seemed to have lost connected to Blynk :-(
6876 : Seemed to have lost connected to Blynk :-(
6877 : Seemed to have lost connected to Blynk :-(
6878 : Seemed to have lost connected to Blynk :-(
6879 : Seemed to have lost connected to Blynk :-(
6880 : Seemed to have lost connected to Blynk :-(
6881 : Seemed to have lost connected to Blynk :-(
6882 : Seemed to have lost connected to Blynk :-(
6883 : Seemed to have lost connected to Blynk :-(
6884 : Seemed to have lost connected to Blynk :-(
6885 : Seemed to have lost connected to Blynk :-(
6886 : Seemed to have lost connected to Blynk :-(
******************************* In reconnect
reconnectBlynk(): WDT disabled
reconnectBlynk(): Before Blynk.connect
[6892397] Connecting to abc.xyz:8442
[6892662] Ready (ping: 76ms).
reconnectBlynk(): After Blynk.connect successfull
reconnectBlynk(): WDT enabled
reconnectBlynk(): restarting timer and exiting function
6887 : All good and connected to Blynk!
6888 : All good and connected to Blynk!
6889 : All good and connected to Blynk!
6890 : All good and connected to Blynk!
6891 : All good and connected to Blynk!
6892 : All good and connected to Blynk!
6893 : All good and connected to Blynk!
6894 : All good and connected to Blynk!
6895 : All good and connected to Blynk!
6896 : All good and connected to Blynk!
6897 : All good and connected to Blynk!
6898 : All good and connected to Blynk!
6899 : All good and connected to Blynk!
6900 : All good and connected to Blynk!

Is this how you want it to work?


#13

hi costas

I’ve attached my serial outputs below for both sceanrio i.e. when I use v0.4.7 and v0.4.8 - hope this makes more sense:

*WM: Connecting to new AP
*WM: Connecting as wifi client...
*WM: Connection result: 
*WM: 3
[39939] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.7 on Arduino

[39941] Connecting to 192.168.0.250:8442
[39956] Ready (ping: 0ms).
BLYNK is Connected!
0 : All good and connected to Blynk!
1 : All good and connected to Blynk!
2 : All good and connected to Blynk!
3 : All good and connected to Blynk!
4 : All good and connected to Blynk!
5 : All good and connected to Blynk!
6 : All good and connected to Blynk!
7 : All good and connected to Blynk!
8 : All good and connected to Blynk!
9 : All good and connected to Blynk!
10 : All good and connected to Blynk!
11 : All good and connected to Blynk!    **[@costas this is where I terminated my local Blynk server]**
12 : Seemed to have lost connected to Blynk :-(
13 : Seemed to have lost connected to Blynk :-(
14 : Seemed to have lost connected to Blynk :-(
15 : Seemed to have lost connected to Blynk :-(
16 : Seemed to have lost connected to Blynk :-(
17 : Seemed to have lost connected to Blynk :-(
18 : Seemed to have lost connected to Blynk :-(
19 : Seemed to have lost connected to Blynk :-(
20 : Seemed to have lost connected to Blynk :-(
21 : Seemed to have lost connected to Blynk :-(
22 : Seemed to have lost connected to Blynk :-(
23 : Seemed to have lost connected to Blynk :-(
******************************* In reconnect
reconnectBlynk(): WDT disabled
reconnectBlynk(): Before Blynk.connect
[64961] Connecting to 192.168.0.250:8442
reconnectBlynk(): After Blynk.connect UN-successfull
reconnectBlynk(): WDT enabled
reconnectBlynk(): restarting timer and exiting function
24 : Seemed to have lost connected to Blynk :-(
25 : Seemed to have lost connected to Blynk :-(
26 : Seemed to have lost connected to Blynk :-(
27 : Seemed to have lost connected to Blynk :-(
28 : Seemed to have lost connected to Blynk :-(
29 : Seemed to have lost connected to Blynk :-(
30 : Seemed to have lost connected to Blynk :-(
31 : Seemed to have lost connected to Blynk :-(
32 : Seemed to have lost connected to Blynk :-(
33 : Seemed to have lost connected to Blynk :-(
34 : Seemed to have lost connected to Blynk :-(
35 : Seemed to have lost connected to Blynk :-(
36 : Seemed to have lost connected to Blynk :-(
37 : Seemed to have lost connected to Blynk :-(
38 : Seemed to have lost connected to Blynk :-(
39 : Seemed to have lost connected to Blynk :-(
40 : Seemed to have lost connected to Blynk :-(
41 : Seemed to have lost connected to Blynk :-(
42 : Seemed to have lost connected to Blynk :-(
43 : Seemed to have lost connected to Blynk :-(
44 : Seemed to have lost connected to Blynk :-(
45 : Seemed to have lost connected to Blynk :-(
46 : Seemed to have lost connected to Blynk :-(
47 : Seemed to have lost connected to Blynk :-(
48 : Seemed to have lost connected to Blynk :-(   **[@costas this is where I restarted my local Blynk Server]**
49 : Seemed to have lost connected to Blynk :-(
50 : Seemed to have lost connected to Blynk :-(
51 : Seemed to have lost connected to Blynk :-(
52 : Seemed to have lost connected to Blynk :-(
53 : Seemed to have lost connected to Blynk :-(
54 : Seemed to have lost connected to Blynk :-(
55 : Seemed to have lost connected to Blynk :-(
******************************* In reconnect
reconnectBlynk(): WDT disabled
reconnectBlynk(): Before Blynk.connect
[96084] Connecting to 192.168.0.250:8442
[96096] Ready (ping: 0ms).
reconnectBlynk(): After Blynk.connect successfull
reconnectBlynk(): WDT enabled
reconnectBlynk(): restarting timer and exiting function
56 : All good and connected to Blynk!
57 : All good and connected to Blynk!
58 : All good and connected to Blynk!
59 : All good and connected to Blynk!
60 : All good and connected to Blynk!
61 : All good and connected to Blynk!
62 : All good and connected to Blynk!
63 : All good and connected to Blynk!
64 : All good and connected to Blynk!
65 : All good and connected to Blynk!
66 : All good and connected to Blynk!
67 : All good and connected to Blynk!
68 : All good and connected to Blynk!
69 : All good and connected to Blynk!
70 : All good and connected to Blynk!
71 : All good and connected to Blynk!
72 : All good and connected to Blynk!
73 : All good and connected to Blynk!
74 : All good and connected to Blynk!
75 : All good and connected to Blynk!
76 : All good and connected to Blynk!
77 : All good and connected to Blynk!
78 : All good and connected to Blynk!
79 : All good and connected to Blynk!
80 : All good and connected to Blynk!
81 : All good and connected to Blynk!
82 : All good and connected to Blynk!
83 : All good and connected to Blynk!
84 : All good and connected to Blynk!
85 : All good and connected to Blynk!
86 : All good and connected to Blynk!
87 : All good and connected to Blynk!
88 : All good and connected to Blynk!
89 : All good and connected to Blynk!
90 : All good and connected to Blynk!

Serial output when I used Blynk v0.4.8 is below:

*WM: Connection result: 
*WM: 3
*WM: IP Address:
*WM: 192.168.0.60
[9474] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.8 on Arduino

[9481] Connecting to 192.168.0.250:8442
[9500] Ready (ping: 0ms).
BLYNK is Connected!
0 : All good and connected to Blynk!
1 : All good and connected to Blynk!
2 : All good and connected to Blynk!
3 : All good and connected to Blynk!
4 : All good and connected to Blynk!
5 : All good and connected to Blynk!
6 : All good and connected to Blynk!
7 : All good and connected to Blynk!
8 : All good and connected to Blynk!
9 : All good and connected to Blynk!
10 : All good and connected to Blynk!
11 : All good and connected to Blynk!
12 : Seemed to have lost connected to Blynk :-(     **[@costas here i terminate blynk local server]**
13 : Seemed to have lost connected to Blynk :-(
14 : Seemed to have lost connected to Blynk :-(
15 : Seemed to have lost connected to Blynk :-(
16 : Seemed to have lost connected to Blynk :-(
17 : Seemed to have lost connected to Blynk :-(
18 : Seemed to have lost connected to Blynk :-(
19 : Seemed to have lost connected to Blynk :-(
20 : Seemed to have lost connected to Blynk :-(
21 : Seemed to have lost connected to Blynk :-(
22 : Seemed to have lost connected to Blynk :-(
23 : Seemed to have lost connected to Blynk :-(
******************************* In reconnect
reconnectBlynk(): WDT disabled
reconnectBlynk(): Before Blynk.connect
[34507] Connecting to 192.168.0.250:8442    **[@costas : i notice it tries to connect twice...not sure why ?]**
[40590] Connecting to 192.168.0.250:8442

 ets Jan  8 2013,rst cause:4, boot mode:(1,7)    **[@costas: wemos crashes :-(]**

wdt reset

#14

That is because of line 59 of BlynkProtocol.h

run(); // Workaround for #325: Getting wrong bytes with ESP8266-SSL

If you are not using SSL then you can comment out this line.


#15

hi costas
I’m not using SSL in my example. Either way I’m not clear how this addresses the reset issue, do I assume from your previous post that you did not manage to replicate the issue I am seeing ?


#16

@mars I haven’t checked your code too closely but with 2 blynk.run() entries in the library and a 5s connection time then it would be > an 8s WDT.

Using 0.4.8, no library mods and the master of Arduino Core then your sketch appears to work just fine on my WeMos.


#17

Hi Costas

I can’t see 2 x blynk.run() entries - I can only see one, can you pls explain where the second one is ?
Also the 5s connection time - can you pls elaborate ?


#18

Paste your live BlynkProtocol.h


#19

File required for 0.4.8 as that is where the second run() was added.

Edited: changed blynk.run() to run()


#20

here is v0.4.8 Blynkprotocol.h

/**
 * @file       BlynkProtocol.h
 * @author     Volodymyr Shymanskyy
 * @license    This project is released under the MIT License (MIT)
 * @copyright  Copyright (c) 2015 Volodymyr Shymanskyy
 * @date       Jan 2015
 * @brief      Blynk protocol implementation
 *
 */

#ifndef BlynkProtocol_h
#define BlynkProtocol_h

#include <string.h>
#include <stdlib.h>
#include <Blynk/BlynkDebug.h>
#include <Blynk/BlynkProtocolDefs.h>
#include <Blynk/BlynkApi.h>
#include <utility/BlynkUtility.h>

template <class Transp>
class BlynkProtocol
    : public BlynkApi< BlynkProtocol<Transp> >
{
    friend class BlynkApi< BlynkProtocol<Transp> >;
public:
    enum BlynkState {
        CONNECTING,
        CONNECTED,
        DISCONNECTED,
    };

    BlynkProtocol(Transp& transp)
        : conn(transp)
        , authkey(NULL)
        , redir_serv(NULL)
        , lastActivityIn(0)
        , lastActivityOut(0)
        , lastHeartbeat(0)
#ifdef BLYNK_MSG_LIMIT
        , deltaCmd(0)
#endif
        , currentMsgId(0)
        , state(CONNECTING)
    {}

    bool connected() { return state == CONNECTED; }

    bool connect(uint32_t timeout = BLYNK_TIMEOUT_MS*3) {
        conn.disconnect();
        state = CONNECTING;
        millis_time_t started = this->getMillis();
        while ((state != CONNECTED) &&
               (this->getMillis() - started < timeout))
        {
            run();
        }
        return state == CONNECTED;
    }

    void disconnect() {
        conn.disconnect();
        state = DISCONNECTED;
        BLYNK_LOG1(BLYNK_F("Disconnected"));
    }

    bool run(bool avail = false);

    // TODO: Fixme
    void startSession() {
        conn.connect();
        state = CONNECTING;
#ifdef BLYNK_MSG_LIMIT
        deltaCmd = 1000;
#endif
        currentMsgId = 0;
        lastHeartbeat = lastActivityIn = lastActivityOut = this->getMillis(); // TODO: - 5005UL
    }

    void sendCmd(uint8_t cmd, uint16_t id = 0, const void* data = NULL, size_t length = 0, const void* data2 = NULL, size_t length2 = 0);

private:
    int readHeader(BlynkHeader& hdr);
    uint16_t getNextMsgId();

protected:
    void begin(const char* auth) {
        this->authkey = auth;

#if defined(BLYNK_NO_FANCY_LOGO)
        BLYNK_LOG1(BLYNK_F("Blynk v" BLYNK_VERSION " on " BLYNK_INFO_DEVICE));
#elif defined(BLYNK_FANCY_LOGO_3D)
        BLYNK_LOG1(BLYNK_F("\n"
            "   ____     ___                      __\n"
            "  /\\  _`\\  /\\_ \\                    /\\ \\  _\n"
            "  \\ \\ \\_\\ \\\\//\\ \\    __  __     ___ \\ \\ \\/ \\\n"
            "   \\ \\  _ <  \\ \\ \\  /\\ \\/\\ \\  /' _ `\\\\ \\ , <\n"
            "    \\ \\ \\_\\ \\ \\_\\ \\_\\ \\ \\_\\ \\ /\\ \\/\\ \\\\ \\ \\\\`\\\n"
            "     \\ \\____/ /\\____\\\\/`____ \\\\ \\_\\ \\_\\\\ \\_\\\\_\\\n"
            "      \\/___/  \\/____/ `/___/\\ \\\\/_/\\/_/ \\/_//_/\n"
            "                         /\\___/\n"
            "                         \\/__/   " BLYNK_VERSION " on " BLYNK_INFO_DEVICE "\n"
        ));
#else
        BLYNK_LOG1(BLYNK_F("\n"
            "    ___  __          __\n"
            "   / _ )/ /_ _____  / /__\n"
            "  / _  / / // / _ \\/  '_/\n"
            " /____/_/\\_, /_//_/_/\\_\\\n"
            "        /___/ v" BLYNK_VERSION " on " BLYNK_INFO_DEVICE "\n"
        ));
#endif

#ifdef BLYNK_DEBUG
        if (size_t ram = BlynkFreeRam()) {
            BLYNK_LOG2(BLYNK_F("Free RAM: "), ram);
        }
#endif
    }
    bool processInput(void);

    Transp& conn;

private:
    const char* authkey;
    char*       redir_serv;
    millis_time_t lastActivityIn;
    millis_time_t lastActivityOut;
    union {
        millis_time_t lastHeartbeat;
        millis_time_t lastLogin;
    };
#ifdef BLYNK_MSG_LIMIT
    millis_time_t deltaCmd;
#endif
    uint16_t currentMsgId;
protected:
    BlynkState state;
};

template <class Transp>
bool BlynkProtocol<Transp>::run(bool avail)
{
    BLYNK_RUN_YIELD();

    if (state == DISCONNECTED) {
        return false;
    }

    const bool tconn = conn.connected();

    if (tconn) {
        if (avail || conn.available() > 0) {
            //BLYNK_LOG2(BLYNK_F("Available: "), conn.available());
            //const unsigned long t = micros();
            if (!processInput()) {
                conn.disconnect();
// TODO: Only when in direct mode?
#ifdef BLYNK_USE_DIRECT_CONNECT
                state = CONNECTING;
#endif
                //BlynkOnDisconnected();
                return false;
            }
            //BLYNK_LOG2(BLYNK_F("Proc time: "), micros() - t);
        }
    }

    const millis_time_t t = this->getMillis();

    if (state == CONNECTED) {
        if (!tconn) {
            state = CONNECTING;
            lastHeartbeat = t;
            //BlynkOnDisconnected();
            return false;
        }

        if (t - lastActivityIn > (1000UL * BLYNK_HEARTBEAT + BLYNK_TIMEOUT_MS*3)) {
#ifdef BLYNK_DEBUG
            BLYNK_LOG6(BLYNK_F("Heartbeat timeout: "), t, BLYNK_F(", "), lastActivityIn, BLYNK_F(", "), lastHeartbeat);
#else
            BLYNK_LOG1(BLYNK_F("Heartbeat timeout"));
#endif
            conn.disconnect();
            state = CONNECTING;
            //BlynkOnDisconnected();
            return false;
        } else if ((t - lastActivityIn  > 1000UL * BLYNK_HEARTBEAT ||
                    t - lastActivityOut > 1000UL * BLYNK_HEARTBEAT) &&
                    t - lastHeartbeat   > BLYNK_TIMEOUT_MS)
        {
            // Send ping if we didn't either send or receive something
            // for BLYNK_HEARTBEAT seconds
            sendCmd(BLYNK_CMD_PING);
            lastHeartbeat = t;
        }
    } else if (state == CONNECTING) {
#ifdef BLYNK_USE_DIRECT_CONNECT
        if (!tconn)
            conn.connect();
#else
        if (tconn && (t - lastLogin > BLYNK_TIMEOUT_MS)) {
            BLYNK_LOG1(BLYNK_F("Login timeout"));
            conn.disconnect();
            state = CONNECTING;
            return false;
        } else if (!tconn && (t - lastLogin > 5000UL)) {
            conn.disconnect();
            if (!conn.connect()) {
                lastLogin = t;
                return false;
            }

#ifdef BLYNK_MSG_LIMIT
            deltaCmd = 1000;
#endif
            sendCmd(BLYNK_CMD_LOGIN, 1, authkey, strlen(authkey));
            lastLogin = lastActivityOut;
            return true;
        }
#endif
    }
    return true;
}

template <class Transp>
BLYNK_FORCE_INLINE
bool BlynkProtocol<Transp>::processInput(void)
{
    BlynkHeader hdr;
    const int ret = readHeader(hdr);

    if (ret == 0) {
        return true; // Considered OK (no data on input)
    }

    if (ret < 0 || hdr.msg_id == 0) {
#ifdef BLYNK_DEBUG
        BLYNK_LOG2(BLYNK_F("Bad hdr len: "), ret);
#endif
        return false;
    }

    if (hdr.type == BLYNK_CMD_RESPONSE) {
        lastActivityIn = this->getMillis();

#ifndef BLYNK_USE_DIRECT_CONNECT
        if (state == CONNECTING && (1 == hdr.msg_id)) {
            switch (hdr.length) {
            case BLYNK_SUCCESS:
            case BLYNK_ALREADY_REGISTERED:
                BLYNK_LOG3(BLYNK_F("Ready (ping: "), lastActivityIn-lastHeartbeat, BLYNK_F("ms)."));
                lastHeartbeat = lastActivityIn;
                state = CONNECTED;
                this->sendInfo();
                BLYNK_RUN_YIELD();
                BlynkOnConnected();
                return true;
            case BLYNK_INVALID_TOKEN:
                BLYNK_LOG1(BLYNK_F("Invalid auth token"));
                break;
            default:
                BLYNK_LOG2(BLYNK_F("Connect failed. code: "), hdr.length);
            }
            return false;
        }
        if (BLYNK_NOT_AUTHENTICATED == hdr.length) {
            return false;
        }
#endif
        // TODO: return code may indicate App presence
        return true;
    }

    if (hdr.length > BLYNK_MAX_READBYTES) {
#ifdef BLYNK_DEBUG
        BLYNK_LOG2(BLYNK_F("Packet too big: "), hdr.length);
#endif
        // TODO: Flush
        conn.connect();
        return true;
    }

    uint8_t inputBuffer[hdr.length+1]; // Add 1 to zero-terminate
    if (hdr.length != conn.read(inputBuffer, hdr.length)) {
#ifdef DEBUG
        BLYNK_LOG1(BLYNK_F("Can't read body"));
#endif
        return false;
    }
    inputBuffer[hdr.length] = '\0';

    BLYNK_DBG_DUMP(">", inputBuffer, hdr.length);

    lastActivityIn = this->getMillis();

    switch (hdr.type)
    {
    case BLYNK_CMD_LOGIN: {
#ifdef BLYNK_USE_DIRECT_CONNECT
        if (strncmp(authkey, (char*)inputBuffer, 32)) {
            BLYNK_LOG1(BLYNK_F("Invalid token"));
            sendCmd(BLYNK_CMD_RESPONSE, hdr.msg_id, NULL, BLYNK_INVALID_TOKEN);
            break;
        }
#endif
        if (state == CONNECTING) {
            BLYNK_LOG1(BLYNK_F("Ready"));
            state = CONNECTED;
            this->sendInfo();
            BlynkOnConnected();
        }
        sendCmd(BLYNK_CMD_RESPONSE, hdr.msg_id, NULL, BLYNK_SUCCESS);
    } break;
    case BLYNK_CMD_PING: {
        sendCmd(BLYNK_CMD_RESPONSE, hdr.msg_id, NULL, BLYNK_SUCCESS);
    } break;
    case BLYNK_CMD_REDIRECT: {
        if (!redir_serv) {
             redir_serv = (char*)malloc(32);
        }
        BlynkParam param(inputBuffer, hdr.length);
        uint16_t redir_port = BLYNK_DEFAULT_PORT; // TODO: Fixit

        BlynkParam::iterator it = param.begin();
        if (it >= param.end())
            return false;
        strncpy(redir_serv, it.asStr(), 32);
        if (++it < param.end())
            redir_port = it.asLong();
        BLYNK_LOG4(BLYNK_F("Redirecting to "), redir_serv, ':', redir_port);
        conn.disconnect();
        conn.begin(redir_serv, redir_port);
        lastLogin = lastActivityIn - 5000L;  // Reconnect immediately
        state = CONNECTING;
    } break;
    case BLYNK_CMD_HARDWARE:
    case BLYNK_CMD_BRIDGE: {
        currentMsgId = hdr.msg_id;
        this->processCmd(inputBuffer, hdr.length);
        currentMsgId = 0;
    } break;
    case BLYNK_CMD_INTERNAL: {
        BlynkReq req = { 0 };
        BlynkParam param(inputBuffer, hdr.length);
        BlynkParam::iterator it = param.begin();
        if (it >= param.end())
            return true;

        uint32_t cmd32;
        memcpy(&cmd32, it.asStr(), sizeof(cmd32));

        if (++it >= param.end())
            return true;
        char* start = (char*)it.asStr();
        BlynkParam param2(start, hdr.length - (start - (char*)inputBuffer));

        switch (cmd32) {
        case BLYNK_INT_RTC:  BlynkWidgetWriteInternalPinRTC(req, param2);    break;
        case BLYNK_INT_OTA:  BlynkWidgetWriteInternalPinOTA(req, param2);    break;
        case BLYNK_INT_ACON: BlynkWidgetWriteInternalPinACON(req, param2);   break;
        case BLYNK_INT_ADIS: BlynkWidgetWriteInternalPinADIS(req, param2);   break;
        }
    } break;
    case BLYNK_CMD_DEBUG_PRINT: {
        if (hdr.length) {
            BLYNK_LOG2(BLYNK_F("Server: "), (char*)inputBuffer);
        }
    } break;
    default: {
#ifdef BLYNK_DEBUG
        BLYNK_LOG2(BLYNK_F("Invalid header type: "), hdr.type);
#endif
        // TODO: Flush
        conn.connect();
    } break;
    }

    return true;
}

template <class Transp>
int BlynkProtocol<Transp>::readHeader(BlynkHeader& hdr)
{
    size_t rlen = conn.read(&hdr, sizeof(hdr));
    if (rlen == 0) {
        return 0;
    }

    if (sizeof(hdr) != rlen) {
        return -1;
    }

    BLYNK_DBG_DUMP(">", &hdr, sizeof(BlynkHeader));

    hdr.msg_id = ntohs(hdr.msg_id);
    hdr.length = ntohs(hdr.length);

    return rlen;
}

#ifndef BLYNK_SEND_THROTTLE
#define BLYNK_SEND_THROTTLE 0
#endif

#ifndef BLYNK_SEND_CHUNK
#define BLYNK_SEND_CHUNK 1024 // Just a big number
#endif

template <class Transp>
void BlynkProtocol<Transp>::sendCmd(uint8_t cmd, uint16_t id, const void* data, size_t length, const void* data2, size_t length2)
{
    if (0 == id) {
        id = getNextMsgId();
    }

    if (!conn.connected() || (cmd != BLYNK_CMD_RESPONSE && cmd != BLYNK_CMD_PING && cmd != BLYNK_CMD_LOGIN && state != CONNECTED) ) {
#ifdef BLYNK_DEBUG
        BLYNK_LOG2(BLYNK_F("Cmd skipped:"), cmd);
#endif
        return;
    }

    const size_t full_length = (sizeof(BlynkHeader)) +
                               (data  ? length  : 0) +
                               (data2 ? length2 : 0);

#if defined(BLYNK_SEND_ATOMIC) || defined(ESP8266) || defined(SPARK) || defined(PARTICLE) || defined(ENERGIA)
    // Those have more RAM and like single write at a time...

    uint8_t buff[full_length];

    BlynkHeader* hdr = (BlynkHeader*)buff;
    hdr->type = cmd;
    hdr->msg_id = htons(id);
    hdr->length = htons(length+length2);

    size_t pos = sizeof(BlynkHeader);
    if (data && length) {
        memcpy(buff + pos, data, length);
        pos += length;
    }
    if (data2 && length2) {
        memcpy(buff + pos, data2, length2);
    }

    size_t wlen = 0;
    while (wlen < full_length) {
        const size_t chunk = BlynkMin(size_t(BLYNK_SEND_CHUNK), full_length - wlen);
        BLYNK_DBG_DUMP("<", buff + wlen, chunk);
        const size_t w = conn.write(buff + wlen, chunk);
        ::delay(BLYNK_SEND_THROTTLE);
        if (w == 0) {
#ifdef BLYNK_DEBUG
            BLYNK_LOG1(BLYNK_F("Cmd error"));
#endif
            conn.disconnect();
            state = CONNECTING;
            //BlynkOnDisconnected();
            return;
        }
        wlen += w;
    }

#else

    BlynkHeader hdr;
    hdr.type = cmd;
    hdr.msg_id = htons(id);
    hdr.length = htons(length+length2);

    BLYNK_DBG_DUMP("<", &hdr, sizeof(hdr));
    size_t wlen = conn.write(&hdr, sizeof(hdr));
    ::delay(BLYNK_SEND_THROTTLE);

    if (cmd != BLYNK_CMD_RESPONSE) {
        if (length) {
            BLYNK_DBG_DUMP("<", data, length);
            wlen += conn.write(data, length);
            ::delay(BLYNK_SEND_THROTTLE);
        }
        if (length2) {
            BLYNK_DBG_DUMP("<", data2, length2);
            wlen += conn.write(data2, length2);
            ::delay(BLYNK_SEND_THROTTLE);
        }
    }

#endif

    if (wlen != full_length) {
#ifdef BLYNK_DEBUG
        BLYNK_LOG4(BLYNK_F("Sent "), wlen, '/', full_length);
#endif
        conn.disconnect();
        state = CONNECTING;
        //BlynkOnDisconnected();
        return;
    }

    const millis_time_t ts = this->getMillis();
#if defined BLYNK_MSG_LIMIT && BLYNK_MSG_LIMIT > 0
    BlynkAverageSample<32>(deltaCmd, ts - lastActivityOut);
    //BLYNK_LOG2(BLYNK_F("Delta: "), deltaCmd);
    if (deltaCmd < (1000/BLYNK_MSG_LIMIT)) {
        BLYNK_LOG_TROUBLE(BLYNK_F("flood-error"));
        conn.disconnect();
        state = CONNECTING;
        //BlynkOnDisconnected();
    }
#endif
    lastActivityOut = ts;

}

template <class Transp>
uint16_t BlynkProtocol<Transp>::getNextMsgId()
{
    static uint16_t last = 0;
    if (currentMsgId != 0)
        return currentMsgId;
    if (++last == 0)
        last = 1;
    return last;
}

#endif

#21

That is not v0.4.8

See https://github.com/blynkkk/blynk-library/blob/master/src/Blynk/BlynkProtocol.h as at 19 June 2016, release date for 0.4.8.

Line 59 of the correct version is the additional run() that was added. Check your libraries thoroughly.

Line 215 containing 5000UL dictates connection will take 5s, you can change to 500UL for faster connections.


#22

I will check it thoroughly - admittley I installed 0.4.8 using the Arduino Blynk: Check for Updates option - is that not the recommended way ?


#23

When I last looked, couple of days ago, GitHub said manual installation.


#24

hi costas

I managed to learn a little more that may be helpful for others in the community - so thought I would share my learnings:

  1. I was a little confused as to why in my scenario it was triggering a wdt reset given the lines before I call Blynk.connect(4777) I specifically disable the software watchdog with EST.wdtDisable( ). However I learnt at https://github.com/esp8266/Arduino/blob/4897e0006b5b0123a2fa31f67b14a3fff65ce561/cores/esp8266/Esp.cpp#L97 that disabling the software watchdog invokes the hardware watchdog to kick in within 6 seconds and therefore the reason for my system reset[reason cause 4] i.e. hardware watchdog]

  2. I ran my code without disabling the software watchdog and it worked fine. The only mod i did was change BlynkProtocol.h line 215 from 5000UL to 500UL but I left the second run in at line 59.

  3. In order to make my code work with the software watchdog disabled before I call Blynk.connect(4333); I had to both delete line 59 (i.e. the run command) in BlynkProtocol.h and reduce in line 215 the reconnect time to 500UL.

Hope this helps others as well.


Blynk.connect(timeout), timeout not working correct