Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // bluetooth
- #include <SoftwareSerial.h>
- SoftwareSerial mySerial(PA9, PA10); // RX, TX
- // end bluetooth
- // wire i2c
- #include <Wire.h>
- TwoWire Wire2(PB11,PB10);
- // bme280
- #include <SPI.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_BME280.h>
- #define BME_SCK 13
- #define BME_MISO 12
- #define BME_MOSI 11
- #define BME_CS 10
- #define SEALEVELPRESSURE_HPA (1013.25)
- Adafruit_BME280 bme; // I2C
- //Adafruit_BME280 bme(BME_CS); // hardware SPI
- //Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
- // end bme280
- // tongkat suhu
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define ONE_WIRE_BUS A2
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- // end tongkat suhu
- // lcd
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- // button
- #include <ezButton.h>
- ezButton button1(PB12);
- ezButton button2(PB13);
- // end button
- const int ledStatus = PC14;
- int menu = 1;
- int state ;
- int ledState = LOW;
- unsigned long previousMillis1 = 0;
- const long interval1 = 1000;
- unsigned long sensorMilis_1 = 0;
- const long sensorinterval_1 = 1000;
- byte Weather1[] = {
- B11111,
- B01010,
- B01010,
- B01010,
- B01110,
- B01110,
- B01110,
- B01110
- };
- byte Weather2[] = {
- B01110,
- B01010,
- B10001,
- B10001,
- B11001,
- B11101,
- B11111,
- B01110
- };
- byte Temp1[] = {
- B01110,
- B01010,
- B01010,
- B01110,
- B11111,
- B11111,
- B11111,
- B01110
- };
- byte Temp2[] = {
- B00110,
- B01001,
- B01001,
- B00110,
- B00000,
- B00000,
- B00000,
- B00000
- };
- byte Pressure[] = {
- B01110,
- B10011,
- B10101,
- B10001,
- B01110,
- B01110,
- B01110,
- B01110
- };
- byte Humidity[] = {
- B00100,
- B01110,
- B01110,
- B01110,
- B10011,
- B10101,
- B11001,
- B01110
- };
- byte Attitude[] = {
- B11111,
- B01110,
- B10101,
- B00100,
- B00100,
- B10101,
- B01110,
- B11111
- };
- void setup() {
- mySerial.begin(9600);
- mySerial.println("Menunggu koneksi");
- Serial.begin(9600);
- Wire.setSDA(PB9);
- Wire.setSCL(PB8);
- Wire.begin();
- Wire2.begin();
- sensors.begin();
- lcd.init();
- pinMode(ledStatus, OUTPUT);
- digitalWrite(ledStatus, LOW);
- button1.setDebounceTime(50);
- button2.setDebounceTime(50);
- lcd.createChar(0, Weather1);
- lcd.createChar(1, Weather2);
- lcd.createChar(2, Temp1);
- lcd.createChar(3, Temp2);
- lcd.createChar(4, Pressure);
- lcd.createChar(5, Humidity);
- lcd.createChar(6, Attitude);
- sendCommand("AT");
- sendCommand("AT+ADVI0");
- sendCommand("AT+ADTY0");
- sendCommand("AT+BAUD0");
- sendCommand("AT+NAMEHomeStation");
- sendCommand("AT+PASS445653");
- sendCommand("AT+SHOW0");
- sendCommand("AT+TYPE1");
- lcd.backlight();
- Serial.println();
- lcd.setCursor(0,0);
- lcd.print("ON");
- Serial.println("ON");
- delay(3000);
- lcd.clear();
- Serial.println();
- if (! bme.begin(0x76, &Wire2)) {
- Serial.println("Tidak ada sensor BME280, cek rangkaianmu!");
- while (1);
- }
- /*
- * // weather monitoring
- * Serial.println("-- Weather Station Scenario --");
- * Serial.println("forced mode, 1x temperature / 1x humidity / 1x pressure oversampling,");
- * Serial.println("filter off");
- * bme.setSampling(Adafruit_BME280::MODE_FORCED,
- * Adafruit_BME280::SAMPLING_X1, // temperature
- * Adafruit_BME280::SAMPLING_X1, // pressure
- * Adafruit_BME280::SAMPLING_X1, // humidity
- * Adafruit_BME280::FILTER_OFF );
- *
- * // suggested rate is 1/60Hz (1m)
- * // delayTime = 60000;
- */
- /*
- * // humidity sensing
- * Serial.println("-- Humidity Sensing Scenario --");
- * Serial.println("forced mode, 1x temperature / 1x humidity / 0x pressure oversampling");
- * Serial.println("= pressure off, filter off");
- * bme.setSampling(Adafruit_BME280::MODE_FORCED,
- * Adafruit_BME280::SAMPLING_X1, // temperature
- * Adafruit_BME280::SAMPLING_NONE, // pressure
- * Adafruit_BME280::SAMPLING_X1, // humidity
- * Adafruit_BME280::FILTER_OFF );
- *
- * // suggested rate is 1Hz (1s)
- * // delayTime = 1000;
- */
- // indoor navigation
- Serial.println("-- Indoor Navigation Scenario --");
- Serial.println("normal mode, 16x pressure / 2x temperature / 1x humidity oversampling,");
- Serial.println("0.5ms standby period, filter 16x");
- bme.setSampling(Adafruit_BME280::MODE_NORMAL,
- Adafruit_BME280::SAMPLING_X2, // temperature
- Adafruit_BME280::SAMPLING_X16, // pressure
- Adafruit_BME280::SAMPLING_X1, // humidity
- Adafruit_BME280::FILTER_X16,
- Adafruit_BME280::STANDBY_MS_0_5 );
- // suggested rate is 25Hz
- // 1 + (2 * T_ovs) + (2 * P_ovs + 0.5) + (2 * H_ovs + 0.5)
- // T_ovs = 2
- // P_ovs = 16
- // H_ovs = 1
- // = 40ms (25Hz)
- // with standby time that should really be 24.16913... Hz
- // delayTime = 41;
- /*
- * // gaming
- * Serial.println("-- Gaming Scenario --");
- * Serial.println("normal mode, 4x pressure / 1x temperature / 0x humidity oversampling,");
- * Serial.println("= humidity off, 0.5ms standby period, filter 16x");
- * bme.setSampling(Adafruit_BME280::MODE_NORMAL,
- * Adafruit_BME280::SAMPLING_X1, // temperature
- * Adafruit_BME280::SAMPLING_X4, // pressure
- * Adafruit_BME280::SAMPLING_NONE, // humidity
- * Adafruit_BME280::FILTER_X16,
- * Adafruit_BME280::STANDBY_MS_0_5 );
- *
- * // Suggested rate is 83Hz
- * // 1 + (2 * T_ovs) + (2 * P_ovs + 0.5)
- * // T_ovs = 1
- * // P_ovs = 4
- * // = 11.5ms + 0.5ms standby
- * // delayTime = 12;
- */
- lcd.clear();
- Serial.println();
- lcd.setCursor(0,0);
- lcd.write(byte(0));
- lcd.setCursor(0,1);
- lcd.write(byte(1));
- lcd.setCursor(1,0);
- lcd.print("H");
- Serial.print("H");delay(500);
- lcd.print("O");
- Serial.print("O");delay(500);
- lcd.print("M");
- Serial.print("M");delay(500);
- lcd.print("E");
- Serial.print("E");delay(500);
- Serial.print(" ");
- lcd.setCursor(1,1);
- lcd.print("S");
- Serial.print("S");delay(500);
- lcd.print("T");
- Serial.print("T");delay(500);
- lcd.print("A");
- Serial.print("A");delay(500);
- lcd.print("T");
- Serial.print("T");delay(500);
- lcd.print("I");
- Serial.print("I");delay(500);
- lcd.print("O");
- Serial.print("O");delay(500);
- lcd.print("N");
- Serial.print("N");delay(3000);
- lcd.clear();
- updateMenu();
- }
- void sendCommand(const char * command){
- Serial.print("Command send :");
- Serial.println(command);
- mySerial.println(command);
- //delay bentar
- delay(100);
- char reply[100];
- int i = 0;
- while (mySerial.available()) {
- reply[i] = mySerial.read();
- i += 1;
- }
- //biar string end
- reply[i] = '\0';
- Serial.print(reply); //tulis jawaban dari BLE
- Serial.println("Sukses");
- }
- void loop() {
- bme.takeForcedMeasurement();
- button1.loop();
- button2.loop();
- if (button1.isPressed())
- menu++;
- updateMenu();
- if (button2.isPressed())
- menu = 0;
- if (millis() - previousMillis1 >= interval1) {
- previousMillis1 = millis();
- if (ledState == LOW) {
- ledState = HIGH;
- } else {
- ledState = LOW;
- }
- // set the LED with the ledState of the variable:
- digitalWrite(ledStatus, ledState);
- }
- }
- void updateMenu() {
- switch (menu) {
- case 0:
- menu = 1;
- break;
- case 1:
- if(millis() - sensorMilis_1 >= sensorinterval_1) {
- sensorMilis_1 = millis();
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.write(byte(2));
- lcd.print("SUHU");
- lcd.setCursor(0,1);
- lcd.print(bme.readTemperature());
- lcd.print(" ");
- lcd.write(byte(3));
- lcd.print("C");
- Serial.println("");
- Serial.print("SUHU = ");
- Serial.print(bme.readTemperature());
- Serial.print(" C");
- }
- break;
- case 2:
- if(millis() - sensorMilis_1 >= sensorinterval_1) {
- sensorMilis_1 = millis();
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.write(byte(4));
- lcd.print("KELEMBAPAN");
- lcd.setCursor(0,1);
- lcd.print(bme.readHumidity());
- lcd.print(" %");
- Serial.println("");
- Serial.print("KELEMBAPAN = ");
- Serial.print(bme.readHumidity());
- Serial.print(" %");
- }
- break;
- case 3:
- if(millis() - sensorMilis_1 >= sensorinterval_1) {
- sensorMilis_1 = millis();
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.write(byte(5));
- lcd.print("TEKANAN");
- lcd.setCursor(0,1);
- lcd.print(bme.readPressure() / 100.0F);
- lcd.print(" hPa");
- Serial.println("");
- Serial.print("TEKANAN = ");
- Serial.print(bme.readPressure() / 100.0F);
- Serial.print(" hPa");
- }
- break;
- case 4:
- if(millis() - sensorMilis_1 >= sensorinterval_1) {
- sensorMilis_1 = millis();
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.write(byte(6));
- lcd.print("KETINGGIAN");
- lcd.setCursor(0,1);
- lcd.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
- lcd.print(" m");
- Serial.println("");
- Serial.print("KETINGGIAN = ");
- Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
- Serial.print(" m");
- }
- break;
- case 5:
- sensors.requestTemperatures();
- if(millis() - sensorMilis_1 >= sensorinterval_1) {
- sensorMilis_1 = millis();
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.write(byte(2));
- lcd.print("TONGKAT SUHU");
- lcd.setCursor(0,1);
- lcd.print(sensors.getTempCByIndex(0));
- lcd.print(" ");
- lcd.write(byte(3));
- lcd.print("C");
- Serial.println("");
- Serial.print("TONGKAT SUHU = ");
- Serial.print(sensors.getTempCByIndex(0));
- Serial.print(" C");
- }
- break;
- case 6:
- menu = 0;
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement