메뉴 건너뛰기

프로그래밍


아두이노에서 두근두근 OPEN API를 인증키와 함께 POST로 호출하는 예제 코드입니다.

(The way how to call DKDK Open API from Arduino by HTTPS POST Method)


#include <WiFiClientSecure.h>

  const int httpPort = 443; //for https
  const char* host = "api.dkdk.io";

void requestCall() {
  WiFiClientSecure client;

  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  String url = "/v2/dkdk"; //resource path

  // post data
  String jsonbody = String("{\r\n \"user_uuid\" : \"MY_USER_UUID\",\r\n  \"action\" : \"touch\"\r\n  \"friend_uuids\" : [\"1234-567\"]\r\n  \"pattern_uuid\" : \"1234-1234\"\r\n}";

  // This will send the request to the server
  String requestBody = String("POST ") + url + " HTTP/1.1\r\n" +
             "Host: " + host + "\r\n" +
             "dkdk-token: DKDK-TOKEN\r\n" +
             "Content-Type: application/json\r\n" +
             "Cache-Control: no-cache\r\n" +
             "Content-length: " + String(jsonbody.length()) + "\r\n\r\n" +
             jsonbody;

  client.print(requestBody); 
 
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
}


건투를 빕니다!






위로