NANO<>ESP - shift BLYNK to ESP-01

In my project NANO <> ESP8266 (hardware connection) and BLYNK of course I needed more free memory in NANO. I couldn’t change the pcb. I decided to move BLYNK from NANO to ESP-01.
This is the first running program.
My goal is to program ESP-01 there is no need to change the program during program changes NANO.

BTW
This may be a way to start BLYNK with any processor !?

Code for NANO

void setup()
{
  Serial.begin(115200);  // 
}
void loop()
{
  recivestrfromserial();
}
//>>>>>>>>>>>>>>>>>>> my Programs

void Program_vPin5(String strvPin) {
}
//etc ...

void Program_vPin29(String strvPin) {
}

//from APP to NANO   >>>>>>>>>>>>>>>>
// equivalent BLYNK_WRITE(VPin)

void Blynkwrite(String str2) {
  byte pin2;
  String strdata2;
  if (str2.charAt(0) != 'V') pin2 = 199; else {
    String istr = str2.substring(1, str2.indexOf(':')); // nr vPin
    pin2 = istr.toInt();
    strdata2 = str2.substring(str2.indexOf(':') + 1); // if nr vPin typ int
  }
  switch (pin2 + 1) { // Switch to progrma for recived vPin
    case 5: Program_vPin5(strdata2);  break;
    // etc....
    case 30: Program_vPin29(strdata2);  break;
   // case 200:   Serial.println("ERROR - no V");  break; //Program for error
    default: ;
  }
}

// equivalent   Blynk.virtualWrite(VPin, Data);

void BlynkvirtualWrite (byte vPin, String z) {
  String  str1 = 'v' + String(vPin) + ':' + z;
  sendstrtoserial(str1);
}

// ....................................... recive string from serial

bool  stringComplete = false;
String inputString = "";
byte licznikodbioru = 0;

void recivestrfromserial () {
  if (stringComplete) {
    Blynkwrite(inputString);
    inputString = "";
    stringComplete = false;
    licznikodbioru = 0;
  }
}

void serialEvent() { //odbiór string z Serial
  // Serial.println("odbior");
  while (Serial.available()) {
    licznikodbioru++;
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    }
    if (licznikodbioru > 50) inputString = "";
  }
}

// ................................. send string to serial
void sendstrtoserial (String toserial) {
  Serial.println(toserial);
}

for ESP-01


#define BLYNK_PRINT Serial    
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char ssid[] = "xxx";
char pass[] = "yyyyyyyyyyyyyy";
char auth[] = "sssssssssssssssssssssssss";

WidgetLED led1(V30); // vLED indikator ESP for test
WidgetTerminal terminal31(V31); //terminal ESP for test

WidgetLED led00(V0); // vLEDs for NANO
WidgetLED led01(V1); //
WidgetLED led02(V2); //
WidgetLED led03(V3); //
WidgetLED led04(V4); //

#include <WidgetRTC.h>
WidgetRTC rtc;
BLYNK_ATTACH_WIDGET(rtc, V25); //rtc for NANO
WidgetTerminal terminal29(V29); //terminal for NANO

BLYNK_WRITE_DEFAULT() // send info form APP to NANO
{
  String strtosend = "";
  int pin = request.pin;      // Which exactly pin is handled?
  strtosend += 'V' + String(pin) + ':';
  for (auto i = param.begin(); i < param.end(); ++i) { //only 1 param now
    strtosend += i.asString();
  }
  Serial.println(strtosend);
}

void Blynkwrite(String str2) {//  command from NANO to APP
  byte pin2;
  String strdata2;
  int data2;
  if (str2.charAt(0) != 'v') pin2 = 199; else {
    String istr = str2.substring(1, str2.indexOf(':')); // nr vPin
    pin2 = istr.toInt();
    strdata2 = str2.substring(str2.indexOf(':') + 1); // if nr vPin typ int
    data2 = strdata2.toInt();
  }
  switch (pin2 + 1) { // Switch to progrma for recived vPin
    case 1: if (data2) led00.on(); else led00.off();  break; //V0 to V4 - vLEDs
    case 2: ;  break;
    case 3: ;  break;
    case 4: ;  break;
    case 5: ;  break;
    case 6: ESP_BlynkvirtualWrite_int(pin2, data2);  break;  //V5 to V24 /data int
    //etc..                                                   // V25 rtc

    case 26: ESP_BlynkvirtualWrite_str(pin2, strdata2);  break; // V26 to V 28 data str
                                                               
    case 30: ;  break;                                           // V29 terminal
    //   case 200:   Serial.println("ERROR - Brak char V");  break; //Program for error
    default: ;
  }
}

void ESP_BlynkvirtualWrite_int(byte vPin, int intdata)
{
  Blynk.virtualWrite(vPin, intdata);
}
void ESP_BlynkvirtualWrite_str(byte vPin, String strdata)
{
  Blynk.virtualWrite(vPin, strdata);
}

// ....................................... recive string from serial
bool  stringComplete = false;
String inputString = "";
byte licznikodbioru = 0;

void recivestrfromserial () {
  if (stringComplete) {
    Serial.println("odebralem  " + String(inputString)); //test
    Blynkwrite(inputString);
    inputString = "";
    stringComplete = false;
    licznikodbioru = 0;
  }
}

void myserialEvent() { // string z Serial
  while (Serial.available()) {
    //  Serial.println("odbior");
    licznikodbioru++;
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    }
    if (licznikodbioru > 50) inputString = "";
  }
}

void setup()
{
  Serial.begin(115200);
  WifiBlynk(); //logowanie do wifi i blynk
} //end setup

void loop()  //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> loop
{
  Blynk.run();
  myserialEvent();
  recivestrfromserial();
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  end loop

void WifiBlynk() { //logowanie do wifi i blynk nie wstrzymujące wejścia do pętli głównej
  WiFi.begin(ssid, pass);
  Blynk.config(auth);
  WiFi.status();
  Blynk.connect();
}

bool isFirstConnect = true;
BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();  //ważna procedura - po starcie arduino synchronizuje wszystkiw wirtualne piny ze zmiennymi programu
    isFirstConnect = false;
  }
}

The second version of the program extended by changing the vPins parameters.
Basic program NANO has 4 KB and allows to control data and parameters of most widgets in the APP via BLYNK installed in the ESP-01. Renumbering the pins do not require any changes in the program ESP (except for the widget terminal).

ESP-01 code


#define BLYNK_PRINT Serial    
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char ssid[] = "aaaa";
char pass[] = "bbbbbbbbbbbbbb";
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxx";

#include <Timers.h> //  my favorite timer 
Timers <4> akcja; 

WidgetLED led1(V30); // vLED ESP indicator for test
WidgetTerminal terminal31(V31); //ESP terminal  for test

WidgetLED led00(V0); // vLEDs for NANO   - declaration for all vLEDs

WidgetTerminal terminal29(V29); //terminal for NANO

BLYNK_WRITE_DEFAULT() // send data form APP to NANO
{
  String strtosend = "";
  int pin = request.pin;      // Which exactly pin is handled?
  strtosend += 'V' + String(pin) + ':';
  for (auto i = param.begin(); i < param.end(); ++i) { //only 1 param now
    strtosend += i.asString();
  }
  Serial.println(strtosend);
}

void Blynkwrite(String str2) {//  data from NANO to APP

  String istr = str2.substring(1, str2.indexOf(':')); // nr vPin
  byte pin2 = istr.toInt();
  String  strdata2 = str2.substring(str2.indexOf(':') + 1); // if nr vPin typ int
  int  data2 = strdata2.toInt();
  char chardata2[10];
  int dl =  strdata2.length() + 1;
  strdata2.toCharArray(chardata2, dl);

  if (str2.charAt(0) == 'L') led00.setVPin(pin2); led00.setValue(data2); led00.setVPin(0);  //send LEDs data to APP
  if (str2.charAt(0) == 'S')  Blynk.virtualWrite(pin2, strdata2); //send str to APP
  if (str2.charAt(0) == 'I')  Blynk.virtualWrite(pin2, data2); //send int to APP
  if (str2.charAt(0) == 'C')  Blynk.setProperty(pin2, "color", chardata2);  // send color
  if (str2.charAt(0) == 'N')  Blynk.setProperty(pin2, "onLabel", chardata2); //send button label to APP
  if (str2.charAt(0) == 'F')  Blynk.setProperty(pin2, "offLabel", chardata2); //send button label to APP
}

// ....................................... recive string from serial
bool  stringComplete = false;
String inputString = "";
byte licznikodbioru = 0;
int startrecive = 0;

void recivestrfromserial () {
  if (stringComplete) {
  //  Serial.println("od  " + String(inputString)); //test
    Blynkwrite(inputString);
    inputString = "";
    stringComplete = false;
    licznikodbioru = 0;
    startrecive = 0;
  }
}

void myserialEvent() { // string z Serial
  if (Serial.available()) {
    licznikodbioru++;
    char inChar = (char)Serial.read();
    if (startrecive == 0) {
      if ((inChar == 'S') || (inChar == 'I') || (inChar == 'L') || (inChar == 'C') || (inChar == 'N') || (inChar == 'F'))  startrecive = 1;
    }
    if (startrecive == 1) {
      if (inChar == '\r')  {
        stringComplete = true;
        inChar = '\0';
        inputString += inChar;
        startrecive = 0;
      } else  inputString += inChar;
    }
    if (licznikodbioru > 50) inputString = "";
  }
}

void setup()
{
  akcja.attach(0, 1500, timer1sek); 
  Serial.begin(115200);
  WifiBlynk(); //log do wifi i blynk
} //end setup

void loop()  
{
  akcja.process(); //
  Blynk.run();
  myserialEvent();
  recivestrfromserial();
}  //>>  end loop

//>>>>>>>>>>>>>>> tests
void timer1sek() { //test
  blinkvLed();
  blinkLedNANO();
  //    Serial.println("ECHO ESP");
}
bool wsk3 = 0;
void  blinkvLed() { //test
  wsk3 = !wsk3;
  if (wsk3)  led1.off(); else  led1.on();
}

void  blinkLedNANO() { //test
  String strtosend = 'V' + String(0) + ':' + String(wsk3);
   Serial.println(strtosend);
}

void WifiBlynk() { //logo wifi i blynk
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
  Blynk.config(auth);
  WiFi.status();
  Blynk.connect();
}

bool isFirstConnect = true;
BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();  
    isFirstConnect = false;
  }
}

NANO code

#include <Timers.h> //  my favorite timer 
Timers <4> akcja; // 
#define led_green 7  // test
#define led_red   8 // test

//>>>>>>>>>>>>>>>>>>> my NANO Programs 

void Program_vPin0(String strvPin) { // blink led from ESP timer
  digitalWrite(led_red, intfromstr(strvPin));
}
//etc ...

void Program_vPin29(String strvPin) {
}
int intfromstr(String str) {
  String    strdata3 = str.substring(str.indexOf(':') + 1); // if nr vPin typ int
  return strdata3.toInt();
}

// >>>>>>>>>>>>>>>>  vPin from APP to NANO  
// equivalent BLYNK_WRITE(VPin)

void Blynkwrite(String str2) {

  String istr = str2.substring(1, str2.indexOf(':')); // nr vPin
  byte pin2 = istr.toInt();
  String  strdata2 = str2.substring(str2.indexOf(':') + 1); // 
  int  data2 = strdata2.toInt();

  if (str2.charAt(0) == 'V') {

    switch (pin2 + 1) { // Switch to progrma for recived vPin
      case 1: Program_vPin0(strdata2);  break;
      // etc....
      case 30: Program_vPin29(strdata2);  break;
      default: ;
    }
  }
}

// equivalent   Blynk.virtualWrite(VPin, Data);

void BlynkvirtualWrite(char Ch, byte vPin, String str) { // send to APP
  String  str1 = Ch + String(vPin) + ':' + str; // string to send 
  sendstrtoserial(str1);
}

void BlynkvirtualWrite_str (byte vPin1, String str1) { // send str to APP
  BlynkvirtualWrite('S', vPin1, str1);
}

void BlynkvirtualWrite_int (byte vPin2, int z)  { // send int to APP
  BlynkvirtualWrite('I', vPin2, String(z));
}

void BlynkvirtualWrite_led(byte vPin3, byte Value) { // send LEDs data to APP
BlynkvirtualWrite('L', vPin3, String(Value));
}

void BlynkvirtualWrite_col(byte vPin4, String col) { // send color data to APP
BlynkvirtualWrite('C', vPin4, col);
}

void BlynkvirtualWrite_On(byte vPin5, String ON) { // send onLabels data to APP
BlynkvirtualWrite('N', vPin5, ON);
}
void BlynkvirtualWrite_Off(byte vPin6, String OFF) { // send offLabel data to APP
 BlynkvirtualWrite('F', vPin6, OFF);
}

// ................................. send string to serial
void sendstrtoserial (String toserial) {
  Serial.println(toserial);
}
// ....................................... recive string from serial

bool  stringComplete = false;
String inputString = "";
byte licznikodbioru = 0;
int startrecive = 0;

void recivestrfromserial () {
  if (stringComplete) {
    Blynkwrite(inputString);
    inputString = "";
    stringComplete = false;
    licznikodbioru = 0;
    startrecive = 0;
  }
}

void serialEvent() { //odbiór string z Serial
  if (Serial.available()) {
    licznikodbioru++;
    char inChar = (char)Serial.read();
    if (startrecive == 0) {
      if ((inChar == 'V'))  startrecive = 1;
    }
    if (startrecive == 1) {
      if (inChar == '\r')  {
        stringComplete = true;
        inChar = '\0';
        inputString += inChar;
        startrecive = 0;
      } else  inputString += inChar;
    }
    if (licznikodbioru > 50) inputString = "";
  }
}

void setup()
{
  pinMode(led_red, OUTPUT); //test
  pinMode(led_green, OUTPUT); //test
  akcja.attach(0, 5000, timer1sek); //  5 sek

  Serial.begin(115200);
}
void loop()
{
  akcja.process(); // timer
  recivestrfromserial();
}

//================================== tests
void timer1sek() { //test
  blinkLedgreen(); // blink led on NANO
  blinkvLed(); // tests
  blinkvLed2(); / /tests
}

bool flagaled = 0;
void blinkLedgreen() { //test
  flagaled = !flagaled;
  digitalWrite(led_green, flagaled);
}
int x = 0;
void blinkvLed() { //for test
  if (flagaled)   BlynkvirtualWrite_led(3, 255); else    BlynkvirtualWrite_led(3, 0);
  x++;
  BlynkvirtualWrite_int(7, x);

}
void blinkvLed2() { //for test
  String col1 = "#FF0000";
  String col2 = "#00FF00";
  if (flagaled)   BlynkvirtualWrite_led(4, 255); else    BlynkvirtualWrite_led(4, 0);
  if (flagaled)   BlynkvirtualWrite_col(5, col1); else    BlynkvirtualWrite_col(5, col2);
  if (flagaled)   BlynkvirtualWrite_Off(5, "UP"); else    BlynkvirtualWrite_Off(5, "DOWN");
}