Project tutorial
Meet Butler and Cue

Meet Butler and Cue

Provocation 1: Counter Culture

  • 687 views
  • 0 comments
  • 2 respects

Components and supplies

Ard yun
Arduino Yun
microcontroller for Butler
×1
Momentary Button
buttons for Butler
×2

About this project





Code

butler.txtPlain text
The Arduino code for Butler.
#include <Bridge.h>
#include <Process.h>
#include <Temboo.h>
#include "TembooAccount.h"
#include "TwilioAccount.h"

const int LED = 13;
const int FOOD = 12;
const int DRINK = 7;
boolean TEXTED = false;

int RECEIVED = 0;
const int FRIENDS = 4;
String friends[] = {"Drake", "Eric", "Jon", "Chris"};
String oldIDs[] = {"", "", "", ""};
String numbers[] = {"+1 XXX-XXX-XXXX", "+1 XXX-XXX-XXXX", "+1 XXX-XXX-XXXX", "+1 XXX-XXX-XXXX"};

void setup() {
  Bridge.begin();
  Serial.begin(9600);

  //Configure pins
  pinMode(FOOD, INPUT_PULLUP);
  pinMode(DRINK, INPUT_PULLUP);
  
  //Initialize message IDs
  for (int i = 0; i < FRIENDS; i++) {
    String initID = "";
    TembooChoreo incomingSMS = createIncomingSMSChoreo(numbers[i]);
    incomingSMS.run();

    while (incomingSMS.available()) {
      String field = incomingSMS.readStringUntil('\x1F');
      field.trim();
      String data = incomingSMS.readStringUntil('\x1E');
      data.trim();

      if (field == "sid") { initID = data; }
    }
    delay(1000); incomingSMS.close();
    oldIDs[i] = initID;
  }
}

TembooChoreo createOutgoingSMSChoreo(String firstName, String number, String type) {
  TembooChoreo OutgoingSMSChoreo;
  OutgoingSMSChoreo.begin();

  //Choreo Setup
  OutgoingSMSChoreo.setAccountName(TEMBOO_ACCOUNT);
  OutgoingSMSChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
  OutgoingSMSChoreo.setAppKey(TEMBOO_APP_KEY);
  OutgoingSMSChoreo.setChoreo("/Library/Twilio/SMSMessages/SendSMS");

  //API Inputs
  OutgoingSMSChoreo.addInput("AccountSID", TWILIO_ACCOUNT_SID);
  OutgoingSMSChoreo.addInput("AuthToken", TWILIO_AUTH_TOKEN);
  OutgoingSMSChoreo.addInput("From", TWILIO_NUMBER);
  OutgoingSMSChoreo.addInput("To", number);

  //Message Body
  String message = "Hey " + firstName + ", Sofia is making " + type + " at her apartment - want to join?";
  OutgoingSMSChoreo.addInput("Body", message);

  return OutgoingSMSChoreo;
}

TembooChoreo createIncomingSMSChoreo(String number) {
  TembooChoreo IncomingSMSChoreo;
  IncomingSMSChoreo.begin();

  //Choreo Setup
  IncomingSMSChoreo.setAccountName(TEMBOO_ACCOUNT);
  IncomingSMSChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
  IncomingSMSChoreo.setAppKey(TEMBOO_APP_KEY);
  IncomingSMSChoreo.setChoreo("/Library/Twilio/SMSMessages/GetLastMessageFromNumber");

  //API Inputs
  IncomingSMSChoreo.addInput("AccountSID", TWILIO_ACCOUNT_SID);
  IncomingSMSChoreo.addInput("AuthToken", TWILIO_AUTH_TOKEN);
  IncomingSMSChoreo.addInput("From", number);
  IncomingSMSChoreo.addInput("ResponseMode", "verbose");

  //Output Filters
  IncomingSMSChoreo.addOutputFilter("sid", "/sid", "Response");
  IncomingSMSChoreo.addOutputFilter("body", "/body", "Response");

  return IncomingSMSChoreo;
}

void speak(String text) {
  Process p;
  p.begin("/mnt/sda1/tts.py");
  p.addParameter(text);
  p.run();
}

void loop() {
  String newID, body;
  int foodValue = digitalRead(FOOD);
  int drinkValue = digitalRead(DRINK);

  //Button Pressed
  if (foodValue == LOW || drinkValue == LOW) {
    String type = "stuff";
    if (foodValue == LOW) { type = "food"; } else if (drinkValue == LOW) { type = "tea"; }
    digitalWrite(LED, HIGH);
    speak("Sending text to your friends");
    int success = 0;

    for (int i = 0; i < FRIENDS; i++) {
      TembooChoreo outgoingSMS = createOutgoingSMSChoreo(friends[i], numbers[i], type);
      success = success + outgoingSMS.run();
      delay(1000); outgoingSMS.close();
    }

    if (success == 0) {
      speak("Texts sent!");
    } else {
      speak("Texts failed.");
    }
    
    RECEIVED = 0;
    TEXTED = (success == 0) ? true : false;
  } else if (TEXTED) {
    digitalWrite(LED, LOW);

    //Checking for new texts
    for (int i = 0; i < FRIENDS; i++) {
      TembooChoreo incomingSMS = createIncomingSMSChoreo(numbers[i]);
      incomingSMS.run();

      while (incomingSMS.available()) {
        String field = incomingSMS.readStringUntil('\x1F');
        field.trim();
        String data = incomingSMS.readStringUntil('\x1E');
        data.trim();

        if (field == "sid") {
          newID = data;
        } else if (field == "body") {
          body = data;
        }
        Serial.println(body);
      }
      delay(1000); incomingSMS.close();

      if (oldIDs[i] != newID) {
        oldIDs[i] = newID;
        RECEIVED++;
        String message = friends[i] + " says, '" + body + "'.";
        speak(message);
      }
    }
    
    if (RECEIVED == FRIENDS) { TEXTED = false; RECEIVED = 0; }
    delay(7000);
  }
}
tts.pyPython
Python script on Linux side for Butler.
#! /usr/bin/python

import urllib
import pycurl
import os
import sys
from subprocess import call
import string

url = 'http://tts-api.com/tts.mp3?q=' + urllib.quote(sys.argv[1])
filename = 'audio.mp3'

print('==> downloading audio from: ' + url)
f = open(filename, 'wb')
c = pycurl.Curl()
c.setopt(c.FOLLOWLOCATION, 1L)
c.setopt(c.URL, url)
c.setopt(c.WRITEFUNCTION, f.write)
c.perform()

c.close()
f.close()

call(['/usr/bin/madplay', filename])
cue.txtPlain text
The Arduino code for Cue.
#include <Wtv020sd16p.h>
#include <SimpleTimer.h>
#include <Servo.h>

// motor variables
SimpleTimer timer;
Servo servo;
boolean locked = false;
int pos = 0;
int seconds = 3;
const int BUTTON = 10;
const int SERVO = 9;
const int LED = 13;

// audio variables
const int microphonePin = 0;
const int ledPin = 13;
int sample;
const int threshold = 500;
int resetPin = 2;  // The pin number of the reset pin.
int clockPin = 3;  // The pin number of the clock pin.
int dataPin = 4;  // The pin number of the data pin.
int busyPin = 5;  // The pin number of the busy pin.
Wtv020sd16p wtv020sd16p(resetPin,clockPin,dataPin,busyPin);
int count = 0;

void lock() {
  if (locked) {
    for (pos = 180; pos >= 1; pos--) {
      servo.write(pos); delay(15);
      locked = false;
    }
  } else {
    for (pos = 0; pos <= 180; pos++) {
      servo.write(pos); delay(15);
      locked = true;
    }
  }
}

void setup() {
  Serial.begin(9600);
  wtv020sd16p.reset();
  servo.attach(SERVO);
  pinMode(BUTTON, INPUT_PULLUP);
}

void loop() {
  int buttonValue = digitalRead(BUTTON);
  timer.run();
  
  // put your main code here, to run repeatedly:
  if (buttonValue == LOW) {
    digitalWrite(LED, LOW);
    lock();
    timer.setTimeout(seconds * 1000, lock); 
  }
  
  digitalWrite(LED, HIGH);
  
  sample = analogRead(microphonePin);
  Serial.println(sample);
  if (sample < threshold) { count++; } else { count = 0;}
  
  if (count >= 5) {
    wtv020sd16p.playVoice(3);
    delay(3000);
    count = 0;
  }
}

Comments

Team members

Img 3336 (2) tgy9fmancy
Sofia Dewar
  • 3 projects
  • 3 followers
999065 637926997850 1457589512 n
Oscar Segovia
  • 1 project
  • 5 followers
Me copy
Andrea Deng
  • 4 projects
  • 6 followers
Default
Drake Myers
  • 2 projects
  • 3 followers
Profile square
Jon Lai
  • 3 projects
  • 2 followers

Published on

February 24, 2015

Members who respect this project

Photo

and 1 other

See similar projects
you might like

Similar projects you might like

Secure YUN

Project tutorial by Abhishek

  • 1,652 views
  • 1 comment
  • 12 respects

Simple UNO calculator

Project tutorial by Joprp05

  • 6,350 views
  • 0 comments
  • 6 respects

Post Data to a Google Spreadsheet with Temboo

Project tutorial by Temboo_OFFICIAL

  • 1,366 views
  • 0 comments
  • 7 respects

Ultrasonic Map-Maker using an Arduino Yun

Project tutorial by Satyavrat Wagle

  • 51,862 views
  • 37 comments
  • 152 respects

IMU to You!

by Gregory O. Voronin

  • 11,720 views
  • 40 comments
  • 34 respects

Display Current Weather with Arduino and Python!

Project tutorial by Arduino “having11” Guy

  • 6,710 views
  • 3 comments
  • 19 respects
Add projectSign up / Login