Bag - 0

Labs

 

Parts List

What Y0u Will Need

step01_parts


Step 1

Setting Up The Raspberry Pi

step02_prep

After you have all the parts above, you can begin setting up your Raspberry Pi. The first thing you’ll need to do is download an operating system for it. I used the Raspbian operating system available from this download link.
Once it’s downloaded, you will need to burn it to an SD card, and you can follow the steps listed at this website to help you do that for your specific operating system (PC, Mac, Linux). When you’ve succesfully burned the image to the SD card, insert it into your Raspberry Pi. Then to boot the Pi up, you’ll need a Monitor, keyboard, mouse, ethernet connection, and a 5v micro-usb power supply. With all of those things attached, the Pi should boot up and you should see the Raspbian desktop when it’s done.
Before moving on to step two, we should set a static IP address for our Raspberry Pi. An IP address is a set of numbers that a router assigns to each device that connects to the internet. Right now, our Pi’s IP address is randomly assigned, which means it will probably change next time we reboot the Pi. So giving it a static IP address will tell the router to reserve a set of numbers specifically for our Raspberry Pi so that it never changes.
To do this, hover over the network icon in the upper right-hand corner of the desktop. It will tell you your current IP address, which will probably look like 192.168.0.4 or 192.168.1.4. Write down this number, and then right-click on the icon and select “wifi settings”. From the drop down menu, select “wlan0”. Then type in the base numbers from the IP address you wrote down (ex. 192.168.0.). For the last set of numbers, choose a number that is unique and isn’t shared with any other device on your network. Generally anything over 100 is reserved for static IP addresses. I am going with “192.168.0.111”, but you can choose whatever you like. Click “OK” and then reboot your computer.
Find Your IP Go To WiFi settings Set A Static IP
step03_IP step04_menu step05_settings


Step 2

Setting Up Python

The great thing about the Pi is that it already has a few programming environments pre-installed. The simple one I like to use is Python. Normally I’d advise using Python 3, but the webcam software we’ll be using isn’t  yet compatible with it, we’re forced to use Python 2.7. From the menu, you could go to “Programming” and choose “Python 2” and it will open up the Python interface. However, it’s just as easy to write our Python code from the command line. So these steps will show you how to set up Python from the command line.

  • Update the repositories
    sudo apt-get update
  • Upgrade your software
    sudo apt-get upgrade
  • Launch python
    python
  • When you see the >>>> prompt, you can start writing your python code. Here’s an example
    print('Hello Tinkernerdz!')
  • The result should be a response saying “Hello Tinkernerdz!”. To exit out of python, type this:
    exit()

It’s standard practice in Python to create your programs in a virtual container to organize the code and its dependences. This is called a virtual environment. So I’m going to pretend like I know what I’m doing and walk you through how to set up and use it.

  • Install the python virtual environment using “pip”
    sudo pip install virtualenv
  • Once it’s installed, you can create a virtual environment called “test”
    virtualenv test
  • To activate and use the “test” virtual environment, you will need to use this command
    source test/bin/activate
  • Now you can use python to write test code and run it and then exit out of python
    python
    1 + 8
    exit()
  • To deactivate the virtual environment type this:
    deactivate

To make the virtual environment workflow easier, you can also install “virtualenvwrapper” which uses easier commands to start and stop virtual environments.

  • Install the python virtual environment wrapper using “pip”
    sudo pip install virtualenvwrapper
  • Set the wrapper work home
    export WORK_HOME=~/Envs
  • Now run it
    source /usr/local/bin/virtualenvwrapper.sh
  • To create AND activate a new virtual environment, all you have to do is type
    mkvirtualenv test
  • The deactivation command is the same
    deactivate
  • If you want to jump back into a virtual environment you were working on, just type
    workon test
  • And another useful command is removing virtual environments
    rmvirtualenv test


Step 3

Email Script For Self-Hosted Email

At this point, we have everything setup to start writing our e-mail script. Let’s download a test image to use for our script
wget http://findicons.com/files/icons/1014/ivista/256/png_file.png
Then let’s create a virtual environment called “mailer”.
mkvirtualenv mailer
Within it, let’s use a text editor to make a python script called py_mail.py
nano py_mail.py
The script below is for use if you are hosting your own e-mail server, such as the Citadel e-mail server that I showed how to make in my last tutorial. If you are wanting a script to use for smtp mail clients such as gmail, you can find that under the next step.
EMAIL SCRIPT FOR SELF-HOSTED EMAIL SERVER
#Set a variable to the sendmail path location on the Pi
SENDMAIL = "/usr/sbin/sendmail"
#Import the MIME Multipart library
from email.mime.multipart import MIMEMultipart
#Import the MIME text library
from email.mime.text import MIMEText
#Import the MIME text library
from email.mime.image import MIMEImage
#Set a variable for the "To" email addresses. You can separate them by commas
TO = ["recipeient_email@testmail.com"]
#Set a variable for the "From" email address
FROM = ["your_from_email@testmail.com"]
#Assemble the message using the MIME protocol
msg = MIMEMultipart('mixed')
msg['Subject'] = 'New Python Test Mail'
msg['From'] = FROM
msg['To'] = ', '.join(TO) #This joins the comma separated emails
body = MIMEText('Hello world', 'plain') #Create a plain text body
msg.attach(body) # attach the body to the message
#Open the image source
img_data = open('png_file.png', 'rb')
#Read the image into the MIME protocol
image = MIMEImage(img_data.read())
#Close the image source
img_data.close()
#Attach the image to the message
msg.attach(image)
#Import the OS library
import os
#Use popen to open the sendmail program
p = os.popen("%s -t -i" % SENDMAIL, "w")
#Write the message as a string to the sendmail program
p.write(msg.as_string())
#Close the sendmail program
p.close()


Step 4

Email Script For Gmail

The last step is to configure citadel using your new domain name. So go back to the “Administration” option and select “Domain Names and Internet Mail Settings”. Under “Local Host Aliases”, type in your new domain name and click “Add”. Then click back on “Administration” and select “Edit Sitewide Administration”. Remove the node name, and add your domain name to the fully qualified domain name textbox. Then save the changes, go back to administration one last time, and restart your server.

EMAIL SCRIPT FOR GMAIL

#Import the SMTP library
import smtplib
#Import the MIME Multipart library
from email.mime.multipart import MIMEMultipart
#Import the MIME text library
from email.mime.text import MIMEText
#Import the MIME text library
from email.mime.image import MIMEImage
#Create a variable equal to your gmail address
gmail_acct = "your_gmail_email_address"

#Create a variable equal to your gmail password
gmail_pwd = "your_gmail_password"
#Set a variable for the "To" email addresses. You can separate them by commas
TO = ["recipeient_email@testmail.com"]
#Set a variable for the "From" email address
FROM = ["your_from_email@testmail.com"]
#Assemble the message using the MIME protocol
msg = MIMEMultipart('mixed')
msg['Subject'] = 'New Python Test Mail'
msg['From'] = FROM
msg['To'] = ', '.join(TO) #This joins the comma separated emails
body = MIMEText('Hello world', 'plain') #Create a plain text body
msg.attach(body) # attach the body to the message
#Open the image source
img_data = open('png_file.png', 'rb')
#Read the image into the MIME protocol
image = MIMEImage(img_data.read())
#Close the image source
img_data.close()
#Attach the image to the message
msg.attach(image)
#Open up the SMTP server
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo() #send an ehlo command
server.starttls() #initialize a tls connection
server.ehlo #send another ehlo command (note the lack of parenthesis)
server.login(gmail_acct, gmail_pwd) #pass the server your email and password
server.sendmail(FROM, TO, msg.as_string()) #pass your message to it as a string
server.close() #close the server connection


Step 5

Sending Text Messages

The last thing I wanted to investigate is text messages. If you look at www.emailtextmessages.com, you’ll see that most all phone carriers have a way to send text messages from email. It generally requries the users phone number, @, and then the carrier name. So adding that to your mailing list should send you a notification through email and text.