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ファイルの構造が変わるとうまく動かなくなります。
東電でも同様に使えると思います。

Committer:
okini3939
Date:
Tue Jul 03 00:32:27 2012 +0000
Revision:
3:ef5af1e8558d
Parent:
1:10bd46941b8b
bugfix and pwm drive to led

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:2a2f00cbc761 1 /*
okini3939 0:2a2f00cbc761 2 * mbed Tiny HTTP Client for Ethernet Interface Library
okini3939 0:2a2f00cbc761 3 * Copyright (c) 2012 Hiroshi Suga
okini3939 0:2a2f00cbc761 4 * Released under the MIT License: http://mbed.org/license/mit
okini3939 0:2a2f00cbc761 5 */
okini3939 0:2a2f00cbc761 6
okini3939 0:2a2f00cbc761 7 /** @file
okini3939 0:2a2f00cbc761 8 * @brief Tiny HTTP Client
okini3939 0:2a2f00cbc761 9 */
okini3939 0:2a2f00cbc761 10
okini3939 0:2a2f00cbc761 11 #include "mbed.h"
okini3939 0:2a2f00cbc761 12 #include "EthernetInterface.h"
okini3939 0:2a2f00cbc761 13 #include "TinyHTTP_b.h"
okini3939 0:2a2f00cbc761 14 #include <ctype.h>
okini3939 0:2a2f00cbc761 15
okini3939 0:2a2f00cbc761 16 //#define DEBUG
okini3939 0:2a2f00cbc761 17
okini3939 0:2a2f00cbc761 18 static TCPSocket *http;
okini3939 0:2a2f00cbc761 19 static onHttpReceiveFunc onHttpReceive;
okini3939 0:2a2f00cbc761 20
okini3939 0:2a2f00cbc761 21 // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
okini3939 0:2a2f00cbc761 22 int base64enc(const char *input, unsigned int length, char *output, int len) {
okini3939 0:2a2f00cbc761 23 static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
okini3939 0:2a2f00cbc761 24 unsigned int c, c1, c2, c3;
okini3939 0:2a2f00cbc761 25
okini3939 0:2a2f00cbc761 26 if (len < ((((length-1)/3)+1)<<2)) return -1;
okini3939 0:2a2f00cbc761 27 for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) {
okini3939 0:2a2f00cbc761 28 c1 = ((((unsigned char)*((unsigned char *)&input[i]))));
okini3939 0:2a2f00cbc761 29 c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0;
okini3939 0:2a2f00cbc761 30 c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0;
okini3939 0:2a2f00cbc761 31
okini3939 0:2a2f00cbc761 32 c = ((c1 & 0xFC) >> 2);
okini3939 0:2a2f00cbc761 33 output[j+0] = base64[c];
okini3939 0:2a2f00cbc761 34 c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4);
okini3939 0:2a2f00cbc761 35 output[j+1] = base64[c];
okini3939 0:2a2f00cbc761 36 c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6);
okini3939 0:2a2f00cbc761 37 output[j+2] = (length>i+1)?base64[c]:'=';
okini3939 0:2a2f00cbc761 38 c = (c3 & 0x3F);
okini3939 0:2a2f00cbc761 39 output[j+3] = (length>i+2)?base64[c]:'=';
okini3939 0:2a2f00cbc761 40 }
okini3939 0:2a2f00cbc761 41 output[(((length-1)/3)+1)<<2] = '\0';
okini3939 0:2a2f00cbc761 42 return 0;
okini3939 0:2a2f00cbc761 43 }
okini3939 0:2a2f00cbc761 44
okini3939 0:2a2f00cbc761 45 // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
okini3939 0:2a2f00cbc761 46 int urlencode(char *str, char *buf, int len) {
okini3939 0:2a2f00cbc761 47 static const char to_hex[] = "0123456789ABCDEF";
okini3939 0:2a2f00cbc761 48 // char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf;
okini3939 0:2a2f00cbc761 49 char *pstr = str, *pbuf = buf;
okini3939 0:2a2f00cbc761 50
okini3939 0:2a2f00cbc761 51 if (len < (strlen(str) * 3 + 1)) return -1;
okini3939 0:2a2f00cbc761 52 while (*pstr) {
okini3939 0:2a2f00cbc761 53 if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') {
okini3939 0:2a2f00cbc761 54 *pbuf++ = *pstr;
okini3939 0:2a2f00cbc761 55 } else if (*pstr == ' ') {
okini3939 0:2a2f00cbc761 56 *pbuf++ = '+';
okini3939 0:2a2f00cbc761 57 } else {
okini3939 0:2a2f00cbc761 58 *pbuf++ = '%';
okini3939 0:2a2f00cbc761 59 *pbuf++ = to_hex[(*pstr >> 4) & 0x0f];
okini3939 0:2a2f00cbc761 60 *pbuf++ = to_hex[*pstr & 0x0f];
okini3939 0:2a2f00cbc761 61 }
okini3939 0:2a2f00cbc761 62 pstr++;
okini3939 0:2a2f00cbc761 63 }
okini3939 0:2a2f00cbc761 64 *pbuf = '\0';
okini3939 0:2a2f00cbc761 65 return 0;
okini3939 0:2a2f00cbc761 66 }
okini3939 0:2a2f00cbc761 67
okini3939 0:2a2f00cbc761 68 void createauth (char *user, char *pwd, char *buf, int len) {
okini3939 0:2a2f00cbc761 69 char tmp[80];
okini3939 0:2a2f00cbc761 70
okini3939 0:2a2f00cbc761 71 strncpy(buf, "Authorization: Basic ", len);
okini3939 0:2a2f00cbc761 72 snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd);
okini3939 0:2a2f00cbc761 73 base64enc(tmp, strlen(tmp), &buf[strlen(buf)], len - strlen(buf));
okini3939 0:2a2f00cbc761 74 strncat(buf, "\r\n", len - strlen(buf));
okini3939 0:2a2f00cbc761 75 }
okini3939 0:2a2f00cbc761 76
okini3939 0:2a2f00cbc761 77 int httpRequest (int method, char *host, int port, char *uri, char *head, char *body, onHttpReceiveFunc func) {
okini3939 0:2a2f00cbc761 78 char buf[1500];
okini3939 0:2a2f00cbc761 79 int i, ret = -1;
okini3939 0:2a2f00cbc761 80
okini3939 0:2a2f00cbc761 81 http = new TCPSocket;
okini3939 0:2a2f00cbc761 82 onHttpReceive = func;
okini3939 0:2a2f00cbc761 83
okini3939 0:2a2f00cbc761 84 if (http->connect(host, port, HTTP_TIMEOUT)) goto exit;
okini3939 1:10bd46941b8b 85 #ifdef DEBUG
okini3939 1:10bd46941b8b 86 printf("connected\r\n");
okini3939 1:10bd46941b8b 87 #endif
okini3939 0:2a2f00cbc761 88
okini3939 0:2a2f00cbc761 89 // send request
okini3939 0:2a2f00cbc761 90 if (method == METHOD_POST) {
okini3939 0:2a2f00cbc761 91 http->send("POST ", 5, HTTP_TIMEOUT);
okini3939 0:2a2f00cbc761 92 } else {
okini3939 0:2a2f00cbc761 93 http->send("GET ", 4, HTTP_TIMEOUT);
okini3939 0:2a2f00cbc761 94 }
okini3939 0:2a2f00cbc761 95 http->send(uri, strlen(uri), HTTP_TIMEOUT);
okini3939 0:2a2f00cbc761 96 http->send(" HTTP/1.1\r\nHost: ", 17, HTTP_TIMEOUT);
okini3939 0:2a2f00cbc761 97 http->send(host, strlen(host), HTTP_TIMEOUT);
okini3939 0:2a2f00cbc761 98 http->send("\r\n", 2, HTTP_TIMEOUT);
okini3939 0:2a2f00cbc761 99 http->send("Connection: close\r\n", 19, HTTP_TIMEOUT);
okini3939 0:2a2f00cbc761 100 if (head) {
okini3939 0:2a2f00cbc761 101 http->send(head, strlen(head), HTTP_TIMEOUT);
okini3939 0:2a2f00cbc761 102 }
okini3939 0:2a2f00cbc761 103 if (method == METHOD_POST) {
okini3939 0:2a2f00cbc761 104 sprintf(buf, "Content-Length: %d\r\n", strlen(body));
okini3939 0:2a2f00cbc761 105 http->send(buf, strlen(buf), HTTP_TIMEOUT);
okini3939 0:2a2f00cbc761 106 }
okini3939 0:2a2f00cbc761 107 http->send("\r\n", 2, HTTP_TIMEOUT);
okini3939 0:2a2f00cbc761 108
okini3939 0:2a2f00cbc761 109 // post method
okini3939 0:2a2f00cbc761 110 if (method == METHOD_POST && body) {
okini3939 0:2a2f00cbc761 111 http->send(body, strlen(body), HTTP_TIMEOUT);
okini3939 0:2a2f00cbc761 112 }
okini3939 0:2a2f00cbc761 113
okini3939 1:10bd46941b8b 114 #ifdef DEBUG
okini3939 1:10bd46941b8b 115 printf("wait for response\r\n");
okini3939 1:10bd46941b8b 116 #endif
okini3939 1:10bd46941b8b 117 // recv
okini3939 0:2a2f00cbc761 118 for (;;) {
okini3939 0:2a2f00cbc761 119 i = http->receive(buf, sizeof(buf) - 1, HTTP_TIMEOUT);
okini3939 0:2a2f00cbc761 120 if (i == 0) break;
okini3939 0:2a2f00cbc761 121
okini3939 0:2a2f00cbc761 122 if (onHttpReceive != NULL) onHttpReceive(buf, i);
okini3939 0:2a2f00cbc761 123 #ifdef DEBUG
okini3939 0:2a2f00cbc761 124 printf(buf);
okini3939 0:2a2f00cbc761 125 #endif
okini3939 0:2a2f00cbc761 126 }
okini3939 0:2a2f00cbc761 127 ret = 0;
okini3939 0:2a2f00cbc761 128
okini3939 0:2a2f00cbc761 129 exit:
okini3939 0:2a2f00cbc761 130 http->close();
okini3939 0:2a2f00cbc761 131 delete http;
okini3939 0:2a2f00cbc761 132
okini3939 0:2a2f00cbc761 133 return ret;
okini3939 0:2a2f00cbc761 134 }