Junichi Katsu
/
Ambient_LM61
LM61のセンサ値をAmbientにアップロードするプログラムです。
main.cpp@0:b3812b1c103d, 2018-08-01 (annotated)
- Committer:
- JKsoft_main
- Date:
- Wed Aug 01 00:14:57 2018 +0000
- Revision:
- 0:b3812b1c103d
- Child:
- 1:d33e49e1e46d
First Release
Who changed what in which revision?
User | Revision | Line number | New 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" |
JKsoft_main | 0:b3812b1c103d | 6 | #include "C12832.h" |
JKsoft_main | 0:b3812b1c103d | 7 | #include "MMA7660.h" |
JKsoft_main | 0:b3812b1c103d | 8 | #include "LM75B.h" |
JKsoft_main | 0:b3812b1c103d | 9 | |
JKsoft_main | 0:b3812b1c103d | 10 | C12832 lcd(D11, D13, D12, D7, D10); |
JKsoft_main | 0:b3812b1c103d | 11 | MMA7660 MMA(D14,D15); |
JKsoft_main | 0:b3812b1c103d | 12 | LM75B sensor(D14,D15); |
JKsoft_main | 0:b3812b1c103d | 13 | |
JKsoft_main | 0:b3812b1c103d | 14 | Serial pc(USBTX,USBRX); |
JKsoft_main | 0:b3812b1c103d | 15 | |
JKsoft_main | 0:b3812b1c103d | 16 | const char API_TOKEN[] = "api-token"; |
JKsoft_main | 0:b3812b1c103d | 17 | const char URL[] = "https://{domain}.cybozu.com/k/v1/record.json"; |
JKsoft_main | 0:b3812b1c103d | 18 | |
JKsoft_main | 0:b3812b1c103d | 19 | int app_id = 2; |
JKsoft_main | 0:b3812b1c103d | 20 | |
JKsoft_main | 0:b3812b1c103d | 21 | bool post_kintone(NetworkInterface* nif, const char *url, int app_id_, char* field_code, float value) |
JKsoft_main | 0:b3812b1c103d | 22 | { |
JKsoft_main | 0:b3812b1c103d | 23 | HttpsRequest* post_req = new HttpsRequest(nif, SSL_CA_PEM, HTTP_POST, url); |
JKsoft_main | 0:b3812b1c103d | 24 | post_req->set_header("X-Cybozu-API-Token", API_TOKEN); |
JKsoft_main | 0:b3812b1c103d | 25 | post_req->set_header("Content-Type", "application/json"); |
JKsoft_main | 0:b3812b1c103d | 26 | |
JKsoft_main | 0:b3812b1c103d | 27 | std::stringstream ss_body; |
JKsoft_main | 0:b3812b1c103d | 28 | |
JKsoft_main | 0:b3812b1c103d | 29 | ss_body << "{\"app\": " << app_id_ << ", \"record\": {\"" << field_code << "\": {\"value\": \"" << value << "\"}}}\n"; |
JKsoft_main | 0:b3812b1c103d | 30 | |
JKsoft_main | 0:b3812b1c103d | 31 | string body = ss_body.str(); |
JKsoft_main | 0:b3812b1c103d | 32 | |
JKsoft_main | 0:b3812b1c103d | 33 | printf("body:%s\r\n",body.c_str()); |
JKsoft_main | 0:b3812b1c103d | 34 | |
JKsoft_main | 0:b3812b1c103d | 35 | HttpResponse* post_res = post_req->send(body.c_str(), body.length()); |
JKsoft_main | 0:b3812b1c103d | 36 | |
JKsoft_main | 0:b3812b1c103d | 37 | printf("res:%s\r\n",post_res->get_body_as_string().c_str()); |
JKsoft_main | 0:b3812b1c103d | 38 | |
JKsoft_main | 0:b3812b1c103d | 39 | |
JKsoft_main | 0:b3812b1c103d | 40 | if(post_res->get_status_code() == 200){ |
JKsoft_main | 0:b3812b1c103d | 41 | delete post_req; |
JKsoft_main | 0:b3812b1c103d | 42 | return true; |
JKsoft_main | 0:b3812b1c103d | 43 | } |
JKsoft_main | 0:b3812b1c103d | 44 | |
JKsoft_main | 0:b3812b1c103d | 45 | delete post_req; |
JKsoft_main | 0:b3812b1c103d | 46 | |
JKsoft_main | 0:b3812b1c103d | 47 | return false; |
JKsoft_main | 0:b3812b1c103d | 48 | } |
JKsoft_main | 0:b3812b1c103d | 49 | |
JKsoft_main | 0:b3812b1c103d | 50 | // main() runs in its own thread in the OS |
JKsoft_main | 0:b3812b1c103d | 51 | int main() { |
JKsoft_main | 0:b3812b1c103d | 52 | NetworkInterface* network = NULL; |
JKsoft_main | 0:b3812b1c103d | 53 | |
JKsoft_main | 0:b3812b1c103d | 54 | pc.baud(115200); |
JKsoft_main | 0:b3812b1c103d | 55 | |
JKsoft_main | 0:b3812b1c103d | 56 | lcd.cls(); |
JKsoft_main | 0:b3812b1c103d | 57 | lcd.locate(0,3); |
JKsoft_main | 0:b3812b1c103d | 58 | lcd.printf("Network Connect.."); |
JKsoft_main | 0:b3812b1c103d | 59 | |
JKsoft_main | 0:b3812b1c103d | 60 | pc.printf("\r\n----- Start -----\r\n"); |
JKsoft_main | 0:b3812b1c103d | 61 | |
JKsoft_main | 0:b3812b1c103d | 62 | network = easy_connect(true); // If true, prints out connection details. |
JKsoft_main | 0:b3812b1c103d | 63 | if (!network) { |
JKsoft_main | 0:b3812b1c103d | 64 | pc.printf("\r\n----- Network Error -----\r\n"); |
JKsoft_main | 0:b3812b1c103d | 65 | return -1; |
JKsoft_main | 0:b3812b1c103d | 66 | } |
JKsoft_main | 0:b3812b1c103d | 67 | |
JKsoft_main | 0:b3812b1c103d | 68 | lcd.printf("OK"); |
JKsoft_main | 0:b3812b1c103d | 69 | pc.printf("\r\n----- Network Connected -----\r\n"); |
JKsoft_main | 0:b3812b1c103d | 70 | |
JKsoft_main | 0:b3812b1c103d | 71 | wait(2.0); |
JKsoft_main | 0:b3812b1c103d | 72 | |
JKsoft_main | 0:b3812b1c103d | 73 | while(1) { |
JKsoft_main | 0:b3812b1c103d | 74 | lcd.cls(); |
JKsoft_main | 0:b3812b1c103d | 75 | lcd.locate(0,3); |
JKsoft_main | 0:b3812b1c103d | 76 | lcd.printf("x=%.2f y=%.2f z=%.2f",MMA.x(), MMA.y(), MMA.z()); |
JKsoft_main | 0:b3812b1c103d | 77 | |
JKsoft_main | 0:b3812b1c103d | 78 | float temp = sensor.read(); |
JKsoft_main | 0:b3812b1c103d | 79 | lcd.locate(0,14); |
JKsoft_main | 0:b3812b1c103d | 80 | lcd.printf("Temp = %.1f\n", temp); |
JKsoft_main | 0:b3812b1c103d | 81 | |
JKsoft_main | 0:b3812b1c103d | 82 | bool ret = post_kintone(network, URL, app_id, "temp", temp); |
JKsoft_main | 0:b3812b1c103d | 83 | pc.printf("\n----- HTTPS POST response [%s]----- \n\r",ret== true ? "OK" : "NG"); |
JKsoft_main | 0:b3812b1c103d | 84 | |
JKsoft_main | 0:b3812b1c103d | 85 | wait(10.0); |
JKsoft_main | 0:b3812b1c103d | 86 | } |
JKsoft_main | 0:b3812b1c103d | 87 | } |
JKsoft_main | 0:b3812b1c103d | 88 |