Last active
August 29, 2015 14:05
-
-
Save famanson/c99950ce785877853ff0 to your computer and use it in GitHub Desktop.
Arduino GMail Notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
int led = 13, override = HIGH; | |
int OVERRIDE_PIN = 2; | |
const int SERVO_COUNT = 2; | |
Servo servoArray[SERVO_COUNT]; | |
int servoPins[SERVO_COUNT] = {9,10}; | |
int servoStates[SERVO_COUNT] = {270,10}; | |
void blink(int duration, int repeat) { | |
for (int i = 0; i < repeat; i++) { | |
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) | |
delay(duration); | |
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW | |
delay(2*duration); | |
} | |
} | |
int convertInt(String s) { | |
char floatbuf[32]; // make this at least big enough for the whole string | |
s.toCharArray(floatbuf, sizeof(floatbuf)); | |
int i = atoi(floatbuf); | |
return i; | |
} | |
void setup() { | |
blink(200, 3); //start | |
Serial.begin(9600); | |
// while the serial stream is not open, do nothing: | |
while (!Serial); | |
// Ready the servos | |
for (int i=0;i<SERVO_COUNT;i++) { | |
servoArray[i].attach(servoPins[i]); | |
delay(200); | |
servoArray[i].write(servoStates[i]); | |
} | |
delay(200); | |
// Set up the override pin | |
delay(1000); | |
digitalWrite(OVERRIDE_PIN, HIGH); | |
blink(200, 2); //ready | |
} | |
String servoIdStr, stateStr; | |
void loop() { | |
if(Serial.available() > 0) { | |
for (int i=0;i<SERVO_COUNT;i++) { | |
servoIdStr = Serial.readStringUntil(':'); | |
if (i == SERVO_COUNT-1) { | |
stateStr = Serial.readStringUntil('\n'); | |
} else { | |
stateStr = Serial.readStringUntil(','); | |
} | |
servoIdStr.trim(); | |
stateStr.trim(); | |
int servoId = servoIdStr.toInt(), state = stateStr.toInt(); | |
if (servoStates[servoId] != state) { | |
servoArray[servoId].write(state); | |
servoStates[servoId] = state; | |
} | |
} | |
} else { | |
blink(200, 1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from optparse import OptionParser | |
import serial | |
import feedparser | |
import time | |
mailboxes = { | |
"junk": { | |
"username":"", | |
"password":"", | |
"servo": "1", | |
"init": "10", # servo acting weirdly at 0 and 180 | |
"raised": "90" | |
}, | |
"work": { | |
"username":"", | |
"password":"", | |
"servo": "0", | |
"init": "270", | |
"raised": "90" | |
} | |
} | |
server="mail.google.com" | |
path="/gmail/feed/atom/important" | |
HALFWAY = 90 | |
INIT = 270 | |
def write(message): | |
ser = serial.Serial("/dev/ttyACM0", 9600, timeout=1) | |
try: | |
ser.write(message) | |
finally: | |
ser.close() | |
def main(): | |
message_parts = [] | |
for key, value in mailboxes.iteritems(): | |
email = feedparser.parse("https://" + value["username"] + ":" + value["password"] + "@" + server + path) | |
entries = email.entries | |
if entries: | |
print "got mail", value["username"] | |
message_parts.append(value["servo"] + ":" + value["raised"]) | |
else: | |
message_parts.append(value["servo"] + ":" + value["init"]) | |
write(",".join(message_parts)) | |
if __name__ == '__main__': | |
print 'Mail Notifier v1' | |
parser = OptionParser() | |
parser.add_option("-m", "--message", dest="message") | |
(options, args) = parser.parse_args() | |
if not options.message: | |
while True: | |
main() | |
time.sleep(15) | |
else: | |
write(options.message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment