Hyungsun Yoon

[HW] Init with Arduino Code

1 +# 스마트 약병 IoT 하드웨어 및 연결단 개발
2 +
3 +## 지도교수
4 ++ 유인태 교수님
5 +
6 +## 개발담당자
7 ++ 2015100552 컴퓨터공학과 윤형선
8 +
9 +## 일정
10 ++ 2021.05.03: Init(개인 아두이노 코드 git에서 이전)
1 +
2 +#include <SoftwareSerial.h>
3 +#include <DHT.h>
4 +#include <TM1637Display.h>
5 +#include <Adafruit_NeoPixel.h>
6 +#include <string.h>
7 +
8 +//-------------Bluetooth--------------//
9 +#define BLUETXPIN 2
10 +#define BLUERXPIN 3
11 +
12 +const byte numChars = 15;
13 +char inputdata[numChars]; // an array to store the received data
14 +
15 +boolean newData = false;
16 +
17 +//--------Temperature, Humidity--------//
18 +#define DHTPIN A2
19 +#define DHTTYPE DHT22
20 +
21 +DHT dht(DHTPIN, DHTTYPE);
22 +
23 +//-------------Ultrasonic-------------//
24 +#define USTRIGPIN A0
25 +#define USECHOPIN A1
26 +
27 +long duration;
28 +long distance;
29 +
30 +//--------------Magnetic--------------//
31 +#define MAGPIN 13
32 +int mag_value = 0;
33 +
34 +SoftwareSerial wirelessSerial(BLUETXPIN, BLUERXPIN);
35 +
36 +//--------------7Segment--------------//
37 +#define TM1637CLKPIN 5
38 +#define TM1637DIOPIN 4
39 +
40 +TM1637Display display = TM1637Display(TM1637CLKPIN, TM1637DIOPIN);
41 +
42 +// All segments on:
43 +const uint8_t data[] = {0xff, 0xff, 0xff, 0xff};
44 +// All segments off:
45 +const uint8_t blank[] = {0x00, 0x00, 0x00, 0x00};
46 +
47 +//--------------NeoPixel--------------//
48 +#define NEOPIXELPIN 6
49 +#define NUMLED 8
50 +Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMLED, NEOPIXELPIN, NEO_GRB + NEO_KHZ800);
51 +
52 +//------------Entrypoint--------------//
53 +void setup() {
54 + // Temp, Hum
55 + dht.begin();
56 +
57 + // Ultrasonic
58 + pinMode(USTRIGPIN, OUTPUT);
59 + pinMode(USECHOPIN, INPUT);
60 +
61 + // Magnetic
62 + pinMode(MAGPIN, INPUT);
63 +
64 + // Bluetooth
65 + wirelessSerial.begin(9600);
66 +
67 + // 7Segment
68 + display.clear();
69 + display.setBrightness(7);
70 +
71 + // NeoPixel
72 + strip.begin();
73 + strip.show();
74 +
75 + // Test
76 + Serial.begin(9600);
77 +}
78 +
79 +//------------Functions-------------//
80 +void recvWithEndMarker() {
81 + static byte ndx = 0;
82 + char endMarker = '\n';
83 + char rc;
84 +
85 + while (wirelessSerial.available() > 0 && newData == false) {
86 + rc = wirelessSerial.read();
87 +
88 + if (rc != endMarker) {
89 + inputdata[ndx] = rc;
90 + ndx++;
91 + if (ndx >= numChars) {
92 + ndx = numChars - 1;
93 + }
94 + }
95 + else {
96 + inputdata[ndx] = '\0'; // terminate the string
97 + ndx = 0;
98 + newData = true;
99 + }
100 + }
101 + if (newData == true) {
102 + Serial.print("TESTTSETESTST ...... ");
103 + Serial.println(inputdata);
104 + Serial.println(strlen(inputdata));
105 +
106 + int inputdata_len = strlen(inputdata) - 10;
107 + Serial.print("INPUTDATALEN: ");
108 + Serial.println(inputdata_len);
109 + inputdata[inputdata_len] = '\0';
110 +
111 + Serial.println(inputdata);
112 + Serial.println(strlen(inputdata));
113 + }
114 +}
115 +
116 +void showNewData() {
117 + Serial.print("This just in ... ");
118 + Serial.println(inputdata);
119 + Serial.println(strlen(inputdata));
120 +}
121 +
122 +void ultrasonicSensor() {
123 + // Send signal
124 + digitalWrite(USTRIGPIN, LOW);
125 + delayMicroseconds(2);
126 + digitalWrite(USTRIGPIN, HIGH);
127 + delayMicroseconds(10);
128 + digitalWrite(USTRIGPIN, LOW);
129 +
130 + // Save duration(Oneway x 2)
131 + duration = pulseIn(USECHOPIN, HIGH);
132 +
133 + // Calculate distance
134 + Serial.print("Duration: ");
135 + Serial.println(duration);
136 + distance = (duration * 17) / 1000 ;
137 +
138 + // Test
139 + Serial.print("Distance: ");
140 + Serial.print(distance);
141 + Serial.println("cm");
142 +
143 + // Send thw BLE
144 + wirelessSerial.write(distance);
145 +}
146 +
147 +void temphumSensor() {
148 + // Get humidity and temperature
149 + float h = dht.readHumidity();
150 + float t = dht.readTemperature();
151 +
152 + // Test
153 + Serial.print("Humidity: ");
154 + Serial.print(h);
155 + Serial.print(" %\t");
156 + Serial.print("Temperature: ");
157 + Serial.print(t);
158 + Serial.println(" *C");
159 +
160 + // Send thw BLE
161 + wirelessSerial.write(h);
162 + wirelessSerial.write(t);
163 +}
164 +
165 +void magneticSensor() {
166 + // Get mag value
167 + mag_value = digitalRead(MAGPIN);
168 +
169 + // Test
170 + Serial.print("Magnetic: ");
171 + Serial.println(mag_value);
172 +
173 + // Send thw BLE
174 + wirelessSerial.write(mag_value);
175 +}
176 +
177 +void cycleNeoPixel() {
178 + for (int i = 0; i < NUMLED; i++) {
179 + strip.setPixelColor(i % NUMLED, 0, 0, 0);
180 + strip.setPixelColor((i + 1) % NUMLED, 0, 1, 2);
181 + strip.setPixelColor((i + 2) % NUMLED, 0, 4, 3);
182 + strip.setPixelColor((i + 3) % NUMLED, 0, 16, 9);
183 + strip.setPixelColor((i + 4) % NUMLED, 0, 30, 16);
184 + strip.setPixelColor((i + 5) % NUMLED, 0, 60, 31);
185 + strip.setPixelColor((i + 6) % NUMLED, 0, 0, 0);
186 + strip.setPixelColor((i + 7) % NUMLED, 1, 2, 0);
187 +
188 + strip.show();
189 + delay(80);
190 + }
191 +}
192 +
193 +void offNeoPixel() {
194 + for (int i = 0; i < NUMLED; i++) {
195 + strip.setPixelColor(i, 0, 0, 0);
196 + }
197 + strip.show();
198 +}
199 +
200 +void doseDisplay() {
201 + Serial.print("Dose: ");
202 + Serial.println(inputdata);
203 + Serial.println(int(inputdata[0] - '0'));
204 + for (int i = 0; i < 10; i++) {
205 + cycleNeoPixel();
206 + display.showNumberDec(int(inputdata[0] - '0'), false, 1, 3);
207 + }
208 + display.clear();
209 + offNeoPixel();
210 +}
211 +
212 +void dateDisplay() {
213 + Serial.print("Last time: ");
214 + Serial.print(inputdata[0]);
215 + Serial.print(inputdata[1]);
216 + Serial.print(inputdata[2]);
217 + Serial.println(inputdata[3]);
218 +
219 + display.showNumberDec(int(inputdata[0] - '0'), false, 1, 0);
220 + display.showNumberDec(int(inputdata[1] - '0'), false, 1, 1);
221 + display.showNumberDec(int(inputdata[2] - '0'), false, 1, 2);
222 + display.showNumberDec(int(inputdata[3] - '0'), false, 1, 3);
223 + delay(5000);
224 + display.clear();
225 +}
226 +
227 +
228 +//---------------Main----------------//
229 +void loop() {
230 + // Data
231 + if (newData == false) {
232 + recvWithEndMarker();
233 + }
234 +
235 + // Call Functions
236 + if (newData == true) {
237 + showNewData();
238 + // Menu
239 + if (inputdata[0] == 'A') {
240 + ultrasonicSensor();
241 + }
242 + else if (inputdata[0] == 'B') {
243 + temphumSensor();
244 + }
245 + else if (inputdata[0] == 'C') {
246 + magneticSensor();
247 + }
248 + else if (strlen(inputdata) < 4) {
249 + doseDisplay();
250 + } else {
251 + dateDisplay();
252 + }
253 + newData = false;
254 + }
255 +}
...\ No newline at end of file ...\ No newline at end of file
1 +from bluetooth import *
2 +
3 +socket = BluetoothSocket( RFCOMM )
4 +socket.connect(("04:A3:16:99:C7:42", 1))
5 +print("bluetooth connected!")
6 +
7 +msg = input("send message : ")
8 +socket.send(msg)
9 +
10 +print("finished")
11 +socket.close()
...\ No newline at end of file ...\ No newline at end of file