Asking for code examples for ESP32 using the BME280 Sensor

Hello! Can someone help me put together code for a PUT request to HTTPS for my ESP32 DevKit, where I just want to send some temperature data? I'm learning code as I go, but have some problems putting it all together. I also want to use HTTPS without the cert or fingerprint. I don't worry to much if a middle man gets my temperature data. I want to use the on-board WiFi controller and it needs to be a PUT request using the URL. The following code is just the sensor posting to the Serial Monitor. I want to send the data to my website using HTTPS with no cert using a PUT request (webserver has a PHP file with a GET request already working). Would be nice if code had comments and was optimized and without memory leaks (since it's C++ which doesn't have garbage collection). I see some examples uses WiFi.h and some use WiFiMulti.h, and some use WiFiClientSecure.h, HTTPClient.h and HTTPClientSecure.h. I don't really know what library is needed for this task. Thanks in advance!

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C

unsigned long delayTime;

void setup() {
    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 --");
    delayTime = 1000;


void loop() { 

void printValues() {
    Serial.print("Temperature = ");
    Serial.println(" *C");

    Serial.print("Pressure = ");
    Serial.print(bme.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print("Approx. Altitude = ");
    Serial.println(" m");

    Serial.print("Humidity = ");
    Serial.println(" %");


Current PUT request data needed to be sent are something along the lines of:

    // send the HTTP PUT request
    client.print(F("GET /sensors/write_data.php"));
    client.println(F(" HTTP/1.0"));
    client.println("Connection: close");

please for me too!

:smiley: :grinning_face_with_smiling_eyes: :grinning:

It's illogical to define the standard pressure in your sketch - you have to assume Adafruit left that so their example works OK. You'll find it's about twice a year when the pressure at mean sea level is exactly that number in your location.

Much better, if your sensor is in a fixed position to set your elevation.

Remember from a pressure reading you either need to know the current pressure at sea level then you can determine your elevation. Or if you know your elevation you can determine the current pressure at sea level. You can't determine both simultaneously.

Your fixed elevation can be determined with a GPS receiver (probably the one in your mobile phone) with reasonable accuracy.

You then use the following "simple" floating point maths to determine the other two values.

#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
Adafruit_BME280 bme;  

const float myElevation = 122.5; // example to show how my code works
const float currentMSL = 1009.14; // current reading at 122.5 metres above MSL

float elevation(float msl, float pressure) {
  float metres;
  metres = (44330.0 * (1 - pow(pressure / msl, 1 / 5.255)));
  return metres;

float msl(float elevation, float pressure) {
  float hPa;
  hPa = pressure / pow(1 - (elevation / 44330.0), 5.255);
  return hPa;

void setup() {
  unsigned status;
  status = bme.begin();

void loop() {
  Serial.println(elevation(currentMSL, bme.readPressure()));
  Serial.println(msl(myElevation, bme.readPressure()));

Doing the HTTP GET request to a PHP program isn't something I'd do. My preferred method of sending IoT sensor data back to a server is MQTT using the PubSubClient library.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* SSID = "<change me to your SSID>";
const char* PWD = "<change me to your secret>";
const char* MQTT_broker = ""; // Raspberry Pi broker

WiFiClient WiFiCL;
PubSubClient client(WiFiCL);

void setup() {
  WiFi.begin(SSID, PWD);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    client.setServer(MQTT_broker, 1883);
  String clientId = "ESPClient-";
  clientId += String(random(0xffff), HEX);

void loop() {
  char payload[10];
  float pressure = 1009.4;
  snprintf(payload, sizeof(payload), "%f", pressure);
  client.publish("tester/out/pressure", payload);

It's fairly simple to glue those two pieces of code together.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.