Tuesday, 24 June 2014

How To Send Email With Python (smtplib and MIMEText)

It is actually pretty easy to  send emails with python. The smtplib module provides the necessary "tools" to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

In this script I am using gmail, but it is not limited to Google as long as your provide the correct smtp server + port.

Python 2.7.3 

  1. from email.mime.text import MIMEText  
  2. import smtplib  
  4. def sendmail(to,subject,text):  
  5.     user = 'name@gmail.com'  
  6.     pwd = 'password'  
  7.     msg = MIMEText(text)  
  8.     msg['From'] = 'james@gmail.com'  
  9.     msg['To'] = to  
  10.     msg['Subject'] = subject  
  11.     try:  
  12.         smtpServer = smtplib.SMTP('smtp.gmail.com', 587)  
  13.         smtpServer.ehlo()  
  14.         smtpServer.starttls()  
  15.         smtpServer.ehlo()  
  16.         smtpServer.login(userpwd)  
  17.         smtpServer.sendmail(user, to, msg.as_string())  
  18.         smtpServer.close()  
  19.         print "mail sent."  
  20.     except SMTPException:  
  21.         print "failed."  

Let's take it for a test drive... 

How To Send Email With Python (smtplib and MIMEText)

 Seems to work.

How To Send Email With Python

Monday, 16 June 2014

Automatically Check The Raspberry Pi's Temperature (+Prevent Overheating)

No need to install anything. Just open a terminal and execute the following command to get the temperature of your RasPi.
/opt/vc/bin/vcgencmd measure_temp

Automate the process

The below script will automatically reboot your Raspberry Pi should it reach a specified threshold (maxtemp). First of all make sure you can reboot/shutdown your raspi without linux asking you for a password. 

user@user:~$ sudo visudo
Add the following to the sudoers file. 
user ALL=NOPASSWD: /sbin/shutdown

The script

Save as temp.sh. Maxtemp specifies the temperature cut-off point, at which the Raspi will reboot. 
#title           :temp.sh  
#description     :Checks Temperature of Raspberry Pi and reboots at maxtemp.  
#author          :Mike M | www.linuxx.eu  
#notes           :Add "user ALL=NOPASSWD: /sbin/shutdown" to sudoers file   
temp=`/opt/vc/bin/vcgencmd measure_temp | cut -c 6-7`  
if [ $temp -ge $maxtemp ]; then  
xmessage  "Alert! Temp above 75. System rebooting in 3 minutes"  
sudo /sbin/shutdown -r +3  
elif [ $temp -lt $maxtemp ]; then  
exit 0  

Create a cronjob

user@user:~$ sudo crontab

Add the following

*/5 * * * * sh /home/user/temp.sh
This will run the temp.sh script every 5 minutes and make sure that the Raspberry Pi does not overheat.

Saturday, 14 June 2014

Streaming Video From Your Raspberry Pi Camera Module with VLC

If you want to build a surveillance cam you might want to have a look at motion, but for simple video streaming I 'd go for vlc.

Enable Camera Module

sudo raspi-config

Install VLC Media Player

sudo apt-get install vlc

Stream (RTSP)

 raspivid -o - -t 0 -n -w 800 -h 600 -fps 20 | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}' :demux=h264

- w: width
- h: heigt
- fps: frames per second

Viewing The RTSP Stream 

You need a video player capable of RTSP (e.g. VLC). Open a network stream:


###.###.###.### is the IP address of your raspberry pi. Also, do not forget the '/'  at the end, otherwise it won't work.

Streaming Over HTTP

 raspivid -o - -t 0 -n -w 800 -h 600 -fps 20 | cvlc -vvv stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8554}' :demux=h264

Streaming Video From Your Rasperry Pi Camera Module with VLC
Camera Module

Sunday, 8 June 2014

How to Setup Wi-Fi On Your Raspberry Pi via the Command Line

Configure Your AP Details

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Make sure the file reads like this

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

    pairwise=CCMP TKIP
    group=CCMP TKIP

Configuring the interface

 sudo nano /etc/network/interfaces

Add the following

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

sudo ifconfig wlan0 down
sudo ifconfig wlan0 up

Reboot. Done.

Configure WPA2 On Raspberry Pi
Model B