forked from hi-rou/hirou-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoBLE.ino
138 lines (115 loc) · 4.98 KB
/
ArduinoBLE.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <ArduinoBLE.h>
#include <Adafruit_NeoPixel.h>
#include "HX711.h" // HX711 라이브러리 추가
// BLE 서비스 및 특성 UUID 정의
BLEService myService("12345678-1234-5678-1234-567812345678");
BLEStringCharacteristic myCharacteristic("87654321-8765-4321-8765-432187654321",
BLERead | BLEWrite, 20); // 최대 20바이트 문자열
#define PIN 4 // RGB LED 스트립이 연결된 아두이노의 핀 번호
#define NUMPIXELS 18 // LED 스트립에 있는 LED 개수
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
// HX711 모듈 핀 정의
#define LOADCELL_DOUT_PIN 3
#define LOADCELL_SCK_PIN 2
HX711 scale;
// dtostrf 함수 직접 구현
char* dtostrf(double val, signed char width, unsigned char prec, char *sout) {
sprintf(sout, "%*.*f", width, prec, val);
return sout;
}
void setup() {
Serial.begin(9600); // 시리얼 통신 시작
pixels.begin(); // NeoPixel 시작
if (!BLE.begin()) { // BLE 초기화
Serial.println("BLE 시작 실패");
while (1);
}
BLE.setLocalName("하이루"); // BLE 디바이스 이름 설정
BLE.setAdvertisedService(myService); // 광고할 서비스 추가
myService.addCharacteristic(myCharacteristic); // 서비스에 특성 추가
BLE.addService(myService); // BLE 서비스 추가
myCharacteristic.writeValue("물좀마셔조"); // 초기 특성 값 설정
BLE.advertise(); // BLE 광고 시작
Serial.println("BLE 디바이스가 준비되었습니다. 이제 연결할 수 있습니다.");
// 로드셀 초기화
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.tare(); // 시작 시 영점 조절
// 스케일 팩터 설정(로드셀)
scale.set_scale(400100.00);
// 초기 상태 (연결 대기 중) 노란색으로 설정
setAllPixelsColor(255, 255, 0); // 노란색
}
void loop() {
// BLE 중앙 장치와의 연결 대기
BLEDevice central = BLE.central();
// 중앙 장치가 연결되면
if (central) {
Serial.print("연결됨: ");
Serial.println(central.address());
// 연결되었을 때 초록색으로 설정
setAllPixelsColor(0, 255, 0); // 초록색
// 중앙 장치와 연결된 동안 파란색으로 유지
while (central.connected()) {
setAllPixelsColor(0, 0, 255); // 파란색
// 여기서 로드셀의 무게를 읽고 시리얼 모니터에 출력합니다.
float weight = scale.get_units(10); // 로드셀로부터 무게를 읽어옴, 5번 측정 후 평균값 사용, g에서 kg으로 변환
Serial.print("실시간 무게: ");
Serial.print(weight);
Serial.println(" kg"); // 단위를 kg으로 표시
if (myCharacteristic.written()) {
// 특성에 새로운 값이 쓰여진 경우
String value = myCharacteristic.value();
Serial.print("새로운 값: ");
Serial.println(value);
if (value == "A") {
// 영점 조절
scale.tare();
Serial.println("영점 조절 완료");
setAllPixelsColor(255, 105, 180); // 핑크색
delay(5000); // 5초 대기
setAllPixelsColor(0, 0, 255); // 다시 파란색으로 변경
} else if (value == "B") {
// 현재 무게를 BLE를 통해 전송
char weightStr[10];
dtostrf(weight, 1, 2, weightStr); // 무게를 문자열로 변환
myCharacteristic.writeValue(weightStr);
Serial.println("무게 전송");
setAllPixelsColor(255, 165, 0); // 주황색
delay(5000); // 5초 대기
setAllPixelsColor(0, 0, 255); // 다시 파란색으로 변경
} else if (value == "C") {
Serial.println("목표치 달성"); // 시리얼 모니터에 "목표치 달성" 출력
setAllPixelsColor(0, 255, 0); // LED를 초록색으로 변경
delay(5000); // 5초 대기
setAllPixelsColor(0, 0, 255); // 다시 파란색으로 변경
} else if (value == "D") {
// "물 추가"
Serial.println("물 추가");
setAllPixelsColor(0, 191, 255); // 하늘색
delay(5000); // 5초 대기
setAllPixelsColor(0, 0, 255); // 다시 파란색으로 변경
} else if (value == "E") {
// "물 버림"
Serial.println("물 버림");
setAllPixelsColor(255, 255, 0); // 노란색
delay(5000); // 5초 대기
setAllPixelsColor(0, 0, 255); // 다시 파란색으로 변경
}
}
}
// 중앙 장치와의 연결이 끊어지면 붉은색으로 설정
Serial.print("연결 해제: ");
Serial.println(central.address());
setAllPixelsColor(255, 0, 0); // 붉은색
} else {
// 중앙 장치가 연결되지 않았을 때 붉은색으로 설정
setAllPixelsColor(255, 0, 0); // 붉은색
}
}
// 모든 LED에 동일한 색상을 설정하는 함수
void setAllPixelsColor(byte red, byte green, byte blue) {
for(int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(red, green, blue));
}
pixels.show();
}