Skip to content

Instantly share code, notes, and snippets.

@artistio
Created February 10, 2019 11:40
Show Gist options
  • Save artistio/a66872d495489a401b526aff60643738 to your computer and use it in GitHub Desktop.
Save artistio/a66872d495489a401b526aff60643738 to your computer and use it in GitHub Desktop.
Program untuk membaca BME280 dengan NodeMCU
/* NodeMCU_BME280.ino
* Program untuk membaca suhu dan kelembaban dari sensor BME280 (I2C)
* menggunakan NodeMCU
*
* Hak cipta (c) 2019 x.benny.id. All rights reserved.
* https://x.benny.id
*/
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <ESP8266WiFi.h>
// Inisialisasi BME280
Adafruit_BME280 bme;
void setup() {
// Memulai Serial untuk debug
Serial.begin(115600);
Serial.setTimeout(2000);
// Wait for serial to initialize.
while(!Serial) { }
Serial.println("Device Started");
Serial.println("-------------------------------------");
Serial.println("Running NodeMCU_BME280!");
Serial.println("-------------------------------------");
// Mendeteksi keberadaan BME280
if (!bme.begin()) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
delay(1000);
}
// Set BME Mode to Forced Mode
// Code from Adafruit sample library
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 );
}
void loop() {
int BMEReadResult = 0;
float p = 0;
float h = 0;
float f = 0;
float t = 0;
while (BMEReadResult == 0) {
Serial.println("Reading BME280");
// Membaca Tekanan Udara
p = bme.readPressure();
if (isnan(p)) {
Serial.println("Failed to read from Pressure from BME sensor!");
delay(100);
continue;
}
// Membaca suhu
t = bme.readTemperature();
if (isnan(t)) {
Serial.println("Failed to read from Temperature from BME sensor!");
delay(100);
continue;
}
// Konversi suhu dari celcius ke farenheit
f = round(t * 9/5 + 32);
// Membaca kelembaban udara
h = bme.readHumidity();
if (isnan(h)) {
Serial.println("Failed to read from Humidity from BME sensor!");
delay(100);
continue;
}
BMEReadResult = 1;
// Mengirimkan hasil ke serial
Serial.print("Pressure (Pa): ");
Serial.println(p);
Serial.print("Pressure (mBar): ");
Serial.println(p/100);
Serial.print("Temperature (C/F): ");
Serial.print(t);
Serial.print("/");
Serial.println(f);
Serial.print("Humidity (%): ");
Serial.println(h);
}
delay(60000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment