RubyからArduinoにこんにちわー!できたので, twitterでリプライ来たら通知するようにしてみました。
ruby側はtwitterのuserstreamを監視してリプライきたらシリアルポートにユーザ名を書き込むだけ
#!/usr/bin/env ruby # encoding : utf-8 require 'userstream' require 'yaml' require 'serialport' sp = SerialPort.new("/dev/ttyACM0", 9600, 8, 1, SerialPort::NONE) stream_config = YAML.load_file(File.expand_path(File.dirname(__FILE__) + '/config.yml')) UserStream.configure do |config| config.consumer_key = stream_config[:consumer_key] config.consumer_secret = stream_config[:consumer_secret] config.oauth_token = stream_config[:oauth_token] config.oauth_token_secret = stream_config[:oauth_token_secret] end client = UserStream.client client.user do |status| if status.has_key? "text" if status.in_reply_to_screen_name == "nefo_mi" sp.puts "@#{status.user.screen_name} " end end end sp.close
Arduino側はシリアルポートに書き込まれた文字を受け取ってLCDに表示させてLEDを交互にチカチカさせればOK
#include <LiquidCrystal.h> int incomingByte; char inByte; String inMsg; boolean led; int light; LiquidCrystal lcd(7,8,9,10,11,12,13); void setup() { lcd.begin(2, 16); lcd.clear(); lcd.print("Twitter Mention"); lcd.setCursor(0, 1); lcd.print("Notification"); Serial.begin(9600); Serial.println("setupDone"); led = false; light = 0; } void loop() { if (Serial.available() > 0) { inByte = Serial.read(); if((inByte >= 65 && inByte <= 90) || (inByte >= 97 && inByte <= 122) || (inByte >= 48 && inByte <= 57) || (inByte == 64) || (inByte == 95)){ inMsg.concat(inByte); } if (inByte == 10 || inByte == 13){ inByte = 0; lcd.clear(); lcd.print(inMsg); inMsg = ""; led = true; light = 0; } } if (led){ if (light % 2 == 0){ digitalWrite(5, HIGH); digitalWrite(6, LOW); }else{ digitalWrite(5, LOW); digitalWrite(6, HIGH); } delay(500); if(++light >= 100){ led = false; digitalWrite(5, LOW); digitalWrite(6, LOW); } } }
おらねこからリプライ送ってみた。
USBから電源とってると電力が弱いのかLEDの光が弱いー
ACアダプタから電源とるとシリアル通信できないという・・・
イーサネットシールド欲しいw
たわごと
接着剤ではくっつかないよね・・・