IoT/Arduino

[Wemos D1] 2. Thinkspeak에 데이터를 올리는 방법

반나무 2020. 8. 27. 20:53

두번째로 @Thinkspeak에 데이터를 올리는 방법을 소개하겠습니다.

 

우선 Thinkspeak란 IoT센서 값이나 다양한 데이터들을 모아 그래프로 볼 수 있게 해주는? 사이트라고 생각해주시면 되겠습니다.

 

https://thingspeak.com/

 

IoT Analytics - ThingSpeak Internet of Things

Weather Station This project shows how to build an Arduino-based weather station that sends data to ThingSpeak. Once the data is collected, MATLAB is used to view trends of the data, plot histograms of the data, calculate dew point from the raw temperature

thingspeak.com

우선 여기 회원가입을 하신다음

 

새로운 채널을 만들어줍니다.

Name : 채널이름

Description : 채널설명

Field 1 : 저장될 데이터 필드 1

 

더필요하면 2,3 쭉쭉 만들어 사용하면됩니다.


원하는 대로 만드셨다면 

API Keys를 눌러 API창으로 들어가 본인의 API키를 확인합니다.

  ////////////////////////////////////
  // 토양 수분센서 단
  
  // thingSpeak에 값 전송 하는데 필요한 대기시간 15초 이상 
  unsigned long currentMillis = millis();
  if((currentMillis % 16000) == 0){
    
    // 토양 수분센서로부터 측정된 값을 읽습니다.
    int value = analogRead(soil);
  
    // 토양 수분센서로부터 측정된 값를 시리얼 모니터에 출력합니다.
    Serial.print("read sensor value : ");
    Serial.println(value);
    
    // server api.thingspeak.com의 포트 80에 접속한다.
    if(client.connect(server, 80)){ // 접속성공시 true, 실패시 false return
      // apiKey의 뒤에 field1값을 붙이서 보낼 값을 만들어준다.
      String postStr = apiKey;
      postStr += "&field1=";
      postStr += String(value);
      postStr += "\r\n\r\n";
      
      // POST로 값 전송
      client.print("POST /update HTTP/1.1\n");
      client.print("Host: api.thingspeak.com\n");
      client.print("Connection: close\n");
      client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
      client.print("Content-Type: application/x-www-form-urlencoded\n");
      client.print("Content-Length: ");
      client.print(postStr.length());
      client.print("\n\n");
      client.print(postStr);
    }
  }

 

반응형