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

Dependencies:   mbed GSwifi (old)

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Wed Oct 17 13:52:01 2012 +0000
Parent:
2:8646918ffff2
Commit message:
fix

Changed in this revision

7seg.cpp Show annotated file Show diff for this revision Revisions of this file
7seg.h Show annotated file Show diff for this revision Revisions of this file
GSwifi.lib Show annotated file Show diff for this revision Revisions of this file
WDT.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 8646918ffff2 -r 4bf32f60a770 7seg.cpp
--- a/7seg.cpp	Wed Oct 17 13:43:52 2012 +0000
+++ b/7seg.cpp	Wed Oct 17 13:52:01 2012 +0000
@@ -1,19 +1,12 @@
 #include "mbed.h"
 #include "7seg.h"
 
-#define LED_NUM 6
-#define DIGITS_NUM 37
-
-const int digits[DIGITS_NUM] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, // 0-9
-                                0x77, 0x7c, 0x58, 0x5e, 0x79, 0x71, 0x3d, 0x74, 0x04, 0x1e, // a-j
-                                0x76, 0x38, 0x54, 0x54, 0x5c, 0x73, 0x67, 0x50, 0x6d, 0x78, // k-t
-                                0x1c, 0x1c, 0x1c, 0x76, 0x6e, 0x5b, 0x40}; // u-z,-
-
 static DigitalOut led_k[8] = {p34, p36, p26, p24, p23, p35, p30, p25};
-static DigitalOut led_a[LED_NUM] = {p16, p17, p18, p19, p15, p14};
+static DigitalOut led_a[4] = {p16, p17, p18, p19};
 static Ticker tick;
 
-static int ledbuf[LED_NUM];
+static int ledbuf[4] = {-1, -1, -1, -1};
+const int digits[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
 
 void leddisp () {
     static int n = 0;
@@ -26,37 +19,40 @@
     led_a[n] = 1; // common off
 
     n ++;
-    if (n >= LED_NUM) n = 0;
+    if (n >= 4) n = 0;
 
     // led on
-    bit = 1;
-    for (i = 0; i < 8; i ++) {
-        led_k[i] = ledbuf[n] & bit ? 0 : 1;
-        bit = bit << 1;
+    if (ledbuf[n] >= 0) {
+        bit = 1;
+        for (i = 0; i < 7; i ++) {
+            led_k[i] = digits[ledbuf[n] & 0x7f] & bit ? 0 : 1;
+            bit = bit << 1;
+        }
+        led_k[7] = ledbuf[n] & 0x80 ? 0 : 1;
     }
     led_a[n] = 0; // common on
 }
 
 void writeled (int num, int dot, int mask, int zero) {
     if (((num >= 1000) || (zero & 0x08)) && (mask & 0x08)) {
-        ledbuf[0] = digits[num / 1000 % 10];
+        ledbuf[0] = num / 1000 % 10;
     } else {
-        ledbuf[0] = 0;
+        ledbuf[0] = -1;
     }
     if (((num >= 100) || (zero & 0x04)) && (mask & 0x04)) {
-        ledbuf[1] = digits[(num / 100) % 10];
+        ledbuf[1] = (num / 100) % 10;
     } else {
-        ledbuf[1] = 0;
+        ledbuf[1] = -1;
     }
     if (((num >= 10) || (zero & 0x02)) && (mask & 0x02)) {
-        ledbuf[2] = digits[(num / 10) % 10];
+        ledbuf[2] = (num / 10) % 10;
     } else {
-        ledbuf[2] = 0;
+        ledbuf[2] = -1;
     }
     if (mask & 0x01) {
-        ledbuf[3] = digits[num % 10];
+        ledbuf[3] = num % 10;
     } else {
-        ledbuf[3] = 0;
+        ledbuf[3] = -1;
     }
 
     if (dot >= 1 && dot <= 4) {
@@ -64,20 +60,11 @@
     }
 }
 
-void writeled5 (int n, int d, int f) {
-    if (f) {
-        ledbuf[n] = digits[d];
-    } else {
-        ledbuf[n] = d;
-    }
-}
-
 void initled () {
     int i;
     
-    for (i = 0; i < LED_NUM; i ++) {
+    for (i = 0; i < 4; i ++) {
         led_a[i] = 1;
-        ledbuf[i] = 0;
     }
     for (i = 0; i < 8; i ++) {
         led_k[i] = 1;
diff -r 8646918ffff2 -r 4bf32f60a770 7seg.h
--- a/7seg.h	Wed Oct 17 13:43:52 2012 +0000
+++ b/7seg.h	Wed Oct 17 13:52:01 2012 +0000
@@ -1,5 +1,4 @@
 
 void writeled (int num, int dot, int mask, int zero);
-void writeled5 (int n, int d, int f);
 void initled ();
 void startled ();
diff -r 8646918ffff2 -r 4bf32f60a770 GSwifi.lib
--- a/GSwifi.lib	Wed Oct 17 13:43:52 2012 +0000
+++ b/GSwifi.lib	Wed Oct 17 13:52:01 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/gsfan/code/GSwifi/#5febfc399099
+http://mbed.org/users/gsfan/code/GSwifi/#983f4e832a3e
diff -r 8646918ffff2 -r 4bf32f60a770 WDT.lib
--- a/WDT.lib	Wed Oct 17 13:43:52 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/okini3939/code/WDT/#f7baa89f84bc
diff -r 8646918ffff2 -r 4bf32f60a770 main.cpp
--- 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 ++;