Arduino local code problem - manual switch and buzzer doesn't work with Blynk USB connection code

Hi everyone, I’m doing a project for an engineering paper and have encountered a problem. The issue is that my locally connected buttons and buzzer are not working. If I comment out the line Blynk.begin(Serial, BLYNK_AUTH_TOKEN); then the button and buzzer work fine. Is the problem related to the Serial port, that there is only one on the Arduino UNO? Is it somehow possible to solve it differently? I connect via USB with the arduino, the same with the blynk platform.

Blynk.begin() is a blocking function. If no internet or Blynk server connection can be established then all code execution will stop.
There are potential solutions to this, but I don’t think they will work using your hardware.

If you’re using the serial connection method then are you running the latest version of the blynk-ser.bat script (with set SERV_ADDR=blynk.cloud) and have you edited the script to use the correct COM port and baud rate?

What does the script terminal window show?

Pete.

set COMM_PORT=COM5
set COMM_BAUD=9600
set SERV_ADDR=blynk.cloud
set SERV_PORT=80
it’s my configuration blynk-ser.bat. I am using the latest version and the arduino is hooked up to COM5.
In the console I see ERROR Input/Output (5) reconnecting in … sec.
In another sketch where I only have a connection with Blynk without operating the device with a manual button (it is a buzzer), with the same port configuration, the connection in the console is correct .

In that case you’re going to have to provide much more info if you want assistance with your problem.

Pete.

This code is not working

#define BLYNK_TEMPLATE_ID           "TMPL4bpytZN-Z"
#define BLYNK_TEMPLATE_NAME         "ArduinoApp"
#define BLYNK_AUTH_TOKEN            "GjGg9LZ9jaCwY5Gx-a4xlQxL1vftoucz"

#define BLYNK_PRINT SwSerial


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

#include <BlynkSimpleStream.h>
#include <DHT.h>


#define firePin 10
#define smokePin A0
#define buzzer 12
int fire_Val = 0;
int smoke_Val = 0;
WidgetLED led(V1);

//Door bell
const int ButtonPin = 6;
const int BuzzerPin = 7;
int ButtonState = 0;

#define DHTPIN 9       

//DHT 11
#define DHTTYPE DHT11     

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 

  if (isnan(h) || isnan(t)) {
    SwSerial.println("Failed to read from DHT sensor!");
    return;
  }
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void sendSensorFire()
{
 fire_Val = digitalRead(firePin);
  if (fire_Val == LOW)
  {
    Blynk.logEvent("fire_event");
    //digitalWrite(GREEN, LOW);
    //digitalWrite(RED, HIGH);
    digitalWrite(buzzer, HIGH);
    Blynk.virtualWrite(V0, 1);
  }

  else
  {
    //digitalWrite(GREEN, HIGH);
    //digitalWrite(RED, LOW);
    digitalWrite(buzzer, LOW);
    Blynk.virtualWrite(V0, 0);
  }  
}

void sendSensorSmoke()
{
 smoke_Val = analogRead(smokePin);
 Blynk.virtualWrite(V2, smoke_Val);

  if (smoke_Val > 50)
  {
    Blynk.logEvent("smoke_event");
    digitalWrite(buzzer, HIGH);
    led.on(); //V1 virtual pin LED
  }

  else
  {
    digitalWrite(buzzer, LOW);
    led.off();
  }  
}


void doorBell()
{
  //Door bell
  ButtonState = digitalRead(ButtonPin);
  if (ButtonState == HIGH) {
      tone(BuzzerPin, 500, 300);
      delay(100);
      tone(BuzzerPin, 600, 300);
      delay(100);
      tone(BuzzerPin, 700, 300);
      delay(100);
      tone(BuzzerPin, 800, 300);
      delay(100); 
  } 
  else {
  }
}


void setup()
{

  // Debug console
  SwSerial.begin(115200);

  pinMode(firePin, INPUT);
  pinMode(smokePin, INPUT);
  pinMode(buzzer, OUTPUT);

  //Door bell
  pinMode(BuzzerPin, OUTPUT);
  pinMode(ButtonPin, INPUT);


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

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1000L, sendSensorFire);
  timer.setInterval(1000L, sendSensorSmoke);
}

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

This code is working

#define BLYNK_TEMPLATE_ID           "TMPL4bpytZN-Z"
#define BLYNK_TEMPLATE_NAME         "ArduinoApp"
#define BLYNK_AUTH_TOKEN            "GjGg9LZ9jaCwY5Gx-a4xlQxL1vftoucz"

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT SwSerial


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

#include <BlynkSimpleStream.h>
#include <DHT.h>


#define firePin 10
#define smokePin A0
#define buzzer 12
int fire_Val = 0;
int smoke_Val = 0;
WidgetLED led(V1);


#define DHTPIN 9          // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
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 sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    SwSerial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void sendSensorFire()
{
 fire_Val = digitalRead(firePin);
  if (fire_Val == LOW)
  {
    Blynk.logEvent("fire_event");
    //digitalWrite(GREEN, LOW);
    //digitalWrite(RED, HIGH);
    digitalWrite(buzzer, HIGH);
    Blynk.virtualWrite(V0, 1);
    //Serial.print("fIRE Level: ");
    //Serial.println(fire_Val);
    
  }

  else
  {
    //digitalWrite(GREEN, HIGH);
    //digitalWrite(RED, LOW);
    digitalWrite(buzzer, LOW);
    Blynk.virtualWrite(V0, 0);
    //Serial.print("fIRE Level: ");
    //Serial.println(fire_Val);
    
  }  
}

void sendSensorSmoke()
{
 smoke_Val = analogRead(smokePin);
 Blynk.virtualWrite(V2, smoke_Val);

  if (smoke_Val > 50)
  {
    Blynk.logEvent("smoke_event");
    //digitalWrite(GREEN, LOW);
    //digitalWrite(RED, HIGH);
    digitalWrite(buzzer, HIGH);
    led.on(); //V1 virtual pin LED
    
    //Serial.print("fIRE Level: ");
    //Serial.println(fire_Val);
    //led.on(); //V1 virtual pin LED
  }

  else
  {
    //digitalWrite(GREEN, HIGH);
    //digitalWrite(RED, LOW);
    digitalWrite(buzzer, LOW);
    //Blynk.virtualWrite(V2, 0);
    //Serial.print("fIRE Level: ");
    //Serial.println(fire_Val);
    led.off();
  }  
}


void setup()
{

  // Debug console
  SwSerial.begin(115200);

  pinMode(firePin, INPUT);
  pinMode(smokePin, INPUT);
  pinMode(buzzer, OUTPUT);


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

  dht.begin();
  

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1000L, sendSensorFire);
  timer.setInterval(1000L, sendSensorSmoke);
}

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

Please edit your post, using the pencil icon at the bottom, and delete your screenshot and replace it with the code text, with triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Please do the same thing if you post batch file terminal output.

Working code, non-working code and batch file terminal output for the working code and the non-working code are probably going to the minimum amount of data that you need to share.

Pete.

Sorry, the post above has already been edited and the code in both cases is now visible.
In not working code in the console i see:

ERROR Input/Output (5)

In the correct connection with working code i see:

Connecting device at COM5 to blynk.cloud:80...
OpenC0C("\\.\COM5", baud=9600, data=8, parity=no, stop=1) - OK
Connect (“blynk.cloud", “80") - OK
InOut () START
DSR is OFF

Any solutions?

Nothing obvious jumps out, except that you’re using GPIO10 twice, but you’re doing that in both sketches.

You probably won’t get much further without seeing the debug serial output from your SoftwareSerial port (which won’t work effectively at anything higher than 9600 baud on your device).
To do that you’re going to need a TTL to USB (FTDI) adapter.

Is there any reason why you’re using an Uno and Serial connection rather than something like a NodeMCU with WiFi connectivity?

Pete.