Help migrating to Blynk IoT with ESP8266 Wifi Shield

Hi there, I’m really struggling to migrate my project to Blynk IoT following the recent upgrade, and would appreciate any tips/help. The project (a basic smart lamp) had been running flawlessly for the 18 months prior, but I now can’t figure out how to get it back online.

I’m running a Teensy 3.2 with ESP8266 Wifi Shield and the code below. I’m sure there are many issues you could find in the code as I’m not a programmer, but it had been working fine.

All I’ve tried so far is to add the 3 new lines (with template ID etc) at the top, but no success connecting. The serial monitor suggests it still connects to my wifi, and the encoder reading interrupts and PWM outputs still work fine.

Any tips much appreciated as I miss my light! Thanks, Tom

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings

#define BLYNK_TEMPLATE_ID   "TMPLjG9uGLrl"
#define BLYNK_DEVICE_NAME   "ESP8266"
#define BLYNK_AUTH_TOKEN    "********************"

// ------ Define Serial ports and Include Libraries --------------------------------------
#define BLYNK_PRINT Serial
#define EspSerial Serial1
#include <ESP8266_Lib.h>
#include <YmrBlynkSimpleShieldEsp8266.h>  // NEW
#include <avr/wdt.h>                      // NEW

BlynkTimer timer;

// ------ Wifi password and Blynk Authorization code -------------------------------------
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "************";
char pass[] = "**********";

// ------ Define variables for Encoder Reading and PWM control ---------------------------
int mainLightPWM = 0;
int readLightPWM = 0;
bool encoderMchanB = 0;
bool encoderRchanB = 0;
static int delta = 10;
static int delta2 = 5;
static int mainLightMaxPWM = 255;
static int readLightMaxPWM = 80;

// ------ Define Pin Numbers -------------------------------------------------------------
static int MainEnc_A_Pin = 18;
static int MainEnc_B_Pin = 17;
static int ReadEnc_A_Pin = 19;
static int ReadEnc_B_Pin = 20;
static int MainPWMpin = 21;
static int ReadPWMpin = 22;

// ----- Initialize wifi over serial with ESP --------------------------------------------
ESP8266 wifi(&EspSerial);

// ----------- SETUP and initialize pins and connect to wifi -----------------------------
void setup () {
  pinMode(MainEnc_B_Pin, INPUT);
  pinMode(MainEnc_A_Pin, INPUT);
  pinMode(ReadEnc_A_Pin, INPUT);
  pinMode(ReadEnc_B_Pin, INPUT);
  pinMode(MainPWMpin, OUTPUT);
  pinMode(ReadPWMpin, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(MainEnc_A_Pin), encoderMchanA, RISING);
  attachInterrupt(digitalPinToInterrupt(ReadEnc_A_Pin), encoderRchanA, RISING);
  timer.setInterval(6000L, checkBlynk);
  EspSerial.begin(115200);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
}

//BLYNK_CONNECTED() {
  // ----- Run this when V1 changes (virtual pin connected to main light) ------------------
  BLYNK_WRITE(V1) {
    int mainLightTargetPWM = 25*(param.asInt());
    FadeMain(mainLightTargetPWM);
  }
  
  void FadeMain(int mainLightTargetPWM) {
    if (mainLightTargetPWM > mainLightPWM) {
      while (mainLightPWM < mainLightTargetPWM) {
        mainLightPWM += delta2;
        //Serial.println(mainLightPWM);
        analogWrite(MainPWMpin, mainLightPWM);
        delay(20);
      } 
    }
    else if (mainLightTargetPWM < mainLightPWM) {
      while (mainLightPWM > mainLightTargetPWM) {
        mainLightPWM -= delta2;
        //Serial.println(mainLightPWM);
        analogWrite(MainPWMpin, mainLightPWM);
        delay(20);
      } 
    }
  }
  
  // ------ Run this when V2 changes (virtual pin connected to reading light) -------------
  BLYNK_WRITE(V2) {
    int readLightTargetPWM = 8*(param.asInt());
    FadeRead(readLightTargetPWM);
  }
  
  void FadeRead(int readLightTargetPWM) {
    if (readLightTargetPWM > readLightPWM) {
      while (readLightPWM < readLightTargetPWM) {
        readLightPWM += delta2;
        //Serial.println(mainLightPWM);
        analogWrite(ReadPWMpin, readLightPWM);
        delay(20);
      } 
    }
    else if (readLightTargetPWM < readLightPWM) {
      while (readLightPWM > readLightTargetPWM) {
        readLightPWM -= delta2;
        //Serial.println(mainLightPWM);
        analogWrite(ReadPWMpin, readLightPWM);
        delay(20);
      } 
    }
  }
//}

// ------ Run Blynk code ----------------------------------------------------------------
void loop() {
  Blynk.run();
  timer.run();
}

// ------ MAIN LIGHT ENCODER READING ----------------------------------------------------
void encoderMchanA() {
  encoderMchanB = digitalRead(MainEnc_B_Pin);
  if (encoderMchanB > 0 && mainLightPWM > 0) {
    mainLightPWM -= delta;
    //Serial.println(mainLightPWM);
    analogWrite(MainPWMpin, mainLightPWM);
  }
  else if (encoderMchanB < 1 && mainLightPWM < mainLightMaxPWM){
    mainLightPWM += delta;
    //Serial.println(mainLightPWM);
    analogWrite(MainPWMpin, mainLightPWM);
  }
}

// ------ READING LIGHT ENCODER READING ------------------------------------------------
void encoderRchanA() {
  encoderRchanB = digitalRead(ReadEnc_B_Pin);
  if (encoderRchanB > 0 && readLightPWM > 0) {
    readLightPWM -= delta;
    //Serial.println(readLightPWM);
    analogWrite(ReadPWMpin, readLightPWM);
  }
  else if (encoderRchanB < 1 && readLightPWM < readLightMaxPWM){
    readLightPWM += delta;
    //Serial.println(readLightPWM);
    analogWrite(ReadPWMpin, readLightPWM);
  }
}

// ------- CHECK BLYNK ------------------------------------------------------------------
void checkBlynk(){  
 if(!Blynk.connected()){
    Serial.println("Not connected to Blynk server"); 
    wdt_enable(WDTO_1S);
  }
  Serial.println("connected to Blynk server");
} 

Where did this library com from?

What exactly does the serial monitor show?
What version of the Blynk library are you using?

Pete.

Hi Pete, thanks for the speedy response. Good question, I cobbled together pretty much everything from code/libraries shamelessly stolen from various sites, but can’t find any reference to this library anymore?

The serial monitor says:

[10908] +CIFSR:STAIP,“192.168.1.89”
+CIFSR:STAMAC,“50:02:91:79:d7:b4”
[10908] Connected to WiFi

I had Blynk Library 0.6.1 installed, so updated this evening to the latest version, but it now won’t compile, stating “fatal error: YmrBlynkSimpleShieldEsp8266.h: No such file or directory compilation terminated.”

I tried reverting back to 0.6.1 but I still get the same error.

The ‘library’ seems to be an h file on my computer as follows:

/**
 * @file       YmrBlynkSimpleShieldEsp8266.h
 * @author     Volodymyr Shymanskyy
 * @license    This project is released under the MIT License (MIT)
 * @copyright  Copyright (c) 2015 Volodymyr Shymanskyy
 * @date       Jun 2015
 * @brief
 * Modifications by Yaamr Nov 17
 */

#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>
#include <avr/wdt.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"));
			 wdt_enable(WDTO_1S);
             //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"));
			wdt_enable(WDTO_1S);
            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

You can’t use 0.6.1 with Blynk IoT, it needs to be 1.x.x

Pete.

All fixed now - found another library to allow Teensy to run latest blynk with an ESP WiFi shield.

Thanks for your help!