http://mbed.org/users/okini3939/notebook/tsutenkaku/

Dependencies:   GSwifi (old) WDT mbed

Fork of GSwifi_denki-yohou by Suga koubou

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Wed Oct 17 13:43:52 2012 +0000
Parent:
1:beff52210994
Commit message:
1st build

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 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/7seg.cpp	Wed Sep 05 00:51:19 2012 +0000
+++ b/7seg.cpp	Wed Oct 17 13:43:52 2012 +0000
@@ -1,12 +1,19 @@
 #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[4] = {p16, p17, p18, p19};
+static DigitalOut led_a[LED_NUM] = {p16, p17, p18, p19, p15, p14};
 static Ticker tick;
 
-static int ledbuf[4] = {-1, -1, -1, -1};
-const int digits[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
+static int ledbuf[LED_NUM];
 
 void leddisp () {
     static int n = 0;
@@ -19,40 +26,37 @@
     led_a[n] = 1; // common off
 
     n ++;
-    if (n >= 4) n = 0;
+    if (n >= LED_NUM) n = 0;
 
     // led on
-    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;
+    bit = 1;
+    for (i = 0; i < 8; i ++) {
+        led_k[i] = ledbuf[n] & bit ? 0 : 1;
+        bit = bit << 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] = num / 1000 % 10;
+        ledbuf[0] = digits[num / 1000 % 10];
     } else {
-        ledbuf[0] = -1;
+        ledbuf[0] = 0;
     }
     if (((num >= 100) || (zero & 0x04)) && (mask & 0x04)) {
-        ledbuf[1] = (num / 100) % 10;
+        ledbuf[1] = digits[(num / 100) % 10];
     } else {
-        ledbuf[1] = -1;
+        ledbuf[1] = 0;
     }
     if (((num >= 10) || (zero & 0x02)) && (mask & 0x02)) {
-        ledbuf[2] = (num / 10) % 10;
+        ledbuf[2] = digits[(num / 10) % 10];
     } else {
-        ledbuf[2] = -1;
+        ledbuf[2] = 0;
     }
     if (mask & 0x01) {
-        ledbuf[3] = num % 10;
+        ledbuf[3] = digits[num % 10];
     } else {
-        ledbuf[3] = -1;
+        ledbuf[3] = 0;
     }
 
     if (dot >= 1 && dot <= 4) {
@@ -60,11 +64,20 @@
     }
 }
 
+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 < 4; i ++) {
+    for (i = 0; i < LED_NUM; i ++) {
         led_a[i] = 1;
+        ledbuf[i] = 0;
     }
     for (i = 0; i < 8; i ++) {
         led_k[i] = 1;
--- a/7seg.h	Wed Sep 05 00:51:19 2012 +0000
+++ b/7seg.h	Wed Oct 17 13:43:52 2012 +0000
@@ -1,4 +1,5 @@
 
 void writeled (int num, int dot, int mask, int zero);
+void writeled5 (int n, int d, int f);
 void initled ();
 void startled ();
--- a/GSwifi.lib	Wed Sep 05 00:51:19 2012 +0000
+++ b/GSwifi.lib	Wed Oct 17 13:43:52 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/gsfan/code/GSwifi/#983f4e832a3e
+http://mbed.org/users/gsfan/code/GSwifi/#5febfc399099
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WDT.lib	Wed Oct 17 13:43:52 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/okini3939/code/WDT/#f7baa89f84bc
--- a/main.cpp	Wed Sep 05 00:51:19 2012 +0000
+++ b/main.cpp	Wed Oct 17 13:43:52 2012 +0000
@@ -1,13 +1,16 @@
 #include "mbed.h"
 #include "GSwifi.h"
 #include "7seg.h"
+#include "WDT.h"
 
-#define HTTP_HOST "www.kepco.co.jp"
-#define HTTP_URI "/yamasou/juyo1_kansai.csv"
-#define SSID "ROBOBA"
-#define PASS "roboba1234567"
+#define HTTP_HOST "www.jma.go.jp"
+#define HTTP_URI "/en/yoho/331.html"
 #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
@@ -20,74 +23,111 @@
 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 denki_flg = 0;
-volatile int denki_capacity = 0;
-volatile int denki_usage = 0;
+volatile int weather_count = 0, weather_data = 0, weather_flg = 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[1024], data[80];
-    static char buf[200], data[100];
+    static char buf[20], data[100];
     int i;
     int 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;
+    while (last) {
+      len = gs.recv(cid, buf, last < sizeof(buf) ? last : sizeof(buf));
+      if (len == 0) break;
+      last = last - len;
 
-        if (denki_flg <= 1) {
-            // header
-            if (strncmp(buf, "200 OK", 6) == 0) {
-                i += 7;
-                denki_flg = 2;
-            }
-            continue;
-        }
+      for (i = 0; i < len; i ++) {
+        if (buf[i] == '\r') continue;
 
         // body        
         if (buf[i] == '\n') {
+            // end of line
             data[n] = 0;
-            switch (denki_flg) {
-            case 2:
-                // update
-                break;
-            case 4:
-                // capacity
-                if (data[0] >= '0' && data[0] <= '9') {
-                    denki_capacity = atoi(data);
+            if (strstr(data, "class=\"info")) {
+                n = weather_info(&data[17]);
+                weather_flg ++;
+                if (n && weather_flg == 2) {
+                    weather_data = n;
                 }
-                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) {
@@ -96,9 +136,9 @@
             }
         }
 
-    }
+      }
 
-  }
+    }
 }
 
 float get_temp () {
@@ -116,8 +156,7 @@
 }
 
 int main() {
-    int flg = 1, r;
-    float denki_percentage = 0;
+    int flg = 0, r;
     Host host, ntp;
     int count = 0;
     time_t time;
@@ -127,6 +166,7 @@
 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
     pc.baud(115200);
 #endif
+    bz = 1;
     gs_reset = 0;
     gs_wakeup = 1;
     wait_ms(100);
@@ -134,64 +174,93 @@
     wait_ms(500);
     led1 = 1;
 
-    if (gs.connect(GSSEC_WPA2_PSK, SSID, PASS, 1)) {
-        return -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();
     }
 
+    wdt.init(8);
+
+    host.setName(HTTP_HOST);
     ntp.setName(NTP_HOST);
+
     gs.ntpdate(ntp, 0);
-    wait(15);
+    wait(10);
     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 || gs.getStatus() == GSSTAT_WAKEUP) {
-            led1 = 1;
-            if (gs.getStatus() == GSSTAT_WAKEUP) {
-                gs.wakeup();
+        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 --;
             }
 
-            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
+            wdt.kick();
+            wait(3);
+            gs.poll();
+            wdt.kick();
+            
+            // NTP date
+            r = gs.getTime();
+            if (r) {
+                time = r + (9 * 3600); // JST
+            }
+
+            led2 = 0;
+            wdt.kick();
+          } else {
+            // re-connected
+            led2 = 1;
+            if (gs.connect(SECURE, SSID, PASS, 1)) {
+              led4 = 1;
             } else {
-#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
-                pc.printf("http error\r\n");
-#endif
+              led4 = 0;
             }
-            flg = 0;
-            
-            wait(5);
-            time = gs.getTime() + (9 * 3600);
-
-            gs.standby(180000);
+            led2 = 0;
+          }
         }
 
         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 < 4000) {
+        if (count > 0 /* && count < 6000 */ && !remote_flg) {
             // time blink
             if (count % 400 == 0) { // 0sec
                 writeled(t->tm_hour * 100 + t->tm_min, 2, 0x0f, 0x0f);
@@ -199,23 +268,34 @@
             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);
+            writeled(get_temp() * 100, 2, 0x0e, 0);
+            writeled5(3, 12, 1);
+*/
         }
 
-        if (count % 400 == 0) { // 0sec
+        if (count % 400 == 0) { // every 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 == 40) { // 0.1sec
+        if (count % 400 == 10) { // every 0.1sec
             led1 = 0;
 
             time ++;
+            if (flg) flg --;
         }
 
         count ++;