Wio 3G with Firebase example

Dependencies:   mbed-http

Fork of Wio_3G_HTTP-GET-example by Toyomasa Watarai

Files at this revision

API Documentation at this revision

Comitter:
MACRUM
Date:
Sun Aug 26 01:51:34 2018 +0000
Parent:
78:7f66af24c678
Commit message:
Enabled Firebase config

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
mbedtls_entropy_config.h Show annotated file Show diff for this revision Revisions of this file
ssl_ca_pem.h Show diff for this revision Revisions of this file
--- 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"
+"Z24xEzARBgNVBAMTCkdsb2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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);
 }
+
--- a/mbed_app.json	Thu Aug 23 10:12:47 2018 +0000
+++ b/mbed_app.json	Sun Aug 26 01:51:34 2018 +0000
@@ -1,5 +1,6 @@
 {
     "macros": [
+        "MBEDTLS_USER_CONFIG_FILE=\"mbedtls_entropy_config.h\""
     ],
     "config": {
         "sock-type": "TCP",
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbedtls_entropy_config.h	Sun Aug 26 01:51:34 2018 +0000
@@ -0,0 +1,38 @@
+/*
+ *  Copyright (C) 2006-2018, Arm Limited, All Rights Reserved
+ *  SPDX-License-Identifier: Apache-2.0
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License"); you may
+ *  not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  This file is part of Mbed TLS (https://tls.mbed.org)
+ */
+
+#if !defined(MBEDTLS_ENTROPY_HARDWARE_ALT) && \
+    !defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_TEST_NULL_ENTROPY)
+#error "This hardware does not have an entropy source."
+#endif /* !MBEDTLS_ENTROPY_HARDWARE_ALT && !MBEDTLS_ENTROPY_NV_SEED &&
+        * !MBEDTLS_TEST_NULL_ENTROPY */
+
+#if !defined(MBEDTLS_SHA1_C)
+#define MBEDTLS_SHA1_C
+#endif /* !MBEDTLS_SHA1_C */
+
+/*
+ *  This value is sufficient for handling 2048 bit RSA keys.
+ *
+ *  Set this value higher to enable handling larger keys, but be aware that this
+ *  will increase the stack usage.
+ */
+#define MBEDTLS_MPI_MAX_SIZE        256
+
+#define MBEDTLS_MPI_WINDOW_SIZE     1
--- a/ssl_ca_pem.h	Thu Aug 23 10:12:47 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,29 +0,0 @@
-const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
-"MIIFADCCA+igAwIBAgIBBzANBgkqhkiG9w0BAQsFADCBjzELMAkGA1UEBhMCVVMx\n"
-"EDAOBgNVBAgTB0FyaXpvbmExEzARBgNVBAcTClNjb3R0c2RhbGUxJTAjBgNVBAoT\n"
-"HFN0YXJmaWVsZCBUZWNobm9sb2dpZXMsIEluYy4xMjAwBgNVBAMTKVN0YXJmaWVs\n"
-"ZCBSb290IENlcn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