Skip to content

Instantly share code, notes, and snippets.

@didasy
Created August 4, 2017 15:45
Show Gist options
  • Select an option

  • Save didasy/c8d9e95d49143963dc33ea658ee72f63 to your computer and use it in GitHub Desktop.

Select an option

Save didasy/c8d9e95d49143963dc33ea658ee72f63 to your computer and use it in GitHub Desktop.
Arduino sketch for BME280 to spew JSON data
#include <Wire.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
void setup() {
Serial.begin(9600);
// default settings
bool status = bme.begin();
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
// wait 1s for sensor initialization
delay(1000);
}
void loop() {
// get data
float temp = bme.readTemperature(); // in C
float pres = bme.readPressure() / 100.0F; // in hPa
float alt = bme.readAltitude(SEALEVELPRESSURE_HPA); // in M
float hum = bme.readHumidity(); // in %
// send to serial
printDataJSON(temp, pres, alt, hum);
// delay 1000ms before sending another data
delay(1000);
}
// Print the data as JSON to serial
void printDataJSON(float temp, float pres, float alt, float hum) {
Serial.print("{");
Serial.print("\"temp\":");
Serial.print(temp);
Serial.print(",");
Serial.print("\"pressure\":");
Serial.print(pres);
Serial.print(",");
Serial.print("\"altitude\":");
Serial.print(alt);
Serial.print(",");
Serial.print("\"humidity\":");
Serial.print(hum);
Serial.print("}");
Serial.print("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment