Kansai Electric Power usage meter (Denki-yohou)

Dependencies:   mbed mbed-rtos EthernetInterface

関西電力 でんき予報メーター

関電のでんき予報のリアルタイム値(使用電力状況データ CSV )をもとに、mbedのLEDを点灯させます。

そのほかの情報はこちらへ: http://mbed.org/users/okini3939/notebook/denki-yohou/

Import programdenki-yohou_b

Kansai Electric Power usage meter (Denki-yohou)

  • LED1 動作中表示
  • LED2 70%以上
  • LED3 85%以上
  • LED4 95%以上

新しい Ethernet Interface ライブラリと、独自の Tiny HTTP クライアント ライブラリを使っています。

CSVの解析処理をはしょって改行を頼りにしているので、CSVファイルの構造が変わるとうまく動かなくなります。
東電でも同様に使えると思います。

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Mon Jul 02 06:55:49 2012 +0000
Parent:
0:2a2f00cbc761
Child:
2:b50c974ed0d5
Commit message:
bugfix

Changed in this revision

TinyHTTP_b.cpp Show annotated file Show diff for this revision Revisions of this file
TinyHTTP_b.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/TinyHTTP_b.cpp	Mon Jul 02 05:54:16 2012 +0000
+++ b/TinyHTTP_b.cpp	Mon Jul 02 06:55:49 2012 +0000
@@ -82,6 +82,9 @@
     onHttpReceive = func;
     
     if (http->connect(host, port, HTTP_TIMEOUT)) goto exit;
+#ifdef DEBUG
+        printf("connected\r\n");
+#endif
 
     // send request
     if (method == METHOD_POST) {
@@ -108,7 +111,10 @@
         http->send(body, strlen(body), HTTP_TIMEOUT);
     }
 
-    // recv dummy
+#ifdef DEBUG
+        printf("wait for response\r\n");
+#endif
+    // recv
     for (;;) {
         i = http->receive(buf, sizeof(buf) - 1, HTTP_TIMEOUT);
         if (i == 0) break;
--- a/TinyHTTP_b.h	Mon Jul 02 05:54:16 2012 +0000
+++ b/TinyHTTP_b.h	Mon Jul 02 06:55:49 2012 +0000
@@ -15,7 +15,7 @@
 #include "EthernetInterface.h"
 
 #define HTTP_PORT 80
-#define HTTP_TIMEOUT 15000 // ms
+#define HTTP_TIMEOUT 3000 // ms
 
 #define METHOD_GET 0
 #define METHOD_POST 1
--- a/main.cpp	Mon Jul 02 05:54:16 2012 +0000
+++ b/main.cpp	Mon Jul 02 06:55:49 2012 +0000
@@ -11,31 +11,33 @@
 
 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
 
-int denki_usage = 0;
-struct tm denki_update;
-int year, month, day, hour, minute;
+volatile int denki_flg = 0;
+volatile int denki_usage = 0;
+volatile int year, month, day, hour, minute;
+
 
 void callback_denkiyohou (char *buf, int len) {
-    static int flg = 0, n = 0;
-    static char data[200];
+    static int n = 0;
+    static char data[100];
     int i;
 
     for (i = 0; i < len; i ++) {
         if (buf[i] == '\r') continue;
 
-        switch (flg) {
+        switch (denki_flg) {
         case 0:
             // header
             if (buf[i] == '\n') {
-                flg ++;
+                denki_flg ++;
             }
             break;
         case 1:
             // end of header
             if (buf[i] == '\n') {
-                flg ++;
+                n = 0;
+                denki_flg ++;
             } else {
-                flg = 0;
+                denki_flg = 0;
             }
             break;
         case 2:
@@ -44,8 +46,9 @@
                 data[n] = 0;
                 sscanf(data, "%d/%d/%d %d:%d", &year, &month, &day, &hour, &minute);
                 n = 0;
-                flg ++;
-            } else {
+                denki_flg ++;
+            } else
+            if (n < sizeof(data)) {
                 data[n] = buf[i];
                 n ++;
             }
@@ -54,8 +57,9 @@
             // peak
             if (buf[i] == '\n') {
                 n = 0;
-                flg ++;
-            } else {
+                denki_flg ++;
+            } else
+            if (n < sizeof(data)) {
                 data[n] = buf[i];
                 n ++;
             }
@@ -64,8 +68,9 @@
             // yosou
             if (buf[i] == '\n') {
                 n = 0;
-                flg ++;
-            } else {
+                denki_flg ++;
+            } else
+            if (n < sizeof(data)) {
                 data[n] = buf[i];
                 n ++;
             }
@@ -78,8 +83,9 @@
                     denki_usage = atoi(data);
                 }
                 n = 0;
-                flg ++;
-            } else {
+                denki_flg ++;
+            } else
+            if (n < sizeof(data)) {
                 data[n] = buf[i];
                 n ++;
             }
@@ -87,7 +93,7 @@
         default:
             // text
             if (buf[i] == '\n') {
-                flg ++;
+                denki_flg ++;
             }
             break;
         }
@@ -104,7 +110,7 @@
         return -1;
     }
     
-    pc.printf("Denki-yohou %s\r\n", HTTP_HOST);
+    pc.printf("Denki-yohou: %s\r\n", HTTP_HOST);
 
     timer.start();
     while(1) {
@@ -112,6 +118,7 @@
     
         if (flg || timer.read() >= 300) {
             timer.reset();
+            denki_flg = 0;
             r = httpRequest(METHOD_GET, HTTP_HOST, 80, HTTP_URI, NULL, NULL, &callback_denkiyohou);
             if (r == 0) {
                 pc.printf("%04d-%02d-%02d %02d:%02d : ", year, month, day, hour, minute);
@@ -120,6 +127,8 @@
                 led2 = denki_usage >= 70 ? 1 : 0;
                 led3 = denki_usage >= 85 ? 1 : 0;
                 led4 = denki_usage >= 95 ? 1 : 0;
+            } else {
+                pc.printf("http error\r\n");
             }
             flg = 0;
         }