Wednesday, 21 August 2013

Send IP info on boot up.

Send a SMS with your internal and External IP for your Raspberry PI

First Grab a Text Local account from 

CODE: SELECT ALL
<?php
//  Pi - SMS - What is my IP bro?!
//  By Robert Wiggins
//  txt3rob@gmail.com
// Paypal Donate and let me get a Raspi-Cam!

// get the local hosts ip address
$ip = str_replace("\n","",shell_exec("ifconfig eth0 | grep 'inet addr' | awk -F':' {'print $2'} | awk -F' ' {'print $1'}"));


// External IP
$externalContent = file_get_contents('http://www.icanhazip.com/');


// Configuration variables
$info = "1";
$test = "0";

// Data for text message
$uname = '';
$pword = '';
$from = "Robbies PI";
$selectednums = "44";
$message = "You Pi Info LAN:".$ip." Ext: ".$externalContent." Raspberry Pi LCD";
$message = urlencode($message);

// Prepare data for POST request
$data = "uname=".$uname."&pword=".$pword."&message=".$message
."&from=". $from."&selectednums=".$selectednums."&info=".$info."&test=".$test;

// Send the POST request with cURL
$ch = curl_init('https://www.txtlocal.com/sendsmspost.php'); //note https for SSL
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch); //This is the result from Textlocal
curl_close($ch);

//echo
echo ('You Pi Info LAN: '.$ip.' Ext: '.$externalContent.' Raspberry Pi LCD');
?>


Add A New Cron Job

To create a new job to Cron we will modify the “crontab”. This is a table that contains the list of jobs that Cron will monitor and run according to it’s details. To edit it we use the command :
sudo crontab -e
Each user of the system (ie “pi”) can have its own Crontab but in this case we want to add it as an admin so we prefix our “crontab -e” command with “sudo”. You should see something that looks like this :
Crontab Example - Python at Boot
Using your cursor keys scroll to the bottom and add the following line :
@reboot php /var/www/piboot.php &
This tells Cron that every boot (or reboot or start-up) we want to run PHP with the script piboot.php. The “&” at the end of the line means the command is run in the background and it won’t stop the system booting up as before.
Your screen should look something like this :
Crontab Example - Python at Boot
To save these changes click “CTRL-X”, then “Y” and finally “Return”. You should now be back at the command prompt.
To start testing you can now reboot using :
sudo reboot
The Cron Job part was taken from - http://www.raspberrypi-spy.co.uk/2013/07/running-a-python-script-at-boot-using-cron/

8 comments:

  1. Nice idea!

    I wrote a daemon to blink the OK LED on the Pi with the IP address -- works well, though it's a bit slow! It also writes the IP into a file in ~pi, so if you have another machine handy with a card reader you could have a peek and then reboot hoping that the IP stays the same. The code is here: https://github.com/hamishcunningham/pi-tronics/tree/master/blinkip

    We've also got some stuff about doing SMS notifications in various ways here -- http://pi.gate.ac.uk/pages/notipi.html -- comments welcome!

    My favourite solution is with PageKite, but I haven't written it up yet...

    Hamish
    http://pi.gate.ac.uk/

    ReplyDelete
  2. Hi Hamish,

    All looks good there I'm going add a few bits myself i.e for twitter and for it to display the ip on my adafruits LCD.

    I found textlocal being great for support for sms

    ReplyDelete
  3. cool -- what are the adafruit screens like? they look neat :-)

    I've written up the pagekite stuff now and linked this post -- http://pi.gate.ac.uk/posts/2013/08/25/winking-pi/ and http://pi.gate.ac.uk/pages/blinkip.html

    have a good one
    h

    ReplyDelete
  4. anyone tried with berryio? it sends the ipadd upon boot up. that means free and no need to subscribe for sms.

    ReplyDelete
  5. the question is how do you know where the berryio webserver is running? i.e. which ip address it is on -- unless it is using a centralised proxying service like pagekite -- http://pi.gate.ac.uk/pages/blinkip.html#section-4. -- which is definitely a good solution :-)

    best, hamish
    http://pi.gate.ac.uk/

    ReplyDelete
  6. this is free for ssh tunneling but cannot install at the moment https://yaler.net/raspi
    for sending the ip add, from what I have experienced so far, it sends my local ip using msmtprc. so you just setup it using their script and you're ready to go...

    ReplyDelete
  7. Great post, you have pointed out some fantastic points , I likewise think this s a very wonderful website. http://myspyapp.com/sms/

    ReplyDelete
  8. i cherish perusing this article so beautiful!!great work! software controllo cellulare

    ReplyDelete

Amazon

Donate

Donate Towards More Raspberry PI's for Projects