Serial data out

I have a situation where l want to be able to control a device that responds to serial commands, what l would like to know can l send serial strings from an esp32 or esp8266 on response from a button operating on a device running a blynk app.
I have seen that l can read data from a controller to a blynk app, but l as yet can’t see how to go from blynk to an external devices serial input to make it respond to a command.
Help with direction on this would ne appreciate, in the mean time I’ll keep reading !
Thanks in advance.

Blynk is very fussy about timing, and likes a very clean void loop which executes extremely rapidly in all circumstances.
Serial monitoring code usually needs to sit in the void loop and listen for serial data arriving, and process it when it does arrive. This process is also very fussy about timing, especially at higher baud rates This can lead to Blynk disconnections if you aren’t very careful.

The simplest solution would probably be to use an ESP32 and use its multi-threaded capability. You could run Blynk in one thread and the serial monitor in another.

The other option is to use the Blynk HTTP(S) API to push data to the Blynk server, rather than maintaining a permanent connection to the Blynk server
I guess the decision is based in what exactly it is that you’re trying to achieve.

Pete.

Hi Pete, thanks for your reply, if l may just explain myself hopefully make it clearer. I have a video switcher, that has the capacity to control it from its RS232 input. What l would like to do is have a buttons in blynk that will result in a string being sent out to the Serial input of the device and therefore change the inputs on the video switcher.
This is the first time l have looked at this platform to be honest l just want to see if l can do it. The more l read about blynk the more l like what l see and what others have achieved,thanks in advance.

I have a decent serial read/send function that doesn’t seem to interfere with blynk when called in a blynk.timer

I’m away from my computer right now but I’ll send it later today, alternatively you can check my posts, a good portion of them have my code in it which if you browse through has the serial send and read function in it and the blynk timer in setup.

That would be great, thanks for your offer to share, regards
Bruce

Forgot to add that l am new to Blynk and this forum and so far am enjoying both

Ah, sorry, I obviously didn’t read your post properly.
Sending serial data is much easier than listening g for it.

The first thingy do is to get it working without Blynk, probably sending the serial output in response to the press of a physical button.

Then it’s fairly simple to get this working with Blynk in response to a button connected to a virtual pin.

Pete.

Thanks Pete, that’s the course I’ll go down, thanks again, regards
Bruce

Not sure if this will be useful to your project, but here is what I use for serial communication between an Atmega328p and an ESP32CAM that has the Blynk connection. In my program I can get away with sending single character commands between hardware which helps simplify things. I run non blocking timers on both sides so they aren’t spamming each other with commands.

Atmega code snippet:

// UART Communication
char camRx;                             // Command Character Received from ESP32-Cam
char coopTx;                            //Communication From Coop Command
bool newDataRx = false;          //Has CoopCommand received a new command from the ESP32-Cam?
unsigned long serialDelay = 1000;      //delay to send coop status updates
unsigned long lastSerialSend = 0;       //the last time an update was sent

void camCommand() {
  if (Serial.available() > 0) {
    camRx = Serial.read();
    newDataRx = true;
  }
  if (newDataRx == true) {
    if (camRx == 'U') { //If the ESP32 says to put the door up
      photocellReadingLevel = '3';
      lastPhotocellReadingTime = millis();
      newDataRx = false;
    }
    else if (camRx == 'D') { //If the ESP32 says to put the door down
      photocellReadingLevel = '1';
      lastPhotocellReadingTime = millis();
      newDataRx = false;
    }
    else if (camRx == 'L') { //If the ESP32 says it is taking a picture
      digitalWrite(layLightRelay, HIGH);
    }
    else if (camRx == 'N') { //If the ESP32 says it is NOT taking a picture
      digitalWrite(layLightRelay, LOW);
    }
  }
  if ((unsigned long)(millis() - lastSerialSend) >= serialDelay) {
    lastSerialSend = millis();
    if (doorClosed) { // If door is closed
      Serial.print('S');
    }
    else if (doorOpen) { // If door is open
      Serial.print('O');
    }
    else if (doorOpenMove) { //If door is opening
      Serial.print('U');
    }
    else if (doorCloseMove) { //If door is closing
      Serial.print('D');
    }
  }
}

ESP32 Code Snippet:

void setup() {
timer.setInterval(500,coopCom);
}
void coopCom ( void ) {
 
  if (Serial.available() > 0) {
    coopRx = Serial.read();
    newDataRx = true;
  }
  if (newDataRx == true) {
    if (coopRx == 'O') { //If CoopCommand says the door is up
      led1.on();
      led2.off();
      led3.off();
      led4.off();
      newDataRx = false;
    }
    if (coopRx == 'S') { //If CoopCommand says the door is down
 
      led1.off();
      led2.on();
      led3.off();
      led4.off();
      newDataRx = false;
    }
    if (coopRx == 'U') { //If CoopCommand says the door is opening
      led1.off();
      led2.off();
      led3.on();
      led4.off();
      newDataRx = false;
    }
    if (coopRx == 'D') { //If CoopCommand says the door is closing
      led1.off();
      led2.off();
      led3.off();
      led4.on();
      newDataRx = false;
    }
  }
}
 
// Put the door up
 
BLYNK_WRITE(DOORUP) {
Serial.print('U');
}
 
// Put the door down
 
BLYNK_WRITE(DOORDOWN) {
Serial.print('D');
}
 

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