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ファイルの構造が変わるとうまく動かなくなります。
東電でも同様に使えると思います。
main.cpp@3:ef5af1e8558d, 2012-07-03 (annotated)
- Committer:
- okini3939
- Date:
- Tue Jul 03 00:32:27 2012 +0000
- Revision:
- 3:ef5af1e8558d
- Parent:
- 2:b50c974ed0d5
bugfix and pwm drive to led
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 3:ef5af1e8558d | 1 | #include "mbed.h" |
okini3939 | 3:ef5af1e8558d | 2 | #include "EthernetInterface.h" |
okini3939 | 3:ef5af1e8558d | 3 | #include "TinyHTTP_b.h" |
okini3939 | 3:ef5af1e8558d | 4 | |
okini3939 | 3:ef5af1e8558d | 5 | // Kansai Electric Power |
okini3939 | 3:ef5af1e8558d | 6 | #define HTTP_HOST "www.kepco.co.jp" |
okini3939 | 3:ef5af1e8558d | 7 | #define HTTP_URI "/yamasou/juyo1_kansai.csv" |
okini3939 | 3:ef5af1e8558d | 8 | |
okini3939 | 3:ef5af1e8558d | 9 | Serial pc(USBTX, USBRX); |
okini3939 | 3:ef5af1e8558d | 10 | EthernetInterface eth; |
okini3939 | 3:ef5af1e8558d | 11 | |
okini3939 | 3:ef5af1e8558d | 12 | DigitalOut led1(LED1); |
okini3939 | 3:ef5af1e8558d | 13 | PwmOut led2(LED2), led3(LED3), led4(LED4); |
okini3939 | 3:ef5af1e8558d | 14 | |
okini3939 | 3:ef5af1e8558d | 15 | volatile int denki_flg = 0; |
okini3939 | 3:ef5af1e8558d | 16 | volatile int denki_capacity = 0; |
okini3939 | 3:ef5af1e8558d | 17 | volatile int denki_usage = 0; |
okini3939 | 3:ef5af1e8558d | 18 | volatile int year, month, day, hour, minute; |
okini3939 | 3:ef5af1e8558d | 19 | |
okini3939 | 3:ef5af1e8558d | 20 | |
okini3939 | 3:ef5af1e8558d | 21 | void callback_denkiyohou (char *buf, int len) { |
okini3939 | 3:ef5af1e8558d | 22 | static int n = 0; |
okini3939 | 3:ef5af1e8558d | 23 | static char data[100]; |
okini3939 | 3:ef5af1e8558d | 24 | int i; |
okini3939 | 3:ef5af1e8558d | 25 | |
okini3939 | 3:ef5af1e8558d | 26 | for (i = 0; i < len; i ++) { |
okini3939 | 3:ef5af1e8558d | 27 | if (buf[i] == '\r') continue; |
okini3939 | 3:ef5af1e8558d | 28 | |
okini3939 | 3:ef5af1e8558d | 29 | if (denki_flg <= 1) { |
okini3939 | 3:ef5af1e8558d | 30 | // header |
okini3939 | 3:ef5af1e8558d | 31 | if (buf[i] == '\n') { |
okini3939 | 3:ef5af1e8558d | 32 | n = 0; |
okini3939 | 3:ef5af1e8558d | 33 | denki_flg ++; |
okini3939 | 3:ef5af1e8558d | 34 | } else { |
okini3939 | 3:ef5af1e8558d | 35 | denki_flg = 0; |
okini3939 | 3:ef5af1e8558d | 36 | } |
okini3939 | 3:ef5af1e8558d | 37 | continue; |
okini3939 | 3:ef5af1e8558d | 38 | } |
okini3939 | 3:ef5af1e8558d | 39 | |
okini3939 | 3:ef5af1e8558d | 40 | // body |
okini3939 | 3:ef5af1e8558d | 41 | if (buf[i] == '\n') { |
okini3939 | 3:ef5af1e8558d | 42 | data[n] = 0; |
okini3939 | 3:ef5af1e8558d | 43 | switch (denki_flg) { |
okini3939 | 3:ef5af1e8558d | 44 | case 2: |
okini3939 | 3:ef5af1e8558d | 45 | // update |
okini3939 | 3:ef5af1e8558d | 46 | break; |
okini3939 | 3:ef5af1e8558d | 47 | case 4: |
okini3939 | 3:ef5af1e8558d | 48 | // capacity |
okini3939 | 3:ef5af1e8558d | 49 | if (data[0] >= '0' && data[0] <= '9') { |
okini3939 | 3:ef5af1e8558d | 50 | denki_capacity = atoi(data); |
okini3939 | 3:ef5af1e8558d | 51 | } |
okini3939 | 3:ef5af1e8558d | 52 | break; |
okini3939 | 3:ef5af1e8558d | 53 | case 7: |
okini3939 | 3:ef5af1e8558d | 54 | // yosou1 |
okini3939 | 3:ef5af1e8558d | 55 | break; |
okini3939 | 3:ef5af1e8558d | 56 | case 10: |
okini3939 | 3:ef5af1e8558d | 57 | // yosou2 |
okini3939 | 3:ef5af1e8558d | 58 | break; |
okini3939 | 3:ef5af1e8558d | 59 | default: |
okini3939 | 3:ef5af1e8558d | 60 | // text |
okini3939 | 3:ef5af1e8558d | 61 | if (data[0] == 'D' && data[1] == 'A') { |
okini3939 | 3:ef5af1e8558d | 62 | denki_flg = denki_flg < 100 ? 100 : 200; |
okini3939 | 3:ef5af1e8558d | 63 | } |
okini3939 | 3:ef5af1e8558d | 64 | break; |
okini3939 | 3:ef5af1e8558d | 65 | } |
okini3939 | 3:ef5af1e8558d | 66 | if (denki_flg > 200 && data[n - 1] >= '0' && data[n - 1] <= '9') { |
okini3939 | 3:ef5af1e8558d | 67 | sscanf(data, "%d/%d/%d,%d:%d,%d", &year, &month, &day, &hour, &minute, &denki_usage); |
okini3939 | 3:ef5af1e8558d | 68 | } |
okini3939 | 3:ef5af1e8558d | 69 | n = 0; |
okini3939 | 3:ef5af1e8558d | 70 | denki_flg ++; |
okini3939 | 3:ef5af1e8558d | 71 | } else { |
okini3939 | 3:ef5af1e8558d | 72 | // data |
okini3939 | 3:ef5af1e8558d | 73 | if (n < sizeof(data) - 1) { |
okini3939 | 3:ef5af1e8558d | 74 | data[n] = buf[i]; |
okini3939 | 3:ef5af1e8558d | 75 | n ++; |
okini3939 | 3:ef5af1e8558d | 76 | } |
okini3939 | 3:ef5af1e8558d | 77 | } |
okini3939 | 3:ef5af1e8558d | 78 | |
okini3939 | 3:ef5af1e8558d | 79 | } |
okini3939 | 3:ef5af1e8558d | 80 | } |
okini3939 | 3:ef5af1e8558d | 81 | |
okini3939 | 3:ef5af1e8558d | 82 | int main() { |
okini3939 | 3:ef5af1e8558d | 83 | Timer timer; |
okini3939 | 3:ef5af1e8558d | 84 | int flg = 1, r; |
okini3939 | 3:ef5af1e8558d | 85 | float denki_percentage = 0; |
okini3939 | 3:ef5af1e8558d | 86 | |
okini3939 | 3:ef5af1e8558d | 87 | pc.baud(115200); |
okini3939 | 3:ef5af1e8558d | 88 | eth.init(); //Use DHCP |
okini3939 | 3:ef5af1e8558d | 89 | if (eth.connect()) { |
okini3939 | 3:ef5af1e8558d | 90 | return -1; |
okini3939 | 3:ef5af1e8558d | 91 | } |
okini3939 | 3:ef5af1e8558d | 92 | |
okini3939 | 3:ef5af1e8558d | 93 | pc.printf("Denki-yohou: %s\r\n", HTTP_HOST); |
okini3939 | 3:ef5af1e8558d | 94 | |
okini3939 | 3:ef5af1e8558d | 95 | timer.start(); |
okini3939 | 3:ef5af1e8558d | 96 | while(1) { |
okini3939 | 3:ef5af1e8558d | 97 | led1 = 1; |
okini3939 | 3:ef5af1e8558d | 98 | |
okini3939 | 3:ef5af1e8558d | 99 | if (flg || timer.read() >= 180) { // 3min |
okini3939 | 3:ef5af1e8558d | 100 | timer.reset(); |
okini3939 | 3:ef5af1e8558d | 101 | denki_flg = 0; |
okini3939 | 3:ef5af1e8558d | 102 | r = httpRequest(METHOD_GET, HTTP_HOST, 80, HTTP_URI, NULL, NULL, &callback_denkiyohou); |
okini3939 | 3:ef5af1e8558d | 103 | if (r == 0) { |
okini3939 | 3:ef5af1e8558d | 104 | denki_percentage = (float)denki_usage / (float)denki_capacity * 100.0; |
okini3939 | 3:ef5af1e8558d | 105 | |
okini3939 | 3:ef5af1e8558d | 106 | pc.printf("%04d-%02d-%02d %02d:%02d :", year, month, day, hour, minute); |
okini3939 | 3:ef5af1e8558d | 107 | pc.printf(" %d0 MW / %d0 MW", denki_usage, denki_capacity); |
okini3939 | 3:ef5af1e8558d | 108 | pc.printf(" (%0.1f %%)\r\n", denki_percentage); |
okini3939 | 3:ef5af1e8558d | 109 | |
okini3939 | 3:ef5af1e8558d | 110 | led2 = denki_percentage >= 70 ? (denki_percentage >= 77.5 ? 1 : 0.5) : 0; |
okini3939 | 3:ef5af1e8558d | 111 | led3 = denki_percentage >= 85 ? (denki_percentage >= 90 ? 1 : 0.5) : 0; |
okini3939 | 3:ef5af1e8558d | 112 | led4 = denki_percentage >= 95 ? (denki_percentage >= 97 ? 1 : 0.5) : 0; |
okini3939 | 3:ef5af1e8558d | 113 | } else { |
okini3939 | 3:ef5af1e8558d | 114 | pc.printf("http error\r\n"); |
okini3939 | 3:ef5af1e8558d | 115 | } |
okini3939 | 3:ef5af1e8558d | 116 | flg = 0; |
okini3939 | 3:ef5af1e8558d | 117 | } |
okini3939 | 3:ef5af1e8558d | 118 | |
okini3939 | 3:ef5af1e8558d | 119 | wait(0.1); |
okini3939 | 3:ef5af1e8558d | 120 | led1 = 0; |
okini3939 | 3:ef5af1e8558d | 121 | wait(0.9); |
okini3939 | 3:ef5af1e8558d | 122 | } |
okini3939 | 3:ef5af1e8558d | 123 | } |