kohacraft Lab
/
kc_tenkiyoho_LED
tenki yoho LED (with ESP-WROOM-02)
Diff: main.cpp
- Revision:
- 4:1c1fb59cf7d9
- Parent:
- 3:5689bfb78c7b
--- a/main.cpp Sun Sep 06 09:53:14 2015 +0000 +++ b/main.cpp Wed May 04 02:58:14 2016 +0000 @@ -11,12 +11,12 @@ #define baudrate 115200 -static char rxbuff[1000]; //取得する文字列 +static char rxbuff[1500]; //取得する文字列 int buffAdr = 0; const char SSID[] = "your ssid"; //アクセスポイントのSSID const char PASS[] = "password"; //アクセスポイントのパスワード -const char WOEID[] = "28413961"; //天気を調べる場所 http://woeid.rosselliot.co.nz/ で郵便番号で検索して得られる8桁 +const char WOEID[] = "28413961"; //天気を調べる場所 http://woeid.rosselliot.co.nz/ で郵便番号で検索して得られる番号 //データを受信する bool recvData() @@ -35,7 +35,7 @@ { rxbuff[buffAdr++] = wifi.getc(); //Serial.getc() headerCount++; - if( headerCount <= 2400 ) //最初の2400文字捨てる + if( headerCount <= 1500 ) //最初の2400文字捨てる buffAdr=0; if( buffAdr >= sizeof(rxbuff)-1 ) //バッファ溢れしたら終了 { @@ -66,47 +66,46 @@ return false; } + //天気コードを抽出 + char *codeStart,*codeEnd; + //int weatherCord; + codeStart = strstr(forecast, "code="); + codeStart = strchr( codeStart , '\"' ); + codeStart++; //"の次の文字のポインタ + codeEnd = strchr( codeStart , '\"' ); + *codeEnd = '\0'; + *weatherCord = atoi( codeStart ); //最高気温を数字に変換 + + //最高気温を抽出 + char *hiStart,*hiEnd; + //int tempHi; + hiStart = strstr(codeEnd+1, "high="); + hiStart = strchr( hiStart , '\"' ); + hiStart++; //"の次の文字のポインタ + hiEnd = strchr( hiStart , '\"' ); + *hiEnd = '\0'; + *tempHi = atoi( hiStart ); //最高気温を数字に変換 + //最低気温を抽出 char *lowStart,*lowEnd; //int tempLow; - lowStart = strstr(forecast, "low"); + lowStart = strstr(hiEnd+1, "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 = strstr(lowEnd+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; } @@ -146,7 +145,7 @@ wifi.printf("AT\r\n"); wait(2); red = 0.9; green = 1.0; blue = 0.0; - wifi.printf("AT+CWMODE=3\r\n"); + wifi.printf("AT+CWMODE=1\r\n"); wait(2); red = 0.6; green = 0.8; blue = 0.3; //アクセスポイントに接続 @@ -161,30 +160,39 @@ while(1) { //サーバに接続 - wifi.printf("AT+CIPSTART=\"TCP\",\"weather.yahooapis.com\",80\r\n"); + wifi.printf("AT+CIPSTART=\"TCP\",\"query.yahooapis.com\",80\r\n"); wait(2); //サーバに要求 - wifi.printf("AT+CIPSEND=72\r\n"); - wait(2); + char str[200]; + sprintf(str , "GET http://query.yahooapis.com/v1/public/yql?q=SELECT%%20*%%20FROM%%20weather.forecast%%20WHERE%%20woeid%%3D%%22%s%%22%20and%%20u%%3D%%22c%%22\r\nHost: query.yahooapis.com\r\nConnection: close\r\n\r\n\r\n",WOEID); + wifi.printf("AT+CIPSEND=%d\r\n",strlen(str)); + wait(1); //受信 - wifi.printf("GET http://weather.yahooapis.com/forecastrss?u=c&w=%s HTTP/1.1\r\n\r\n",WOEID); + wifi.printf("%s",str); recvData(); //切断 wifi.printf("AT+CIPCLOSE\r\n"); + wifi.printf("%s\r\n",rxbuff); //文字列探索 int weatherCord , tempHi , tempLow; if( getWeather( rxbuff , &weatherCord , &tempLow , &tempHi ) == false ) //データの受信 { //予報の文字列がない - red = 1.0; - wait(2); + wifi.printf("Can not find strng:forecast\r\n"); + for( int i=0 ; i<5 ; i++ ) + { + red = 1.0; + wait(0.25); + red = 0.0; + wait(0.25); + } break; //最初からやり直す } - + wifi.printf("weatherCord:%d tempLow:%d tempHi:%d\r\n",weatherCord , tempLow , tempHi); //天気をLEDの色に変換する float rStart , gStart , bStart , rEnd , gEnd , bEnd;