Writing code to send email

Listing 5.4 shows sample code that you can use to send emails from the Raspberry Pi when a button is pressed. You can use the same hardware setup you used in the previous section to build this application:

  1. The push button is currently attached to the BCM_GPIO pin 23:
Listing 5.4: Sending Emails from Raspberry Pi when the push button is pressed

#define BLYNK_PRINT stdout

#ifdef RASPBERRY

#include <BlynkApiWiringPi.h>

#else

#include <BlynkApiLinux.h>

#endif

#include <BlynkSocket.h>

#include <BlynkOptionsParser.h>

#include <wiringPi.h>

#include <softPwm.h>

static BlynkTransportSocket _blynkTransport;

BlynkSocket Blynk(_blynkTransport);

#include <stdlib.h>

#include <BlynkWidgets.h>

SimpleTimer timer;

constintbtnPin = 23;

void notifyMe()

{

if(digitalRead(btnPin)== LOW)

{

Blynk.email("[email protected]", "Hello", "Hello Blynkers");

}

}

void setup()

{

wiringPiSetup();

pinMode(btnPin, INPUT);

pullUpDnControl(btnPin, PUD_UP);

timer.setInterval(1000, notifyMe);

}

void loop()

{

Blynk.run();

timer.run();

}

int main(intargc, char* argv[])

{

const char *auth, *serv;

uint16_t port;

parse_options(argc, argv, auth, serv, port);

Blynk.begin(auth, serv, port);

setup();

while(true) {

loop();

}

return 0;

}
  1. The Blynk.email() function allows you to send a maximum of 120 symbols including email, subject, and message:
Blynk.email("[email protected]", "Hello", "Hello Blynkers");
  1. However, you can increase this limit if necessary by adding #define BLYNK_MAX_SENDBYTES N at the beginning of the code, where N can have a maximum value of 1200. As an example, if N = 600, the statement should be:
#define BLYNK_MAX_SENDBYTES 600
  1. Type in the preceding code by opening the main.cpp file. Then, save and exit from the nano text editor by pressing Ctrl + O, followed by Enter, followed by Ctrl + X.
  2. After that, build the project by issuing the following command:
pi@raspberrypi:~/blynk-library/linux $ ./build.sh raspberry
  1. Now, you can run the project with the auth token associated with the project:
pi@raspberrypi:~/blynk-library/linux $sudo ./blynk --token=ca7bed1c92214503a65de8e20164994f
  1. To test the application, press and release the push button attached to the BCM_GPIO pin 23 of the Raspberry Pi. Immediately, you will get an email; if you can't find the email in your inbox, then you can check in the spam or junk folder of your email client application:
Email found in junk folder
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.53.209