むきゅー系技術メモ

技術的な何かとか書きます。たぶん。

Twitterと連携するコーヒーメーカーを作った

オンラインコーヒーメーカー「萌香たん」とはじめるドキドキ☆コーヒーブレイク - mixi Engineers' Blog

ぶっちゃけこれのパクリ

結果

フランちゃんかわいい

使ったもの

仕組み

  1. コーヒーメーカーの表示ランプをフォトトランジスタで読み取る。
  2. Arduinoに入力して一定値を超えた時点でBLEND、下回った時点でDONEのシリアル信号を出力する。
  3. Raspberry Piがそれを受信してTwitterにポストする。

作り方

[5V] - [抵抗] - [フォトトランジスタ] - [GND]
            |_ [ANALOG 0]
  1. フォトトランジスタに適当に抵抗付けて分圧させとく (10kΩくらい?)
  2. Arduinoのアナログ0番ピンに分圧した線を刺す
  3. フォトトランジスタをコーヒーメーカーのランプに黒いビニールテープでくっつける
  4. Arduinoにスケッチを書き込む
  5. ArduinoとRaspberryPiをUSBでつなぐ
  6. RaspberryPiでrubyスクリプトを回す

使ったコード

coffee.rb

require "serialport"
require "twitter"

client = Twitter::REST::Client.new do |config|
  config.consumer_key        = ""
  config.consumer_secret     = ""
  config.access_token        = ""
  config.access_token_secret = ""
end

now = Time.now.strftime("(%H:%M:%S)")
client.update("起動しました " + now)
sp = SerialPort.new('/dev/ttyACM0', 9600, 8, 1, 0)
sleep 3
loop{
    line = sp.gets # read
    now = Time.now.strftime("(%H:%M:%S)")

    if line.index("BLEND")
        client.update("@palon_m コーヒー淹れるねお兄様! " + now)
    elsif line.index("DONE")
        client.update("@palon_m お兄様ー、コーヒーできたよ! " + now)
    end
}

coffee.ino

boolean blending = 0;
int thres = 5;
int count = 0;

void setup() {
  Serial.begin(9800);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}

void loop() {
  int cds = analogRead(0);
  if (cds > 50 && !blending){
    count++;
  } else if (cds <= 50 && blending){
    count++;
  } else if (count > 0) {
    count--; 
  }
  if (count > thres){
    blending = !blending;
    if (blending){
      Serial.println("BLEND");
      digitalWrite(13, HIGH);
    } else { 
      Serial.println("DONE");
      digitalWrite(13, LOW);
    }
    count = 0;
  }
  delay(50);
}

詳細

あとで書く。