kohacraft Lab
/
kc_tenkiyoho_LED
tenki yoho LED (with ESP-WROOM-02)
Diff: main.cpp
- Revision:
- 0:26fecc2ddabd
- Child:
- 1:06fd45432b0c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Aug 24 02:30:31 2015 +0000 @@ -0,0 +1,303 @@ +#include "mbed.h" +#include <string.h> + +Serial wifi(dp16, dp15); // ESP-WROOM-02 tx, rx +DigitalOut wifiRst(dp17); // /ESP-WROOM-02 RESET +DigitalIn lightSensor(dp4); //loom light sensor(CdS:GL5528 conect to dp4 and GND) + +PwmOut red(dp1); //dp1を赤のLEDのPWM出力にします +PwmOut green(dp2); //dp2を緑のLEDのPWM出力にします +PwmOut blue(dp18); //dp18を青のLEDのPWM出力にします + +#define baudrate 115200 + +static char rxbuff[1000]; //取得する文字列 +int buffAdr = 0; + +const char SSID[] = "your SSID"; //アクセスポイントのSSID +const char PASS[] = "password"; //アクセスポイントのパスワード +const char WOEID[] = "15015472"; //天気を調べる場所 http://woeid.rosselliot.co.nz/ で郵便番号で検索して得られる8桁 + +//データを受信する +bool recvData() +{ + bool ret = false; + buffAdr = 0; + for( int i =0 ; i< sizeof( rxbuff ) ; i++ ) + rxbuff[i] = '\0'; + + int headerCount = 0; + + //1000000回ループしたらタイムアウトする + for( int i = 0; i < 1000000 ; i++ ) + { + if( wifi.readable() != 0 ) + { + rxbuff[buffAdr++] = wifi.getc(); //Serial.getc() + headerCount++; + if( headerCount <= 2400 ) //最初の2400文字捨てる + buffAdr=0; + if( buffAdr >= sizeof(rxbuff)-1 ) //バッファ溢れしたら終了 + { + ret = true; + i=1000000; //loopを早く終わらすために + break; + } + + } + } + + //文字列の最後を終端 + rxbuff[sizeof(rxbuff)-1] = '\0'; + return ret; +} + +//文字列から予報天気を抽出 +bool getWeather( char* str , int* weatherCord , int* tempLow , int* tempHi ) +{ + //文字列探索 + char *forecast; + if ( (forecast = strstr(str, "yweather:forecast") ) == NULL ) + { + //取得できず + weatherCord = 0; + tempLow = 0; + tempLow = 0; + return false; + } + + //最低気温を抽出 + char *lowStart,*lowEnd; + //int tempLow; + lowStart = strstr(forecast, "low"); + lowStart = strchr( lowStart , '\"' ); + lowStart++; //"の次の文字のポインタ + lowEnd = strchr( lowStart , '\"' ); + *lowEnd = '\0'; + *tempLow = atoi( lowStart ); //最低気温を数字に変換 + + //最高気温を抽出 + char *hiStart,*hiEnd; + //int tempHi; + hiStart = strstr(lowEnd+1, "high"); + hiStart = strchr( hiStart , '\"' ); + hiStart++; //"の次の文字のポインタ + hiEnd = strchr( hiStart , '\"' ); + *hiEnd = '\0'; + *tempHi = atoi( hiStart ); //最高気温を数字に変換 + + + //天気を抽出 + char *textStart,*textEnd; + char weatherText[32]; + textStart = strstr(hiEnd+1, "text"); + textStart = strchr( textStart , '\"' ); + textStart++; //"の次の文字のポインタ + textEnd = strchr( textStart , '\"' ); + *textEnd = '\0'; + strncpy( weatherText, textStart , 32); + + //天気コードを抽出 + char *codeStart,*codeEnd; + //int weatherCord; + codeStart = strstr(textEnd+1, "code"); + codeStart = strchr( codeStart , '\"' ); + codeStart++; //"の次の文字のポインタ + codeEnd = strchr( codeStart , '\"' ); + *codeEnd = '\0'; + *weatherCord = atoi( codeStart ); //最高気温を数字に変換 + + return true; +} + + +int main() { + + //LEDのPWM周期 + red.period(5); + green.period(5); + blue.period(5); + + lightSensor.mode(PullUp); //室内の明るさセンサをプルアップする + wifi.baud(baudrate); //ビットレートを設定 + + while(1) + { + //無線LANモジュールをリセット + wifiRst = 1; + wifiRst = 0; + wifiRst = 1; + + //無線LANモジュールが安定するのを待つ + for( int i=0 ; i<10 ; i++ ) + { + red = 0.9; green = 1.0; blue = 0.0; + wait( 0.5 ); + red = 0.0; green = 0.0; blue = 0.0; + wait( 0.5 ); + } + + //最初にlanモジュールから出力される文字列を捨てる + char temp; + while( wifi.readable() == 1 ) + temp = wifi.getc(); + + //ATコマンドで無線LANモジュールをクライアントモードにする + wifi.printf("AT\r\n"); + wait(2); + red = 0.9; green = 1.0; blue = 0.0; + wifi.printf("AT+CWMODE=3\r\n"); + wait(2); + red = 0.6; green = 0.8; blue = 0.3; + //アクセスポイントに接続 + wifi.printf("AT+CWJAP=\"%s\",\"%s\"\r\n",SSID,PASS); + red = 0.4; green = 0.6; blue = 0.6; + wait(3); + red = 0.2; green = 0.4; blue = 0.8; + wait(3); + red = 0.0; green = 0.2; blue = 1.0; + + + while(1) + { + //サーバに接続 + wifi.printf("AT+CIPSTART=\"TCP\",\"188.125.93.38\",80\r\n"); + wait(2); + + //サーバに要求 + wifi.printf("AT+CIPSEND=72\r\n"); + wait(2); + + //サーバに要求 + wifi.printf("GET http://weather.yahooapis.com/forecastrss?u=c&w=%s HTTP/1.1\r\n\r\n",WOEID); + if( recvData() == false ) //データの受信 + { + //受信できず + red = 1.0; + wait(2); + break; //最初からやり直す + } + + + //切断 + wifi.printf("AT+CIPCLOSE\r\n"); + + //文字列探索 + int weatherCord , tempHi , tempLow; + if( getWeather( rxbuff , &weatherCord , &tempLow , &tempHi ) == false ) //データの受信 + { + //予報の文字列がない + red = 1.0; + wait(2); + break; //最初からやり直す + } + + + //天気をLEDの色に変換する + float rStart , gStart , bStart , rEnd , gEnd , bEnd; + switch(weatherCord) + { + //雨 + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 11: + case 12: + case 17: + case 18: + case 35: + case 37: + case 38: + case 39: + case 40: + case 45: + case 47: + rStart = 0.0; gStart = 0.0; bStart = 1.0; + rEnd = 0.0; gEnd = 0.3; bEnd = 0.8; + break; + + //雪 + case 7: + case 8: + case 9: + case 10: + case 13: + case 14: + case 15: + case 16: + case 23: + case 41: + case 42: + case 43: + case 46: + rStart = 1.0; gStart = 0.8; bStart = 0.8; + rEnd = 0.8; gEnd = 0.6; bEnd = 0.6; + break; + + //曇り + case 19: + case 20: + case 22: + case 26: + case 27: + case 28: + case 44: + rStart = 0.6; gStart = 0.4; bStart = 0.4; + rEnd = 0.25; gEnd = 0.2; bEnd = 0.15; + break; + + //晴れ + case 21: + case 24: + case 25: + case 29: + case 30: + case 31: + case 32: + case 33: + case 34: + case 36: + rStart = 1.0; gStart = 0.5; bStart = 0.0; + rEnd = 1.0; gEnd = 0.3; bEnd = 0.2; + break; + + default: + rStart = 0.0; gStart = 0.0; bStart = 0.0; + rEnd = 1.0; gEnd = 0.0; bEnd = 1.0; + break; + } + + for(int j=0 ; j<30*60/2 ; j++) //30分ループし続ける + { + //startの色からendの色へと1秒かけて色を変化させる + for( float i=0 ; i<100 ; i++ ) + { + red = rStart + (rEnd-rStart)*i/100.0; + green = gStart + (gEnd-gStart)*i/100.0; + blue = bStart + (bEnd-bStart)*i/100.0; + if( lightSensor != 0 ) + { + red = red*0.1; green = green*0.1; blue = blue*0.1; + } + wait(0.01); + } + //endの色からstartの色へと1秒かけて色を変化させる + for( float i=0 ; i<100 ; i++ ) + { + red = rEnd + (rStart-rEnd)*i/100.0; + green = gEnd + (gStart-gEnd)*i/100.0; + blue = bEnd + (bStart-bEnd)*i/100.0; + if( lightSensor != 0 ) + { + red = red*0.1; green = green*0.1; blue = blue*0.1; + } + wait(0.01); + } + } + } + } +}