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;
}
}