환경 모니터링 스마트 시계 / environmental monitoring smart watch
환경 모니터링 스마트 시계
1
Components
Hardware components
WIZnet - WIZnet Ethernet HAT
x 1
Project description
1. 아이디어
과거의 시계는 시간만 알려주면 충분했지만 지금은 미세먼지가 심각한 건강을 위협하는 문제가 되어 매일마다 미세먼지를 체크하고 대응해야하는 데 이를 손쉽게 알려주는 시계가 있으면 좋겠다고 생각했습니다. 사무실이나 가정에서 벽시계로 사용하면서 미세먼지와 온도를 한눈에 볼 수 있으면서 공기 질 또는 온도 상태가 위험 수준인 경우 문자로 알림 메시지를 보내주는 시계가 있으면 유용하지 않을까 하여 제작해보기로 하였습니다.
2. 개요
- Raspberry Pi Pico 사용 내장된 RTC로 현재 시간 측정
- WIZnet RP2040-HAT또는 W5100S-EVB-Pico를 사용하여 유선 네트워크 구축
- NTP(Network Time Protocol)를 사용하여 수동으로 시간을 맞추는 대신 자동으로 인터넷을 통한 시간 교정
- 미세먼지 센서를 통한 PM2.5 미세먼지 측정
- 온도 센서를 통한 온도 측정
- 사용자가 시계 IP로 접속하면 시계의 상태를 볼 수 있으며 몇가지 세팅을 할 수 있도록 하는 HTTP 웹 서버 기능
- 특정 온도보다 높거나 낮은 경우 또는 미세먼지 수치가 특정 수치보다 높은 경우 지정된 번호로 문자 메시지 전송
- 미세먼지 수치 정도에 따른 시계 테두리 조명 색상 표시
ETC. 참조 코드
ChatGPT를 사용해서 작성한 예시 코드입니다. 참조해서 사용할 수 있습니다.
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <NTPClient.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <SoftwareSerial.h>
#include <TinyGsmClient.h>
#include <GPRSClient.h>
// PM2.5 미세먼지 센서와 BME280 온도/습도/기압 센서를 위한 객체 생성
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);
#define PM_SERIAL_RX 5
#define PM_SERIAL_TX 6
SoftwareSerial pmsSerial(PM_SERIAL_RX, PM_SERIAL_TX);
#define PMS_RX 5
#define PMS_TX 6
SoftwareSerial pms(PMS_RX, PMS_TX);
#define PMS5003_TIMEOUT 1000
#include "PMS.h"
PMS pms5003;
// GPRS 연결 설정
#define APN "your_apn"
#define GPRS_USER "your_user"
#define GPRS_PASS "your_pass"
#define PHONE_NUMBER "+1234567890"
#define MESSAGE_BODY "Warning: temperature is above normal range."
// GPRSClient를 이용한 SMS 전송을 위한 객체 생성
TinyGsm modem(Serial1);
GPRSClient client(modem);
// 네트워크 설정 및 클라이언트 객체 생성
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 1, 177);
IPAddress server(192, 168, 1, 1);
EthernetClient ethClient;
EthernetUDP udp;
NTPClient timeClient(udp, "pool.ntp.org");
// HTTP 요청을 처리하기 위한 버퍼 생성
char buffer[256];
void setup() {
// 네트워크 및 클라이언트 초기화
Ethernet.begin(mac, ip);
timeClient.begin();
pmsSerial.begin(9600);
pms5003.init(pmsSerial);
Serial1.begin(115200);
modem.restart();
modem.init();
modem.waitForNetwork();
Serial.begin(9600);
}
void loop() {
// 현재 시간 업데이트
timeClient.update();
// 온도/습도/기압 센서에서 데이터 수집
float temperature = bme.readTemperature();
float humidity = bme.readHumidity();
float pressure = bme.readPressure() / 100.0F;
// PM2.5 미세먼지 센서에서 데이터 수집
if (pms5003.read()) {
float pm25 = pms5003.getPM25();
// PM2.5 농도에 따라 시계 테두리 조명 색상 변경
if (pm25 <= 15) {
analogWrite(3, 0);
analogWrite(5, 255);
analogWrite(6, 0);
} else if (pm25 <= 35) {
analogWrite(3
// check if the temperature is above the threshold
if (temperature > TEMP_THRESHOLD) {
// send SMS alert
sendSMSAlert("+1234567890", "Temperature alert: " + String(temperature) + " degrees Celsius");
}
// check if the PM2.5 value is above the threshold
if (pm25 > PM25_THRESHOLD) {
// send SMS alert
sendSMSAlert("+1234567890", "PM2.5 alert: " + String(pm25) + " ug/m3");
}
// set LED color based on PM2.5 value
if (pm25 <= 15) {
setLEDColor(0, 255, 0); // green
} else if (pm25 <= 35) {
setLEDColor(255, 255, 0); // yellow
} else if (pm25 <= 75) {
setLEDColor(255, 127, 0); // orange
} else {
setLEDColor(255, 0, 0); // red
}
// handle incoming HTTP requests
WiFiClient client = server.available();
if (client) {
Serial.println("New client connected.");
while (client.connected()) {
if (client.available()) {
String request = client.readStringUntil('\r');
Serial.println(request);
if (request.indexOf("/get_data") != -1) {
// return JSON data with sensor values
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println("Access-Control-Allow-Origin: *");
client.println();
client.println("{\"temperature\":" + String(temperature) + ",\"pm25\":" + String(pm25) + "}");
} else if (request.indexOf("/set_led") != -1) {
// parse RGB values from query parameters
int rIndex = request.indexOf("r=") + 2;
int gIndex = request.indexOf("&g=") + 3;
int bIndex = request.indexOf("&b=") + 3;
int r = request.substring(rIndex, gIndex - 3).toInt();
int g = request.substring(gIndex, bIndex - 3).toInt();
int b = request.substring(bIndex).toInt();
// set LED color
setLEDColor(r, g, b);
// return success message
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/plain");
client.println("Access-Control-Allow-Origin: *");
client.println();
client.println("LED color set.");
} else {
// handle other requests with 404 error
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/plain");
client.println("Access-Control-Allow-Origin: *");
client.println();
client.println("Not found.");
}
break;
}
}
client.stop();
Serial.println("Client disconnected.");
}
}
