/* * つぶやき君 * * Special Thanks. * Twitterライブラリ * http://d.hatena.ne.jp/NeoCat/20090505/1241466723 * * DHCPライブラリも必要です * http://blog.jordanterrell.com/post/Arduino-DHCP-Library-Version-04.aspx * * <仕様> * 基本的に15分間隔で「室温」と「部屋の明るさ」をつぶやく * 設定温度を超えたら「暑いよ、暑いよ」とつぶやき続ける * 明るさが変わったら、その時点でつぶやく * <使い方> * 26行目の MACアドレス を書き換えてください * あとは、ブログ記事を参考に */ #include #include #include #include byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //MACアドレス boolean ipAcquired = false; char msg[256]; // 設定値 #define UPDATE_INTERVAL 10 //データ更新間隔(約 x秒) #define MIN_INTERVAL 15 //最低、何分ごとにポストするか(分) #define POST_THRESHOLD 100 //明るさがどれぐらい変わったらポストするか(0-1023) #define ALERT_TEMPERATURE 32 //警告を発する温度 // 温度 明るさ設定 #define LED 13 //デジタル 13ピン #define ANALOG_TEMPERATURE 0 //アナログ 0ピン #define ANALOG_BRIGHTNESS 1 //アナログ 1ピン int vol_temp = 0; //温度センサから読み取る値 int sum_temp = 0; //平均値算出用の合計 int i; double temp_double; //温度計算用 int temp_1 = 0; //温度 整数部 int temp_2 = 0; //温度 小数部 int brightness = 0; //明るさ取得用 int b_old = 0; //明るさ状態 前回(0-5) int b_now = 0; //明るさ状態 現在(0-5) int posts = 1; //Twitter用カウンタ int isPost = 0; //Twitterにポストするか否か int loop_cnt = 0; //最低更新間隔 void setup() { analogReference(INTERNAL); pinMode(LED, OUTPUT); Serial.begin(9600); Serial.println("program start..."); } void setupDHCP() { // GET IP Address! Serial.println("getting ip..."); int result = Dhcp.beginWithDHCP(mac); if(result == 1) { ipAcquired = true; byte buffer[6]; Serial.println("ip acquired..."); Dhcp.getLocalIp(buffer); Serial.print("ip address: "); printArray(&Serial, ".", buffer, 4, 10); delay(3000); Serial.println("connecting..."); } else{ Serial.println("unable to acquire ip address..."); } } void printArray(Print *output, char* delimeter, byte* data, int len, int base) { char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; for(int i = 0; i < len; i++) { if(i != 0){ output->print(delimeter); } output->print(itoa(data[i], buf, base)); } output->println(); } void loop() { delay(UPDATE_INTERVAL*1000); // 初期化 isPost = 0; char buff[256]; sprintf(buff,"No.%d ", posts); strcpy(msg, buff); // 温度取得 sum_temp = 0; for( i = 0; i < 50 ; i++) { vol_temp = analogRead(ANALOG_TEMPERATURE); sum_temp = sum_temp + vol_temp; delay(2); } // 温度計算(整数部.小数部) temp_double =(double)(sum_temp) / 50.0 * 1.1 / 1024.0 * 100.0; temp_1 = (int)(temp_double); temp_2 = (int)((temp_double - (double)temp_1) * 100.0); Serial.print(temp_1); Serial.print("."); Serial.println(temp_2); sprintf(buff,"温度:%d", temp_1); strcat(msg, buff); sprintf(buff,".%d ℃ ", temp_2); strcat(msg, buff); if(temp_1 >= ALERT_TEMPERATURE-2){ sprintf(buff,"暑いです、暑いです "); strcat(msg, buff); isPost = 1; } else if(temp_1 >= ALERT_TEMPERATURE){ sprintf(buff,"暑くなってきました "); strcat(msg, buff); isPost = 1; } // 明るさ取得 brightness = analogRead(ANALOG_BRIGHTNESS); Serial.println(brightness); if(brightness > 1000){ sprintf(buff," / 明るいです (%d)", brightness); b_now = 4; } else if(brightness > 800){ sprintf(buff," / やや明るいです (%d)", brightness); b_now = 3; } else if(brightness > 500){ sprintf(buff," / 少し暗くなりました (%d)", brightness); b_now = 2; } else if(brightness > 300){ sprintf(buff," / ほぼ真っ暗です (%d)", brightness); b_now = 1; } else{ sprintf(buff," / 完全に真っ暗です (%d)", brightness); b_now = 0; } strcat(msg, buff); if( b_now != b_old ) { b_old = b_now; isPost = 1; } if(isPost || loop_cnt >= MIN_INTERVAL*60/UPDATE_INTERVAL ) { setupDHCP(); // IPチェック if(ipAcquired == false){ delay(3000); return; } Twitter twitter("ID:PASSWORD"); Serial.println(msg); posts = posts + 1; loop_cnt = 0; if (twitter.post(msg)) { int status = twitter.wait(); if (status == 200) { Serial.println("Twitter POST OK."); } else { Serial.print("Twitter FAILED : code "); Serial.println(status); } } else { Serial.println("Twitter connection FAILD."); } /* // wait 15min unsigned long waitTime = 60000UL*15UL; delay(waitTime); */ } loop_cnt = loop_cnt + 1; }