Kansai Electric Power usage meter (Denki-yohou) for GainSpan Wi-Fi http://mbed.org/users/okini3939/notebook/denki-yohou/

Dependencies:   mbed GSwifi (old)

Revision:
3:4bf32f60a770
Parent:
2:8646918ffff2
--- a/main.cpp	Wed Oct 17 13:43:52 2012 +0000
+++ b/main.cpp	Wed Oct 17 13:52:01 2012 +0000
@@ -1,16 +1,13 @@
 #include "mbed.h"
 #include "GSwifi.h"
 #include "7seg.h"
-#include "WDT.h"
 
-#define HTTP_HOST "www.jma.go.jp"
-#define HTTP_URI "/en/yoho/331.html"
+#define HTTP_HOST "www.kepco.co.jp"
+#define HTTP_URI "/yamasou/juyo1_kansai.csv"
+#define SSID "ssid"
+#define PASS "password"
 #define NTP_HOST "ntp1.sakura.ad.jp"
 
-#define SECURE GSSEC_WPA2_PSK
-#define SSID "SSID"
-#define PASS "password"
-
 #define VREF 3.3
 #define B 3435 // thermistor B
 #define T0 25.0
@@ -23,111 +20,74 @@
 DigitalOut gs_reset(p9), gs_wakeup(p10);
 DigitalOut led1(LED1);
 PwmOut led2(LED2), led3(LED3), led4(LED4);
-DigitalOut bz(p13);
 #elif defined(TARGET_LPC11U24)
 GSwifi gs(p9, p10, p21, p22); // TX, RX, CTS, RTS
-DigitalOut gs_reset(P1_14), gs_wakeup(P1_3);
-//DigitalOut gs_reset(p15), gs_wakeup(p14);
+//DigitalOut gs_reset(P1_14), gs_wakeup(P1_3);
+DigitalOut gs_reset(p15), gs_wakeup(p14);
 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
-DigitalOut bz(p13);
 #endif
 
-volatile int weather_count = 0, weather_data = 0, weather_flg = 0;
+volatile int denki_flg = 0;
+volatile int denki_capacity = 0;
+volatile int denki_usage = 0;
 volatile int year, month, day, hour, minute;
-volatile int remote_flg = 0;
 
 AnalogIn adtemp(p20);
-Watchdog wdt;
-
-extern "C" void HardFault_Handler() {
-    register unsigned int _msp __asm("msp");
-    printf("Hard Fault! address: %08x\n", *((unsigned int *)(_msp + 24)));
-    while(1); 
-}  
-
-int weather_info (char *buf) {
-    int i, w = 0;
-    char *tmp = NULL;
-
-    for (i = 0; i < strlen(buf); i ++) {
-        if (buf[i] == ',') {
-            buf[i] = 0;
-            tmp = &buf[i + 1];
-        } else
-        if (buf[i] == '<') {
-            buf[i] = 0;
-            break;
-        }
-    }
-
-    if (tmp) {
-    } else
-    if (strstr(buf, "PARTLY CLOUDY")) {
-        return 0x73; // WY
-    } else
-    if (strstr(buf, "MOSTLY CLOUDY")) {
-        return 0x37; // YW
-    }
-
-    if (strstr(buf, "CLEAR")) {
-        w = 0x70; // W
-    } else
-    if (strstr(buf, "CLOUDY")) {
-        w = 0x30; // Y
-    } else
-    if (strstr(buf, "RAIN") || strstr(buf, "SHOWERS")) {
-        w = 0x40; // B
-    } else
-    if (strstr(buf, "SNOW")) {
-        w = 0x60; // P
-    }
-
-    if (tmp) {
-        if (strstr(tmp, "CLEAR") || strstr(tmp, "PARTLY CLOUDY")) {
-            w |= 0x07; // W
-        } else
-        if (strstr(tmp, "CLOUDY")) {
-            w |= 0x03; // Y
-        } else
-        if (strstr(tmp, "RAIN") || strstr(tmp, "SHOWERS")) {
-            w |= 0x04; // B
-        } else
-        if (strstr(tmp, "SNOW")) {
-            w |= 0x06; // P
-        }
-    } else {
-        w |= (w >> 4);
-    }
-
-    return w;
-}
 
 void callback_http (int cid, int len) {
     static int n = 0;
-    static char buf[20], data[100];
+//    static char buf[1024], data[80];
+    static char buf[200], data[100];
     int i;
     int last = len;
 
-    while (last) {
-      len = gs.recv(cid, buf, last < sizeof(buf) ? last : sizeof(buf));
-      if (len == 0) break;
-      last = last - len;
+  while (last) {
+    len = gs.recv(cid, buf, last < sizeof(buf) ? last : sizeof(buf));
+    last = last - len;
+
+    for (i = 0; i < len; i ++) {
+        if (buf[i] == '\r') continue;
 
-      for (i = 0; i < len; i ++) {
-        if (buf[i] == '\r') continue;
+        if (denki_flg <= 1) {
+            // header
+            if (strncmp(buf, "200 OK", 6) == 0) {
+                i += 7;
+                denki_flg = 2;
+            }
+            continue;
+        }
 
         // body        
         if (buf[i] == '\n') {
-            // end of line
             data[n] = 0;
-            if (strstr(data, "class=\"info")) {
-                n = weather_info(&data[17]);
-                weather_flg ++;
-                if (n && weather_flg == 2) {
-                    weather_data = n;
+            switch (denki_flg) {
+            case 2:
+                // update
+                break;
+            case 4:
+                // capacity
+                if (data[0] >= '0' && data[0] <= '9') {
+                    denki_capacity = atoi(data);
                 }
+                break;
+            case 7:
+                // yosou1
+                break;
+            case 10:
+                // yosou2
+                break;
+            default:
+                // text
+                if (data[0] == 'D' && data[1] == 'A') {
+                    denki_flg = denki_flg < 100 ? 100 : 200;
+                }
+                break;
+            }
+            if (denki_flg > 200 && data[n - 1] >= '0' && data[n - 1] <= '9') {
+                sscanf(data, "%d/%d/%d,%d:%d,%d", &year, &month, &day, &hour, &minute, &denki_usage);
             }
             n = 0;
+            denki_flg ++;
         } else {
             // data
             if (n < sizeof(data) - 1) {
@@ -136,9 +96,9 @@
             }
         }
 
-      }
+    }
 
-    }
+  }
 }
 
 float get_temp () {
@@ -156,7 +116,8 @@
 }
 
 int main() {
-    int flg = 0, r;
+    int flg = 1, r;
+    float denki_percentage = 0;
     Host host, ntp;
     int count = 0;
     time_t time;
@@ -166,7 +127,6 @@
 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
     pc.baud(115200);
 #endif
-    bz = 1;
     gs_reset = 0;
     gs_wakeup = 1;
     wait_ms(100);
@@ -174,93 +134,64 @@
     wait_ms(500);
     led1 = 1;
 
-    startled();
-    writeled5(0, 18, 1); // i
-    writeled5(1, 23, 1); // n
-    writeled5(2, 18, 1); // i
-    writeled5(3, 29, 1); // t
-
-    if (gs.connect(SECURE, SSID, PASS, 1)) {
-        writeled5(0, 14, 1); // e
-        writeled5(1, 27, 1); // r
-        writeled5(2, 27, 1); // r
-        writeled5(3, 0, 0); //
-        writeled5(4, 2, 0); // R
-        writeled5(5, 2, 0); // R
-        for (;;) Sleep();
+    if (gs.connect(GSSEC_WPA2_PSK, SSID, PASS, 1)) {
+        return -1;
     }
 
-    wdt.init(8);
-
-    host.setName(HTTP_HOST);
     ntp.setName(NTP_HOST);
-
     gs.ntpdate(ntp, 0);
-    wait(10);
+    wait(15);
     gs.ntpdate(ntp, 3 * 60 * 60);
 
 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
     pc.printf("Denki-yohou: %s\r\n", HTTP_HOST);
 #endif
+    host.setName(HTTP_HOST);
 
+    startled();
     while(1) {
         gs.poll();
-        wdt.kick();
         
-        if (flg == 0) {
-          if (gs.isConnected()) {
-            flg = 180;
-            led2 = 1;
-            
-            // Weather
-            if (weather_count == 0) {
-                weather_flg = 0;
-                r = gs.httpGet(host, HTTP_URI, 0, &callback_http);
-                if (r >= 0) {
-                    led3 = 0;
-                } else {
-                    led3 = 1;
-                }
-                gs.poll();
-                weather_count = 20;
-            } else {
-                weather_count --;
+        if (flg || gs.getStatus() == GSSTAT_WAKEUP) {
+            led1 = 1;
+            if (gs.getStatus() == GSSTAT_WAKEUP) {
+                gs.wakeup();
             }
 
-            wdt.kick();
-            wait(3);
-            gs.poll();
-            wdt.kick();
+            r = gs.httpGet(host, HTTP_URI, 0, &callback_http);
+            if (r >= 0) {
+                denki_percentage = (float)denki_usage / (float)denki_capacity * 100.0;
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+                pc.printf("%04d-%02d-%02d %02d:%02d :", year, month, day, hour, minute);
+                pc.printf(" %d MW / %d MW", denki_usage * 10, denki_capacity * 10);
+                pc.printf(" (%0.1f %%)\r\n", denki_percentage);
+                led2 = denki_percentage >= 70 ? (denki_percentage >= 77.5 ? 1 : 0.5) : 0;
+                led3 = denki_percentage >= 85 ? (denki_percentage >= 90 ? 1 : 0.5) : 0;
+                led4 = denki_percentage >= 95 ? (denki_percentage >= 97 ? 1 : 0.5) : 0;
+#elif defined(TARGET_LPC11U24)
+                led2 = denki_percentage >= 70 ? 1 : 0;
+                led3 = denki_percentage >= 85 ? 1 : 0;
+                led4 = denki_percentage >= 95 ? 1 : 0;
+#endif
+            } else {
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+                pc.printf("http error\r\n");
+#endif
+            }
+            flg = 0;
             
-            // NTP date
-            r = gs.getTime();
-            if (r) {
-                time = r + (9 * 3600); // JST
-            }
+            wait(5);
+            time = gs.getTime() + (9 * 3600);
 
-            led2 = 0;
-            wdt.kick();
-          } else {
-            // re-connected
-            led2 = 1;
-            if (gs.connect(SECURE, SSID, PASS, 1)) {
-              led4 = 1;
-            } else {
-              led4 = 0;
-            }
-            led2 = 0;
-          }
+            gs.standby(180000);
         }
 
         if (count == 0) { // 0sec
             // time
             t = localtime(&time);
             writeled(t->tm_hour * 100 + t->tm_min, 2, 0x0f, 0x0f);
-            writeled5(4, weather_data & 0x07, 0);
-            writeled5(5, (weather_data >> 4) & 0x07, 0);
-            remote_flg = 0;
         } else
-        if (count > 0 /* && count < 6000 */ && !remote_flg) {
+        if (count > 0 && count < 4000) {
             // time blink
             if (count % 400 == 0) { // 0sec
                 writeled(t->tm_hour * 100 + t->tm_min, 2, 0x0f, 0x0f);
@@ -268,34 +199,23 @@
             if (count % 400 == 200) { // 0.1sec
                 writeled(t->tm_hour * 100 + t->tm_min, 0, 0x0f, 0x0f);
             }
-/*
+        } else
+        if (count == 4000) { // 10sec
+            // denki yohou
+            writeled(denki_percentage * 100, 2, 0x0e, 0);
         } else
         if (count == 6000) { // 15sec
             // temp.
-            writeled(get_temp() * 100, 2, 0x0e, 0);
-            writeled5(3, 12, 1);
-*/
+            writeled(get_temp() * 10, 3, 0x07, 0);
         }
 
-        if (count % 400 == 0) { // every 0sec
+        if (count % 400 == 0) { // 0sec
             led1 = 1;
-
-            // oclock buzzer
-            t = localtime(&time);
-            if (t->tm_min == 0) {
-                if (t->tm_sec == 0) {
-                    bz = 0;
-                } else
-                if (t->tm_sec == 1) {
-                    bz = 1;
-                }
-            }
         } else
-        if (count % 400 == 10) { // every 0.1sec
+        if (count % 400 == 40) { // 0.1sec
             led1 = 0;
 
             time ++;
-            if (flg) flg --;
         }
 
         count ++;