***To have a full description of the project follow this link ------> PROJECT***
UPDATEI've modified the original project described in the link above in order to realize the Gmail lamp notifier.
The following are the programs that I've used. One is the Arduino sketch used to control the led strip and the other is the Python code used to control both the audio effects and the connection with the Gmail account.
Arduino Code:#include <SD.h>
#include <SPI.h>
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 60
#define DATA_PIN 7
#define iter 10
#define pirpin 49
char val;
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() {
// this line sets the LED strip type - refer fastLED documeantion for more details https://github.com/FastLED/FastLED
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
Serial.begin(9600);
delay(2000);
}
void loop() {
val = Serial.read();
if(val == '2'){
delay(500);
for(int i=0;i<iter;i++){
detect_thunder();
delay(random(10,30));
}
delay(600);
detect_thunder();
delay(100);
detect_thunder();
delay(100);
detect_thunder();
delay(100);
detect_thunder();
delay(700);
detect_thunder();
delay(50);
}
}
void detect_thunder() {
//3 types of lightning. Each cycle random one.
switch(random(1,3)){
case 1:
thunderburst();
delay(random(10,500));
//Serial.println("Thunderburst");
break;
case 2:
rolling();
//Serial.println("Rolling");
break;
case 3:
crack();
delay(random(50,250));
//Serial.println("Crack");
break;
}
}
//Turn all the lights off.
void reset(){
for (int i=0;i<NUM_LEDS;i++){
leds[i] = CHSV( 0, 0, 0);
}
FastLED.show();
}
void rolling(){
// every LED with 1/10 chance
// of being turned on, up to 10 times, with a random delay wbetween each time
for(int r=0;r<random(2,10);r++){
//iterate through every LED
for(int i=0;i<NUM_LEDS;i++){
if(random(0,100)>90){
leds[i] = CHSV( 0, 0, 255);
}
else{
//dont need reset as we're blacking out other LEDs here
leds[i] = CHSV(0,0,0);
}
}
FastLED.show();
delay(random(5,100));
reset();
}
}
void crack(){
//turn everything white
for(int i=0;i<NUM_LEDS;i++) {
leds[i] = CHSV( 0, 0, 255);
}
FastLED.show();
delay(random(10,100));
reset();
}
void thunderburst(){
// this thunder works by lighting two random lengths
// of the strand from 10-20 pixels.
int rs1 = random(0,NUM_LEDS/2);
int rl1 = random(10,20);
int rs2 = random(rs1+rl1,NUM_LEDS);
int rl2 = random(10,20);
//repeat this chosen strands a few times
for(int r = 0;r<random(3,6);r++){
for(int i=0;i< rl1; i++){
leds[i+rs1] = CHSV( 0, 0, 255);
}
if(rs2+rl2 < NUM_LEDS){
for(int i=0;i< rl2; i++){
leds[i+rs2] = CHSV( 0, 0, 255);
}
}
FastLED.show();
//stay illuminated for a set time
delay(random(10,50));
reset();
delay(random(10,50));
}
}
Python code (Python version 2.7):Change the USERNAME and PASSWORD fields to match your account.
You can adjust the minimum number of unread messages needed to trigger the cloud and the check frequency of the mail box by changing the NEWMAIL_OFFSET and MAIL_CHECK_FREQ variables respectively.
#!/usr/bin/env python
import pyaudio
import wave
import sys
import serial
from imapclient import IMAPClient
import time
chunk = 1024
DEBUG = True
HOSTNAME = 'imap.gmail.com'
USERNAME = 'USERNAME'
PASSWORD = 'XXXXXXXX'
MAILBOX = 'Inbox'
NEWMAIL_OFFSET = 1 # my unread messages never goes to zero, yours might
MAIL_CHECK_FREQ = 30 # check mail every 60 seconds
#validation
if len(sys.argv) < 2:
print "Open Serial Port.\n\n" +\
"Usage: %s serial_port" % sys.argv[0]
sys.exit(-1)
#opens serial port
ser = serial.Serial(sys.argv[1], 9600)
time.sleep(2)
def loop():
server = IMAPClient(HOSTNAME, use_uid=True, ssl=True)
server.login(USERNAME, PASSWORD)
if DEBUG:
print('Logging in as ' + USERNAME)
select_info = server.select_folder(MAILBOX)
print('%d messages in INBOX' % select_info['EXISTS'])
folder_status = server.folder_status(MAILBOX, 'UNSEEN')
newmails = int(folder_status['UNSEEN'])
if DEBUG:
print "You have", newmails, "new emails!"
if newmails > NEWMAIL_OFFSET:
print "funziona"
ser.write('2')
ser.flush()
wf = wave.open('test.wav', 'rb')
p = pyaudio.PyAudio()
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
data = wf.readframes(chunk)
while data != '':
# writing to the stream is what *actually* plays the sound.
stream.write(data)
data = wf.readframes(chunk)
# cleanup stuff.
stream.close()
p.terminate()
else:
print "non va"
time.sleep(MAIL_CHECK_FREQ)
if __name__ == '__main__':
try:
print 'Press Ctrl-C to quit.'
while True:
loop()
finally:
print 'exit'
Comments