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:
- 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;
}
- 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");
- 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
- 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.
- After that, build the project by issuing the following command:
pi@raspberrypi:~/blynk-library/linux $ ./build.sh raspberry
- Now, you can run the project with the auth token associated with the project:
pi@raspberrypi:~/blynk-library/linux $sudo ./blynk --token=ca7bed1c92214503a65de8e20164994f
- 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