SoftwareSerial

Hello,
I have short question regarding this code. This code is
used for sending an e-mail, when a button is pressed.
:

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT SwSerial
/* Set this to a bigger number, to enable sending longer messages */
//#define BLYNK_MAX_SENDBYTES 128
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleStream.h>

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

void emailOnButtonPress()
{
  // *** WARNING: You are limited to send ONLY ONE E-MAIL PER 15 SECONDS! ***

  // Let's send an e-mail when you press the button
  // connected to digital pin 2 on your Arduino

  int isButtonPressed = !digitalRead(2); // Invert state, since button is "Active LOW"

  if (isButtonPressed) // You can write any condition to trigger e-mail sending
  {
    SwSerial.println("Button is pressed."); // This can be seen in the Serial Monitor
    Blynk.email("your_email@mail.com", "Subject: Button Logger", "You just pushed the button...");

    // Or, if you want to use the email specified in the App (like for App Export):
    //Blynk.email("Subject: Button Logger", "You just pushed the button...");
  }
}

void setup()
{
  // Debug console
  SwSerial.begin(9600);

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

  // Send e-mail when your hardware gets connected to Blynk Server
  // Just put the recepient's "e-mail address", "Subject" and the "message body"
  Blynk.email("your_email@mail.com", "Subject", "My Blynk project is online.");

  // Setting the button
  pinMode(2, INPUT_PULLUP);
  // Attach pin 2 interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(2), emailOnButtonPress, CHANGE);
}

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

I understand the mehthod of UART, but
why: SoftwareSerial SwSerial(10, 11)?
Why are they used as virtual RX/TX. Is not ist obsolete, because nothing is actually connected to these pins.
What specific task do the pins 10 and 11 have?

Please help me.
I have been searching a lot.

The code is designed to run on an Arduino Uno or similar, that doesn’t have any built-in internet connectivity and has only one hardware serial port.
In this example, the connection to the internet will be via a a USB connection to a PC. The PC will be running a script to allow USB to Internet connection.
The Arduino will connect to the PC with the single hardware serial port. Without a second serial port, it will be impossible to see debug messages, so SoftwareSerial is needed to create a virtual serial port for debugging. To be able to view these debug messages, an FTDI adaptor is needed that will be connected to pins 10 and 11 on the Arduino (with Tx/Rx transposed).

Pete.

2 Likes

thanks a lot Pete :smiley: