Get RaspberryPi IP
Check the IP of Raspberry Pi is very painful because the IP of the Raspberry Pi will change automatically after a period of time if you didn’t assign a static IP to it. Checking the IP of the Raspberry Pi requires the keyboard, monitor, and it requires time to check it.
One way to solve this problem is let the Raspberry Pi to automatically send its IP to somewhere, like Google Sheet. This can make life better. Following are the steps to achieve it.
Connect the Raspberry Pi with WiFi
You need to use keyboard and monitor to set up the Raspberry Pi’s WiFi firstly, also check the IP, because we need to use the scp
command to send file to Raspberry Pi.
Use gspread to send IP to google sheet
Set up googld cloud project
- Head to Google Developers Console and create a new project.
- Press the Google API icon of this new project and press “Enable APIs and services”
- Enable Google Drive and Google Sheets API
- Press the credentials icon and press “create credentials” and select “service account key”. Press create to download a json file for credentials
Set up python file
- Download the
pi_logger.py
from this link, edit it - Use
scp
command to send this file together the json file to the Raspberry Pi - Install the requested libraries which are included in the
pi_logger.py
file.- Note: You need to use
sudo pip3
to install the libraries, otherwise it cannot be ran by bash later
- Note: You need to use
- Ran
python3 pi_logger.py
to test it. Go to your Google Sheet you should find a file created and shared by Raspberry Pi
Write a bash file to launch the python file
- Create a bash file named
launcher.sh
- In the file, type
sudo python3 /home/pi/%your path%/pi_logger.py
, save it - Type
sh /home/pi/%your path%/launcher.sh
to test the code, it should have the same result.- Note: if the terminal said there is error when importing libraries, you just need to uninstall relevant libraries and use
sudo pip3
to install it back
- Note: if the terminal said there is error when importing libraries, you just need to uninstall relevant libraries and use
Automatcally run the bash file
- In Pi’s terminal, type
sudo crontab -e
- Type
*/2 * * * * /home/pi/justdiedbot/launcher.sh >/home/pi/logs/cronlog.log 2>&1
, which means the Raspberry Pi will send do the sending action every 2 minutes
Reference
[1] https://www.instructables.com/id/Raspberry-Pi-Launch-Python-script-on-startup/
[2] https://raspberrypi.stackexchange.com/questions/78141/python-script-fails-with-importerror-when-run-from-rc-local