kintone REST API sample
Dependencies: C12832 HTTPClient LM75B SNICInterface mbed-rtos mbed
main.cpp@2:de803d46330e, 2017-08-24 (annotated)
- Committer:
- takeuchiyoshihiko
- Date:
- Thu Aug 24 02:13:46 2017 +0000
- Revision:
- 2:de803d46330e
- Parent:
- 1:875f37e8e786
- Child:
- 3:57ffcb4c9f5d
fix kintone parameter
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takeuchiyoshihiko | 2:de803d46330e | 1 | #define POST_URL "https://{subdomain}.cybozu.com/k/v1/record.json" |
takeuchiyoshihiko | 1:875f37e8e786 | 2 | #define APP_ID xxx |
takeuchiyoshihiko | 2:de803d46330e | 3 | #define API_TOKEN "xxx" |
takeuchiyoshihiko | 1:875f37e8e786 | 4 | #define DEMO_AP_SSID "xxx" |
takeuchiyoshihiko | 1:875f37e8e786 | 5 | #define DEMO_AP_SECUTIRY_KEY "xxx" |
takeuchiyoshihiko | 0:a064b2730f3e | 6 | |
takeuchiyoshihiko | 0:a064b2730f3e | 7 | #include "mbed.h" |
takeuchiyoshihiko | 0:a064b2730f3e | 8 | #include "LM75B.h" |
takeuchiyoshihiko | 0:a064b2730f3e | 9 | #include "C12832.h" |
takeuchiyoshihiko | 0:a064b2730f3e | 10 | #include "HTTPClient.h" |
takeuchiyoshihiko | 0:a064b2730f3e | 11 | #include "SNIC_WifiInterface.h" |
takeuchiyoshihiko | 0:a064b2730f3e | 12 | |
takeuchiyoshihiko | 0:a064b2730f3e | 13 | C12832 lcd(p5, p7, p6, p8, p11); |
takeuchiyoshihiko | 0:a064b2730f3e | 14 | LM75B sensor(p28, p27); |
takeuchiyoshihiko | 0:a064b2730f3e | 15 | |
takeuchiyoshihiko | 0:a064b2730f3e | 16 | #if defined(TARGET_LPC1768) |
takeuchiyoshihiko | 0:a064b2730f3e | 17 | C_SNIC_WifiInterface wifi( p9, p10, NC, NC, p30 ); |
takeuchiyoshihiko | 0:a064b2730f3e | 18 | #elif defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_L152RE) |
takeuchiyoshihiko | 0:a064b2730f3e | 19 | C_SNIC_WifiInterface wifi( D8, D2, NC, NC, D3); |
takeuchiyoshihiko | 0:a064b2730f3e | 20 | #elif defined(TARGET_K64F) |
takeuchiyoshihiko | 0:a064b2730f3e | 21 | C_SNIC_WifiInterface wifi( D1, D0, NC, NC, D2); |
takeuchiyoshihiko | 0:a064b2730f3e | 22 | #endif |
takeuchiyoshihiko | 0:a064b2730f3e | 23 | #define DEMO_AP_SECURITY_TYPE e_SEC_WPA2_AES |
takeuchiyoshihiko | 0:a064b2730f3e | 24 | |
takeuchiyoshihiko | 0:a064b2730f3e | 25 | |
takeuchiyoshihiko | 0:a064b2730f3e | 26 | |
takeuchiyoshihiko | 0:a064b2730f3e | 27 | void display(char *message) |
takeuchiyoshihiko | 0:a064b2730f3e | 28 | { |
takeuchiyoshihiko | 0:a064b2730f3e | 29 | lcd.cls(); |
takeuchiyoshihiko | 0:a064b2730f3e | 30 | lcd.locate(0,3); |
takeuchiyoshihiko | 0:a064b2730f3e | 31 | lcd.printf("%s", message); |
takeuchiyoshihiko | 0:a064b2730f3e | 32 | wait(0.1); |
takeuchiyoshihiko | 0:a064b2730f3e | 33 | } |
takeuchiyoshihiko | 0:a064b2730f3e | 34 | |
takeuchiyoshihiko | 0:a064b2730f3e | 35 | void post(void) |
takeuchiyoshihiko | 0:a064b2730f3e | 36 | { |
takeuchiyoshihiko | 0:a064b2730f3e | 37 | HTTPClient http; |
takeuchiyoshihiko | 0:a064b2730f3e | 38 | HTTPResult ret; |
takeuchiyoshihiko | 0:a064b2730f3e | 39 | char str[512]; |
takeuchiyoshihiko | 0:a064b2730f3e | 40 | char message[128]; |
takeuchiyoshihiko | 0:a064b2730f3e | 41 | |
takeuchiyoshihiko | 0:a064b2730f3e | 42 | HTTPText inText(str, 1024); |
takeuchiyoshihiko | 0:a064b2730f3e | 43 | |
takeuchiyoshihiko | 0:a064b2730f3e | 44 | char post_template[] = "{\"app\": %i, \"record\": {\"temp\": {\"value\": \"%.1f\"}}}\n"; |
takeuchiyoshihiko | 0:a064b2730f3e | 45 | char header_template[] = "X-Cybozu-API-Token: %s\nContent-Type: application/json\n"; |
takeuchiyoshihiko | 0:a064b2730f3e | 46 | char post_data[256]; |
takeuchiyoshihiko | 0:a064b2730f3e | 47 | char header_data[256]; |
takeuchiyoshihiko | 0:a064b2730f3e | 48 | sprintf(post_data, post_template, APP_ID, (float)sensor); |
takeuchiyoshihiko | 0:a064b2730f3e | 49 | sprintf(header_data, header_template, API_TOKEN); |
takeuchiyoshihiko | 0:a064b2730f3e | 50 | |
takeuchiyoshihiko | 0:a064b2730f3e | 51 | HTTPText outText(post_data, strlen(post_data)); |
takeuchiyoshihiko | 0:a064b2730f3e | 52 | |
takeuchiyoshihiko | 0:a064b2730f3e | 53 | http.setHeader(header_data) ; |
takeuchiyoshihiko | 0:a064b2730f3e | 54 | http.setSSLversion(3) ; /* TLSv1.2 */ |
takeuchiyoshihiko | 0:a064b2730f3e | 55 | ret = http.post(POST_URL, outText, &inText); |
takeuchiyoshihiko | 0:a064b2730f3e | 56 | if (ret == HTTP_OK) { |
takeuchiyoshihiko | 0:a064b2730f3e | 57 | sprintf(message, "OK: %s", str); |
takeuchiyoshihiko | 0:a064b2730f3e | 58 | display(message); |
takeuchiyoshihiko | 0:a064b2730f3e | 59 | } else { |
takeuchiyoshihiko | 0:a064b2730f3e | 60 | sprintf(message, "ERROR: %d", http.getHTTPResponseCode()); |
takeuchiyoshihiko | 0:a064b2730f3e | 61 | display(message); |
takeuchiyoshihiko | 0:a064b2730f3e | 62 | } |
takeuchiyoshihiko | 0:a064b2730f3e | 63 | } |
takeuchiyoshihiko | 0:a064b2730f3e | 64 | |
takeuchiyoshihiko | 0:a064b2730f3e | 65 | int main() |
takeuchiyoshihiko | 0:a064b2730f3e | 66 | { |
takeuchiyoshihiko | 0:a064b2730f3e | 67 | int check = 0; |
takeuchiyoshihiko | 0:a064b2730f3e | 68 | char message[128]; |
takeuchiyoshihiko | 0:a064b2730f3e | 69 | |
takeuchiyoshihiko | 0:a064b2730f3e | 70 | // Initialize Wi-Fi interface |
takeuchiyoshihiko | 0:a064b2730f3e | 71 | check = wifi.init(); |
takeuchiyoshihiko | 0:a064b2730f3e | 72 | if (check != 0) { |
takeuchiyoshihiko | 0:a064b2730f3e | 73 | display("Wifi could not be initialized"); |
takeuchiyoshihiko | 0:a064b2730f3e | 74 | return -1; |
takeuchiyoshihiko | 0:a064b2730f3e | 75 | } else { |
takeuchiyoshihiko | 0:a064b2730f3e | 76 | display("wifi initialized successfully"); |
takeuchiyoshihiko | 0:a064b2730f3e | 77 | } |
takeuchiyoshihiko | 0:a064b2730f3e | 78 | wait(0.5); |
takeuchiyoshihiko | 0:a064b2730f3e | 79 | |
takeuchiyoshihiko | 0:a064b2730f3e | 80 | // good form to make sure you are disconnected from all AP's |
takeuchiyoshihiko | 0:a064b2730f3e | 81 | check = wifi.disconnect(); |
takeuchiyoshihiko | 0:a064b2730f3e | 82 | if (check != 0) { |
takeuchiyoshihiko | 0:a064b2730f3e | 83 | display("disconnect failed"); |
takeuchiyoshihiko | 0:a064b2730f3e | 84 | return -1; |
takeuchiyoshihiko | 0:a064b2730f3e | 85 | } else { |
takeuchiyoshihiko | 0:a064b2730f3e | 86 | display("disconnection successful"); |
takeuchiyoshihiko | 0:a064b2730f3e | 87 | } |
takeuchiyoshihiko | 0:a064b2730f3e | 88 | wait(0.3); |
takeuchiyoshihiko | 0:a064b2730f3e | 89 | |
takeuchiyoshihiko | 0:a064b2730f3e | 90 | // Connect AP |
takeuchiyoshihiko | 0:a064b2730f3e | 91 | wifi.connect( DEMO_AP_SSID |
takeuchiyoshihiko | 0:a064b2730f3e | 92 | , strlen(DEMO_AP_SSID) |
takeuchiyoshihiko | 0:a064b2730f3e | 93 | , DEMO_AP_SECURITY_TYPE |
takeuchiyoshihiko | 0:a064b2730f3e | 94 | , DEMO_AP_SECUTIRY_KEY |
takeuchiyoshihiko | 0:a064b2730f3e | 95 | , strlen(DEMO_AP_SECUTIRY_KEY) ); |
takeuchiyoshihiko | 0:a064b2730f3e | 96 | wait(0.5); |
takeuchiyoshihiko | 0:a064b2730f3e | 97 | |
takeuchiyoshihiko | 0:a064b2730f3e | 98 | // Get DHCP IP |
takeuchiyoshihiko | 0:a064b2730f3e | 99 | check = wifi.setIPConfig(true); |
takeuchiyoshihiko | 0:a064b2730f3e | 100 | if (check != 0) { |
takeuchiyoshihiko | 0:a064b2730f3e | 101 | display("SetIPConfig failed"); |
takeuchiyoshihiko | 0:a064b2730f3e | 102 | return -1; |
takeuchiyoshihiko | 0:a064b2730f3e | 103 | } else { |
takeuchiyoshihiko | 0:a064b2730f3e | 104 | display("SetIPConfig successful"); |
takeuchiyoshihiko | 0:a064b2730f3e | 105 | } |
takeuchiyoshihiko | 0:a064b2730f3e | 106 | sprintf(message, "IP: %s", wifi.getIPAddress()); |
takeuchiyoshihiko | 0:a064b2730f3e | 107 | display(message); |
takeuchiyoshihiko | 0:a064b2730f3e | 108 | |
takeuchiyoshihiko | 0:a064b2730f3e | 109 | while (true) { |
takeuchiyoshihiko | 0:a064b2730f3e | 110 | post(); |
takeuchiyoshihiko | 0:a064b2730f3e | 111 | wait(60); |
takeuchiyoshihiko | 0:a064b2730f3e | 112 | } |
takeuchiyoshihiko | 0:a064b2730f3e | 113 | |
takeuchiyoshihiko | 0:a064b2730f3e | 114 | wifi.disconnect(); |
takeuchiyoshihiko | 0:a064b2730f3e | 115 | } |