Toyomasa Watarai
/
Wio_3G_HTTP-GET-Firebase-example
Wio 3G with Firebase example
Fork of Wio_3G_HTTP-GET-example by
Diff: main.cpp
- Revision:
- 79:7cade4176d12
- Parent:
- 78:7f66af24c678
--- a/main.cpp Thu Aug 23 10:12:47 2018 +0000 +++ b/main.cpp Sun Aug 26 01:51:34 2018 +0000 @@ -1,123 +1,73 @@ #include "mbed.h" -#include <sstream> #include "easy-connect.h" #include "https_request.h" -#include "ssl_ca_pem.h" -// For your API Token, refer to "API token" in your application setting page -const char API_TOKEN[] = "api-token"; -// Your domain name can be seen in the usage explanation with curl. -const char URL[] = "https://{domain}.cybozu.com/k/v1/record.json"; - -// app_id, application id, can be checked with your application's URL -// e.g. https://{domain}.cybozu.com/k/2/ -> app_id is 2 -int app_id = 1; - -#if defined(__CC_ARM) -// To avoid "invalid multibyte character sequence" warning -#pragma diag_suppress 870 -#endif +Serial pc(USBTX, USBRX); -#if !defined(TARGET_WIO_3G) -#error Selected target is not supported. -#endif - -// on-board resources -Serial pc(USBTX, USBRX, 115200); -DigitalOut GrovePower(PB_10, 1); - -#define D20 (PB_4) +/* List of trusted root CA certificates + * This is the root CA for Google from GeoTrustGlobalCA + * + * To add more root certificates, just concatenate them. + */ +const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" +"MIIDujCCAqKgAwIBAgILBAAAAAABD4Ym5g0wDQYJKoZIhvcNAQEFBQAwTDEgMB4G\n" +"A1UECxMXR2xvYmFsU2lnbiBSb290IENBIC0gUjIxEzARBgNVBAoTCkdsb2JhbFNp\n" +"Z24xEzARBgNVBAMTCkdsb2JhbFNpZ24wHhcNMDYxMjE1MDgwMDAwWhcNMjExMjE1\n" +"MDgwMDAwWjBMMSAwHgYDVQQLExdHbG9iYWxTaWduIFJvb3QgQ0EgLSBSMjETMBEG\n" +"A1UEChMKR2xvYmFsU2lnbjETMBEGA1UEAxMKR2xvYmFsU2lnbjCCASIwDQYJKoZI\n" +"hvcNAQEBBQADggEPADCCAQoCggEBAKbPJA6+Lm8omUVCxKs+IVSbC9N/hHD6ErPL\n" +"v4dfxn+G07IwXNb9rfF73OX4YJYJkhD10FPe+3t+c4isUoh7SqbKSaZeqKeMWhG8\n" +"eoLrvozps6yWJQeXSpkqBy+0Hne/ig+1AnwblrjFuTosvNYSuetZfeLQBoZfXklq\n" +"tTleiDTsvHgMCJiEbKjNS7SgfQx5TfC4LcshytVsW33hoCmEofnTlEnLJGKRILzd\n" +"C9XZzPnqJworc5HGnRusyMvo4KD0L5CLTfuwNhv2GXqF4G3yYROIXJ/gkwpRl4pa\n" +"zq+r1feqCapgvdzZX99yqWATXgAByUr6P6TqBwMhAo6CygPCm48CAwEAAaOBnDCB\n" +"mTAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUm+IH\n" +"V2ccHsBqBt5ZtJot39wZhi4wNgYDVR0fBC8wLTAroCmgJ4YlaHR0cDovL2NybC5n\n" +"bG9iYWxzaWduLm5ldC9yb290LXIyLmNybDAfBgNVHSMEGDAWgBSb4gdXZxwewGoG\n" +"3lm0mi3f3BmGLjANBgkqhkiG9w0BAQUFAAOCAQEAmYFThxxol4aR7OBKuEQLq4Gs\n" +"J0/WwbgcQ3izDJr86iw8bmEbTUsp9Z8FHSbBuOmDAGJFtqkIk7mpM0sYmsL4h4hO\n" +"291xNBrBVNpGP+DTKqttVCL1OmLNIG+6KYnX3ZHu01yiPqFbQfXf5WRDLenVOavS\n" +"ot+3i9DAgBkcRcAtjOj4LaR0VknFBbVPFd5uRHg5h6h+u/N5GJG79G+dwfCMNYxd\n" +"AfvDbbnvRG15RjF+Cv6pgsH/76tuIMRQyV+dTZsXjAzlAcmgQWpzU/qlULRuJQ/7\n" +"TBj0/VLZjmmx6BEP3ojY+x1J96relc8geMJgEtslQIxq/H5COEBkEveegeGTLg==\n" +"-----END CERTIFICATE-----"; -// Grove sensors - -// buzzer -DigitalOut buzzer(D38); +void dump_response(HttpResponse* res) { + mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); -// button or touch sensor -InterruptIn btn(D20); -int id = 0; - -void push() -{ - id++; + mbedtls_printf("Headers:\n"); + for (size_t ix = 0; ix < res->get_headers_length(); ix++) { + mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); + } + mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); } -// JSON simplicity parser -char* j_paser( const char *buf , char *word , char *out ) -{ - int i = 0; - char *p; - char _word[64] = "\"\0"; - - strcat(_word , word ); - strcat(_word , "\"" ); - - p = strstr( (char*)buf , _word ) + 2 + strlen(_word); +int main() { + pc.baud(115200); - while( (p[i] != ',')&&(p[i] != '\n')&&(p[i] != '"') ) { - out[i] = p[i]; - i++; - } - out[i] = '\0'; - - return p; -} - -// main method. retrieve data from kintone by HTTP GET -int main() -{ - btn.mode(PullUp); - btn.fall(push); // set interrupt handler - - NetworkInterface* network = NULL; - network = easy_connect(true); // If true, prints out connection details. + NetworkInterface* network = easy_connect(true); if (!network) { - pc.printf("\n----- Network Error -----\n"); - return -1; + return 1; } - pc.printf("\n----- Network Connected -----\n"); - wait(2.0); - - while(1) { - // Set url - std::stringstream ss_url; - std::string s_url(URL); + // GET request to firebase.google.com + { + printf("\n----- HTTPS GET request -----\n"); - ss_url << s_url << "?app=" << app_id << "&id=" << id; - string url = ss_url.str(); - pc.printf("%s\n", url.c_str()); - - pc.printf("\n----- HTTPS GET request -----\n"); - HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, url.c_str()); - get_req->set_header("X-Cybozu-API-Token", API_TOKEN); - HttpResponse* get_res = get_req->send(); - - pc.printf("\n----- HTTPS GET response [%d]-----\n", get_res->get_status_code()); + HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://firebase.google.com/styles/docs.css"); + get_req->set_debug(true); - if(get_res->get_status_code() == 200) { - pc.printf("\n----- HTTPS GET response 200 -----\n"); - const char* body = get_res->get_body_as_string().c_str(); - - pc.printf("%s\n", body); - - // Response JSON parse - char value[256]; - - char* p = j_paser(body, "日付", value); - j_paser(p,"value", value); - pc.printf("date:%s\n", value); - - p = j_paser(body, "文字列__1行_", value); - j_paser(p,"value", value); - pc.printf("%s\n", value); - - buzzer = 1; - wait(0.1); - buzzer = 0; + HttpResponse* get_res = get_req->send(); + if (!get_res) { + printf("HttpRequest failed (error code %d)\n", get_req->get_error()); + return 1; } + printf("\n----- HTTPS GET response -----\n"); + dump_response(get_res); delete get_req; - wait(10.0); } + + Thread::wait(osWaitForever); } +