Git Product home page Git Product logo

send-email-with-device-ip-address's Introduction

send-email-with-device-ip-address

Where is my Raspberry Pi? Let your Pi send you an email with its IP-address. A few simple lines of Python Code can make your developer life much easier.

A very common challenge for Pi Developer is the challenge, that the Pi does not have a display build in. You can buy very good, and cheap Touchdisplays for the Pi to get rid of that problem. But if you’re using your Pi as “hiddden” device or on board of a car like my Sunfounder Pi Car, you cannot attach easily a display or HDMI cable, without disambling the whole car.

Sunfounder Raspberry Pi Car

Even if you manage to attach a monitor to this car, it is really annoying and uncomfortable to run after your driving car carrying the monitor all the time.

Email from my car

Prerequisities

The following project is writen in Python 3. So you will need Python 3 on your machine. The easiest way to install it is, executing the following lines in a command shell.

sudo apt-get install python-dev
curl -O https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py

If you don't want to use your personal email account & password on the device, you should create a new mailbox for that, I suggest GMail, but it is up to you. The only requirement is, that it is accessible via smtp.

Installation

You only need the file main.py locally on your Raspberry Pi device. I copied it in to the folder /home/pi/apps/send-email-with-device-ip-address/

You can add your credentials directly to access the mail server there, or create a file called 'secrets.py' and add your personal settings to connect to a mail server. I used that way and gitignored this file to keep my credentials away from your eyes.

sender_address = "[email protected]"
sender_password = "your password"
sender_server = 'smtp.gmail.com'
sender_port = 587
recipient_address = "[email protected]"

Test it!

Before you add this file to the boot sequence or cronjob, please test the file by executing the following line in the command shell:

python /home/pi/apps/send-email-with-device-ip-address/main.py

And it should display you something like that:

Output of Python App on Pi

Sending the email takes up to two minutes. I currently haven't exactly figured out, why it takes so long.

Running it on every start of device

Currently I only use this tool on Raspberry Pi's based on Raspbian. So this is the steps you need to install it as startup app.

To execute the script on every reboot, you need to add the python app to the rc.local file.

sudo nano /etc/rc.local 

Add the following line to it. The command sleep 30 is necessary to avoid an error on start, because it takes a while to get an valid IP-address on startup. Use The '&' for running in background.

(sleep 30; python /home/pi/apps/send-email-with-device-ip-address/main.py)&

Test it, but simply rebooting your system with

sudo reboot

You should receive an email within two minutes.

Running it every hour

In some scenarios it is useful to get an update of the current ip address every hour. Under some conditions ip addressses can change.

sudo nano /etc/crontab

Add the following line to get updates every hour:

0 * * * * root python /home/pi/apps/send-email-with-device-ip-address/main.py &


To test this feature, you need to wait until the next full hour.

Summary

This small tutorial should help you getting an ip address from an headless device. You can use the ip address to access the device like a Raspberry Pi with Putty or other ssh tools.

Feedback is always welcome, please feel free to comment.

send-email-with-device-ip-address's People

Contributors

oliverscheer avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.