Sunday, May 28, 2017

Raspeberry Pi - Connect to Wifi and send ip information to email



There is problem with the current setting of my Raspeberry Pi. Every time when it is started, I need to log into my account and connect to wifi. In order to use ssh on my desktop, I also need the ip information of the Raspberry Pi.

To automate this part, we need to write some autostart program. Essentially, we want to write scripts that can be executed upon the start.

In ArchLinux, it can be doen through systemctl. The documentation can be found here:
https://wiki.archlinux.org/index.php/systemd

It takes a while to read through all the sections. Some Useful examples can be found here:
https://www.freedesktop.org/software/systemd/man/systemd.service.html

Here is what we need to do:

Step 1: Create the script that need to be executed upon the start.

In our case, we want to have something like

#!/bin/sh
ip link set wlan0 down
netctl start home_wifi
ifconfig | mail -v -s "IP information" user@gmail.com


After editing the script, make it executable: chmod + x your-script

Step 2: Create a new service

Go to the /etc/systemd/system folder and create a new file
my-autostart.service

[Unit]
Description=Send ip information through email

[Service]
ExecStart=/path/your-script

[Install]
WantedBy=multi-user.target


Save the edit and enable the service by
systemctl enable my-autostart.service

Caveate
Though it can work, the configuration of the service may not be completely correct. It is better to have more fine control on it.


No comments:

Post a Comment