Sending tweets

The Blynk library provides the Blynk.tweet() function to send tweets of up to 128 characters:

Blynk.tweet("Tweeting from Raspberry Pi");

However, you can increase the message length by including the following statement at the top of the code:

#define BLYNK_MAX_SENDBYTES 256

The example code shown in Listing 5.1 shows how to send a tweet from the Raspberry Pi every 30 seconds:

Listing 5.1: Send a tweet from Raspberry Pi in every 30 seconds

Refer to the following steps:

  1. Open the main.cpp file and type in the preceding code.
  2. The SimpleTimer library is used to handle timed actions. You can instantiate the SimpleTimer library to create an object of the SimpleTimer:
SimpleTimer timer;
  1. The SimpleTimer library is based on millis(); therefore it has 1 ms resolution.
  2. The setInterval() function of the SimpleTimer library allows you to call a function with specified intervals in milliseconds. The preceding code calls the tweetMe() function every 30 seconds. It accepts the time in milliseconds; therefore, 30 seconds is equivalent to 30*1000 milliseconds. You can change the intervals between tweets by adjusting the time, but don't use less than 15 seconds:
void tweetMe() {
...
}
timer.setInterval((30*1000), tweetMe);
  1. The uptime variable holds the time in milliseconds since the program started on the Raspberry Pi:
intuptime = millis()/1000;
  1. A unique tweet can be built by concatenating a constant string with the uptime in seconds as follows. The sprintf() function is used to convert the uptime (an integer) into a string. The strcat() function is then used to concatenate the two strings to build a single string. Make sure to build the string under 128 bytes unless you defined #define BLYNK_MAX_SENDBYTES 256 at the top of the code to use 255 bytes:
char msg[] = "My Raspberry Pi is tweeting using @blynk_app and it's awesome!nTweeting time since startup: ";
char tm[100];
sprintf(tm,"%d",uptime);
strcat(msg,tm);
strcat(msg," seconds.");
Blynk.tweet(msg);
  1. Save the file and exit from the nano text editor by pressing Ctrl + O, followed by Enter, followed by CtrlX.
  2. Build the C++ project with the following command:
pi@raspberrypi:~/blynk-library/linux $ ./build.sh raspberry
  1. Now, run your C++ project with the associated auth token from your computer:
pi@raspberrypi:~/blynk-library/linux $sudo ./blynk --token=ca7bed1c92214503a65de8e20164994f
  1. Log in to your Twitter account and go to the MyTweets page. Wait for 30 seconds, and you will see the first tweet on the top of the page. The next tweet will appear 60 seconds after starting your program, and so on:
Tweeting from Raspberry Pi every 30 seconds
..................Content has been hidden....................

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