Issue with serial COM port Arduino Uno using blynk with usb

Hello,

my project is to monitor home energy using Arduino. I have done the simple blink blynk where I controlled Arduino led to turn off and on using blynk switch with no problem. However, when I added my sketch to display KWH on blynk app I receive this:
"avrdude: ser_open(): can’t open device “\.\COM5”: Access is denied.

Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.
"

here is my code

#include <EmonLib.h>
#include <Blynk.h>
#define BLYNK_PRINT DebugSerial


// You could use a spare Hardware Serial on boards that have it (like Mega)


#include <BlynkSimpleStream.h>
 
EnergyMonitor emon1;
 

#define currCalibration 0.52
 
BlynkTimer timer;

float kWh = 0;
unsigned long lastmillis = millis();
 
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "********";


void myTimerEvent() {
  
    emon1.calcVI(20, 2000);
    Serial.print("Vrms: ");
    Serial.print(emon1.Vrms, 2);
    Serial.print("V");
    Blynk.virtualWrite(V0, emon1.Vrms);
    Serial.print("\tIrms: ");
    Serial.print(emon1.Irms, 4);
    Serial.print("A");
    Blynk.virtualWrite(V1, emon1.Irms);
    Serial.print("\tPower: ");
    Serial.print(emon1.apparentPower, 4);
    Serial.print("W");
    Blynk.virtualWrite(V2, emon1.apparentPower);
    Serial.print("\tkWh: ");
    kWh = kWh + emon1.apparentPower*(millis()-lastmillis)/3600000000.0;
    Serial.print(kWh, 4);
    Serial.println("kWh");
    lastmillis = millis();
    Blynk.virtualWrite(V3, kWh);
}
 

void setup()
{
  // Debug console
 Serial.begin(9600);
 emon1.current(A0, currCalibration); // Current: input pin, calibration.
  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Blynk.begin(Serial, auth);
   timer.setInterval(5000L, myTimerEvent);
}

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

Also, no errors when I verify the sketch and I made sure that the correct port is selected from the blynk-sar found in script file.
When I try to open serial monitor, I get the following:
Error opening serial port ‘COM5’. (Port busy)
My guess is there are more than one sketch fighting for the same port but I do not how.

cmd.exe window (always open):
Connecting device at COM5 to blynk-cloudcom:80…
OpenC0C("\.\COM5", baud=9600, data=8, parity=no, stop=1) - OK
Connect(“blynk-cloudcom”, “80”) - OK
InOut() START
DSR is OFF

You can’t do Serial.prints when using the serial port as your Blynk connection method.

BlynkSimpleStream requires EXCLUSIVE access to the serial port.

Pete.

thank you Pete. ok then is it possible to send sensor data from arduino to Blynk with serial port as my Blynk connection. Ignoring serial.print this time.

Yes, of course. Take a look at the other sketch builder examples

Pete.

Hi again. I edited the sketch but I still get the following :
avrdude: ser_open(): can’t open device “\.\COM5”: Access is denied.

Problem uploading to board. See for suggestions.

here is my edited code for a reference :

#include <EmonLib.h>
#define BLYNK_PRINT SwSerial
#include <SoftwareSerial.h>

SoftwareSerial SwSerial(11, 12); // RX, TX
#include <BlynkSimpleStream.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
#define currCalibration 62
#define Volt 120;
EnergyMonitor emon1;
     
float kWh = 0;
unsigned long lastmillis = millis();
char auth[] = "******";

BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  float  amps = emon1.calcIrms(7400);
  float  watt = amps * Volt; 
  kWh = kWh + watt*(millis()-lastmillis)/3600000000.0;
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, amps);
  Blynk.virtualWrite(V6, kWh); 
  lastmillis = millis(); 
}

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);
  emon1.current(A0, currCalibration); // Current: input pin, calibration.
  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

at this point I think maybe because I was running another sketch on COM5(the only thing plugged to my computer) before I tried to connect to Blynk. I restarted Arduino IDE, and my computer but still same issue.

I’d start by rebooting your PC with nothing plugged into your USB ports or serial ports other than keyboard, mouse etc.
it’s a PC then you need to go to Device Manager and see what COM ports are available, with and without your Arduino plugged-in.

TBH, using this connection method is really only useful as a way of quickly getting up and running with Blynk before you buy some IoT capable hardware such as a NodeMCU or ESP32.

Maybe time to stop wasting time and effort with this and buy some proper hardware.

Pete.

I’d need to order that. I’m behind schedule, and I really need to view the data on Blynk . I got to upload the sketch to Arduino (upload sketch before opening blynk-ser file) but I ran into another issue where the project on Blynk is offline.

I finally got it online. I changed SwSerial to DebugSerial. Then, uploaded the sketch, Finally, opened the Blynk-ser file.

Hi, I have the same problem as you. Is it possible to share your final code again? I tried to change the SwSerial-positions to DebugSerial, but couldn’t solve the problem so far.
Thank you!