Adapt Adafruit BME280 example to have a simple Serial text interface
This commit is contained in:
parent
ea2222ea2b
commit
70984d14d7
1 changed files with 136 additions and 0 deletions
136
bme280/bme280.ino
Normal file
136
bme280/bme280.ino
Normal file
|
@ -0,0 +1,136 @@
|
|||
/***************************************************************************
|
||||
This is a library for the BME280 humidity, temperature & pressure sensor
|
||||
|
||||
Designed specifically to work with the Adafruit BME280 Breakout
|
||||
----> http://www.adafruit.com/products/2650
|
||||
|
||||
These sensors use I2C or SPI to communicate, 2 or 4 pins are required
|
||||
to interface. The device's I2C address is either 0x76 or 0x77.
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit andopen-source hardware by purchasing products
|
||||
from Adafruit!
|
||||
|
||||
Written by Limor Fried & Kevin Townsend for Adafruit Industries.
|
||||
BSD license, all text above must be included in any redistribution
|
||||
See the LICENSE file for details.
|
||||
***************************************************************************/
|
||||
|
||||
#include <Wire.h>
|
||||
#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
|
||||
|
||||
|
||||
// Read buffer
|
||||
#define BUFFER_SIZE 4
|
||||
|
||||
int bufferIndex = 0;
|
||||
char buffer[BUFFER_SIZE];
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while(!Serial); // time to get serial running
|
||||
Serial.println(F("BME280 test"));
|
||||
|
||||
unsigned status;
|
||||
|
||||
// default settings
|
||||
status = bme.begin(0x76);
|
||||
// You can also pass in a Wire library object like &Wire2
|
||||
// status = bme.begin(0x76, &Wire2)
|
||||
if (!status) {
|
||||
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
|
||||
Serial.print("SensorID was: 0x"); Serial.println(bme.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);
|
||||
}
|
||||
|
||||
Serial.println("-- Default Test --");
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
while(Serial.available() > 0) {
|
||||
buffer[bufferIndex] = Serial.read();
|
||||
|
||||
// Serial.print("Reading char, index: ");
|
||||
// Serial.print(bufferIndex, DEC);
|
||||
// Serial.print(", is greater: ");
|
||||
// Serial.print(bufferIndex >= BUFFER_SIZE);
|
||||
// Serial.print(", char: ");
|
||||
// Serial.print(buffer[bufferIndex]);
|
||||
// Serial.print(", hex: ");
|
||||
// Serial.println(buffer[bufferIndex], HEX);
|
||||
|
||||
// If it's a newline character, overwrite it
|
||||
if(buffer[bufferIndex] < 48 || buffer[bufferIndex] > 122) {
|
||||
// Serial.println("Skipping unrecognised char");
|
||||
continue;
|
||||
}
|
||||
|
||||
if(bufferIndex >= BUFFER_SIZE - 1) {
|
||||
// Serial.println("Handling command");
|
||||
handle_command();
|
||||
// Serial.print("Resetting index, is now: ");
|
||||
bufferIndex = 0;
|
||||
// Serial.println(bufferIndex, DEC);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Serial.println("Incrementing counter");
|
||||
bufferIndex += 1;
|
||||
}
|
||||
}
|
||||
|
||||
void handle_command() {
|
||||
// Serial.print("strcmp: ");
|
||||
// Serial.println(strncmp(buffer, "read", BUFFER_SIZE), DEC);
|
||||
|
||||
if(strncmp(buffer, "read", BUFFER_SIZE) == 0) { // 0 = They are the same
|
||||
printValues();
|
||||
}
|
||||
else {
|
||||
Serial.print("Error: Unknown command ");
|
||||
Serial.println(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void printValues() {
|
||||
Serial.print("Temperature ");
|
||||
Serial.println(bme.readTemperature());
|
||||
// Serial.println(" *C");
|
||||
|
||||
Serial.print("Pressure ");
|
||||
|
||||
Serial.println(bme.readPressure() / 100.0F);
|
||||
// Serial.println(" hPa");
|
||||
|
||||
Serial.print("ApproxAltitude ");
|
||||
Serial.println(bme.readAltitude(SEALEVELPRESSURE_HPA));
|
||||
// Serial.println(" m");
|
||||
|
||||
Serial.print("Humidity ");
|
||||
Serial.println(bme.readHumidity());
|
||||
// Serial.println(" %");
|
||||
|
||||
// Serial.println();
|
||||
}
|
Loading…
Reference in a new issue