Advertisement
BimoSora

STM32

Jun 10th, 2021
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.26 KB | None | 0 0
  1. // bluetooth
  2.  
  3. #include <SoftwareSerial.h>
  4.  
  5. SoftwareSerial mySerial(PA9, PA10); // RX, TX
  6. // end bluetooth
  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 bme280
  28.  
  29. // tongkat suhu
  30. #include <OneWire.h>
  31. #include <DallasTemperature.h>
  32. #define ONE_WIRE_BUS A2
  33. OneWire oneWire(ONE_WIRE_BUS);
  34. DallasTemperature sensors(&oneWire);
  35. // end tongkat suhu
  36.  
  37. // lcd
  38. #include <LiquidCrystal_I2C.h>
  39. LiquidCrystal_I2C lcd(0x27, 16, 2);
  40.  
  41. // button
  42. #include <ezButton.h>
  43. ezButton button1(PB12);
  44. ezButton button2(PB13);
  45. // end button
  46.  
  47. const int ledStatus = PC14;
  48. int menu = 1;
  49.  
  50. int state ;
  51. int ledState = LOW;
  52.  
  53. unsigned long previousMillis1 = 0;
  54. const long interval1 = 1000;
  55.  
  56. unsigned long sensorMilis_1 = 0;
  57. const long sensorinterval_1 = 1000;
  58.  
  59. byte Weather1[] = {
  60. B11111,
  61. B01010,
  62. B01010,
  63. B01010,
  64. B01110,
  65. B01110,
  66. B01110,
  67. B01110
  68. };
  69.  
  70. byte Weather2[] = {
  71. B01110,
  72. B01010,
  73. B10001,
  74. B10001,
  75. B11001,
  76. B11101,
  77. B11111,
  78. B01110
  79. };
  80.  
  81. byte Temp1[] = {
  82. B01110,
  83. B01010,
  84. B01010,
  85. B01110,
  86. B11111,
  87. B11111,
  88. B11111,
  89. B01110
  90. };
  91.  
  92. byte Temp2[] = {
  93. B00110,
  94. B01001,
  95. B01001,
  96. B00110,
  97. B00000,
  98. B00000,
  99. B00000,
  100. B00000
  101. };
  102.  
  103. byte Pressure[] = {
  104. B01110,
  105. B10011,
  106. B10101,
  107. B10001,
  108. B01110,
  109. B01110,
  110. B01110,
  111. B01110
  112. };
  113. byte Humidity[] = {
  114. B00100,
  115. B01110,
  116. B01110,
  117. B01110,
  118. B10011,
  119. B10101,
  120. B11001,
  121. B01110
  122. };
  123.  
  124. byte Attitude[] = {
  125. B11111,
  126. B01110,
  127. B10101,
  128. B00100,
  129. B00100,
  130. B10101,
  131. B01110,
  132. B11111
  133. };
  134.  
  135. void setup() {
  136. mySerial.begin(9600);
  137. mySerial.println("Menunggu koneksi");
  138. Serial.begin(9600);
  139.  
  140. Wire.setSDA(PB9);
  141. Wire.setSCL(PB8);
  142. Wire.begin();
  143.  
  144. Wire2.begin();
  145.  
  146. sensors.begin();
  147.  
  148. lcd.init();
  149.  
  150. pinMode(ledStatus, OUTPUT);
  151. digitalWrite(ledStatus, LOW);
  152. button1.setDebounceTime(50);
  153. button2.setDebounceTime(50);
  154.  
  155. lcd.createChar(0, Weather1);
  156. lcd.createChar(1, Weather2);
  157. lcd.createChar(2, Temp1);
  158. lcd.createChar(3, Temp2);
  159. lcd.createChar(4, Pressure);
  160. lcd.createChar(5, Humidity);
  161. lcd.createChar(6, Attitude);
  162.  
  163. sendCommand("AT");
  164. sendCommand("AT+ADVI0");
  165. sendCommand("AT+ADTY0");
  166. sendCommand("AT+BAUD0");
  167. sendCommand("AT+NAMEHomeStation");
  168. sendCommand("AT+PASS445653");
  169. sendCommand("AT+SHOW0");
  170. sendCommand("AT+TYPE1");
  171.  
  172. lcd.backlight();
  173. Serial.println();
  174. lcd.setCursor(0,0);
  175. lcd.print("ON");
  176. Serial.println("ON");
  177. delay(3000);
  178. lcd.clear();
  179. Serial.println();
  180.  
  181. if (! bme.begin(0x76, &Wire2)) {
  182. Serial.println("Tidak ada sensor BME280, cek rangkaianmu!");
  183. while (1);
  184. }
  185.  
  186. /*
  187. * // weather monitoring
  188. * Serial.println("-- Weather Station Scenario --");
  189. * Serial.println("forced mode, 1x temperature / 1x humidity / 1x pressure oversampling,");
  190. * Serial.println("filter off");
  191. * bme.setSampling(Adafruit_BME280::MODE_FORCED,
  192. * Adafruit_BME280::SAMPLING_X1, // temperature
  193. * Adafruit_BME280::SAMPLING_X1, // pressure
  194. * Adafruit_BME280::SAMPLING_X1, // humidity
  195. * Adafruit_BME280::FILTER_OFF );
  196. *
  197. * // suggested rate is 1/60Hz (1m)
  198. * // delayTime = 60000;
  199. */
  200.  
  201. /*
  202. * // humidity sensing
  203. * Serial.println("-- Humidity Sensing Scenario --");
  204. * Serial.println("forced mode, 1x temperature / 1x humidity / 0x pressure oversampling");
  205. * Serial.println("= pressure off, filter off");
  206. * bme.setSampling(Adafruit_BME280::MODE_FORCED,
  207. * Adafruit_BME280::SAMPLING_X1, // temperature
  208. * Adafruit_BME280::SAMPLING_NONE, // pressure
  209. * Adafruit_BME280::SAMPLING_X1, // humidity
  210. * Adafruit_BME280::FILTER_OFF );
  211. *
  212. * // suggested rate is 1Hz (1s)
  213. * // delayTime = 1000;
  214. */
  215.  
  216.  
  217. // indoor navigation
  218. Serial.println("-- Indoor Navigation Scenario --");
  219. Serial.println("normal mode, 16x pressure / 2x temperature / 1x humidity oversampling,");
  220. Serial.println("0.5ms standby period, filter 16x");
  221. bme.setSampling(Adafruit_BME280::MODE_NORMAL,
  222. Adafruit_BME280::SAMPLING_X2, // temperature
  223. Adafruit_BME280::SAMPLING_X16, // pressure
  224. Adafruit_BME280::SAMPLING_X1, // humidity
  225. Adafruit_BME280::FILTER_X16,
  226. Adafruit_BME280::STANDBY_MS_0_5 );
  227.  
  228. // suggested rate is 25Hz
  229. // 1 + (2 * T_ovs) + (2 * P_ovs + 0.5) + (2 * H_ovs + 0.5)
  230. // T_ovs = 2
  231. // P_ovs = 16
  232. // H_ovs = 1
  233. // = 40ms (25Hz)
  234. // with standby time that should really be 24.16913... Hz
  235. // delayTime = 41;
  236.  
  237.  
  238. /*
  239. * // gaming
  240. * Serial.println("-- Gaming Scenario --");
  241. * Serial.println("normal mode, 4x pressure / 1x temperature / 0x humidity oversampling,");
  242. * Serial.println("= humidity off, 0.5ms standby period, filter 16x");
  243. * bme.setSampling(Adafruit_BME280::MODE_NORMAL,
  244. * Adafruit_BME280::SAMPLING_X1, // temperature
  245. * Adafruit_BME280::SAMPLING_X4, // pressure
  246. * Adafruit_BME280::SAMPLING_NONE, // humidity
  247. * Adafruit_BME280::FILTER_X16,
  248. * Adafruit_BME280::STANDBY_MS_0_5 );
  249. *
  250. * // Suggested rate is 83Hz
  251. * // 1 + (2 * T_ovs) + (2 * P_ovs + 0.5)
  252. * // T_ovs = 1
  253. * // P_ovs = 4
  254. * // = 11.5ms + 0.5ms standby
  255. * // delayTime = 12;
  256. */
  257.  
  258. lcd.clear();
  259. Serial.println();
  260.  
  261. lcd.setCursor(0,0);
  262. lcd.write(byte(0));
  263. lcd.setCursor(0,1);
  264. lcd.write(byte(1));
  265.  
  266. lcd.setCursor(1,0);
  267. lcd.print("H");
  268. Serial.print("H");delay(500);
  269. lcd.print("O");
  270. Serial.print("O");delay(500);
  271. lcd.print("M");
  272. Serial.print("M");delay(500);
  273. lcd.print("E");
  274. Serial.print("E");delay(500);
  275.  
  276. Serial.print(" ");
  277. lcd.setCursor(1,1);
  278. lcd.print("S");
  279. Serial.print("S");delay(500);
  280. lcd.print("T");
  281. Serial.print("T");delay(500);
  282. lcd.print("A");
  283. Serial.print("A");delay(500);
  284. lcd.print("T");
  285. Serial.print("T");delay(500);
  286. lcd.print("I");
  287. Serial.print("I");delay(500);
  288. lcd.print("O");
  289. Serial.print("O");delay(500);
  290. lcd.print("N");
  291. Serial.print("N");delay(3000);
  292. lcd.clear();
  293.  
  294. updateMenu();
  295. }
  296.  
  297. void sendCommand(const char * command){
  298. Serial.print("Command send :");
  299. Serial.println(command);
  300. mySerial.println(command);
  301. //delay bentar
  302. delay(100);
  303.  
  304. char reply[100];
  305. int i = 0;
  306. while (mySerial.available()) {
  307. reply[i] = mySerial.read();
  308. i += 1;
  309. }
  310. //biar string end
  311. reply[i] = '\0';
  312. Serial.print(reply); //tulis jawaban dari BLE
  313. Serial.println("Sukses");
  314. }
  315.  
  316. void loop() {
  317. bme.takeForcedMeasurement();
  318.  
  319. button1.loop();
  320. button2.loop();
  321.  
  322. if (button1.isPressed())
  323. menu++;
  324. updateMenu();
  325. if (button2.isPressed())
  326. menu = 0;
  327.  
  328. if (millis() - previousMillis1 >= interval1) {
  329. previousMillis1 = millis();
  330. if (ledState == LOW) {
  331. ledState = HIGH;
  332. } else {
  333. ledState = LOW;
  334. }
  335.  
  336. // set the LED with the ledState of the variable:
  337. digitalWrite(ledStatus, ledState);
  338. }
  339. }
  340.  
  341. void updateMenu() {
  342. switch (menu) {
  343. case 0:
  344. menu = 1;
  345. break;
  346. case 1:
  347. if(millis() - sensorMilis_1 >= sensorinterval_1) {
  348. sensorMilis_1 = millis();
  349. lcd.clear();
  350. lcd.setCursor(0,0);
  351. lcd.write(byte(2));
  352. lcd.print("SUHU");
  353. lcd.setCursor(0,1);
  354. lcd.print(bme.readTemperature());
  355. lcd.print(" ");
  356. lcd.write(byte(3));
  357. lcd.print("C");
  358.  
  359. Serial.println("");
  360. Serial.print("SUHU = ");
  361. Serial.print(bme.readTemperature());
  362. Serial.print(" C");
  363. }
  364. break;
  365. case 2:
  366. if(millis() - sensorMilis_1 >= sensorinterval_1) {
  367. sensorMilis_1 = millis();
  368. lcd.clear();
  369. lcd.setCursor(0,0);
  370. lcd.write(byte(4));
  371. lcd.print("KELEMBAPAN");
  372. lcd.setCursor(0,1);
  373. lcd.print(bme.readHumidity());
  374. lcd.print(" %");
  375.  
  376. Serial.println("");
  377. Serial.print("KELEMBAPAN = ");
  378. Serial.print(bme.readHumidity());
  379. Serial.print(" %");
  380. }
  381. break;
  382. case 3:
  383. if(millis() - sensorMilis_1 >= sensorinterval_1) {
  384. sensorMilis_1 = millis();
  385. lcd.clear();
  386. lcd.setCursor(0,0);
  387. lcd.write(byte(5));
  388. lcd.print("TEKANAN");
  389. lcd.setCursor(0,1);
  390. lcd.print(bme.readPressure() / 100.0F);
  391. lcd.print(" hPa");
  392.  
  393. Serial.println("");
  394. Serial.print("TEKANAN = ");
  395. Serial.print(bme.readPressure() / 100.0F);
  396. Serial.print(" hPa");
  397. }
  398. break;
  399. case 4:
  400. if(millis() - sensorMilis_1 >= sensorinterval_1) {
  401. sensorMilis_1 = millis();
  402. lcd.clear();
  403. lcd.setCursor(0,0);
  404. lcd.write(byte(6));
  405. lcd.print("KETINGGIAN");
  406. lcd.setCursor(0,1);
  407. lcd.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  408. lcd.print(" m");
  409.  
  410. Serial.println("");
  411. Serial.print("KETINGGIAN = ");
  412. Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  413. Serial.print(" m");
  414. }
  415. break;
  416. case 5:
  417. sensors.requestTemperatures();
  418. if(millis() - sensorMilis_1 >= sensorinterval_1) {
  419. sensorMilis_1 = millis();
  420. lcd.clear();
  421. lcd.setCursor(0,0);
  422. lcd.write(byte(2));
  423. lcd.print("TONGKAT SUHU");
  424. lcd.setCursor(0,1);
  425. lcd.print(sensors.getTempCByIndex(0));
  426. lcd.print(" ");
  427. lcd.write(byte(3));
  428. lcd.print("C");
  429.  
  430. Serial.println("");
  431. Serial.print("TONGKAT SUHU = ");
  432. Serial.print(sensors.getTempCByIndex(0));
  433. Serial.print(" C");
  434. }
  435. break;
  436. case 6:
  437. menu = 0;
  438. break;
  439. }
  440. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement