Code clean-up

Dependencies:   Grove_temperature mbed-http

Fork of Wio_3G_HTTP-POST-example by Atsushi Tomizawa

Committer:
MACRUM
Date:
Fri Aug 24 00:41:04 2018 +0000
Revision:
2:687ce876e4ee
Parent:
1:6c0383514e5b
Code clean-up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JKsoft_main 0:b3812b1c103d 1 #include "mbed.h"
JKsoft_main 0:b3812b1c103d 2 #include <sstream>
JKsoft_main 0:b3812b1c103d 3 #include "easy-connect.h"
JKsoft_main 0:b3812b1c103d 4 #include "https_request.h"
JKsoft_main 0:b3812b1c103d 5 #include "ssl_ca_pem.h"
atomichan 1:6c0383514e5b 6 #include "Grove_temperature.h"
atomichan 1:6c0383514e5b 7
atomichan 1:6c0383514e5b 8 #if !defined(TARGET_WIO_3G)
atomichan 1:6c0383514e5b 9 #error Selected target is not supported.
atomichan 1:6c0383514e5b 10 #endif
atomichan 1:6c0383514e5b 11
atomichan 1:6c0383514e5b 12 // For your API Token, refer to "API token" in your application setting page
atomichan 1:6c0383514e5b 13 // Please DO NOT FORGET to assign "add records" to your key.
MACRUM 2:687ce876e4ee 14 const char API_TOKEN[] = "api-key";
MACRUM 2:687ce876e4ee 15
MACRUM 2:687ce876e4ee 16 // Your domain name can be seen in the usage explanation with curl.
JKsoft_main 0:b3812b1c103d 17 const char URL[] = "https://{domain}.cybozu.com/k/v1/record.json";
JKsoft_main 0:b3812b1c103d 18
atomichan 1:6c0383514e5b 19 // app_id, application id, can be checked with your application's URL
atomichan 1:6c0383514e5b 20 // e.g. https://{domain}.cybozu.com/k/3/ -> app_id is 3
atomichan 1:6c0383514e5b 21 int app_id = 3;
JKsoft_main 0:b3812b1c103d 22
MACRUM 2:687ce876e4ee 23 // on-board resources
MACRUM 2:687ce876e4ee 24 Serial pc(USBTX, USBRX, 115200);
MACRUM 2:687ce876e4ee 25 DigitalOut GrovePower(PB_10, 1);
MACRUM 2:687ce876e4ee 26
MACRUM 2:687ce876e4ee 27 // Grove sensors
MACRUM 2:687ce876e4ee 28 // Temperature sensor
MACRUM 2:687ce876e4ee 29 Grove_temperature tempSensor(A4);
MACRUM 2:687ce876e4ee 30
JKsoft_main 0:b3812b1c103d 31 bool post_kintone(NetworkInterface* nif, const char *url, int app_id_, char* field_code, float value)
MACRUM 2:687ce876e4ee 32 {
JKsoft_main 0:b3812b1c103d 33 HttpsRequest* post_req = new HttpsRequest(nif, SSL_CA_PEM, HTTP_POST, url);
JKsoft_main 0:b3812b1c103d 34 post_req->set_header("X-Cybozu-API-Token", API_TOKEN);
JKsoft_main 0:b3812b1c103d 35 post_req->set_header("Content-Type", "application/json");
MACRUM 2:687ce876e4ee 36
JKsoft_main 0:b3812b1c103d 37 std::stringstream ss_body;
MACRUM 2:687ce876e4ee 38
JKsoft_main 0:b3812b1c103d 39 ss_body << "{\"app\": " << app_id_ << ", \"record\": {\"" << field_code << "\": {\"value\": \"" << value << "\"}}}\n";
JKsoft_main 0:b3812b1c103d 40
MACRUM 2:687ce876e4ee 41 string body = ss_body.str();
MACRUM 2:687ce876e4ee 42 pc.printf("body:%s\n",body.c_str());
MACRUM 2:687ce876e4ee 43
JKsoft_main 0:b3812b1c103d 44 HttpResponse* post_res = post_req->send(body.c_str(), body.length());
MACRUM 2:687ce876e4ee 45 pc.printf("res:%s\n",post_res->get_body_as_string().c_str());
MACRUM 2:687ce876e4ee 46
MACRUM 2:687ce876e4ee 47 if(post_res->get_status_code() == 200) {
JKsoft_main 0:b3812b1c103d 48 delete post_req;
JKsoft_main 0:b3812b1c103d 49 return true;
JKsoft_main 0:b3812b1c103d 50 }
MACRUM 2:687ce876e4ee 51
MACRUM 2:687ce876e4ee 52 delete post_req;
JKsoft_main 0:b3812b1c103d 53 return false;
JKsoft_main 0:b3812b1c103d 54 }
JKsoft_main 0:b3812b1c103d 55
JKsoft_main 0:b3812b1c103d 56 // main() runs in its own thread in the OS
MACRUM 2:687ce876e4ee 57 int main()
MACRUM 2:687ce876e4ee 58 {
JKsoft_main 0:b3812b1c103d 59
MACRUM 2:687ce876e4ee 60 NetworkInterface* network = NULL;
JKsoft_main 0:b3812b1c103d 61 network = easy_connect(true); // If true, prints out connection details.
MACRUM 2:687ce876e4ee 62
JKsoft_main 0:b3812b1c103d 63 if (!network) {
MACRUM 2:687ce876e4ee 64 pc.printf("\n----- Network Error -----\n");
JKsoft_main 0:b3812b1c103d 65 return -1;
JKsoft_main 0:b3812b1c103d 66 }
JKsoft_main 0:b3812b1c103d 67
MACRUM 2:687ce876e4ee 68 pc.printf("\n----- Network Connected -----\n");
JKsoft_main 0:b3812b1c103d 69 wait(2.0);
MACRUM 2:687ce876e4ee 70
JKsoft_main 0:b3812b1c103d 71 while(1) {
MACRUM 2:687ce876e4ee 72
atomichan 1:6c0383514e5b 73 float currentTemp = tempSensor.getTemperature();
MACRUM 2:687ce876e4ee 74
atomichan 1:6c0383514e5b 75 bool ret = post_kintone(network, URL, app_id, "temp", currentTemp);
MACRUM 2:687ce876e4ee 76 pc.printf("\n----- HTTPS POST response [%s]-----\n",ret== true ? "OK" : "NG");
MACRUM 2:687ce876e4ee 77
JKsoft_main 0:b3812b1c103d 78 wait(10.0);
JKsoft_main 0:b3812b1c103d 79 }
JKsoft_main 0:b3812b1c103d 80 }
JKsoft_main 0:b3812b1c103d 81