Rishabh Jain
Created February 10, 2025

EnviroScope : The ultimate environmental guardian!

This cutting-edge device measures crucial parameters such as AQI, UV levels, temperature, humidity, etc. with pinpoint accuracy

65
EnviroScope : The ultimate environmental guardian!

Things used in this project

Story

Read more

Schematics

Schematic MCU and ESP32

Code

BME680 code

C/C++
#include "stm32f1xx_hal.h"
#include "bme680.h"  // Include your BME680 library here

// I2C handle declaration
I2C_HandleTypeDef hi2c1;

// Function prototypes
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
void BME680_Init(void);
void BME680_ReadData(void);

int main(void) {
  // Initialize the HAL Library
  HAL_Init();

  // Configure the system clock
  SystemClock_Config();

  // Initialize all configured peripherals
  MX_GPIO_Init();
  MX_I2C1_Init();

  // Initialize the BME680 sensor
  BME680_Init();

  while (1) {
    // Read and print BME680 data
    BME680_ReadData();
    HAL_Delay(1000);  // Delay 1 second
  }
}

// I2C1 Initialization Function
static void MX_I2C1_Init(void) {
  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 100000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK) {
    // Initialization Error
    Error_Handler();
  }
}

// GPIO Initialization Function
static void MX_GPIO_Init(void) {
  // Configure GPIO pins if needed
}

// BME680 Initialization Function
void BME680_Init(void) {
  // Initialize the BME680 sensor here
  // Example: bme680_init(&hi2c1);
}

// BME680 Read Data Function
void BME680_ReadData(void) {
  struct bme680_field_data data;
  // Example: bme680_get_sensor_data(&data, &hi2c1);

  // Print the data
  printf("Temperature: %.2f °C\n", data.temperature / 100.0);
  printf("Pressure: %.2f hPa\n", data.pressure / 100.0);
  printf("Humidity: %.2f %%\n", data.humidity / 1000.0);
  printf("Gas resistance: %d Ohms\n", data.gas_resistance);
}

// System Clock Configuration Function
void SystemClock_Config(void) {
  // Configure the system clock here
}

// Error Handler Function
void Error_Handler(void) {
  while (1) {
    // Stay in an infinite loop in case of an error
  }
}

int __io_putchar(int ch) {
  // Implement the putchar function to use printf
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
  return ch;
}

Credits

Rishabh Jain
7 projects • 24 followers
I am a technical blogger. Passionate about new cutting-edge technologies.

Comments