오류정보와 기능을 조금 추가해 보았습니다.
만약 기상청이 문제가 생겨 접속되지 않으면 노란색 불이 들어오게 됩니다.
그리고 와이파이 접속이 끊어진 경우 기존코드는 다시 전원을 뽑았다 연결해야 하는데
이 소스로 업데이트하시면 자동으로 다시 녹색불이 들어오면서 와이파에 재접속을 하게 됩니다.
수정된 코드는 105~134번줄과 int wColor[7][3]값이 7개로 늘어난 부분입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
#include <ESP8266WiFi.h> #include <Adafruit_NeoPixel.h> #ifdef __AVR__ #include <avr/power.h> // Required for 16 MHz Adafruit Trinket #endif #define PIN D3 #define NUMPIXELS 7 #define CHK_DATACNT 9 #define CHK_DATACNT_WANT 2 Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); int wData[CHK_DATACNT]; int wDataCnt; // 와이파이 ssid을 입력해주세요. //const char* ssid = "decoderGround"; const char* ssid = "harmono"; //const char* ssid = "Decoder4f"; //와이파이 비밀번호를 입력해주세요. const char* password = "decoder@@@"; //const char* password = "decoder4f!!"; const char* host = "www.kma.go.kr"; //const char* host = "www.jaker.zc.bz"; //const char* host = "192.168.0.6"; int bLight = 150; void setup() { pixels.begin(); for (int k = 0; k < NUMPIXELS; k++) { pixels.setPixelColor(k, pixels.Color(0,255,0)); } pixels.setBrightness(bLight); pixels.show(); delay(1000); Serial.begin(115200); delay(10); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } unsigned long previousMillis = 0; const long delayTime = 600000; //1초(1000) 대기시간 //const long delayTime = 30000; //1초(1000) 대기시간 bool firstChk = false; String seq_str = ""; String data_seq = ""; int weatherState = 0;//0 sunny 1 rain 2 snow int prevWState=-1; int wColor[7][3] = { {0,180,255},{0,0,255},{0,0,255},{255,100,30},{0,0,255},{255,0,0},{0,255,0} }; int snowCode = 0; void loop() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= delayTime || firstChk==false) { firstChk = true; previousMillis = currentMillis; Serial.print("connecting to "); Serial.println(host); // Use WiFiClient class to create TCP connections WiFiClient client; const int httpPort = 80; //http://www.kma.go.kr/wid/queryDFSRSS.jsp?zone=1144066000 if (!client.connect(host, httpPort)) { Serial.print("WiFi.status(): "); Serial.println(WiFi.status()); Serial.println("connection failed"); Serial.print("WiFi.status()0000: "); Serial.println(WiFi.status()); if (WiFi.status() == WL_CONNECTED) { Serial.println("WiFi.status() == WL_CONNECTED"); weatherState = 6; if(prevWState!=weatherState) { lightSetting(); prevWState = weatherState; } }else{ Serial.print("ESP.restart()"); Serial.println("WiFi.status() != WL_CONNECTED"); weatherState = 5; if(prevWState!=weatherState) { lightSetting(); prevWState = weatherState; } ESP.restart(); } return; } long randNumber = random(300000); String url = "/wid/queryDFSRSS.jsp?zone=1144066000&rNum="+String(randNumber); // String url = "/test/weather.xml"; // String url = "/weather.xml"; Serial.print("Requesting URL: "); Serial.println(url); // This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(3000); Serial.print("client.available(): "); Serial.println(client.available()); memset(wData, 0, sizeof wData); wDataCnt = 0; while(client.available()){ //라인을 기준으로 문자열을 저장한다. String line = client.readStringUntil('\n'); delay(100); int seq = line.indexOf("<data seq=\""); if (seq >= 0) { seq_str = "<data seq=\""; int seq_end = line.indexOf("\">"); data_seq = line.substring(seq + seq_str.length(), seq_end); if(data_seq.toInt()>CHK_DATACNT-1)break; } // if(data_seq.toInt()>0) //이부분의 주석을 없애면 3시간 뒤 날씨부터 표현됩니다. // { //이부분의 주석을 없애면 3시간 뒤 날씨부터 표현됩니다. int pty= line.indexOf("</pty>"); if(pty>0)//pty라는 줄만 체크하겠다. { String pty_str="<pty>"; String wt_pty = line.substring(line.indexOf(pty_str)+pty_str.length(),pty); Serial.print("data_seq: "); Serial.println(data_seq.toInt()); Serial.print("wt_pty: "); Serial.println(wt_pty); int num = wt_pty.toInt(); wData[wDataCnt] = num; //weatherState = num; wDataCnt++; } // } //이부분의 주석을 없애면 3시간 뒤 날씨부터 표현됩니다. } for (int j = 0; j < CHK_DATACNT_WANT; j++)//전체 예측데이터에서 몇번째 구간까지만 확인해서 날씨상태에 저장할 것인가. { Serial.print("wData[j]: "); Serial.println(wData[j]); if(wData[j]>0){ weatherState = wData[j]; break; } } for(int k=0; k<CHK_DATACNT; k++){//전체 구간을 살펴 눈이오면 위에 체크한 날씨상태를 무시하고 바로 날씨상태를 눈으로 바꾼다. Serial.print("wData[k]: "); Serial.println(wData[k]); if(wData[k]==3){ weatherState = wData[k]; break; } } Serial.print("weatherState: "); Serial.println(weatherState); client.stop(); Serial.println("closing connection"); if(prevWState!=weatherState){ Serial.println("lightSetting"); lightSetting(); prevWState = weatherState; } } } int FadeSpeed = 10; void lightSetting() { for (int j = bLight; j > 0; j=j-1) { pixels.setBrightness(j); pixels.show(); delay(FadeSpeed); } pixels.clear(); for (int m = 1; m < bLight; m=m+1) { pixels.setBrightness(m); for (int k = 0; k < NUMPIXELS; k++) { pixels.setPixelColor(k, pixels.Color(wColor[weatherState][0], wColor[weatherState][1], wColor[weatherState][2])); } pixels.show(); delay(FadeSpeed); } } |