Tenki yoho LED kit2

Dependencies:   mbed

Committer:
kohacraft
Date:
Fri Jun 07 22:48:20 2019 +0000
Revision:
1:1c83ef47372a
Parent:
0:82aac34e1141
Child:
2:4ae4d4564c44
Use OpenWeatherMap

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kohacraft 0:82aac34e1141 1 #include "mbed.h"
kohacraft 0:82aac34e1141 2 #include <string.h>
kohacraft 0:82aac34e1141 3
kohacraft 0:82aac34e1141 4 Serial wifi(dp16, dp15); // ESP-WROOM-02 tx, rx
kohacraft 0:82aac34e1141 5 DigitalOut wifiRst(dp17); // /ESP-WROOM-02 RESET
kohacraft 0:82aac34e1141 6
kohacraft 0:82aac34e1141 7 DigitalOut sunny(dp1); //dp1を晴れのLEDにつなぎます
kohacraft 0:82aac34e1141 8 DigitalOut cloudy(dp2); //dp2を曇りのLEDにつなぎます
kohacraft 0:82aac34e1141 9 DigitalOut rainy(dp4); //dp4を雨のLEDにつなぎます
kohacraft 0:82aac34e1141 10 DigitalOut snowy(dp6); //dp6を雪のLEDににつなぎます
kohacraft 0:82aac34e1141 11
kohacraft 0:82aac34e1141 12 #define baudrate 115200
kohacraft 0:82aac34e1141 13
kohacraft 1:1c83ef47372a 14 static char rxbuff[1000]; //ネットから取得するデータの格納場所
kohacraft 0:82aac34e1141 15 int buffAdr = 0;
kohacraft 0:82aac34e1141 16 const char SSID[] = "your SSDI"; //アクセスポイントのSSID
kohacraft 0:82aac34e1141 17 const char PASS[] = "password"; //アクセスポイントのパスワード
kohacraft 1:1c83ef47372a 18 const char ZIP[] = "100-0000"; //天気予報を調べる郵便番号(半角XXX-XXXX形式)
kohacraft 1:1c83ef47372a 19 const char CITY[] = ""; //天気予報を調べる地名(郵便番号か地名どちらかを設定すればOK)
kohacraft 1:1c83ef47372a 20 const char APIKEY[] = "be69ff90550799b4b72579b61952e680"; //OpenWeatherMapのAPI Key
kohacraft 0:82aac34e1141 21
kohacraft 0:82aac34e1141 22 //データを受信する(1秒くらいは受信する)
kohacraft 0:82aac34e1141 23 bool recvData(int throwDataSize)
kohacraft 0:82aac34e1141 24 {
kohacraft 0:82aac34e1141 25 bool ret = false;
kohacraft 0:82aac34e1141 26 buffAdr = 0;
kohacraft 0:82aac34e1141 27 rxbuff[0] = '\0';
kohacraft 0:82aac34e1141 28
kohacraft 0:82aac34e1141 29 int headerCount = 0;
kohacraft 0:82aac34e1141 30
kohacraft 0:82aac34e1141 31 //700000回ループしたらタイムアウトする(約1秒)
kohacraft 0:82aac34e1141 32 for( int i = 0; i < 700000 ; i++ )
kohacraft 0:82aac34e1141 33 {
kohacraft 0:82aac34e1141 34 if( wifi.readable() != 0 )
kohacraft 0:82aac34e1141 35 {
kohacraft 0:82aac34e1141 36 rxbuff[buffAdr++] = wifi.getc(); //Serial.getc()
kohacraft 0:82aac34e1141 37 headerCount++;
kohacraft 0:82aac34e1141 38 if( headerCount <= throwDataSize ) //最初のthrowDataSize文字捨てる
kohacraft 0:82aac34e1141 39 buffAdr=0;
kohacraft 0:82aac34e1141 40 if( buffAdr >= sizeof(rxbuff)-1 ) //バッファ溢れしたら終了
kohacraft 0:82aac34e1141 41 {
kohacraft 0:82aac34e1141 42 ret = true;
kohacraft 0:82aac34e1141 43 i=700000; //loopを早く終わらすために
kohacraft 0:82aac34e1141 44 break;
kohacraft 0:82aac34e1141 45 }
kohacraft 0:82aac34e1141 46
kohacraft 0:82aac34e1141 47 }
kohacraft 0:82aac34e1141 48 }
kohacraft 0:82aac34e1141 49
kohacraft 0:82aac34e1141 50 //文字列の最後を終端
kohacraft 0:82aac34e1141 51 buffAdr = '\0';
kohacraft 0:82aac34e1141 52 rxbuff[sizeof(rxbuff)-1] = '\0';
kohacraft 0:82aac34e1141 53 return ret;
kohacraft 0:82aac34e1141 54 }
kohacraft 0:82aac34e1141 55
kohacraft 0:82aac34e1141 56 //文字列から予報天気を抽出
kohacraft 1:1c83ef47372a 57 bool getWeather( char* str , int* weatherCord , float* tempLow , float* tempHi )
kohacraft 0:82aac34e1141 58 {
kohacraft 0:82aac34e1141 59 //文字列探索
kohacraft 0:82aac34e1141 60 char *forecast;
kohacraft 1:1c83ef47372a 61 if ( (forecast = strstr(str, "cod\":\"200\"") ) == NULL )
kohacraft 0:82aac34e1141 62 {
kohacraft 0:82aac34e1141 63 //取得できず
kohacraft 0:82aac34e1141 64 weatherCord = 0;
kohacraft 0:82aac34e1141 65 tempLow = 0;
kohacraft 0:82aac34e1141 66 tempLow = 0;
kohacraft 0:82aac34e1141 67 return false;
kohacraft 0:82aac34e1141 68 }
kohacraft 0:82aac34e1141 69
kohacraft 1:1c83ef47372a 70 //最低気温を抽出
kohacraft 1:1c83ef47372a 71 char *lowStart,*lowEnd;
kohacraft 1:1c83ef47372a 72 lowStart = strstr(forecast, "temp_min\":");
kohacraft 1:1c83ef47372a 73 lowStart = strchr( lowStart , ':' );
kohacraft 1:1c83ef47372a 74 lowStart++; //"の次の文字のポインタ
kohacraft 1:1c83ef47372a 75 lowEnd = strchr( lowStart , ',' );
kohacraft 1:1c83ef47372a 76 *lowEnd = '\0';
kohacraft 1:1c83ef47372a 77 *tempLow = atof( lowStart ); //最低気温を数字に変換
kohacraft 1:1c83ef47372a 78 //wifi.printf("Start:%d End:%d Low Temp is %s\n",lowStart,lowEnd,lowStart);
kohacraft 1:1c83ef47372a 79
kohacraft 0:82aac34e1141 80 //最高気温を抽出
kohacraft 0:82aac34e1141 81 char *hiStart,*hiEnd;
kohacraft 1:1c83ef47372a 82 hiStart = strstr(lowEnd+1, "temp_max\":");
kohacraft 1:1c83ef47372a 83 hiStart = strchr( hiStart , ':' );
kohacraft 0:82aac34e1141 84 hiStart++; //"の次の文字のポインタ
kohacraft 1:1c83ef47372a 85 hiEnd = strchr( hiStart , ',' );
kohacraft 0:82aac34e1141 86 *hiEnd = '\0';
kohacraft 1:1c83ef47372a 87 *tempHi = atof( hiStart ); //最高気温を数字に変換
kohacraft 1:1c83ef47372a 88 //wifi.printf("Start:%d End:%d Hi Temp is %s\n",hiStart,hiEnd,hiStart);
kohacraft 0:82aac34e1141 89
kohacraft 1:1c83ef47372a 90 //天気コードを抽出
kohacraft 1:1c83ef47372a 91 char *codeStart,*codeEnd;
kohacraft 1:1c83ef47372a 92 codeStart = strstr(hiEnd+1, "{\"id\":");
kohacraft 1:1c83ef47372a 93 codeStart = strchr( codeStart , ':' );
kohacraft 1:1c83ef47372a 94 codeStart++; //"の次の文字のポインタ
kohacraft 1:1c83ef47372a 95 codeEnd = strchr( codeStart , ',' );
kohacraft 1:1c83ef47372a 96 *codeEnd = '\0';
kohacraft 1:1c83ef47372a 97 *weatherCord = atoi( codeStart ); //最高気温を数字に変換
kohacraft 1:1c83ef47372a 98 //wifi.printf("Start:%d End:%d Code is %s\n",codeStart,codeEnd,codeStart);
kohacraft 1:1c83ef47372a 99
kohacraft 0:82aac34e1141 100 //天気を抽出
kohacraft 0:82aac34e1141 101 char *textStart,*textEnd;
kohacraft 0:82aac34e1141 102 char weatherText[32];
kohacraft 1:1c83ef47372a 103 textStart = strstr(codeEnd+1, "description\":");
kohacraft 1:1c83ef47372a 104 textStart = strchr( textStart+13 , '\"' );
kohacraft 0:82aac34e1141 105 textStart++; //"の次の文字のポインタ
kohacraft 0:82aac34e1141 106 textEnd = strchr( textStart , '\"' );
kohacraft 0:82aac34e1141 107 *textEnd = '\0';
kohacraft 0:82aac34e1141 108 strncpy( weatherText, textStart , 32);
kohacraft 1:1c83ef47372a 109 //wifi.printf("Start:%d End:%d Code is %s\n",textStart,textEnd,textStart);
kohacraft 0:82aac34e1141 110
kohacraft 0:82aac34e1141 111 return true;
kohacraft 0:82aac34e1141 112 }
kohacraft 0:82aac34e1141 113
kohacraft 0:82aac34e1141 114 //LEDを順番に光らせる
kohacraft 0:82aac34e1141 115 void ledIlluminationWait()
kohacraft 0:82aac34e1141 116 {
kohacraft 0:82aac34e1141 117 snowy = 0;
kohacraft 0:82aac34e1141 118 sunny = 1;
kohacraft 0:82aac34e1141 119 wait(0.5);
kohacraft 0:82aac34e1141 120
kohacraft 0:82aac34e1141 121 sunny = 0;
kohacraft 0:82aac34e1141 122 cloudy = 1;
kohacraft 0:82aac34e1141 123 wait(0.5);
kohacraft 0:82aac34e1141 124
kohacraft 0:82aac34e1141 125 cloudy = 0;
kohacraft 0:82aac34e1141 126 rainy = 1;
kohacraft 0:82aac34e1141 127 wait(0.5);
kohacraft 0:82aac34e1141 128
kohacraft 0:82aac34e1141 129 rainy = 0;
kohacraft 0:82aac34e1141 130 snowy = 1;
kohacraft 0:82aac34e1141 131 wait(0.5);
kohacraft 0:82aac34e1141 132
kohacraft 0:82aac34e1141 133 }
kohacraft 0:82aac34e1141 134
kohacraft 0:82aac34e1141 135
kohacraft 0:82aac34e1141 136 //OKの文字列を受信するまで待つ timeが0なら永久に待つ
kohacraft 0:82aac34e1141 137 //time*1[秒]待つ (time=10は10秒待つ)
kohacraft 0:82aac34e1141 138 bool waitOk(int time)
kohacraft 0:82aac34e1141 139 {
kohacraft 0:82aac34e1141 140 bool ret = false;
kohacraft 0:82aac34e1141 141 int addNumber = 1;
kohacraft 0:82aac34e1141 142 if( time == 0 )
kohacraft 0:82aac34e1141 143 {
kohacraft 0:82aac34e1141 144 addNumber = 0;
kohacraft 0:82aac34e1141 145 time = 1;
kohacraft 0:82aac34e1141 146 }
kohacraft 0:82aac34e1141 147 for( int i = 0 ; i<time ; i+=addNumber )
kohacraft 0:82aac34e1141 148 {
kohacraft 0:82aac34e1141 149 recvData(0);
kohacraft 0:82aac34e1141 150 if( strstr(rxbuff, "OK") != NULL )
kohacraft 0:82aac34e1141 151 {
kohacraft 0:82aac34e1141 152 ret = true;
kohacraft 0:82aac34e1141 153 break;
kohacraft 0:82aac34e1141 154 }
kohacraft 0:82aac34e1141 155 }
kohacraft 0:82aac34e1141 156 return ret;
kohacraft 0:82aac34e1141 157 }
kohacraft 0:82aac34e1141 158
kohacraft 0:82aac34e1141 159 int main() {
kohacraft 0:82aac34e1141 160
kohacraft 0:82aac34e1141 161 //天気情報
kohacraft 0:82aac34e1141 162 bool sun = 0;
kohacraft 0:82aac34e1141 163 bool rain = 0;
kohacraft 0:82aac34e1141 164 bool cloud = 0;
kohacraft 0:82aac34e1141 165 bool snow = 0;
kohacraft 0:82aac34e1141 166
kohacraft 0:82aac34e1141 167 wifi.baud(baudrate); //ビットレートを設定
kohacraft 0:82aac34e1141 168
kohacraft 0:82aac34e1141 169 while(1)
kohacraft 0:82aac34e1141 170 {
kohacraft 0:82aac34e1141 171 //無線LANモジュールをリセット
kohacraft 0:82aac34e1141 172 wifiRst = 0;
kohacraft 0:82aac34e1141 173 wait_ms(100);
kohacraft 0:82aac34e1141 174 wifiRst = 1;
kohacraft 0:82aac34e1141 175
kohacraft 0:82aac34e1141 176 //無線LANモジュールが安定するのを待つ
kohacraft 0:82aac34e1141 177 ledIlluminationWait();
kohacraft 0:82aac34e1141 178 ledIlluminationWait();
kohacraft 0:82aac34e1141 179 ledIlluminationWait();
kohacraft 0:82aac34e1141 180 ledIlluminationWait();
kohacraft 0:82aac34e1141 181 ledIlluminationWait();
kohacraft 0:82aac34e1141 182
kohacraft 0:82aac34e1141 183 //最初にlanモジュールから出力される文字列を捨てる
kohacraft 0:82aac34e1141 184 char temp;
kohacraft 1:1c83ef47372a 185 while( wifi.readable() == true )
kohacraft 0:82aac34e1141 186 temp = wifi.getc();
kohacraft 0:82aac34e1141 187
kohacraft 0:82aac34e1141 188 //ATコマンドで無線LANモジュールをクライアントモードにする
kohacraft 1:1c83ef47372a 189 sunny = 1; cloudy = 1; rainy = 0; snowy = 0; //晴れと曇りが光っぱなしの場合はwifiジュールと通信できていない
kohacraft 0:82aac34e1141 190 wifi.printf("AT\r\n");
kohacraft 0:82aac34e1141 191 waitOk(0);
kohacraft 0:82aac34e1141 192
kohacraft 0:82aac34e1141 193 wifi.printf("AT+CWMODE=1\r\n");
kohacraft 0:82aac34e1141 194 waitOk(0);
kohacraft 0:82aac34e1141 195
kohacraft 0:82aac34e1141 196 //アクセスポイントに接続
kohacraft 1:1c83ef47372a 197 sunny = 0; cloudy = 0; rainy = 1; snowy = 1; //雨と雪が光っぱなしの場合はアクセスポイントにつながらない
kohacraft 0:82aac34e1141 198 wifi.printf("AT+CWJAP=\"%s\",\"%s\"\r\n",SSID,PASS);
kohacraft 0:82aac34e1141 199 waitOk(0);
kohacraft 0:82aac34e1141 200
kohacraft 0:82aac34e1141 201 while(1)
kohacraft 0:82aac34e1141 202 {
kohacraft 0:82aac34e1141 203 //LEDを消灯
kohacraft 0:82aac34e1141 204 sunny = 0; cloudy = 0; rainy = 0; snowy = 0; //全てが消え続ける場合はサーバーにつながらない
kohacraft 0:82aac34e1141 205
kohacraft 0:82aac34e1141 206 //サーバに接続
kohacraft 1:1c83ef47372a 207 wifi.printf("AT+CIPSTART=\"TCP\",\"api.openweathermap.org\",80\r\n");
kohacraft 1:1c83ef47372a 208 if( waitOk(3600) == false )
kohacraft 0:82aac34e1141 209 break; //1時間待っても返事が無ければ最初からやり直し
kohacraft 0:82aac34e1141 210
kohacraft 0:82aac34e1141 211 //サーバに天気データを要求
kohacraft 0:82aac34e1141 212 char str[250];
kohacraft 1:1c83ef47372a 213 if( sizeof(CITY) <= 1 )
kohacraft 1:1c83ef47372a 214 //郵便番号で天気を検索
kohacraft 1:1c83ef47372a 215 sprintf(str , "GET http://api.openweathermap.org/data/2.5/forecast?&zip=%s,JP&units=metric&cnt=1&appid=%s\r\nHost:api.openweathermap.org\r\nConnection:close\r\n\r\n",ZIP,APIKEY);
kohacraft 1:1c83ef47372a 216 else
kohacraft 1:1c83ef47372a 217 //地名で天気予報を検索
kohacraft 1:1c83ef47372a 218 sprintf(str , "GET http://api.openweathermap.org/data/2.5/forecast?&q=%s,JP&units=metric&cnt=1&appid=%s\r\nHost:api.openweathermap.org\r\nConnection:close\r\n\r\n",CITY,APIKEY);
kohacraft 1:1c83ef47372a 219
kohacraft 0:82aac34e1141 220 wifi.printf("AT+CIPSEND=%d\r\n",strlen(str));
kohacraft 0:82aac34e1141 221 if( waitOk(3600) == false )
kohacraft 0:82aac34e1141 222 break; //1時間待っても返事が無ければ最初からやり直し
kohacraft 0:82aac34e1141 223
kohacraft 0:82aac34e1141 224 wifi.printf("%s",str);
kohacraft 1:1c83ef47372a 225 recvData(1); //データの受信(最初の1500文字を捨てる)
kohacraft 1:1c83ef47372a 226 wait(1);
kohacraft 1:1c83ef47372a 227 wifi.printf("%s\r\n",rxbuff); //*** デバッグ用 *** バッファの内容を出力
kohacraft 0:82aac34e1141 228
kohacraft 0:82aac34e1141 229 //文字列探索
kohacraft 0:82aac34e1141 230 sunny = 0; cloudy = 0; rainy = 0; snowy = 1; //雪が光っぱなしの場合は天気情報が得られてない
kohacraft 1:1c83ef47372a 231 int weatherCord;
kohacraft 1:1c83ef47372a 232 float tempHi , tempLow;
kohacraft 0:82aac34e1141 233 if( getWeather( rxbuff , &weatherCord , &tempLow , &tempHi ) == false ) //データの受信
kohacraft 0:82aac34e1141 234 {
kohacraft 0:82aac34e1141 235 //予報の文字列がない
kohacraft 0:82aac34e1141 236 wait(30);
kohacraft 0:82aac34e1141 237 break; //30秒待って最初からやり直す
kohacraft 0:82aac34e1141 238 }
kohacraft 0:82aac34e1141 239
kohacraft 0:82aac34e1141 240 //*** ↓デバッグ用 *** 受信した天気情報を出力
kohacraft 1:1c83ef47372a 241 //wifi.printf("weatherCord:%d tempLow:%f tempHi:%f\r\n",weatherCord , tempLow , tempHi);
kohacraft 0:82aac34e1141 242
kohacraft 0:82aac34e1141 243
kohacraft 1:1c83ef47372a 244 //天気コードから天気をLEDの色に変換する
kohacraft 1:1c83ef47372a 245
kohacraft 1:1c83ef47372a 246 if( weatherCord < 600 ){ //雨
kohacraft 1:1c83ef47372a 247 sun = 0;
kohacraft 1:1c83ef47372a 248 rain = 1;
kohacraft 1:1c83ef47372a 249 cloud = 0;
kohacraft 1:1c83ef47372a 250 snow = 0;
kohacraft 1:1c83ef47372a 251 }else if( weatherCord < 700 ){ //雪
kohacraft 1:1c83ef47372a 252 sun = 0;
kohacraft 1:1c83ef47372a 253 rain = 0;
kohacraft 1:1c83ef47372a 254 cloud = 0;
kohacraft 1:1c83ef47372a 255 snow = 1;
kohacraft 1:1c83ef47372a 256 }else if( weatherCord < 800 ){ //曇り
kohacraft 1:1c83ef47372a 257 sun = 0;
kohacraft 1:1c83ef47372a 258 rain = 0;
kohacraft 1:1c83ef47372a 259 cloud = 1;
kohacraft 1:1c83ef47372a 260 snow = 0;
kohacraft 1:1c83ef47372a 261 }else if( weatherCord <= 802 ){ //晴れ
kohacraft 1:1c83ef47372a 262 sun = 1;
kohacraft 1:1c83ef47372a 263 rain = 0;
kohacraft 1:1c83ef47372a 264 cloud = 0;
kohacraft 1:1c83ef47372a 265 snow = 0;
kohacraft 1:1c83ef47372a 266 }else{ //曇り
kohacraft 1:1c83ef47372a 267 sun = 0;
kohacraft 1:1c83ef47372a 268 rain = 0;
kohacraft 1:1c83ef47372a 269 cloud = 1;
kohacraft 1:1c83ef47372a 270 snow = 0;
kohacraft 0:82aac34e1141 271 }
kohacraft 0:82aac34e1141 272
kohacraft 1:1c83ef47372a 273 //LEDを点滅させながら30分待つ
kohacraft 1:1c83ef47372a 274 for(int i=0 ; i<30*60 ; i++)
kohacraft 0:82aac34e1141 275 {
kohacraft 0:82aac34e1141 276 //LEDを点灯
kohacraft 0:82aac34e1141 277 sunny = sun;
kohacraft 0:82aac34e1141 278 rainy = rain;
kohacraft 0:82aac34e1141 279 cloudy = cloud;
kohacraft 0:82aac34e1141 280 snowy = snow;
kohacraft 0:82aac34e1141 281 wait(0.5);
kohacraft 0:82aac34e1141 282
kohacraft 0:82aac34e1141 283 //LEDを消灯
kohacraft 0:82aac34e1141 284 sunny = 0;
kohacraft 0:82aac34e1141 285 rainy = 0;
kohacraft 0:82aac34e1141 286 cloudy = 0;
kohacraft 0:82aac34e1141 287 snowy = 0;
kohacraft 0:82aac34e1141 288 wait(0.5);
kohacraft 0:82aac34e1141 289 }
kohacraft 0:82aac34e1141 290 }
kohacraft 0:82aac34e1141 291 }
kohacraft 0:82aac34e1141 292 }