아두이노 IoT 클라우드 API가 생성한 스켈레톤 템플릿: MKR WiFi 보드에서 수신한 데이터를 표시
아두이노 MKRENV 라이브러리: ENV 실드 센서들의 측정값 읽기
아두이노 IoT 클라우드 Thing은 MKR WiFi 1010이 수집한 6가지 측정값을 보관하는 6개의 속성으로 구성됩니다. 아두이노 스케치는 IoT 클라우드에서 자동 생성되며, 인터넷 연결과 Thing-보드 간 데이터 전송을 처리합니다. 여기에 MKRENV 라이브러리를 추가해 ENV 실드 센서를 관리합니다.
스케치 상세
라이브러리 포함
#include "thingProperties.h"
#include
thingProperties.h: WiFi 연결 기능과 클라우드 연결 관리 포함. 사용자는 WiFi 자격 증명(SSID, PASSWORD)만 입력하면 됩니다.
아두이노 IoT 클라우드는 디바이스 설정 시 보드 암호 칩에 저장된 키로 인증하여 보안 채널을 보장합니다.
Arduino_MKRENV 라이브러리: ENV 실드 모든 센서의 측정값 읽기 기능 제공
Setup 함수
void setup() {
Serial.begin(9600);
delay(1500);
initProperties(); // thingProperties.h에 정의
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
if (!ENV.begin()) {
Serial.println("Failed to initialize MKR ENV shield!");
while(1);
}
}
속성 생성이 완료되면 edit code 버튼을 클릭해 아두이노 크리에이트 웹 에디터로 이동합니다. 첫 번째 탭의 메인 스케치를 아래 코드로 교체하세요.
/* Sketch generated by the Arduino IoT Cloud Thing "env_shield"
https://create.arduino.cc/cloud/things/829941ed-efdd-4572-91c0-e93a732192ec
Arduino IoT Cloud Properties description
The following variables are automatically generated and updated when changes are made to the Thing properties
int humidity;
int lux;
int pressure;
int temperature;
int uva;
int uvb;
int uvi;
Properties which are marked as READ/WRITE in the Cloud Thing will also have functions which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include
void setup() {
Serial.begin(9600);
delay(1500);
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(4);
ArduinoCloud.printDebugInfo();
if (!ENV.begin()) {
Serial.println("Failed to initialize MKR ENV shield!");
while(1);
}
}
void loop() {
ArduinoCloud.update();
humidity = int(ENV.readHumidity());
lux = int(ENV.readIlluminance());
pressure = int(ENV.readPressure());
temperature = int(ENV.readTemperature());
uva = int(ENV.readUVA());
uvb = int(ENV.readUVB());
uvi = int(ENV.readUVIndex());
delay(1000);
}
void onHumidityChange() { }
void onLuxChange() { }
void onPressureChange() { }
void onTemperatureChange() { }
void onUvaChange() { }
void onUvbChange() { }
void onUviChange() { }
스케치를 보드에 업로드하면, 연결이 성립된 후 MKR WiFi 1010이 ENV 실드 센서 값을 주기적으로 클라우드에 동기화하며, 생성된 Thing의 대시보드에서 확인할 수 있습니다.
아두이노 크리에이트의 스케치는 아래 코드 섹션에서도 확인 가능합니다.
아두이노 IoT 클라우드, MKR WiFi 1010, 세련된 MKR ENV 실드로 나만의 홈/가든 센서를 만들어보세요!