BimoSora

home station

Jun 13th, 2021 (edited)
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.42 KB | None | 0 0
  1. // bluetooth
  2. #include <SoftwareSerial.h>
  3.  
  4. SoftwareSerial mySerialOne(PA2, PA3); // RX, TX
  5. SoftwareSerial mySerialTwo(PA9, PA10); // RX, TX
  6. //end
  7.  
  8. // wire i2c
  9. #include <Wire.h>
  10. TwoWire Wire2(PB11,PB10);
  11.  
  12. // bme280
  13. #include <SPI.h>
  14. #include <Adafruit_Sensor.h>
  15. #include <Adafruit_BME280.h>
  16.  
  17. // #define BME_SCK 13
  18. // #define BME_MISO 12
  19. // #define BME_MOSI 11
  20. // #define BME_CS 10
  21.  
  22. #define SEALEVELPRESSURE_HPA (1013.25)
  23.  
  24. Adafruit_BME280 bme; // I2C
  25. //Adafruit_BME280 bme(BME_CS); // hardware SPI
  26. //Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
  27. // end
  28.  
  29. // tongkat suhu
  30. #include <OneWire.h>
  31. #include <DallasTemperature.h>
  32. #define ONE_WIRE_BUS A1
  33. OneWire oneWire(ONE_WIRE_BUS);
  34. DallasTemperature sensors(&oneWire);
  35. // end
  36.  
  37. // lcd
  38. #include <LiquidCrystal_I2C.h>
  39. LiquidCrystal_I2C lcd(0x27, 16, 2);
  40. // end
  41.  
  42. // button
  43. #include <ezButton.h>
  44. ezButton nextbutton(PB12);
  45. ezButton homebutton(PB13);
  46. ezButton uvbutton(PB14);
  47. ezButton sleepbutton(PB15);
  48. // end
  49.  
  50. // pin
  51. const int ledStatus = PC14;
  52. const int leduv = PC15;
  53. // end
  54.  
  55. // menu
  56. int menu = 1;
  57. int uv = 1;
  58. int sleep = 1;
  59. // end
  60.  
  61. // status
  62. int state ;
  63. int ledState = LOW;
  64. byte TwiceDone = 0;
  65. byte TwiceDone2 = 0;
  66. // end
  67.  
  68. // milis interval
  69. unsigned long previousMillis1 = 0;
  70. const long interval1 = 1000;
  71.  
  72. unsigned long sensorMilis_1 = 0;
  73. const long sensorinterval_1 = 1000;
  74. // end
  75.  
  76. // byte char
  77. byte Weather1[] = {
  78. B11111,
  79. B01010,
  80. B01010,
  81. B01010,
  82. B01110,
  83. B01110,
  84. B01110,
  85. B01110
  86. };
  87.  
  88. byte Weather2[] = {
  89. B01110,
  90. B01010,
  91. B10001,
  92. B10001,
  93. B11001,
  94. B11101,
  95. B11111,
  96. B01110
  97. };
  98.  
  99. byte Temp1[] = {
  100. B01110,
  101. B01010,
  102. B01010,
  103. B01110,
  104. B11111,
  105. B11111,
  106. B11111,
  107. B01110
  108. };
  109.  
  110. byte Temp2[] = {
  111. B00110,
  112. B01001,
  113. B01001,
  114. B00110,
  115. B00000,
  116. B00000,
  117. B00000,
  118. B00000
  119. };
  120.  
  121. byte Pressure[] = {
  122. B01110,
  123. B10011,
  124. B10101,
  125. B10001,
  126. B01110,
  127. B01110,
  128. B01110,
  129. B01110
  130. };
  131. byte Humidity[] = {
  132. B00100,
  133. B01110,
  134. B01110,
  135. B01110,
  136. B10011,
  137. B10101,
  138. B11001,
  139. B01110
  140. };
  141.  
  142. byte Attitude[] = {
  143. B11111,
  144. B01110,
  145. B10101,
  146. B00100,
  147. B00100,
  148. B10101,
  149. B01110,
  150. B11111
  151. };
  152. // end
  153.  
  154. void setup() {
  155. // buetooth status
  156. mySerialOne.begin(9600);
  157. mySerialOne.println("Menunggu koneksi");
  158. mySerialTwo.begin(9600);
  159. mySerialTwo.println("Menunggu koneksi");
  160. // end
  161.  
  162. Serial.begin(9600);
  163.  
  164. Wire.setSDA(PB9);
  165. Wire.setSCL(PB8);
  166. Wire.begin();
  167.  
  168. Wire2.begin();
  169.  
  170. sensors.begin();
  171.  
  172. pinMode(ledStatus, OUTPUT);
  173. pinMode(leduv, OUTPUT);
  174. digitalWrite(ledStatus, HIGH);
  175. digitalWrite(leduv, HIGH);
  176. nextbutton.setDebounceTime(50);
  177. homebutton.setDebounceTime(50);
  178. uvbutton.setDebounceTime(50);
  179. sleepbutton.setDebounceTime(50);
  180.  
  181. if (!bme.begin(0x76, &Wire2)) {
  182. Serial.println("Tidak ada sensor BME280, cek rangkaianmu!");
  183. while (1);
  184. }
  185.  
  186. sendCommand("AT");
  187. sendCommand("AT+ADVI0");
  188. sendCommand("AT+ADTY0");
  189. sendCommand("AT+BAUD0");
  190. sendCommand("AT+NAMEHomeStation");
  191. sendCommand("AT+PASS445653");
  192. sendCommand("AT+SHOW0");
  193. sendCommand("AT+TYPE1");
  194.  
  195. lcd.init();
  196. lcd.backlight();
  197.  
  198. lcd.createChar(0, Weather1);
  199. lcd.createChar(1, Weather2);
  200. lcd.createChar(2, Temp1);
  201. lcd.createChar(3, Temp2);
  202. lcd.createChar(4, Pressure);
  203. lcd.createChar(5, Humidity);
  204. lcd.createChar(6, Attitude);
  205.  
  206. Serial.println();
  207. lcd.setCursor(0,0);
  208. lcd.print("O");
  209. Serial.print("O");
  210. delay(500);
  211. lcd.setCursor(1,0);
  212. lcd.print("N");
  213. Serial.print("N");
  214. delay(3000);
  215. lcd.clear();
  216. Serial.println();
  217.  
  218. // bme setup
  219. /*
  220. * // weather monitoring
  221. * Serial.println("-- Weather Station Scenario --");
  222. * Serial.println("forced mode, 1x temperature / 1x humidity / 1x pressure oversampling,");
  223. * Serial.println("filter off");
  224. * bme.setSampling(Adafruit_BME280::MODE_FORCED,
  225. * Adafruit_BME280::SAMPLING_X1, // temperature
  226. * Adafruit_BME280::SAMPLING_X1, // pressure
  227. * Adafruit_BME280::SAMPLING_X1, // humidity
  228. * Adafruit_BME280::FILTER_OFF );
  229. *
  230. * // suggested rate is 1/60Hz (1m)
  231. * // delayTime = 60000;
  232. */
  233.  
  234. /*
  235. * // humidity sensing
  236. * Serial.println("-- Humidity Sensing Scenario --");
  237. * Serial.println("forced mode, 1x temperature / 1x humidity / 0x pressure oversampling");
  238. * Serial.println("= pressure off, filter off");
  239. * bme.setSampling(Adafruit_BME280::MODE_FORCED,
  240. * Adafruit_BME280::SAMPLING_X1, // temperature
  241. * Adafruit_BME280::SAMPLING_NONE, // pressure
  242. * Adafruit_BME280::SAMPLING_X1, // humidity
  243. * Adafruit_BME280::FILTER_OFF );
  244. *
  245. * // suggested rate is 1Hz (1s)
  246. * // delayTime = 1000;
  247. */
  248.  
  249.  
  250. // indoor navigation
  251. Serial.println("-- Indoor Navigation Scenario --");
  252. Serial.println("normal mode, 16x pressure / 2x temperature / 1x humidity oversampling,");
  253. Serial.println("0.5ms standby period, filter 16x");
  254. bme.setSampling(Adafruit_BME280::MODE_NORMAL,
  255. Adafruit_BME280::SAMPLING_X2, // temperature
  256. Adafruit_BME280::SAMPLING_X16, // pressure
  257. Adafruit_BME280::SAMPLING_X1, // humidity
  258. Adafruit_BME280::FILTER_X16,
  259. Adafruit_BME280::STANDBY_MS_0_5 );
  260.  
  261. // suggested rate is 25Hz
  262. // 1 + (2 * T_ovs) + (2 * P_ovs + 0.5) + (2 * H_ovs + 0.5)
  263. // T_ovs = 2
  264. // P_ovs = 16
  265. // H_ovs = 1
  266. // = 40ms (25Hz)
  267. // with standby time that should really be 24.16913... Hz
  268. // delayTime = 41;
  269.  
  270.  
  271. /*
  272. * // gaming
  273. * Serial.println("-- Gaming Scenario --");
  274. * Serial.println("normal mode, 4x pressure / 1x temperature / 0x humidity oversampling,");
  275. * Serial.println("= humidity off, 0.5ms standby period, filter 16x");
  276. * bme.setSampling(Adafruit_BME280::MODE_NORMAL,
  277. * Adafruit_BME280::SAMPLING_X1, // temperature
  278. * Adafruit_BME280::SAMPLING_X4, // pressure
  279. * Adafruit_BME280::SAMPLING_NONE, // humidity
  280. * Adafruit_BME280::FILTER_X16,
  281. * Adafruit_BME280::STANDBY_MS_0_5 );
  282. *
  283. * // Suggested rate is 83Hz
  284. * // 1 + (2 * T_ovs) + (2 * P_ovs + 0.5)
  285. * // T_ovs = 1
  286. * // P_ovs = 4
  287. * // = 11.5ms + 0.5ms standby
  288. * // delayTime = 12;
  289. */
  290. // end
  291.  
  292. lcd.clear();
  293. Serial.println();
  294.  
  295. lcd.setCursor(0,0);
  296. lcd.write(byte(0));
  297. lcd.setCursor(0,1);
  298. lcd.write(byte(1));
  299.  
  300. lcd.setCursor(1,0);
  301. lcd.print("H");
  302. Serial.print("H");delay(500);
  303. lcd.print("O");
  304. Serial.print("O");delay(500);
  305. lcd.print("M");
  306. Serial.print("M");delay(500);
  307. lcd.print("E");
  308. Serial.print("E");delay(500);
  309.  
  310. Serial.print(" ");
  311. lcd.setCursor(1,1);
  312. lcd.print("S");
  313. Serial.print("S");delay(500);
  314. lcd.print("T");
  315. Serial.print("T");delay(500);
  316. lcd.print("A");
  317. Serial.print("A");delay(500);
  318. lcd.print("T");
  319. Serial.print("T");delay(500);
  320. lcd.print("I");
  321. Serial.print("I");delay(500);
  322. lcd.print("O");
  323. Serial.print("O");delay(500);
  324. lcd.print("N");
  325. Serial.print("N");delay(3000);
  326. lcd.clear();
  327. lcd.noBacklight();
  328.  
  329. // menu update
  330. updateMenu();
  331. updateUv();
  332. updateSleep();
  333. // end
  334. }
  335.  
  336. void loop() {
  337. // sleep loop
  338. sleepbutton.loop();
  339.  
  340. // sleep button
  341. if (sleepbutton.isPressed())
  342. sleep++;
  343. updateSleep();
  344. // end
  345. }
  346.  
  347. // sleep
  348. void updateSleep(){
  349. switch (sleep) {
  350. case 0:
  351. sleep = 1;
  352. break;
  353.  
  354. // sleep
  355. case 1:
  356. if(TwiceDone < 2)
  357. {
  358. sendCommand("AT+SLEEP");
  359. sendCommand("AT+PWRM1");
  360. TwiceDone = TwiceDone+1;
  361. }
  362. lcd.clear();
  363. lcd.noBacklight();
  364. BME280_Sleep(0x76);
  365. digitalWrite(PA2, LOW);
  366.  
  367. menu = 0;
  368. uv = 0;
  369.  
  370. digitalWrite(ledStatus, HIGH);
  371. digitalWrite(leduv, HIGH);
  372. break;
  373.  
  374. // Wakeup
  375. case 2:
  376. if(TwiceDone2 < 5)
  377. {
  378. sendCommand("I am iron man, I am iron man, I am iron man, I am iron man, I am iron man, I am iron man, I am iron man...");
  379. TwiceDone2 = TwiceDone2+1;
  380. }
  381. lcd.backlight();
  382. bme.takeForcedMeasurement();
  383. nextbutton.loop();
  384. homebutton.loop();
  385. uvbutton.loop();
  386.  
  387. BME280_Wakeup(0x76);
  388. digitalWrite(PA2, HIGH);
  389.  
  390. // menu
  391. if (nextbutton.isPressed())
  392. menu++;
  393. updateMenu();
  394. if (homebutton.isPressed())
  395. menu = 0;
  396.  
  397. // uv
  398. if (uvbutton.isPressed())
  399. uv++;
  400. updateUv();
  401.  
  402. // blink
  403. blink();
  404. break;
  405. case 3:
  406. sleep = 0;
  407. break;
  408. }
  409. }
  410. // end
  411.  
  412. // bluetooth command setup
  413. void sendCommand(const char * command){
  414. Serial.print("Command send :");
  415. Serial.println(command);
  416. mySerialOne.println(command);
  417. //delay bentar
  418. delay(100);
  419.  
  420. char reply[100];
  421. int i = 0;
  422. while (mySerialOne.available()) {
  423. reply[i] = mySerialOne.read();
  424. i += 1;
  425. }
  426. //biar string end
  427. reply[i] = '\0';
  428. Serial.print(reply); //tulis jawaban dari BLE
  429. Serial.println("Sukses");
  430. }
  431. // end
  432.  
  433. // blink
  434. void blink() {
  435. if (millis() - previousMillis1 >= interval1) {
  436. previousMillis1 = millis();
  437. if (ledState == LOW) {
  438. ledState = HIGH;
  439. } else {
  440. ledState = LOW;
  441. }
  442.  
  443. // set the LED with the ledState of the variable:
  444. digitalWrite(ledStatus, ledState);
  445. }
  446. }
  447. // end
  448.  
  449. // bme mode
  450. // bme sleep mode
  451. void BME280_Sleep(int device_address) {
  452. // BME280 Register 0xF4 (control measurement register) sets the device mode, specifically bits 1,0
  453. // The bit positions are called 'mode[1:0]'.
  454. // Mode[1:0] Mode
  455. // 00 'Sleep' mode
  456. // 01 / 10 'Forced' mode, use either '01' or '10'
  457. // 11 'Normal' mode
  458. Wire.beginTransmission(device_address);
  459. Wire.write((uint8_t)0xF4); // Select Control Measurement Register
  460. Wire.write((uint8_t)0b00000000); // Send '00' for Sleep mode
  461. Wire.endTransmission();
  462. }
  463. // bme wakeup mode
  464. void BME280_Wakeup(int device_address) {
  465. // BME280 Register 0xF4 (control measurement register) sets the device mode, specifically bits 1,0
  466. // The bit positions are called 'mode[1:0]'.
  467. // Mode[1:0] Mode
  468. // 00 'Sleep' mode
  469. // 01 / 10 'Forced' mode, use either '01' or '10'
  470. // 11 'Normal' mode
  471. Wire.beginTransmission(device_address);
  472. Wire.write((uint8_t)0xF4); // Select Control Measurement Register
  473. Wire.write((uint8_t)0b00000011); // Send '11' for Wakeup mode
  474. Wire.endTransmission();
  475. }
  476. // end
  477.  
  478. // uv
  479. void updateUv() {
  480. switch (uv) {
  481. case 0:
  482. uv = 1;
  483. break;
  484. case 1:
  485. digitalWrite(leduv, HIGH);
  486. break;
  487. case 2:
  488. digitalWrite(leduv, LOW);
  489. break;
  490. case 3:
  491. uv = 0;
  492. break;
  493. }
  494. }
  495. // end
  496.  
  497. // menu
  498. void updateMenu() {
  499. switch (menu) {
  500. case 0:
  501. menu = 1;
  502. break;
  503. case 1:
  504. if(millis() - sensorMilis_1 >= sensorinterval_1) {
  505. sensorMilis_1 = millis();
  506. lcd.clear();
  507. lcd.setCursor(0,0);
  508. lcd.write(byte(2));
  509. lcd.print("SUHU");
  510. lcd.setCursor(0,1);
  511. lcd.print(bme.readTemperature());
  512. lcd.print(" ");
  513. lcd.write(byte(3));
  514. lcd.print("C");
  515. }
  516. break;
  517. case 2:
  518. if(millis() - sensorMilis_1 >= sensorinterval_1) {
  519. sensorMilis_1 = millis();
  520. lcd.clear();
  521. lcd.setCursor(0,0);
  522. lcd.write(byte(4));
  523. lcd.print("KELEMBAPAN");
  524. lcd.setCursor(0,1);
  525. lcd.print(bme.readHumidity());
  526. lcd.print(" %");
  527. }
  528. break;
  529. case 3:
  530. if(millis() - sensorMilis_1 >= sensorinterval_1) {
  531. sensorMilis_1 = millis();
  532. lcd.clear();
  533. lcd.setCursor(0,0);
  534. lcd.write(byte(5));
  535. lcd.print("TEKANAN");
  536. lcd.setCursor(0,1);
  537. lcd.print(bme.readPressure() / 100.0F);
  538. lcd.print(" hPa");
  539. }
  540. break;
  541. case 4:
  542. if(millis() - sensorMilis_1 >= sensorinterval_1) {
  543. sensorMilis_1 = millis();
  544. lcd.clear();
  545. lcd.setCursor(0,0);
  546. lcd.write(byte(6));
  547. lcd.print("KETINGGIAN");
  548. lcd.setCursor(0,1);
  549. lcd.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  550. lcd.print(" mdpl");
  551. }
  552. break;
  553. case 5:
  554. if(millis() - sensorMilis_1 >= sensorinterval_1) {
  555. sensorMilis_1 = millis();
  556.  
  557. int ketinggian_udara = bme.readAltitude(SEALEVELPRESSURE_HPA) / 1.0936132983;
  558.  
  559. lcd.clear();
  560. lcd.setCursor(0,0);
  561. lcd.write(byte(6));
  562. lcd.print("KETINGGIAN");
  563. lcd.setCursor(0,1);
  564. lcd.print(ketinggian_udara);
  565. lcd.print(" Kaki");
  566. }
  567. break;
  568. case 6:
  569. sensors.requestTemperatures();
  570. if(millis() - sensorMilis_1 >= sensorinterval_1) {
  571. sensorMilis_1 = millis();
  572. lcd.clear();
  573. lcd.setCursor(0,0);
  574. lcd.write(byte(2));
  575. lcd.print("TONGKAT SUHU");
  576. lcd.setCursor(0,1);
  577. lcd.print(sensors.getTempCByIndex(0));
  578. lcd.print(" ");
  579. lcd.write(byte(3));
  580. lcd.print("C");
  581. }
  582. break;
  583. case 7:
  584. menu = 0;
  585. break;
  586. }
  587. }
  588. // end
Add Comment
Please, Sign In to add comment