Korea Weather Information System
[Items]
1. PHPoC Black
2. RS232 Board (PES-2201)
3. Color LCD Display (MDP070N)
[How it works]
1. PHPoC Black takes weather information such as temperature, humidity, precipitation probability, sky condition, etc from Korea Meteorological Administration homepage via HTTP.
2. PHPoC Black sends the weather information to the LCD display.
[Video Clip]
[Source Codes]
Code:
<?php include "/lib/sn_dns.php"; define ("DEBUG", 1) define ("USE_DISPLAY", 1) define ("LOCAL_COUNT", 4) define ("ITEM_COUNT", 7) $addr = "www.kma.go.kr"; // TCP Server Address $port = 80; // TCP port number $rr = ""; // Seoul, Busan, Incheon, Jeju $local = array( "1159068000", "2644058000", "2871025000", "5013025300"); // 지역, 예보 시간, 온도, 하늘 상태, 강수 형태, 강수 확률, 습도 $info = array( "", "", "", "", "", "", "" ); $region_text = ""; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // M Display //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function SendCommand($sdata) { $rdata = ""; $timeout = 0; $pid = pid_open("/mmap/uart0"); // Use uart0 pid_ioctl($pid, "set baud 115200"); // Serial baudrate 115200bps if(DEBUG) echo $sdata; pid_write($pid, $sdata); while(pid_ioctl($pid, "get rxlen") < 1 && $timeout < 5) { $timeout++; usleep(100000); } // Read data pid_read($pid, $rdata); if(DEBUG) echo $rdata. "\r\n"; pid_close($pid); } function LoadFont($path) { $command = sprintf("\x0aLoadFont \"%s\"\x0d", $path); SendCommand($command); } function SetColor($color) { $command = sprintf("\x0aColor %s\x0d", $color); SendCommand($command); } function DrawText($x, $y, $text, $mode = 0) { if($mode == 1) // align : center { $command = sprintf("\x0aFillTextCenter %d %d \"%s\" 0\x0d", $x, $y, $text); } else if($mode == 2) // align : right { $command = sprintf("\x0aFillTextRight %d %d \"%s\" 0\x0d", $x, $y, $text); } else // align : left { $command = sprintf("\x0aFillText %d %d \"%s\" 0\x0d", $x, $y, $text); } SendCommand($command); } function DrawImage($x, $y, $path) { $command = sprintf("\x0aImage %d %d \"%s\"\x0d", $x, $y, $path); SendCommand($command); } function Clear() { SendCommand("\x0aClear\x0d"); } function SetFontSize($size) { $command = sprintf("\x0aFontSize %d\x0d", $size); SendCommand($command); } function DisableFlush() { SendCommand("\x0aDisableFlush\x0d"); } function EnableFlush() { SendCommand("\x0aEnableFlush\x0d"); } function CreateLayer($x, $y, $width, $height, $id) { $command = sprintf("\x0aCreateLayer %d %d %d %d %d\x0d", $x, $y, $width, $height, $id); SendCommand($command); } function DestroyLayer($index) { $command = sprintf("\x0aDestroyLayer %d\x0d", $index); SendCommand($command); } function SelectLayer($id) { $command = sprintf("\x0aLayer %d\x0d", $id); SendCommand($command); } function Init() { // Destroy Layer 1 (Max Layer : 15) for($i = 0; $i < 1; $i++) { DestroyLayer($i); } Clear(); EnableFlush(); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Print information function PrintInfo() { global $info; if(DEBUG) { for($i = 0; $i < ITEM_COUNT; $i++) { echo $info[$i]." "; } echo "\r\n"; } if(USE_DISPLAY) { DisableFlush(); Clear(); // Draw Image DrawImage(0, 0, "/image/bk2.png"); // Set font size SetFontSize(80); if($info[0] == "서울특별시") { $region_text = "Seoul"; }else if($info[0] == "제주특별자치도"){ $region_text = "Jeju"; }else if ($info[0] == "인천광역시") { $region_text = "Incheon"; }else if ($info[0] == "부산광역시") { $region_text = "Busan"; }else { $region_text = $info[0]; } DrawText(550, 120, $region_text, 1); SetFontSize(100); DrawText(460, 270, $info[2]); $y = 355; SetFontSize(32); DrawText(440, $y, $info[6]); DrawText(555, $y, $info[5]); $date1 = date("H:i"); DrawText(665, $y, $date1); $x = 68; $y = 118; if($info[4] == "1") // rain { DrawImage($x, $y, "/image/weather5.png"); } else if($info[4] == "2") // rain/snow { DrawImage($x, $y, "/image/weather6.png"); } else if($info[4] == "3") // snow { DrawImage($x, $y, "/image/weather7.png"); } else if($info[3] == "1") // clear { DrawImage($x, $y, "/image/weather1.png"); } else if($info[3] == "2") // partly cloudy { DrawImage($x, $y, "/image/weather2.png"); } else if($info[3] == "3") // cloudy { DrawImage($x, $y, "/image/weather3.png"); } else if($info[3] == "4") // { DrawImage($x, $y, "/image/weather4.png"); } EnableFlush(); } } // 날씨 정보 업데이트 function Update($server_ip, $server_port, $local) { global $info; $pid = pid_open("/mmap/tcp0"); // Use tcp0 pid_bind($pid, "", 0); $try_count = 0; do { pid_connect($pid, $server_ip, $server_port); // Connect TCP $state = pid_ioctl($pid, "get state"); // Get TCP state $try_count++; sleep(1); if($try_count > 10) { printf("TCP connection is failed[%s]\r\n", $local); pid_close($pid); return; } } while($state == TCP_CONNECTED); $send = sprintf("GET /wid/queryDFSRSS.jsp?zone=%s HTTP/1.0\r\n", $local); pid_send($pid, $send); pid_send($pid, "HOST: www.kma.go.kr\r\n"); pid_send($pid, "Connection: close\r\n\r\n"); usleep(100000); $line = ""; $bseq0 = false; $bend = false; while(pid_ioctl($pid, "get state") == TCP_CONNECTED) { $rwbuf = ""; $rwlen = 0; if($bend) { $rwlen = pid_recv($pid, $rwbuf, 1024); usleep(100000); continue; } else { $rwlen = pid_recv($pid, $rwbuf, 1); } if($rwlen == 0) { usleep(100000); continue; } if($rwbuf == "\n") { if(is_int(strpos($line, "<category>"))) { $pos1 = strpos($line, ">") + 1; $pos2 = strpos($line, "</"); $data = substr($line, $pos1, $pos2 - $pos1); $pos1 = strpos($data, " "); $data = substr($data, 0, $pos1); $info[0] = $data; } if(is_int(strpos($line, "<data seq=\"0\">"))) { $bseq0 = true; $line = ""; continue; } if($bseq0 == false) { $line = ""; continue; } if(is_int(strpos($line, "<temp>"))) { $pos1 = strpos($line, ">") + 1; $pos2 = strpos($line, "</"); $data = substr($line, $pos1, $pos2 - $pos1); $info[2] = $data."℃"; } if(is_int(strpos($line, "<sky>"))) { $pos1 = strpos($line, ">") + 1; $pos2 = strpos($line, "</"); $data = substr($line, $pos1, $pos2 - $pos1); $info[3] = $data; } if(is_int(strpos($line, "<pty>"))) { $pos1 = strpos($line, ">") + 1; $pos2 = strpos($line, "</"); $data = substr($line, $pos1, $pos2 - $pos1); $info[4] = $data; } if(is_int(strpos($line, "<pop>"))) { $pos1 = strpos($line, ">") + 1; $pos2 = strpos($line, "</"); $data = substr($line, $pos1, $pos2 - $pos1); $info[5] = $data. "%"; } if(is_int(strpos($line, "<reh>"))) { $pos1 = strpos($line, ">") + 1; $pos2 = strpos($line, "</"); $data = substr($line, $pos1, $pos2 - $pos1); $info[6] = $data. "%"; $bend = true; } $line = ""; } else { $line .= $rwbuf; } } PrintInfo(); pid_close($pid); } if(USE_DISPLAY) { Init(); LoadFont("/gothic.ttf"); DrawImage(0, 0, "/image/bk1.png"); SetColor("FFFFFF"); if(USE_DISPLAY) { SetFontSize(72); DrawText(400, 240, "Searching...", 1); } } while(1) { dns_setup(0); dns_send_query($addr, RR_A); $rr = dns_loop(); if($rr === false) { sleep(1); } else if($rr == "") { sleep(10); break; } else { echo "Start update[". $rr. "]\r\n"; for($i = 0; $i < LOCAL_COUNT; $i++) { Update($rr, $port, $local[$i]); if(USE_DISPLAY == 0) { sleep(1); } } } } ?>