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 Sep 05 00:51:19 2012 +0000
Parent:
0:d8a2cab7091c
Child:
2:8646918ffff2
Commit message:
add ntp clock

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
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/7seg.cpp	Tue Jul 10 05:28:10 2012 +0000
+++ b/7seg.cpp	Wed Sep 05 00:51:19 2012 +0000
@@ -12,14 +12,16 @@
     static int n = 0;
     int i, bit;
     
+    // led off
     for (i = 0; i < 8; i ++) {
         led_k[i] = 1;
     }
-    led_a[n] = 1;
+    led_a[n] = 1; // common off
 
     n ++;
     if (n >= 4) n = 0;
 
+    // led on
     if (ledbuf[n] >= 0) {
         bit = 1;
         for (i = 0; i < 7; i ++) {
@@ -28,26 +30,26 @@
         }
         led_k[7] = ledbuf[n] & 0x80 ? 0 : 1;
     }
-    led_a[n] = 0;
+    led_a[n] = 0; // common on
 }
 
-void writeled (int num, int dot, int place) {
-    if (num >= 1000 && place & 0x08) {
-        ledbuf[0] = num / 1000;
+void writeled (int num, int dot, int mask, int zero) {
+    if (((num >= 1000) || (zero & 0x08)) && (mask & 0x08)) {
+        ledbuf[0] = num / 1000 % 10;
     } else {
         ledbuf[0] = -1;
     }
-    if (num >= 100 && place & 0x04) {
+    if (((num >= 100) || (zero & 0x04)) && (mask & 0x04)) {
         ledbuf[1] = (num / 100) % 10;
     } else {
         ledbuf[1] = -1;
     }
-    if (num >= 10 && place & 0x02) {
+    if (((num >= 10) || (zero & 0x02)) && (mask & 0x02)) {
         ledbuf[2] = (num / 10) % 10;
     } else {
         ledbuf[2] = -1;
     }
-    if (place & 0x01) {
+    if (mask & 0x01) {
         ledbuf[3] = num % 10;
     } else {
         ledbuf[3] = -1;
--- a/7seg.h	Tue Jul 10 05:28:10 2012 +0000
+++ b/7seg.h	Wed Sep 05 00:51:19 2012 +0000
@@ -1,4 +1,4 @@
 
-void writeled (int num, int dot, int place);
+void writeled (int num, int dot, int mask, int zero);
 void initled ();
 void startled ();
--- a/GSwifi.lib	Tue Jul 10 05:28:10 2012 +0000
+++ b/GSwifi.lib	Wed Sep 05 00:51:19 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/gsfan/code/GSwifi/#2f6062c6d018
+http://mbed.org/users/gsfan/code/GSwifi/#983f4e832a3e
--- a/main.cpp	Tue Jul 10 05:28:10 2012 +0000
+++ b/main.cpp	Wed Sep 05 00:51:19 2012 +0000
@@ -4,8 +4,9 @@
 
 #define HTTP_HOST "www.kepco.co.jp"
 #define HTTP_URI "/yamasou/juyo1_kansai.csv"
-#define SSID "SSID"
-#define PASS "pass1234567"
+#define SSID "ROBOBA"
+#define PASS "roboba1234567"
+#define NTP_HOST "ntp1.sakura.ad.jp"
 
 #define VREF 3.3
 #define B 3435 // thermistor B
@@ -21,8 +22,8 @@
 PwmOut led2(LED2), led3(LED3), led4(LED4);
 #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(p27), gs_wakeup(p28);
+//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);
 #endif
 
@@ -33,7 +34,7 @@
 
 AnalogIn adtemp(p20);
 
-void callback_http (int cid, int acid, int len) {
+void callback_http (int cid, int len) {
     static int n = 0;
 //    static char buf[1024], data[80];
     static char buf[200], data[100];
@@ -101,7 +102,7 @@
 }
 
 float get_temp () {
-    float ad, v, r, t;
+    float v, r, t;
 
     v = adtemp * VREF;
     r = v / ((VREF - v) / RU);
@@ -117,8 +118,10 @@
 int main() {
     int flg = 1, r;
     float denki_percentage = 0;
-    Host host;
+    Host host, ntp;
     int count = 0;
+    time_t time;
+    struct tm *t;
 
     initled();
 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
@@ -135,6 +138,11 @@
         return -1;
     }
 
+    ntp.setName(NTP_HOST);
+    gs.ntpdate(ntp, 0);
+    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
@@ -149,7 +157,7 @@
             if (gs.getStatus() == GSSTAT_WAKEUP) {
                 gs.wakeup();
             }
-            
+
             r = gs.httpGet(host, HTTP_URI, 0, &callback_http);
             if (r >= 0) {
                 denki_percentage = (float)denki_usage / (float)denki_capacity * 100.0;
@@ -172,17 +180,33 @@
             }
             flg = 0;
             
+            wait(5);
+            time = gs.getTime() + (9 * 3600);
+
             gs.standby(180000);
         }
 
-        count ++;
-        if (count > 4000) count = 0; // 10sec
-
         if (count == 0) { // 0sec
-            writeled(denki_percentage * 100, 2, 0x0e);
+            // time
+            t = localtime(&time);
+            writeled(t->tm_hour * 100 + t->tm_min, 2, 0x0f, 0x0f);
         } else
-        if (count == 2000) { // 5sec
-            writeled(get_temp() * 10, 3, 0x07);
+        if (count > 0 && count < 4000) {
+            // time blink
+            if (count % 400 == 0) { // 0sec
+                writeled(t->tm_hour * 100 + t->tm_min, 2, 0x0f, 0x0f);
+            } else
+            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() * 10, 3, 0x07, 0);
         }
 
         if (count % 400 == 0) { // 0sec
@@ -190,8 +214,13 @@
         } else
         if (count % 400 == 40) { // 0.1sec
             led1 = 0;
+
+            time ++;
         }
 
+        count ++;
+        if (count >= 8000) count = 0; // 20sec
+
         Sleep();
     }
 }
--- a/mbed.bld	Tue Jul 10 05:28:10 2012 +0000
+++ b/mbed.bld	Wed Sep 05 00:51:19 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/737756e0b479
+http://mbed.org/users/mbed_official/code/mbed/builds/737756e0b479
\ No newline at end of file