ESP8266_Lib.h vs ESP8266WiFi.h (The difference between these 2 libraries)

Hello, first of all, I am new to programming and I had also tried for a week to solve this problem. In addition, english is not my native language, so I am really sorry for my bad english.

I am working on a project, but there are several problem occurred. The first problem is how to indicate the WiFi connection status, not Blynk.connected() status. I had read many topics regarding this issue but there are no solution for my problem because all the solution is using #include <ESP8266WiFi.h> library. Here is the story…

I am using WEMOS ATMega2560 with an embedded esp8266 WiFi chip on it. Here is the link of the board " https://opencircuit.shop/Product/MEGA-plus-WiFi-R3-ATmega2560-plus-ESP8266. ".

By following the instructions on how to configure the board from this youtube link " https://www.youtube.com/watch?v=i6dPyuWtdpU&t=335s ", the #include <library.h> code used from the youtube tutorial is #include <ESP8266_Lib.h> . Not #include <ESP8266WiFi.h> . Board selected on Arduino IDE is Arduino Mega. Therefore, if I #include <ESP8266WiFi.h> , there are errors, library not found since the #include <ESP8266WiFi.h> only available if i compile using ESP8266 board.

So, this code while (WiFi.status() != WL_CONNECTED) is not workable when I used #include <ESP8266_Lib.h> . I guess it need #include <ESP8266WiFi.h> … correct me if i’m wrong. The idea behind this problem is, I want to make 2 indicators which indicates the connection of the board to the router, and the connection with the blynk… So, what can I do now when to indicate the wifi status ??

The 2nd problem. Same project, but this problem happen when I’m doing void connection_check() for every 5 seconds interval. Look at my snipped code first.

void loop(){
 timer.run();
 Blynk.run();
 if ( Serial3.available() )   {            //
   Serial.write( Serial3.read() );}        //
                                           // I dont know, I just followed youtube code
 if ( Serial.available() )    {            //
   Serial3.write( Serial.read() );}        //
}


void connection_check(){
 if(Blynk.connected() == true){
   digitalWrite(blynk_connect_pin, HIGH);
   digitalWrite(blynk_disconnect_pin, LOW);
   Blynk.run();
   Serial.println("Blynk Connected");}

 if(Blynk.connected() == false){
   Serial.println("Reconnecting...");
   digitalWrite(blynk_connect_pin, LOW);
   digitalWrite(blynk_disconnect_pin, HIGH);
   Blynk.connect();
 }
}

There are 2 Blynk.run() in the code. 1 is inside void loop() and the other is when if (Blynk.connected() == true) ". Why I did this ?? because, if I erase the Blynk.run() at void loop, some alien language appeared in the serial. I’m sure it is not about the baud rate… here is the serial monitor output… But, i wanted to run Blynk.run() whenever blynk is connected

Blynk Connected
Blynk Connected
Blynk Connected

+IPD,1,5 //(alien symbol)
Reconnecting...
Blynk Connected
Blynk Connected

after 3 to 4 times void connection_check()… blynk will always disconnect and trying to reconnect… however, it does reconnect… but it disconnect again after several void connection_check()

If i put Blynk.run() inside void loop(). it works fine. What am i missing here ? Please help and teach me… Here is the full code… THANKS IN ADVANCE

Regards, Hamdan

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

char auth[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
char ssid[] = "MyWiFi";
char pass[] = "MyWiFi12345";

BlynkTimer timer;   // Blynk timer library

#define EspSerial Serial3
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);

#define blynk_connect_pin 3     // green led connected indicator
#define blynk_disconnect_pin 2    // red led disconnected indicator

void setup() {
  Serial.begin(115200);                       //
  Serial3.begin(115200);                      // I dont know, I just followed youtube code
  EspSerial.begin(ESP8266_BAUD);              //

  pinMode(blynk_connect_pin, OUTPUT);
  pinMode(blynk_disconnect_pin, OUTPUT);
  
  Serial.println("Getting Started...");       //  
  digitalWrite(blynk_connect_pin, HIGH);      //  both LED indicator on for 2 seconds
  digitalWrite(blynk_disconnect_pin, HIGH);   // 
  delay(2000);                                
  digitalWrite(blynk_connect_pin, LOW);       //
  digitalWrite(blynk_disconnect_pin, LOW);    //  both LED indicator turn off
  delay(1000);                                //
  digitalWrite(blynk_disconnect_pin, HIGH);   //  after 1 second, disconnect pin turned on
  Serial.print("Connecting to: ");            //              
  Serial.println(ssid);                       //
  Blynk.begin(auth, wifi, ssid, pass);        //
  digitalWrite(blynk_disconnect_pin, LOW);    //  turning off disconnect led
  digitalWrite(blynk_connect_pin, HIGH);      //  turning on connected led
  Serial.println("Connected");                //
  delay(1000);                                //
  Serial.println("ALL READY !!!"); 
  timer.setInterval(5000L, connection_check); } // check connection every 10 seconds

void loop(){
  timer.run();
  Blynk.run();
  if ( Serial3.available() )   {            //
    Serial.write( Serial3.read() );}        //
                                            // I dont know, I just followed youtube code
  if ( Serial.available() )    {            //
    Serial3.write( Serial.read() );}        //
}


void connection_check(){
  if(Blynk.connected() == true){                // if blynk is connected
    digitalWrite(blynk_connect_pin, HIGH);      // connected led turned on
    digitalWrite(blynk_disconnect_pin, LOW);    // disconnected led turned off
    Blynk.run();                                // runs blynk communication
    Serial.println("Blynk Connected");}         //

  if(Blynk.connected() == false){               // if blynk is not connected
    Serial.println("Reconnecting...");          //
    digitalWrite(blynk_connect_pin, LOW);       // connected led turned off
    digitalWrite(blynk_disconnect_pin, HIGH);   // disconnected led turned on
    Blynk.connect();                            // trying to connect to led
  }
}

What happens if you change this to…
(wifi.status() != WL_CONNECTED)

The WiFi object in the BlynkSimpleShieldEsp8266.h wrapper library is referenced as wifi rather than WiFi but I can’t remember if the .status value is available within the wrapper.

ETA - just checked the library and…
bool connected() { return status; } is a public method, so (wifi.status() != WL_CONNECTED) should work.

Pete.

1 Like

Sorry for the late reply.

(wifi.status() != WL_CONNECTED) does not not working. it shows ‘class ESP8266’ has no member name ‘status’…

I also couldn’t identify the bool connected() { return status; } statement within the BlynkSimpleShieldEsp8266Esp8266.h library. I am very grateful if you can help me to find that boolean within the library.

Thanks Pete !

I attached the blynksimplees8266.h library here for your reference.

/**
 * @file       BlynkSimpleShieldEsp8266.h
 * @author     Volodymyr Shymanskyy
 * @license    This project is released under the MIT License (MIT)
 * @copyright  Copyright (c) 2015 Volodymyr Shymanskyy
 * @date       Jun 2015
 * @brief
 *
 */

#ifndef BlynkSimpleShieldEsp8266_h
#define BlynkSimpleShieldEsp8266_h

#ifdef ESP8266
#error This code is not intended to run on the ESP8266 platform! Please check your Tools->Board setting.
#endif

#ifndef BLYNK_INFO_CONNECTION
#define BLYNK_INFO_CONNECTION  "ESP8266"
#endif

#ifndef BLYNK_ESP8266_MUX
#define BLYNK_ESP8266_MUX  1
#endif

#define BLYNK_SEND_ATOMIC
#define BLYNK_SEND_CHUNK 40

#include <BlynkApiArduino.h>
#include <Blynk/BlynkProtocol.h>
#include <utility/BlynkFifo.h>
#include <ESP8266_Lib.h>

class BlynkTransportShieldEsp8266
{
    static void onData(uint8_t mux_id, uint32_t len, void* ptr) {
        ((BlynkTransportShieldEsp8266*)ptr)->onData(mux_id, len);
    }

    void onData(uint8_t mux_id, uint32_t len) {
        if (mux_id != BLYNK_ESP8266_MUX) {
            return;
        }
        //BLYNK_LOG4("Got: ", len, ", Free: ", buffer.free());
        if (buffer.free() < len) {
          BLYNK_LOG1(BLYNK_F("Buffer overflow"));
          return;
        }
        while (len) {
            if (client->getUart()->available()) {
                uint8_t b = client->getUart()->read();
                buffer.put(b);
                len--;
            }
        }
    }

public:
    BlynkTransportShieldEsp8266()
        : client(NULL)
        , status(false)
        , domain(NULL)
        , port(0)
    {}

    void setEsp8266(ESP8266* esp8266) {
        client = esp8266;
        client->setOnData(onData, this);
    }

    //TODO: IPAddress

    void begin(const char* d,  uint16_t p) {
        domain = d;
        port = p;
    }

    bool connect() {
        if (!domain || !port)
            return false;
        status = client->createTCP(BLYNK_ESP8266_MUX, domain, port);
        return status;
    }

    void disconnect() {
        status = false;
        buffer.clear();
        client->releaseTCP(BLYNK_ESP8266_MUX);
    }

    size_t read(void* buf, size_t len) {
        millis_time_t start = BlynkMillis();
        //BLYNK_LOG4("Waiting: ", len, " Buffer: ", buffer.size());
        while ((buffer.size() < len) && (BlynkMillis() - start < 1500)) {
            client->run();
        }
        return buffer.get((uint8_t*)buf, len);
    }
    size_t write(const void* buf, size_t len) {
        if (client->send(BLYNK_ESP8266_MUX, (const uint8_t*)buf, len)) {
            return len;
        }
        return 0;
    }

    bool connected() { return status; }

    int available() {
        client->run();
        //BLYNK_LOG2("Still: ", buffer.size());
        return buffer.size();
    }

private:
    ESP8266* client;
    bool status;
    BlynkFifo<uint8_t,256> buffer;
    const char* domain;
    uint16_t    port;
};

class BlynkWifi
    : public BlynkProtocol<BlynkTransportShieldEsp8266>
{
    typedef BlynkProtocol<BlynkTransportShieldEsp8266> Base;
public:
    BlynkWifi(BlynkTransportShieldEsp8266& transp)
        : Base(transp)
        , wifi(NULL)
    {}

    bool connectWiFi(const char* ssid, const char* pass)
    {
        BlynkDelay(500);
        BLYNK_LOG2(BLYNK_F("Connecting to "), ssid);
        /*if (!wifi->restart()) {
            BLYNK_LOG1(BLYNK_F("Failed to restart"));
            return false;
        }*/
        if (!wifi->kick()) {
             BLYNK_LOG1(BLYNK_F("ESP is not responding"));
             //TODO: BLYNK_LOG_TROUBLE(BLYNK_F("esp8266-not-responding"));
             return false;
        }
        if (!wifi->setEcho(0)) {
            BLYNK_LOG1(BLYNK_F("Failed to disable Echo"));
            return false;
        }
        String ver = wifi->ESP8266::getVersion();
        BLYNK_LOG1(ver);
        if (!wifi->enableMUX()) {
            BLYNK_LOG1(BLYNK_F("Failed to enable MUX"));
        }
        if (!wifi->setOprToStation()) {
            BLYNK_LOG1(BLYNK_F("Failed to set STA mode"));
            return false;
        }
        if (wifi->joinAP(ssid, pass)) {
            String my_ip = wifi->getLocalIP();
            BLYNK_LOG1(my_ip);
        } else {
            BLYNK_LOG1(BLYNK_F("Failed to connect WiFi"));
            return false;
        }
        BLYNK_LOG1(BLYNK_F("Connected to WiFi"));
        return true;
    }

    void config(ESP8266&    esp8266,
                const char* auth,
                const char* domain = BLYNK_DEFAULT_DOMAIN,
                uint16_t    port   = BLYNK_DEFAULT_PORT)
    {
        Base::begin(auth);
        wifi = &esp8266;
        this->conn.setEsp8266(wifi);
        this->conn.begin(domain, port);
    }

    void begin(const char* auth,
               ESP8266&    esp8266,
               const char* ssid,
               const char* pass,
               const char* domain = BLYNK_DEFAULT_DOMAIN,
               uint16_t    port   = BLYNK_DEFAULT_PORT)
    {
        config(esp8266, auth, domain, port);
        connectWiFi(ssid, pass);
        while(this->connect() != true) {}
    }

private:
    ESP8266* wifi;
};

static BlynkTransportShieldEsp8266 _blynkTransport;
BlynkWifi Blynk(_blynkTransport);

#include <BlynkWidgets.h>

#endif

Maybe you should try a simple Find command within whatever text editor you are viewing the library files with…

Pete.

I don’t see what is your goal.

Do you want to write a sketch for ATmega and use the esp8266 as network adapter for Blynk? Then put AT firmware in esp8266 and use the example for the Arduino Mega with esp8266 shield. Blynk will handle the WiFi connection.

Or you want to write a sketch for esp8266 which will handle the Blynk and networking part of your project? Then use the example for the esp8266 as Arduino.

Hello Juraj.

What I want to do is to establish a void wifi_connection_check(). Not, Blynk.connected() = true . Therefore, i need to find the bool which represents the wifi connection. Esp8266Wifi library contains the (wifi.status() != WL_CONNECTED) . But not in BlynkSimpleShieldEsp8266.h library.

Thanks.
Atikahamdan

I already done that with ctrl+F. Unfortunately, still cant find it.

However, thanks for the reply Pete.