wferdwtr
Posts: 1
Joined: Tue Feb 12, 2013 3:07 pm

Glowing Red Alert Button, The Octoalert

Fri Mar 29, 2013 8:46 pm

My 3 year old son is a huge fan of the TV show the Octonauts (http://www.theoctonauts.com/) . It is about a group of animals that live underwater and help solve problems in the ocean. When they have a problem they hit the "Octoalert" a big red button on the wall. My son loves this and even pretends to hit an imaginary button on the wall when he has a problem.

I used the RPi to build him his own Octoalert. I can send emails to the RPi that contain "missions" when the RPi sees a new message it sets off alert and flash a big red light. There is also a large red button that my son can hit and it will play back the last "mission" I have sent him.

The code is not pretty and the wiring is messy but it works and he loves it. Below is a list of parts and the code that runs this. I am not a coder or a hardware guy so if you see anything that I should change let me know. One thing I know I want to change is the voice that reads the message. I use espeak which sounds very robotic, I tried festival which sounds better but loads too slow.

Here are the items I purchased to do build it in addition to the RPi:

Adafruit
  • Half-size breadboard[ID:64] = $5.00
  • Adafruit Pi Cobbler Breakout Kit for Raspberry Pi[ID:914] = $7.95
  • Powerswitch tail 2[ID:268] = $25.95
  • Breadboarding wire bundle[ID:153] = $6.00
  • Diffused White 10mm LED (25 pack)[ID:846] = $11.95
Sparkfun
  • Big Dome Push Button - Red = $9.95
Amazon
  • Red Police Beacon Light = $13.50
  • Edimax EW-7811Un 150 Mbps Wireless 11n Nano Size USB Adapter = $9.99
This is the main code that gets the email and activates the light:

Code: Select all


import imaplib, email, RPi.GPIO as GPIO
import time
from espeak import espeak
import pygame


#Load Sound Effect
pygame.init()
sound = pygame.mixer.Sound("/home/pi/testcode/octoalert/OctoAlertSound.wav")

#Set up Espeak
espeak.set_parameter(espeak.Parameter.Rate, 100)

#get email parameters
imap_host = 'imap.gmail.com'
imap_user = '[email protected]'
imap_pass = 'your_password'

#Set up Lights
GPIO.setwarnings(False) 
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)


while True :
	## open a connection 
	imap = imaplib.IMAP4_SSL(imap_host)

	## login
	imap.login(imap_user, imap_pass)

	## get status for the mailbox (folder) INBOX
	folderStatus, UnseenInfo = imap.status('INBOX', "(UNSEEN)")

	#Set counter of unread email
        NotReadCounter = int(UnseenInfo[0].split()[2].strip(').,]'))
	


	if NotReadCounter > 0:
		GPIO.output(17, True)
		GPIO.output(18, True)
		## select a specific folder
		data = imap.select('INBOX')
 
		## fetching the full message
		resp, items = imap.search(None, 'All')
		email_ids  = items[0].split()
		latest_email_id = email_ids[-1]

                resp, AllTheMessage= imap.fetch(latest_email_id, '(RFC822)')
		mail = email.message_from_string(AllTheMessage[0][1])
                
                ##Parse the mail 
		for part in mail.walk():
  			# multipart are just containers, so we skip them
  			if part.get_content_maintype() == 'multipart':
      				continue
 
  			# we are interested only in the simple text messages
  			if part.get_content_subtype() != 'plain':
    				continue
 
                        ##Save the email text to a file so the button script can read it 			
                        payload = part.get_payload()
  			filename = "file.txt"
			f = open(filename, "w")
			f.write(payload)
			f.close()
                        #Play the sound, flash the lights and read the message
			sound.play()
			i=1
			while i < 60:
        			if i%2==0:
            				GPIO.output(17, True)
        			elif i==31:
					espeak.synth(payload)
            				GPIO.output(17, False)
        			else:
            				GPIO.output(17, False)
        			i=i+1
        			time.sleep(.5)
		## moving/copying messages around folders
		status, messages  = imap.copy(latest_email_id, 'reported.messages')
		time.sleep(30)
		GPIO.output(18, False)
		GPIO.output(17, False)		
	imap.logout()
	loop=0
	#print "end While Loop " 
	#print loop
	loop = loop+1
	time.sleep(30)
This is the code that runs when the button is pressed:

Code: Select all

import RPi.GPIO as GPIO
from espeak import espeak
import pygame
import time
import subprocess

#Load Sound Effect
pygame.init()
sound = pygame.mixer.Sound("/home/pi/testcode/octoalert/OctoAlertSound.wav")

#set buttons
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#23 is the switch
GPIO.setup(23,GPIO.IN)
#17 is the sire light
GPIO.setup(17, GPIO.OUT)
#18 is the LED in the button
GPIO.setup(18, GPIO.OUT)



#initialise a previous input variable to 1 
prev_input = 1
while True:
  #take a reading
  input = GPIO.input(23)
  #if the last reading was low and this one high, run loop
  if ((not prev_input) and input):
    #print("Button pressed")
    #light the lights
    GPIO.output(17, True)
    GPIO.output(18, True) 
    #play the sounds   
    sound.play()
    #read the message
    filename = 'file.txt'
    f = open(filename, "r")
    payload = f.read()
    f.close()
    #flash the lights for another 30 seconds
    i = 1
    while i < 60:
        if i%2==0:
            GPIO.output(17, True)
        elif i==31
            #Give the sound 15 seconds to play then read the message
	    espeak.synth(payload)
	    GPIO.output(17, False)
	else:
            GPIO.output(17, False)
	i=i+1
	time.sleep(.5)
    GPIO.output(17, False)
    GPIO.output(18, False)
  #update previous input
  prev_input = input
  #slight pause to debounce
  time.sleep(0.05)


User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Glowing Red Alert Button, The Octoalert

Sun Mar 31, 2013 4:09 pm

Great Dad! In no time he will be programming his own missions!

Enjoy!
Bill

User avatar
StevenP
Posts: 56
Joined: Tue Aug 14, 2012 7:55 pm
Location: Germany / UK

Re: Glowing Red Alert Button, The Octoalert

Sun Mar 31, 2013 4:48 pm

Really super project. Well done. I think it should be on Liz's raspberry pi news front page, honestly.
"The time has come," the Walrus said,
"To talk of many things..."

dan3008
Posts: 1172
Joined: Wed Aug 15, 2012 1:05 pm

Re: Glowing Red Alert Button, The Octoalert

Mon Apr 01, 2013 3:51 pm

Can we have a demo video of it working? That is amazing.

Internet Dad of the year award goes to you :D
dan3008 wrote:Pays your money, takes your choice

tgruben
Posts: 1
Joined: Sat Sep 06, 2014 11:59 am

Re: Glowing Red Alert Button, The Octoalert

Sat Sep 06, 2014 12:03 pm

Any chance of a schematic of your messy wiring layout? Sounds awesome

Jcpgce2014
Posts: 1
Joined: Sun Jan 10, 2016 7:56 pm

Re: Glowing Red Alert Button, The Octoalert

Sun Jan 10, 2016 7:58 pm

Love this. This is exactly what I wanted to do for my three year old.

Amazing!

Return to “Other projects”