Arduino Email Sending

Try this sketch (without email) that includes PIR and tone etc.
Remember to include your working token before the upload to Arduino.

/* 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>

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

BlynkTimer timer;

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int pinSpeaker = 10;           //Set up a speaker on a PWM pin (digital 9, 10, or 11)

// 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()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    playTone(300, 160);
    delay(150);

    
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
      digitalWrite(ledPin, LOW); // turn LED OFF
      playTone(0, 0);
      delay(300);    
      if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  } 
}

// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);
    }
}

void setup()
{
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(pinSpeaker, OUTPUT);
  // 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);

  // Setup a function to be called every 437ms
  timer.setInterval(437L, myTimerEvent);
}

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

Let me know if PIR and tone work OK.

So now i just plug in my circuit and upload this, but how do i check the tone? i am sorry i am new to this

I thought you had the hardware for this i.e. a buzzer.

Sketch should work without the buzzer, just no sound.

When i turn it on i only hear a huge buz from the buzzer but my led or PIR doesnt seem to work

i am not sure i took out the buzzer, in my final thing i want to have a buzzer if thats fine. I dont think the PIR sensor works as whenever i move infront of it it does not light up the LED.

only the led in the Arduino the built in one blinks

Add a Blynk LED on V0 and add the email widget to your project.
Then try this sketch without buzzer but with PIR and email.
Enter your email address in the sketch and your token.

// PIR4Blynker.ino on Arduino
/* 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>

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

BlynkTimer timer;

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int pinSpeaker = 10;            //Set up a speaker on a PWM pin (digital 9, 10, or 11)
long lastTrigger;                     // wait at least 15s before sending email / push again
unsigned int pir = LOW; // declare variable to store PIR status
char Mail[] = "xxxxxxxxx";            //  ENTER YOUR EMAIL ADDRESS
String Subject = "PIR Activated";     // subject message
String MsgBody = "Intruder detected"; // message content

// 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()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
  pir = digitalRead(inputPin);
  if (pir == HIGH) // If alarm enabled
  {
      digitalWrite(ledPin, HIGH);  // turn LED ON
      Blynk.virtualWrite(V0, 255);
      if(lastTrigger == 0 || millis() > lastTrigger + 15123L)
      {         
        Blynk.email(Mail, Subject, MsgBody);  
        lastTrigger = millis();       
      }  
  } 
  else
  {
    digitalWrite(ledPin, LOW); // turn LED OFF
    Blynk.virtualWrite(V0, 0);
  }
}

void setup()
{
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(pinSpeaker, OUTPUT);
  // 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);

  // Setup a function to be called every 473ms
  timer.setInterval(473L, myTimerEvent);
}

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

I can see that the PIR sensor is working and the led is working. The problem is the value is not going up on the blynk app and i am not getting the email.

I am not sure what happened but right now my system on terminal is reconnecting which means it is not connected to blynk app i think overtime it will connect back

Nevermind it just went online and tthe value started working

I am not sure if you are controlling it but the led is now blinking constantly just saying and the value reader stopped

No I am not controlling it.

Which LED is blinking (physical, virtual or both)?

I can see your email address in the email widget but did you also enter it in the sketch?

I can mod the sketch so you don’t need to enter it but for now ensure the email is in the sketch.

Have you received any emails yet?

I have entered the email in the sketc, i have still didnt receive any emails i think i have google email which can cause a problem maybe i have to check the settings?
The LED the physical one was working as well as the small orange one on the arduino is blinking.

// PIR4Blynker.ino on Arduino
/* 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>

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

BlynkTimer timer;

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int pinSpeaker = 10;            //Set up a speaker on a PWM pin (digital 9, 10, or 11)
long lastTrigger;                     // wait at least 15s before sending email / push again
unsigned int pir = LOW; // declare variable to store PIR status
char Mail[] = "secret";            //  ENTER YOUR EMAIL ADDRESS
String Subject = "Security Notice";     // subject message
String MsgBody = "Intruder has Been Detected"; // message content

// 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()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
  pir = digitalRead(inputPin);
  if (pir == HIGH) // If alarm enabled
  {
      digitalWrite(ledPin, HIGH);  // turn LED ON
      Blynk.virtualWrite(V0, 255);
      if(lastTrigger == 0 || millis() > lastTrigger + 15123L)
      {         
        Blynk.email(Mail, Subject, MsgBody);  
        lastTrigger = millis();       
      }  
  } 
  else
  {
    digitalWrite(ledPin, LOW); // turn LED OFF
    Blynk.virtualWrite(V0, 0);
  }
}

void setup()
{
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(pinSpeaker, OUTPUT);
  // 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);

  // Setup a function to be called every 473ms
  timer.setInterval(473L, myTimerEvent);
}

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

Should i try using a different email? i will be back in 10 minutes as i can see you are testing it

I am not testing it, that will be a hacker because you keep posting your token in a public forum.
As you don’t have a USB2TTL adaptor and there is a conflict with your pinspeaker I have revised the sketch.
Again add email address and use a new token but send it to me for testing.

// PIR4Blynker.ino on Arduino
/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT SwSerial


//#include <SoftwareSerial.h>
//SoftwareSerial SwSerial(6, 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";

BlynkTimer timer;

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int pinSpeaker = 10;            //Set up a speaker on a PWM pin (digital 9, 10, or 11)
long lastTrigger;                     // wait at least 15s before sending email / push again
unsigned int pir = LOW; // declare variable to store PIR status
char Mail[] = "xxxxxxxxx";            //  ENTER YOUR EMAIL ADDRESS
String Subject = "PIR Activated";     // subject message
String MsgBody = "Intruder detected"; // message content

// 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()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
  pir = digitalRead(inputPin);
  if (pir == HIGH) // If alarm enabled
  {
      digitalWrite(ledPin, HIGH);  // turn LED ON
      Blynk.virtualWrite(V0, 255);
      if(lastTrigger == 0 || millis() > lastTrigger + 15123L)
      {         
        Blynk.email(Mail, Subject, MsgBody);  
        lastTrigger = millis();       
      }  
  } 
  else
  {
    digitalWrite(ledPin, LOW); // turn LED OFF
    Blynk.virtualWrite(V0, 0);
  }
}

void setup()
{
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(pinSpeaker, OUTPUT);
  // 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);

  // Setup a function to be called every 473ms
  timer.setInterval(473L, myTimerEvent);
}

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

i am not having a problem where it sas swserial was not declared

Ah yes comment out the first line too.

[quote="Dankharl, post:116, topic:22898"]
SoftwareSerial SwSerial(10, 11); // RX, TX
[/quote]

SoftwareSerial SwSerial(10, 11); // RX, TX

this one?

I have now activated it using terminal test it i sent you the token

No I already commented that out and changed the pins from 10 and 11 to 6 and 11.

#define BLYNK_PRINT SwSerial

needs to be:

//#define BLYNK_PRINT SwSerial

Plus the other mods I did in the sketch re SW serial etc.