Code clean-up

Dependencies:   Grove_temperature mbed-http

Fork of Wio_3G_HTTP-POST-example by Atsushi Tomizawa

Committer:
atomichan
Date:
Thu Aug 23 04:06:06 2018 +0000
Revision:
1:6c0383514e5b
Parent:
0:b3812b1c103d
Child:
2:687ce876e4ee
part of the preparation works for Ina-city Hackerthon

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