ニコニコ新検索βAPIからデータを取得して有機ELに表示する。

Dependencies:   EthernetInterface GraphicOLED mbed-rtos mbed picojson

Files at this revision

API Documentation at this revision

Comitter:
mia_0032
Date:
Sat Nov 29 13:35:27 2014 +0000
Commit message:
??API??????????OLED?????????????

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
GraphicOLED.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-rtos.lib 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
picojson.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r bea495dbc532 EthernetInterface.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Sat Nov 29 13:35:27 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#de796e2a5e98
diff -r 000000000000 -r bea495dbc532 GraphicOLED.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GraphicOLED.lib	Sat Nov 29 13:35:27 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/va009039/code/GraphicOLED/#a6650dd2dbc8
diff -r 000000000000 -r bea495dbc532 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Nov 29 13:35:27 2014 +0000
@@ -0,0 +1,106 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "EthernetInterface.h"
+#include "GraphicOLED.h"
+#include "picojson.h"
+#include <string>
+
+// debug
+DigitalOut led1(LED1, 0);
+DigitalOut led2(LED2, 0);
+DigitalOut led3(LED3, 0);
+DigitalOut led4(LED4, 0);
+Serial pc(USBTX, USBRX);
+
+GraphicOLED oled(p24, p26, p27, p28, p21, p22);
+
+// ethernet
+DigitalIn lnk(P1_25);
+DigitalIn spd(P1_26);
+DigitalOut speed(p29);
+DigitalOut link(p30);
+
+void flip(void const *args) {
+    speed = !spd;
+    link = !lnk;
+}
+
+const string API_HOST = "api.search.nicovideo.jp";
+const int API_PORT = 80;
+const string API_PATH = "/api/";
+
+const string SEARCH_TAG = "mbed";
+
+int main() {
+    // ethernet led
+    RtosTimer flipper(flip, osTimerPeriodic, NULL);
+    flipper.start(50);
+
+    Thread::wait(2000);
+    oled.cls();
+    oled.printf("Hello world!\r\n");
+
+    EthernetInterface eth;
+    eth.init();
+    eth.connect();
+    
+    oled.cls();
+    oled.printf("IP Address is %s\r\n", eth.getIPAddress());
+    
+    TCPSocketConnection sock;
+    sock.connect(API_HOST.c_str(), API_PORT);
+    
+    char post_data[256];
+    sprintf(post_data, "{\"query\":\"%s\",\"service\":[\"video\"],\"search\":[\"tags\"],\"join\":[\"cmsid\",\"title\",\"start_time\"],\"from\":0,\"size\":1,\"sort_by\":\"start_time\",\"issuer\":\"apiguide\",\"reason\":\"ma10\"}", SEARCH_TAG.c_str());
+    pc.printf("POST Data:\r\n%s\r\nLength:%d\r\n", post_data, strlen(post_data));
+    
+    char http_cmd[1024];
+    sprintf(http_cmd, "POST %s HTTP/1.1\r\nHost: %s:%d\r\nAccept: */*\r\nContent-Length: %d\r\nContent-Type: application/json\r\n\r\n%s\r\n\r\n", API_PATH.c_str(), API_HOST.c_str(), API_PORT, strlen(post_data), post_data);
+    pc.printf("Request:\r\n%s\r\n", http_cmd);
+    sock.send_all(http_cmd, sizeof(http_cmd)-1);
+    
+    char buffer[4048];
+    int ret;
+    ret = sock.receive(buffer, sizeof(buffer)-1);
+    buffer[ret] = '\0';
+    pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
+      
+    sock.close();
+    eth.disconnect();
+    
+    oled.cls();
+    oled.printf("connection closed.\r\n\r\n");
+    
+    string response(buffer);
+    string response_body;
+    response_body = response.substr((int)response.find("\r\n\r\n") + 1);
+    pc.printf("---------------------------\r\n");
+    pc.printf(response_body.c_str());
+    pc.printf("---------------------------\r\n");
+    
+    for(int i=0; i<3;i++){
+        response_body = response_body.substr((int)response_body.find("\n{") + 1);
+    }
+    
+    int start_pos = (int)response_body.find("[") + 1;
+    int end_pos = (int)response_body.find("]");
+    response_body = response_body.substr(start_pos, end_pos - start_pos);
+    pc.printf("---------------------------\r\n");
+    pc.printf(response_body.c_str());
+    pc.printf("---------------------------\r\n");
+    
+    picojson::value v;
+
+    const char *json = response_body.c_str();
+    string err = picojson::parse(v, json, json + strlen(json));
+    pc.printf("--> error %s\r\n", err.c_str());
+
+    pc.printf("--> values %s\r\n", v.get("title").get<string>().c_str());
+    oled.cls();
+    oled.printf(v.get("title").get<string>().c_str());
+    
+    while(1) {
+        led1 = !led1;
+        Thread::wait(1000);
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r bea495dbc532 mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Sat Nov 29 13:35:27 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#f1ef95efa5ad
diff -r 000000000000 -r bea495dbc532 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Nov 29 13:35:27 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5
\ No newline at end of file
diff -r 000000000000 -r bea495dbc532 picojson.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/picojson.lib	Sat Nov 29 13:35:27 2014 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mimil/code/picojson/#2bb500b021e2