Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // bluetooth
- #include <SoftwareSerial.h>
- SoftwareSerial mySerialOne(PA2, PA3); // RX, TX
- SoftwareSerial mySerialTwo(PA9, PA10); // RX, TX
- //end
- // 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
- // tongkat suhu
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define ONE_WIRE_BUS A1
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- // end
- // lcd
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- // end
- // button
- #include <ezButton.h>
- ezButton nextbutton(PB12);
- ezButton homebutton(PB13);
- ezButton uvbutton(PB14);
- ezButton sleepbutton(PB15);
- // end
- // pin
- const int ledStatus = PC14;
- const int leduv = PC15;
- // end
- // menu
- int menu = 1;
- int uv = 1;
- int sleep = 1;
- // end
- // status
- int state ;
- int ledState = LOW;
- byte TwiceDone = 0;
- byte TwiceDone2 = 0;
- // end
- // milis interval
- unsigned long previousMillis1 = 0;
- const long interval1 = 1000;
- unsigned long sensorMilis_1 = 0;
- const long sensorinterval_1 = 1000;
- // end
- // byte char
- 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
- };
- // end
- void setup() {
- // buetooth status
- mySerialOne.begin(9600);
- mySerialOne.println("Menunggu koneksi");
- mySerialTwo.begin(9600);
- mySerialTwo.println("Menunggu koneksi");
- // end
- Serial.begin(9600);
- Wire.setSDA(PB9);
- Wire.setSCL(PB8);
- Wire.begin();
- Wire2.begin();
- sensors.begin();
- pinMode(ledStatus, OUTPUT);
- pinMode(leduv, OUTPUT);
- digitalWrite(ledStatus, HIGH);
- digitalWrite(leduv, HIGH);
- nextbutton.setDebounceTime(50);
- homebutton.setDebounceTime(50);
- uvbutton.setDebounceTime(50);
- sleepbutton.setDebounceTime(50);
- if (!bme.begin(0x76, &Wire2)) {
- Serial.println("Tidak ada sensor BME280, cek rangkaianmu!");
- while (1);
- }
- sendCommand("AT");
- sendCommand("AT+ADVI0");
- sendCommand("AT+ADTY0");
- sendCommand("AT+BAUD0");
- sendCommand("AT+NAMEHomeStation");
- sendCommand("AT+PASS445653");
- sendCommand("AT+SHOW0");
- sendCommand("AT+TYPE1");
- lcd.init();
- lcd.backlight();
- 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);
- Serial.println();
- lcd.setCursor(0,0);
- lcd.print("O");
- Serial.print("O");
- delay(500);
- lcd.setCursor(1,0);
- lcd.print("N");
- Serial.print("N");
- delay(3000);
- lcd.clear();
- Serial.println();
- // bme setup
- /*
- * // 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;
- */
- // end
- 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();
- lcd.noBacklight();
- // menu update
- updateMenu();
- updateUv();
- updateSleep();
- // end
- }
- void loop() {
- // sleep loop
- sleepbutton.loop();
- // sleep button
- if (sleepbutton.isPressed())
- sleep++;
- updateSleep();
- // end
- }
- // sleep
- void updateSleep(){
- switch (sleep) {
- case 0:
- sleep = 1;
- break;
- // sleep
- case 1:
- if(TwiceDone < 2)
- {
- sendCommand("AT+SLEEP");
- sendCommand("AT+PWRM1");
- TwiceDone = TwiceDone+1;
- }
- lcd.clear();
- lcd.noBacklight();
- BME280_Sleep(0x76);
- digitalWrite(PA2, LOW);
- menu = 0;
- uv = 0;
- digitalWrite(ledStatus, HIGH);
- digitalWrite(leduv, HIGH);
- break;
- // Wakeup
- case 2:
- if(TwiceDone2 < 5)
- {
- 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...");
- TwiceDone2 = TwiceDone2+1;
- }
- lcd.backlight();
- bme.takeForcedMeasurement();
- nextbutton.loop();
- homebutton.loop();
- uvbutton.loop();
- BME280_Wakeup(0x76);
- digitalWrite(PA2, HIGH);
- // menu
- if (nextbutton.isPressed())
- menu++;
- updateMenu();
- if (homebutton.isPressed())
- menu = 0;
- // uv
- if (uvbutton.isPressed())
- uv++;
- updateUv();
- // blink
- blink();
- break;
- case 3:
- sleep = 0;
- break;
- }
- }
- // end
- // bluetooth command setup
- void sendCommand(const char * command){
- Serial.print("Command send :");
- Serial.println(command);
- mySerialOne.println(command);
- //delay bentar
- delay(100);
- char reply[100];
- int i = 0;
- while (mySerialOne.available()) {
- reply[i] = mySerialOne.read();
- i += 1;
- }
- //biar string end
- reply[i] = '\0';
- Serial.print(reply); //tulis jawaban dari BLE
- Serial.println("Sukses");
- }
- // end
- // blink
- void blink() {
- 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);
- }
- }
- // end
- // bme mode
- // bme sleep mode
- void BME280_Sleep(int device_address) {
- // BME280 Register 0xF4 (control measurement register) sets the device mode, specifically bits 1,0
- // The bit positions are called 'mode[1:0]'.
- // Mode[1:0] Mode
- // 00 'Sleep' mode
- // 01 / 10 'Forced' mode, use either '01' or '10'
- // 11 'Normal' mode
- Wire.beginTransmission(device_address);
- Wire.write((uint8_t)0xF4); // Select Control Measurement Register
- Wire.write((uint8_t)0b00000000); // Send '00' for Sleep mode
- Wire.endTransmission();
- }
- // bme wakeup mode
- void BME280_Wakeup(int device_address) {
- // BME280 Register 0xF4 (control measurement register) sets the device mode, specifically bits 1,0
- // The bit positions are called 'mode[1:0]'.
- // Mode[1:0] Mode
- // 00 'Sleep' mode
- // 01 / 10 'Forced' mode, use either '01' or '10'
- // 11 'Normal' mode
- Wire.beginTransmission(device_address);
- Wire.write((uint8_t)0xF4); // Select Control Measurement Register
- Wire.write((uint8_t)0b00000011); // Send '11' for Wakeup mode
- Wire.endTransmission();
- }
- // end
- // uv
- void updateUv() {
- switch (uv) {
- case 0:
- uv = 1;
- break;
- case 1:
- digitalWrite(leduv, HIGH);
- break;
- case 2:
- digitalWrite(leduv, LOW);
- break;
- case 3:
- uv = 0;
- break;
- }
- }
- // end
- // menu
- 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");
- }
- 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(" %");
- }
- 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");
- }
- 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(" mdpl");
- }
- break;
- case 5:
- if(millis() - sensorMilis_1 >= sensorinterval_1) {
- sensorMilis_1 = millis();
- int ketinggian_udara = bme.readAltitude(SEALEVELPRESSURE_HPA) / 1.0936132983;
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.write(byte(6));
- lcd.print("KETINGGIAN");
- lcd.setCursor(0,1);
- lcd.print(ketinggian_udara);
- lcd.print(" Kaki");
- }
- break;
- case 6:
- 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");
- }
- break;
- case 7:
- menu = 0;
- break;
- }
- }
- // end
Add Comment
Please, Sign In to add comment