-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_NodeMCU.ino
62 lines (50 loc) · 1.99 KB
/
demo_NodeMCU.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
NodeMCU Demo Code
Read temperature, pressure, and humidity data from an BME280 via I2C
and display the results in the Serial Monitor.
Created June 8 2023
This example code is under MIT license and available at
https://github.com/JZ2211/IIoT_wk4/blob/main/demo_NodeMCU.ino
*/
//libraries for BME280
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme280;
//setup() function only runs once
void setup() {
Serial.begin(115200); // Start the Serial communication to send messages to the computer
while (!Serial) {} //wait till serial is connected
delay(10); //delay 10 milliseoncds to make sure serial port is connected
Serial.println("start...."); // for debugging
char status=bme280.begin();
while (!status){
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x");
Serial.println(bme280.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1) delay(10); //stop the code here if BME280/680 is not found
}
}
//loop() function will repeat running forever
void loop() {
unsigned int temperature, pressure, humidity;
//obtain the results
temperature = bme280.readTemperature();
pressure = bme280.readPressure();
humidity = bme280.readHumidity();
//display the reseults in the Serial Monitor
Serial.print("Time Stamp(s)");
Serial.println(millis()/1000);
Serial.print(" Temperature (degC): ");
Serial.print(temperature);
Serial.print(", Pressure (Pa): ");
Serial.print(pressure);
Serial.print(", Humidity (%): ");
Serial.println(humidity);
delay(2000); //delay 2000 milliseconds so that update data every 2 seconds
}