STM32 with a blynk + esp8266 (esp32) in a transparent mode

I want the blynk to work on STM32, and ESP8266 to work in a transparent mode for transport of a blynk stream.
Communication between microcontrollers can be on any bus: Serial, I2C, SPI, etc …


Code that works on STM32:

#include <Arduino.h>
#include <BlynkSimpleStream.h>

HardwareSerial Serial2(PA3, PA2);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "*************************";

void setup()
{
  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial2.begin(9600);
  Blynk.begin(Serial2, auth);
}

void loop()
{
  Blynk.run();
}

Code that works on ESP8266:

#include <Arduino.h>
#include "ESP8266WiFi.h"
#include <BlynkSimpleStream.h>

byte bufRX;
byte bufTX;

const char* ssid = "TP-LINK_XXXXX";
const char* pass = "**********";

WiFiClient wifiClient;

// This function tries to connect to the cloud using TCP
bool connectBlynk()
{
  wifiClient.stop();
  return wifiClient.connect(BLYNK_DEFAULT_DOMAIN, BLYNK_DEFAULT_PORT);
}

// This function tries to connect to your WiFi network
void connectWiFi()
{
  if (pass && strlen(pass)) 
  {
    WiFi.begin((char*)ssid, (char*)pass);
  }
  else
  {
    WiFi.begin((char*)ssid);
  }

  while (WiFi.status() != WL_CONNECTED) 
  {
    digitalWrite(2, !digitalRead(2));
    delay(500);  
  }
}

// This function is used by Blynk to receive data
size_t BlynkStreamRead(void* buf, size_t len)
{
  return wifiClient.readBytes((byte*)buf, len);
}

// This function is used by Blynk to send data
size_t BlynkStreamWrite(const void* buf, size_t len)
{
  return wifiClient.write((byte*)buf, len);
}

void setup() 
{
  Serial.begin(9600);
  
  connectWiFi();
  connectBlynk();
}

void loop() 
{
  // Reconnect WiFi
  if (WiFi.status() != WL_CONNECTED) 
  {
    connectWiFi();
    return;
  }

  // Reconnect to Blynk Cloud
  if (!wifiClient.connected()) 
  {
    connectBlynk();
    return;
  }

  if (Serial.available() > 0)
  {
    Serial.readBytes(&bufTX, 1);
    BlynkStreamWrite(&bufTX, 1);
  }
  
  if (wifiClient.available() > 0)
  {
    BlynkStreamRead(&bufRX, 1);
    Serial.write(&bufRX, 1);
  } 
}

It works great.

But I want to ask:

  1. Is it optimal to redirect blynk packets this way? 1 byte is sent here. Is it possible to somehow make a whole package, so that in the future it would be appropriate to use DMA? Do I need to separate the buffer for TX and RX?
if (Serial.available() > 0)

  {

    Serial.readBytes(&bufTX, 1);

    BlynkStreamWrite(&bufTX, 1);

  }

  

  if (wifiClient.available() > 0)

  {

    BlynkStreamRead(&bufRX, 1);

    Serial.write(&bufRX, 1);

  }
  1. On STM32 the blynk stream is sent to the Serial2, or to the wire bus, all that supports a simple stream.
Blynk.begin(Serial2, auth);

Is it possible to send a blynk stream to the program buffer? So that I can then wrap the stream from the buffer in my wrapper and then send that modified stream to the serial2 or wire bus? I can’t figure out how to do it instead of Blynk.begin (Serial2, auth); there was something like Blynk.begin (buffers, auth); where buffers is the buffer for the stream: Stream buffers(buf); <- but it doesn’t work, I don’t know how to declare the program buffer as a stream.

help please!

Did so. Everything works fine:

#include <Arduino.h>
#include "ESP8266WiFi.h"

#define BLYNK_DEFAULT_DOMAIN     "blynk-cloud.com"
#define BLYNK_DEFAULT_PORT       80
#define BLYNK_DEFAULT_PORT_SSL   443

const char* ssid = "TP-LINK_xxxxx";
const char* pass = "xxxxxxxxxxxxx";

WiFiClient wifiClient;

// This function tries to connect to the cloud using TCP
bool connectBlynk()
{
  wifiClient.stop();
  return wifiClient.connect(BLYNK_DEFAULT_DOMAIN, BLYNK_DEFAULT_PORT);
}

// This function tries to connect to your WiFi network
void connectWiFi()
{
  if (pass && strlen(pass)) 
  {
    WiFi.begin((char*)ssid, (char*)pass);
  }
  else
  {
    WiFi.begin((char*)ssid);
  }

  while (WiFi.status() != WL_CONNECTED) 
  {
    digitalWrite(2, !digitalRead(2));
    delay(500);  
  }
}

void setup() 
{
  Serial.begin(115200);
  
  connectWiFi();
  connectBlynk();
}

void loop() 
{
  // Reconnect WiFi
  if (WiFi.status() != WL_CONNECTED) 
  {
    connectWiFi();
    return;
  }

  // Reconnect to Blynk Cloud
  if (!wifiClient.connected()) 
  {
    connectBlynk();
    return;
  }

  if (Serial.available())
  {
    wifiClient.write(Serial.read());
  }

  if (wifiClient.available())
  {
    Serial.write(wifiClient.read());
  } 
}
1 Like