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ファイルの構造が変わるとうまく動かなくなります。
東電でも同様に使えると思います。
Revision 0:2a2f00cbc761, committed 2012-07-02
- Comitter:
- okini3939
- Date:
- Mon Jul 02 05:54:16 2012 +0000
- Child:
- 1:10bd46941b8b
- Commit message:
- Kansai Electric Power usage meter (Denki-yohou)
;
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Mon Jul 02 05:54:16 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/EthernetInterface/#c50597f8d7a2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TinyHTTP_b.cpp Mon Jul 02 05:54:16 2012 +0000 @@ -0,0 +1,128 @@ +/* + * mbed Tiny HTTP Client for Ethernet Interface Library + * Copyright (c) 2012 Hiroshi Suga + * Released under the MIT License: http://mbed.org/license/mit + */ + +/** @file + * @brief Tiny HTTP Client + */ + +#include "mbed.h" +#include "EthernetInterface.h" +#include "TinyHTTP_b.h" +#include <ctype.h> + +//#define DEBUG + +static TCPSocket *http; +static onHttpReceiveFunc onHttpReceive; + +// Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) +int base64enc(const char *input, unsigned int length, char *output, int len) { + static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + unsigned int c, c1, c2, c3; + + if (len < ((((length-1)/3)+1)<<2)) return -1; + for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) { + c1 = ((((unsigned char)*((unsigned char *)&input[i])))); + c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0; + c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0; + + c = ((c1 & 0xFC) >> 2); + output[j+0] = base64[c]; + c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4); + output[j+1] = base64[c]; + c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6); + output[j+2] = (length>i+1)?base64[c]:'='; + c = (c3 & 0x3F); + output[j+3] = (length>i+2)?base64[c]:'='; + } + output[(((length-1)/3)+1)<<2] = '\0'; + return 0; +} + +// Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) +int urlencode(char *str, char *buf, int len) { + static const char to_hex[] = "0123456789ABCDEF"; +// char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf; + char *pstr = str, *pbuf = buf; + + if (len < (strlen(str) * 3 + 1)) return -1; + while (*pstr) { + if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') { + *pbuf++ = *pstr; + } else if (*pstr == ' ') { + *pbuf++ = '+'; + } else { + *pbuf++ = '%'; + *pbuf++ = to_hex[(*pstr >> 4) & 0x0f]; + *pbuf++ = to_hex[*pstr & 0x0f]; + } + pstr++; + } + *pbuf = '\0'; + return 0; +} + +void createauth (char *user, char *pwd, char *buf, int len) { + char tmp[80]; + + strncpy(buf, "Authorization: Basic ", len); + snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd); + base64enc(tmp, strlen(tmp), &buf[strlen(buf)], len - strlen(buf)); + strncat(buf, "\r\n", len - strlen(buf)); +} + +int httpRequest (int method, char *host, int port, char *uri, char *head, char *body, onHttpReceiveFunc func) { + char buf[1500]; + int i, ret = -1; + + http = new TCPSocket; + onHttpReceive = func; + + if (http->connect(host, port, HTTP_TIMEOUT)) goto exit; + + // send request + if (method == METHOD_POST) { + http->send("POST ", 5, HTTP_TIMEOUT); + } else { + http->send("GET ", 4, HTTP_TIMEOUT); + } + http->send(uri, strlen(uri), HTTP_TIMEOUT); + http->send(" HTTP/1.1\r\nHost: ", 17, HTTP_TIMEOUT); + http->send(host, strlen(host), HTTP_TIMEOUT); + http->send("\r\n", 2, HTTP_TIMEOUT); + http->send("Connection: close\r\n", 19, HTTP_TIMEOUT); + if (head) { + http->send(head, strlen(head), HTTP_TIMEOUT); + } + if (method == METHOD_POST) { + sprintf(buf, "Content-Length: %d\r\n", strlen(body)); + http->send(buf, strlen(buf), HTTP_TIMEOUT); + } + http->send("\r\n", 2, HTTP_TIMEOUT); + + // post method + if (method == METHOD_POST && body) { + http->send(body, strlen(body), HTTP_TIMEOUT); + } + + // recv dummy + for (;;) { + i = http->receive(buf, sizeof(buf) - 1, HTTP_TIMEOUT); + if (i == 0) break; + + if (onHttpReceive != NULL) onHttpReceive(buf, i); +#ifdef DEBUG + printf(buf); +#endif + } + ret = 0; + +exit: + http->close(); + delete http; + + return ret; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TinyHTTP_b.h Mon Jul 02 05:54:16 2012 +0000 @@ -0,0 +1,41 @@ +/* + * mbed Tiny HTTP Client for Ethernet Interface Library + * Copyright (c) 2012 Hiroshi Suga + * Released under the MIT License: http://mbed.org/license/mit + */ + +/** @file + * @brief Tiny HTTP Client + */ + +#ifndef TinyHTTP_h +#define TinyHTTP_h + +#include "mbed.h" +#include "EthernetInterface.h" + +#define HTTP_PORT 80 +#define HTTP_TIMEOUT 15000 // ms + +#define METHOD_GET 0 +#define METHOD_POST 1 + +typedef void (*onHttpReceiveFunc)(char *buf, int len); + +/** send http request + * @param method METHOD_GET or METHOD_POST + * @param host http server + * @param uri URI + * @param head http header (CR+LF) (or NULL) + * @param body POST body (or NULL) + * @return http code, -1:failue + */ +int httpRequest (int method, char *host, int port, char *uri, char *head, char *body, onHttpReceiveFunc func); + +void createauth (char *user, char *pwd, char *buf, int len); + +int base64enc(const char *input, unsigned int length, char *output, int len); + +int urlencode(char *str, char *buf, int len); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Jul 02 05:54:16 2012 +0000 @@ -0,0 +1,131 @@ +#include "mbed.h" +#include "EthernetInterface.h" +#include "TinyHTTP_b.h" + +// Kansai Electric Power +#define HTTP_HOST "www.kepco.co.jp" +#define HTTP_URI "/yamasou/juyo1_kansai.csv" + +Serial pc(USBTX, USBRX); +EthernetInterface eth; + +DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4); + +int denki_usage = 0; +struct tm denki_update; +int year, month, day, hour, minute; + +void callback_denkiyohou (char *buf, int len) { + static int flg = 0, n = 0; + static char data[200]; + int i; + + for (i = 0; i < len; i ++) { + if (buf[i] == '\r') continue; + + switch (flg) { + case 0: + // header + if (buf[i] == '\n') { + flg ++; + } + break; + case 1: + // end of header + if (buf[i] == '\n') { + flg ++; + } else { + flg = 0; + } + break; + case 2: + // update + if (buf[i] == '\n') { + data[n] = 0; + sscanf(data, "%d/%d/%d %d:%d", &year, &month, &day, &hour, &minute); + n = 0; + flg ++; + } else { + data[n] = buf[i]; + n ++; + } + break; + case 4: + // peak + if (buf[i] == '\n') { + n = 0; + flg ++; + } else { + data[n] = buf[i]; + n ++; + } + break; + case 7: + // yosou + if (buf[i] == '\n') { + n = 0; + flg ++; + } else { + data[n] = buf[i]; + n ++; + } + break; + case 10: + // usage + if (buf[i] == '\n') { + data[n] = 0; + if (data[0] >= '0' && data[0] <= '9') { + denki_usage = atoi(data); + } + n = 0; + flg ++; + } else { + data[n] = buf[i]; + n ++; + } + break; + default: + // text + if (buf[i] == '\n') { + flg ++; + } + break; + } + } +} + +int main() { + Timer timer; + int flg = 1, r; + + pc.baud(115200); + eth.init(); //Use DHCP + if (eth.connect()) { + return -1; + } + + pc.printf("Denki-yohou %s\r\n", HTTP_HOST); + + timer.start(); + while(1) { + led1 = 1; + + if (flg || timer.read() >= 300) { + timer.reset(); + r = httpRequest(METHOD_GET, HTTP_HOST, 80, HTTP_URI, NULL, NULL, &callback_denkiyohou); + if (r == 0) { + pc.printf("%04d-%02d-%02d %02d:%02d : ", year, month, day, hour, minute); + pc.printf("%d %%\r\n", denki_usage); + + led2 = denki_usage >= 70 ? 1 : 0; + led3 = denki_usage >= 85 ? 1 : 0; + led4 = denki_usage >= 95 ? 1 : 0; + } + flg = 0; + } + + wait(0.1); + led1 = 0; + wait(0.9); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Mon Jul 02 05:54:16 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#3cf1dd27c89c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Jul 02 05:54:16 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/976df7c37ad5 \ No newline at end of file