week__12 (ArduinoCloud&NTP) - JINEEH/SmartDevice_JinHee GitHub Wiki
1-1. ArduinoCloudλ₯Ό νμ©ν μ¨μ΅λ&LED μΌμ μ€μ΅ λ° NTP μλ²λ‘ μκ° μ 보 νμΈ
2-1. Arduinoμμ μ 곡νλ IoT ν΄λΌμ°λλ‘ μ¨μ΅λ λ°μ΄ν°λ₯Ό μ€μκ° λͺ¨λν°λ§κ³Ό μΈλΆ LED μ μ΄νλ μ€μ΅μμ μ¬μ©
2-2. μ€μ΅ μ€λΉλ¬Ό: ESP32, ESP32 νμ₯ μ€λ, DH11 μ¨μ΅λ μΌμ, LED λͺ¨λ, μ νΌ μΌμ΄λΈ
2-3. ArduinoCloud μ€μ λ° μ€νμ½λ
- Arduino νμκ°μ λ° ArduinoCloud μ΄λ
- Things -> CREATE THING
- νλ‘μ νΈ μ΄λ¦ λ³κ²½(ESP32)

- led_stateμ temperature μΆκ°νκΈ°(ADD)


- λλ°μ΄μ€μ λ€νΈμν¬ μ€μ




- Secret Key copy ν λ€νΈμν¬ μ°κ²° μ€μ μ μ λ ₯


- Sketch νμμ ".ino" νμΌμ μ€νμ½λ μ λ‘λ ν Dashboardμ μ‘μΈμ€νκΈ°
- μΈλΆ LED μ‘°μ κ°λ₯ν Switch μΆκ°νκΈ°
- μ¨μ΅λ μΌμμ λ°μ΄ν°λ₯Ό κ·Έλνλ‘ λνλ΄λ Chart μΆκ°νκΈ°
- μ€ν μ½λ(8-1)
#include "thingProperties.h"
#include "DHT.h"
int led_pin = 23;
int dh11_pin = 13;
DHT dht(dh11_pin, DHT11);
void setup() {
pinMode(led_pin, OUTPUT);
dht.begin();
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
// Read temperature as Celsius (the default)
temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(temperature)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("Temperature: "));
Serial.println(temperature);
delay(1000);
}
void onLedStateChange() {
// Add your code here to act upon LedState change
Serial.print("LED state is changed : ");
Serial.println(led_state);
if (led_state) {
digitalWrite(led_pin, HIGH);
} else {
digitalWrite(led_pin, LOW);
}
}
- μ€μ¬ κ²°κ³Ό
- μΈλΆ LED ON/OFF μ μ΄μ μ¨μ΅λ μ μ΄ Chart νμΈ
12.8-1.2.mp4
3-1. μλ―Έ: λ€νΈμν¬λ₯Ό ν΅ν΄ μ»΄ν¨ν° μμ€ν
κ°μ μκ°μ λκΈ°ννλ νλ‘ν μ½λ‘ μΈν°λ·κ³Ό λ‘컬 λ€νΈμν¬μμ μ¬μ©
- μλ²μ ν΄λΌμ΄μΈνΈ κ°μ μκ° μ 보λ₯Ό μ£Όκ³ λ°μ μκ°μ λκΈ°νμν΄
- μ λ°ν μμ μκ³ λλ GPSλ₯Ό ν΅ν΄ μ νν μκ°μ μ μ§ν¨
- ν΄λΌμ΄μΈνΈλ μ£ΌκΈ°μ μΌλ‘ NTP μλ²μ μ μνμ¬ μκ° μ 보λ₯Ό κ°±μ ν¨
3-2. NTP κ³μΈ΅ ꡬ쑰
- Stratum 0: μμ μκ³, GPS λ± μ λ μκ°μ μ 곡νλ μ₯μΉ
- Stratum 1: Stratum 0 μ₯μΉμ μ§μ μ°κ²°λ NTP μλ²
- Stratum 2: Stratum 1 μλ²μ λκΈ°νλ μλ²λ€
- Stratum 2~15: NTP μλ²λ₯Ό κ²½μ ν λ λ§λ€ Stratum 1μ© μ¦κ°νλ©° Stratum 2λΆν°λ κ³μΈ΅μ νΈλ¦¬ ꡬ쑰λ₯Ό νμ±ν¨

- 1μ΄ μ΅μμ μλ²μ΄λ©° NTP μλ²λ€μ΄ μ°κ²°λ μλ‘ μ«μκ° μ¦κ°ν¨

3-3. NTP μλ²μμ μκ°μ κ°μ Έμ OLED λμ€νλ μ΄μ νμνλ μ€μ΅
- μ€μ΅ μ€λΉλ¬Ό: ESP32, ESP32 νμ₯ μ€λ, OLED λμ€νλ μ΄, μ νΌ μΌμ΄λΈ
- Adafruit SSD1306 λΌμ΄λΈλ¬λ¦¬ μ€μΉ(Adafruit GFX λΌμ΄λΈλ¬λ¦¬ ν¬ν¨νμ¬ μ 체 μ€μΉ)
- ESP32 νλ‘ μ°κ²°

- μ€ν μ½λ(9-1)
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
// OLED λμ€νλ μ΄ μ΄κΈ°ν
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 μ€λ₯");
while (true);
}
// OLED λμ€νλ μ΄ ν΄λ¦¬μ΄
display.clearDisplay();
}
void loop() {
// "Hello, World!"λ₯Ό ν¬κΈ°κ° λ€λ₯Έ λ κ°μ ν
μ€νΈλ‘ νμ
display.clearDisplay();
// ν¬κΈ° 6μ ν°νΈλ‘ ν
μ€νΈ νμ
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Hello, World!");
// ν¬κΈ° 8μ ν°νΈλ‘ ν
μ€νΈ νμ
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 15);
display.println("Hello, World");
display.display();
delay(2000);
}
- μ€μ΅ κ²°κ³Ό

- display.println(" "); μ λ³νλ₯Ό μ€ λ€λ₯Έ κ²°κ³Ό

- μκ° λκΈ°ννμ¬ λμ§νΈ μκ³ κ΅¬ν(λ€νΈμν¬ μ€μ νμ)
- μ€ν μ½λ(9-2)
#include <WiFi.h> // WiFi ν΅μ μ μν λΌμ΄λΈλ¬λ¦¬
#include <time.h> // μκ°κ³Ό κ΄λ ¨λ ν¨μλ₯Ό μν λΌμ΄λΈλ¬λ¦¬
#include <Adafruit_GFX.h> // λμ€νλ μ΄λ₯Ό μν κ·Έλν½ λΌμ΄λΈλ¬λ¦¬
#include <Adafruit_SSD1306.h> // SSD1306 OLED λμ€νλ μ΄λ₯Ό μν λΌμ΄λΈλ¬λ¦¬
// SSD1306 λμ€νλ μ΄ κ°μ²΄ μ΄κΈ°ν
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire, -1);
const char* ssid = "zineeh"; // μ¬κΈ°μ μ¬μ©νλ WiFi λ€νΈμν¬ μ΄λ¦ (SSID)μ μ
λ ₯νμΈμ
const char* password = "SMARTDEVICE0527"; // μ¬κΈ°μ μ¬μ©νλ WiFi λ€νΈμν¬ λΉλ°λ²νΈλ₯Ό μ
λ ₯νμΈμ
int GMTOffset = 60 * 60 * 9; // μκ° μ€νμ
μ€μ , νκ΅μ UTC/GMT +9μ
λλ€.
int daylightOffset = 0; // κ΅κ°μμ μλ¨Ένμμ μ¬μ©νλ κ²½μ° μ€νμ
κ°μ μ€μ νμΈμ.
const String weekDays[7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; // μμΌ μ΄λ¦ λ°°μ΄
void setup() {
Serial.begin(115200); // λλ²κΉ
μ μν μλ¦¬μΌ ν΅μ μμ
// SSD1306 λμ€νλ μ΄ μ΄κΈ°ν
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 μ΄κΈ°ν μ€ν¨"));
while (1)
; // λμ€νλ μ΄ μ΄κΈ°νμ μ€ν¨νλ©΄ νλ‘κ·Έλ¨ μ€ν μ€μ§
}
delay(2000); // 2μ΄ λμ λκΈ°
display.clearDisplay(); // λμ€νλ μ΄ μ§μ°κΈ°
display.setTextSize(1); // ν
μ€νΈ ν¬κΈ°λ₯Ό 1λ‘ μ€μ
display.setCursor(0, 0); // 컀μ μμΉλ₯Ό λμ€νλ μ΄ μΌμͺ½ μ λͺ¨μλ¦¬λ‘ μ€μ
display.setTextColor(WHITE); // ν
μ€νΈ μμμ ν°μμΌλ‘ μ€μ
WiFi.begin(ssid, password); // μ 곡λ SSIDμ λΉλ°λ²νΈλ₯Ό μ¬μ©νμ¬ WiFi λ€νΈμν¬μ μ°κ²°
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to Wi-Fi!");
// μκ° μ€μ μ μν NTP μλ² μ€μ
configTime(GMTOffset, daylightOffset, "pool.ntp.org", "time.nist.gov");
}
void loop() {
// νμ¬ μκ° κ°μ Έμ€κΈ°
time_t rawtime = time(nullptr);
struct tm* timeinfo = localtime(&rawtime);
// μλ¦¬μΌ λͺ¨λν°μ λ μ§ μΆλ ₯
Serial.print(timeinfo->tm_mday);
Serial.print("/");
Serial.print(timeinfo->tm_mon + 1);
Serial.print("/");
Serial.print(timeinfo->tm_year + 1900);
Serial.print(" ");
// μλ¦¬μΌ λͺ¨λν°μ μκ° μΆλ ₯
Serial.print("Time: ");
Serial.print(timeinfo->tm_hour);
Serial.print(":");
Serial.print(timeinfo->tm_min);
Serial.print(":");
Serial.println(timeinfo->tm_sec);
// OLED λμ€νλ μ΄ μ΄κΈ°ν
display.clearDisplay();
display.setTextSize(3);
display.setTextColor(WHITE);
display.setCursor(0, 0);
// μκ° μΆλ ₯
if (timeinfo->tm_hour < 10)
display.print("0");
display.print(timeinfo->tm_hour);
display.print(":");
if (timeinfo->tm_min < 10)
display.print("0");
display.print(timeinfo->tm_min);
display.print(":");
// μ΄ μΆλ ₯
display.setTextSize(2);
display.setCursor(102, 5);
if (timeinfo->tm_sec < 10)
display.print("0");
display.print(timeinfo->tm_sec);
// λ μ§ μΆλ ₯
display.setTextSize(1);
display.setCursor(0, 25);
display.print(timeinfo->tm_mday);
display.print("/");
display.print(timeinfo->tm_mon + 1);
display.print("/");
display.print(timeinfo->tm_year + 1900);
display.print(" ");
display.print(weekDays[timeinfo->tm_wday]);
// λμ€νλ μ΄μ νμ
display.display();
delay(1000);
}
- μ€μ΅ κ²°κ³Ό
12.9-2.mp4
4-1. μ§λ ¬λ²μ€μ΄λ©° λ§λ보λ, μλ² λλ μμ€ν
, ν΄λ μ ν λ±μ μ μ κΈ°κΈ°λ₯Ό μ°κ²°ν λ μ¬μ©λ¨
- νμ
μ νμ΄ μ°κ²°λ μ§λ ¬ λ°μ΄ν°μ μ§λ ¬ ν΄λμ΄λΌλ λκ°μ μλ°©ν₯ μ€ν 컬λ ν° λΌμΈ μ¬μ©
- μ΅λ μ μμ +5Vμ΄μ§λ§ μΌλ°μ μΌλ‘ +3.3V μμ€ν
μ¬μ©
- IΒ²C λ νΌλ°μ€ λμμΈμ 7λΉνΈμ μ£Όμ 곡κ°μ κ°μ§λ©° λμΌν λ²μ€μ μ΅λ 112κ°μ λ
Έλκ° μ°κ²° κ°λ₯ν¨
- λΉ λ₯Έ μλλ₯Ό νμλ‘ νμ§ μλ κ°λ¨ν μ λΉμ© μ₯μΉμ μ ν©
- ν΄λμ ν κ°μ μ₯μΉμ ν¬ν¨λ λ°κ΄ λ€μ΄μ€λλ₯Ό μ μ΄νλ κ²½μ°, νλμ¨μ΄ λͺ¨λν°λ§κ³Ό μ§λ¨ μΌμ μ 보λ₯Ό μ½λ κ²½μ°, μ€μκ° ν΄λμ μ½λ κ²½μ°, μμ€ν
μμμ μ μμ μ μ΄νλ κ²½μ°
4-2. μ₯μ
- SDAμ SCL λκ°λ₯μ μ λ§μΌλ‘ λ°μ΄ν° μ‘μμ μ΄ κ°λ₯ν¨
- λκΈ°μ ν΅μ μ΄λ―λ‘ λΉλκΈ°μ ν΅μ (UART)μ λΉν΄ λ€μ μμ μ μ
- λ²μ€ ννλ‘ νκ°μ IΒ²C λ§μ€ν°μ λ€μμ μ¬λ μ΄λΈ μ₯μΉ μ°κ²° κ°λ₯
- λ°μ΄ν°λ₯Ό μ μ‘/μμ νλμ λ§μ€ν°μμ μ£Όλνλ©° λ°μ΄ν° μ‘μμ μ μ¬λ μ΄λΈ μ£Όμλ₯Ό λ§μ€ν°μ λͺ
μν΄μΌ ν΅μ μ΄ κ°λ₯
4-3. λ¨μ
- λΉλκΈ°μ ν΅μ (UART)λ³΄λ€ λΉ λ₯΄μ§λ§ SPIμ λΉκ΅νλ©΄ λλ¦Ό
- λ°μ΄μ€ ν΅μ μΌλ‘ νμͺ½μμ λ°μ΄ν°λ₯Ό λ³΄λΌ λ λ°λνΈμμ λ°μ΄ν° μ μ‘ λΆκ°
- ꡬ쑰μ 볡μ‘νμ¬ μ¬μ©νκΈ° μ΄λ ΅κ³ μ μ‘μ μν λ³λμ νλ‘λ₯Ό ꡬμ±ν΄μΌ ν¨
5-1. Floating Point Number(λΆλ μμμ μ): λ§€μ° ν¬κ±°λ μμ μμΉλ₯Ό μ§μμ κ°μ λΆλΆμΌλ‘ λλμ΄ νν
- μμμ νν, μμ£Ό μμ μ νν, μμ£Ό ν° μ νν
5-2. Adafruit: μ€νμμ€ν μ μ νλμ¨μ΄μ μ€κ³ λ° μμ°μ μ λ¬ΈμΌλ‘ νλ λΉκ³΅κ°κΈ°μ
μΌλ‘ λ―Έκ΅ λ³Έμ¬μμ μ νμ μ€κ³ λ° μμ°
- OLED λμ€νλ μ΄ μ€μ΅μμ μ¬μ©νλ©° OLED μ μ΄λ₯Ό μν΄ Adafruit_SSD1306 λΌμ΄λΈλ¬λ¦¬ μ€μΉμ κ°μ²΄ μ μΈ νμ
5-3. OLED λμ€νλ μ΄ μ€μ΅μμ λ°μνλ λ€νΈμν¬ μ°κ²° μ€λ₯(μμ΄ν°): κ°μΈ ν«μ€ν μ°κ²° ν νΈνμ± μ΅λνκ° νμ±νλμ΄ μλμ§ νμΈ
- νΈνμ± μ΅λνλ₯Ό νμ±νμν¨ ν μ€λ₯κ° ν΄κ²°λμ΄ μκ°μ΄ λμ€νλ μ΄μ νμλμμ§λ§ μ νν ν΄κ²°λ°©μμΈμ§ λͺ¨λ¦