[USB + serial monitor help] How do you use spare hardware to enable manual serial monitor writing?

I looked through community for help; I apologize if topic is redundant.

Hardware: Arduino Mega
Connection: USB

I am successfully connected. How do I use a spare hardware serial on my Mega such that I might still be able to output/print strings to serial monitor? Currently (after project is online) I see in serial monitor (if it doesn’t crash due to ‘busy port’) is the auth token being printed over and over.

I’d like to use the alternative hardware connection so that I can manually write to serial the character strings related to Widget menu item chosen. The code below is representative of what I’d like to be capable of…I just don’t know what to connect on the Megaboard nor how to adapt the code accordingly.

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleStream.h>

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

//TOU schedule indicated by user
BLYNK_WRITE(V1) {
  switch (param.asInt())
  {
    case 1: // Item 1
        Serial.println("*****\nCALIFORNIA TOU Schedule chosen.");
        break;
    case 2: // Item 2
        Serial.println("*****\nHAWAII TOU Schedule chosen.");
        break;
    default:
        Serial.println("*****\nUNKNOWN SELECTION");
  }
}

//Time Zone designated by user
BLYNK_WRITE(V2) {
  switch (param.asInt())
  {
    case 1: // Item 1
        Serial.println("*****\nPacific Standard Time (PST) has been selected.");
      break;
    case 2: // Item 2
        Serial.println("*****\nHawaii-Aleutian Standard Time (HST) has been selected.");
      break;
    default:
        Serial.println("*****\nUnknown TIME ZONE selected");
  }
}

BLYNK_WRITE(V3) {
  switch (param.asInt())
  {
    case 1: // Item 1
        Serial.println("*****\nResident's Battery Bank: 24V System");
      break;
    case 2: // Item 2
        Serial.println("*****\nResident's Battery Bank: 48V System");
      break;
    default:
        Serial.println("*****\nUnknown TIME ZONE selected");
  }
}
void setup()
{
  // Debug console
  DebugSerial.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);
}

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

Hello. In case you’re using USB as the communication channel with Blynk you can’t use Serial for prints at same time. As I know. In general USB is not a “real case”. I would recommend you to add some ethernet or wi-fi shield. It will simplify things a lot.

@Dmitriy That’s exactly what I needed to verify, thank you.

Now that I have switched to wifi shield and currently successfully connected to Blynkproject, the serial monitor will print BLYNK_PRINT caption(s) but will not print when an item is chosen on the ‘WidgetMenu’. Any suggestions?
Code:

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <WiFi.h>
#include <BlynkSimpleWifi.h>

char auth[] = "21688275056f4ca18b8b0b49efe72e4e";  // BLYNKprojectUserInfo
char ssid[] = "jones";  // Your WiFi credentials. - jones,UNCA_PSK
char pass[] = "9102742727"; // Set password to "" for open networks. - 9102742727,GoBulldogs!

//TOU Schedule
BLYNK_WRITE(V1) {
  switch (param.asInt())
  {
    case 1: // Item 1
        Serial.println("*****\nCALIFORNIA TOU Schedule chosen.");
        break;
    case 2: // Item 2
        Serial.println("*****\nHAWAII TOU Schedule chosen.");
        break;
    default:
        Serial.println("*****\nUNKNOWN SELECTION");
  }
}
BLYNK_WRITE(V2) {
  switch (param.asInt())
  {
    case 1: // Item 1
        Serial.println("*****\nPacific Standard Time (PST) has been selected.");
      break;
    case 2: // Item 2
        Serial.println("*****\nHawaii-Aleutian Standard Time (HST) has been selected.");
      break;
    default:
        Serial.println("*****\nUnknown TIME ZONE selected");
  }
}
BLYNK_WRITE(V3) {
  switch (param.asInt())
  {
    case 1: // Item 1
        Serial.println("*****\nResident's Battery Bank: 24V System");
      break;
    case 2: // Item 2
        Serial.println("*****\nResident's Battery Bank: 48V System");
      break;
    default:
        Serial.println("*****\nUnknown TIME ZONE selected");
  }
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
 
}
void loop()
{
  Blynk.run();
  }

The code seems fine. Does your menu widget assigned to corresponding virtual pins? Do you see “Device is offline” message on app side?

@Dmitriy pins are correctly designated and I am not offline. The widgets that are pushing data to the project are working correctly. Just the menu widgets are not printing case selection when chosen. Thank you for this app btw…really useful for my sr. design project’s user interface. I’m grateful.

iOS/Ansorid? Latest version?

Firstly, welcome to Blynk! (EDIT - I later saw that you already got the code formatting part :wink: )

Secondly, Are you printing to the serial monitor in the Arduino IDE? Or are you wanting to see the data elsewhere?

If elsewhere, no need to use software serial on an MEGA… there are three other hardware serial ports on pins 13-19. Simply refer to them as Serial1, Serial2, and Serial3, just as you do with Serial (the primary programming port). No need to define pin as is needed with SoftwareSerial.

Because of them. it is entirely possible to easily run a Mega on USB-Link (serial) and have Debug or any other serial prints on one or more of the remaining three

#define BLYNK_PRINT Serial1 // Defines 2nd serial port on MEGA for Blynk data printing
#define BLYNK_DEBUG        // Optional, this enables more detailed prints   

// Debug console
Serial1.begin(9600);  // Pins 18 & 19

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

That’s how I run my Mega (as I do not have a working alternative connection option). I run a TTL-USB adapter on Serial1 and view the output with >Termite terminal program< on the PC and the >Terminal Widget< in the App to view serial data as required.

That all said, if you have Ethernet or Wifi, then use it… less tethering to the PC is always nice :wink: and the MEGA has plenty of pins to spare (and you don’t have to stop and start the USB BAT file every time you update the program).

I think you still need that final break; at the end of each switch statment.

1 Like