Setting up the Raspberry Pi
After the basic setup, i.e.
- bought a Raspberry Pi Starter Kit
- flashed the SD Card with Raspbian
- ran
raspi-config
- installed R with
apt-get install R
, which installed R 3.1.1
I started to install the R packages usually needed for my cron-job tasks (mostly webscraping). I ran into problems with the rvest
package because several packages could not be installed. Maybe there is a more efficient way but I did the following steps:
Install packages for webscraping
To install xml and related R packages (rvest), I needed the libxml2 on the system although apt-get had it, so I manually installed it:
|
|
I also needed python-dev to make libxml2 compile.
|
|
Then built libxml2:
|
|
I also had problems with the curl Package. Installation suggested to install libcurl4-openssl-dev therefore:
|
|
Last problem was the openssl package. Again, I followed the suggestions from the failed R-package installation and installed libssl-dev:
|
|
After that, rvest
installed nicely. However, it took quite a while for the Pi to install all dependencies.
Webscraping Example – A simple frost warning for my plants
A simple Task, my Raspberry Pi is doing for me is sending a frost warning to my email if at 6 pm the weather forecast for the night goes below 3 °C. For this I got an API Key at openweathermap.org. Mind, that openweathermap.org does not like frequent requests (less than 1 per 10 minutes). At the beginning I got blocked.
You can then request some JSON for your city ID using your APPID (API Key):
|
|
Then tidy and extract the values needed. Temperatures are in degrees kelvin so we need to convert to celsius. The date I transform to POSIX.
|
|
Sending results via email
Now for the part sending a mail:
|
|
Finally we have to tell the Raspberry Pi to schedule the script to run daily at early evening. Save the .R file and add it to your crontab:
|
|
The first time you use crontab you are asked to choose an editor. Easiest (at least for me) to use is nano.
Add the following line:
|
|
Which will add the script to your cronjobs scheduling it at 18:00 every day and month.