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