본문 바로가기
kipfa 필기

0712_HttpRequest 예제(성공)

by 헤옹스 2017. 7. 12.

/*

 *  HTTP over TLS (HTTPS) example sketch

 *

 *  This example demonstrates how to use

 *  WiFiClientSecure class to access HTTPS API.

 *  We fetch and display the status of

 *  esp8266/Arduino project continuous integration

 *  build.

 *

 *  Created by Ivan Grokhotkov, 2015.

 *  This example is in public domain.

 */


#include <ESP8266WiFi.h>

#include <WiFiClientSecure.h>


const char* ssid = "kipfa-class1_2.4G";

const char* password = "classroom1";


const char* host = "api.thingspeak.com";

const int httpsPort = 443;


// Use web browser to view and copy

// SHA1 fingerprint of the certificate

const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C";




void setup() {

  Serial.begin(115200);

  Serial.println();

  Serial.print("connecting to ");

  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }

  Serial.println("");

  Serial.println("WiFi connected");

  Serial.println("IP address: ");

  Serial.println(WiFi.localIP());


}


int cnt =0;

void loop() {


  // Use WiFiClientSecure class to create TLS connection

  WiFiClientSecure client;

  Serial.print("connecting to ");

  Serial.println(host);

  if (!client.connect(host, httpsPort)) {

    Serial.println("connection failed");

    return;

  }


/*

  if (client.verify(fingerprint, host)) {

    Serial.println("certificate matches");

  } else {

    Serial.println("certificate doesn't match");

  }

  */


  String url = "/update?api_key=IQ6CHNZA7GFJ06YX&field1=" + String(cnt);

  Serial.print("requesting URL: ");

  Serial.println(url);


  client.print(String("GET ") + url + " HTTP/1.1\r\n" +

               "Host: " + host + "\r\n" +

               "User-Agent: BuildFailureDetectorESP8266\r\n" +

               "Connection: close\r\n\r\n");

  cnt += 10;


  Serial.println("request sent");

  while (client.connected()) {

    String line = client.readStringUntil('\n');

    if (line == "\r") {

      Serial.println("headers received");

      break;

    }

  }

  String line = client.readStringUntil('\n');

  if (line.startsWith("{\"state\":\"success\"")) {

    Serial.println("esp8266/Arduino CI successfull!");

  } else {

    Serial.println("esp8266/Arduino CI has failed");

  }

  Serial.println("reply was:");

  Serial.println("==========");

  Serial.println(line);

  Serial.println("==========");

  Serial.println("closing connection");  

  delay(20000);                             // 20초 간격으로 값을 전송받아서 thinkspeak 서버에 전송된 데이터 손실이 일어나지 않도록 함.

                                            // ThinkSpeak 사이트는 단지 떡밥 site.

                                            // MATLAB 에서 데이터 수집하기 위해 사용자에게 무료로 풀어놓은 무료서버임.

}                                           // 떡밥사이트기때문에 한정된 자원을 투자하여 10, 15초 간격으로 전송한 데이터들은 무시해버림ㅎㅎ 

                                            // 천조국에서 공짜란없당!









'kipfa 필기' 카테고리의 다른 글

0713_먼지센서 구동_Sketch에서 실행  (0) 2017.07.13
0713_먼지센서(SDS011 Air Quality Sensor) 이용하기  (0) 2017.07.13
0712_Blink 예제(성공)  (0) 2017.07.12
0712_예제 - Hello server(성공)  (0) 2017.07.12
0712  (0) 2017.07.12