{"id":17573147,"url":"https://github.com/ajyounguk/bme280-sensor-logger","last_synced_at":"2026-02-03T18:35:03.982Z","repository":{"id":258769549,"uuid":"874900242","full_name":"ajyounguk/bme280-sensor-logger","owner":"ajyounguk","description":"A python app, that can read BME280 sensor data attached to a device, and post the data to MongoDB and/or MQTT for Home Assistant Integration. The app can also collect environmental data from the MetOffice API.","archived":false,"fork":false,"pushed_at":"2024-10-22T12:21:19.000Z","size":370,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-19T21:45:14.784Z","etag":null,"topics":["bme280","data-logging","environmental-data","home-assistant","humidity","iot","metoffice-api","mongo","mongodb","mqtt","pressure","python","sensor","smart-home","temperature","weather-monitoring"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ajyounguk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-10-18T17:06:39.000Z","updated_at":"2025-02-01T10:19:36.000Z","dependencies_parsed_at":"2025-04-12T20:15:10.445Z","dependency_job_id":null,"html_url":"https://github.com/ajyounguk/bme280-sensor-logger","commit_stats":null,"previous_names":["ajyounguk/bme280-sensor-logger"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ajyounguk/bme280-sensor-logger","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajyounguk%2Fbme280-sensor-logger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajyounguk%2Fbme280-sensor-logger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajyounguk%2Fbme280-sensor-logger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajyounguk%2Fbme280-sensor-logger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ajyounguk","download_url":"https://codeload.github.com/ajyounguk/bme280-sensor-logger/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajyounguk%2Fbme280-sensor-logger/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29052635,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-03T15:43:47.601Z","status":"ssl_error","status_checked_at":"2026-02-03T15:43:46.709Z","response_time":96,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bme280","data-logging","environmental-data","home-assistant","humidity","iot","metoffice-api","mongo","mongodb","mqtt","pressure","python","sensor","smart-home","temperature","weather-monitoring"],"created_at":"2024-10-21T21:00:26.375Z","updated_at":"2026-02-03T18:35:03.961Z","avatar_url":"https://github.com/ajyounguk.png","language":"Python","readme":"# BME280 Temperature Sensor Data Logger (Python)\n\nThis Python-based application monitors environmental data using a BME280 sensor and the Met Office API. Temperature, pressure and humdity readings can then be persisted in a Mongo database, or sent to a MQTT for integration with Home Assistant .\n\n## Features\n\nData collection:\n- BM280 Sensor - Captures temperature, humidity, and pressure data from the BME280 sensor. The application captures data from the BME280 sensor at regular intervals defined in the config.\n- Met Office API: Fetches external / outdoor weather data to enhance your sensor's readings.\n\nData persistency:\n- MQTT: Publishes data to an MQTT broker for real-time monitoring (e.g., Home Assistant).\n- MongoDB: Logs sensor and Met Office data to MongoDB for historical records.\n\nConfiguration File:\nConfiguration parameters in .json file for reading frequency, sensor, MetOffice, Mongo and MQQT parameters. \n\n\u003csub\u003eexample Chart generated from BME280 data posted to Mongo Atlas and charted with Mongo Charts (all in free tier)\u003csub\u003e\n![MongoChart](/screenshots/mongoChart.png?raw=true)\n\n\n## Todo \n- [ ] Post MQTT data in the format for Home Assistant autodiscovery \n- [ ] Change MetOffice API from [DataPoint](https://www.metoffice.gov.uk/services/data/datapoint) to [DataHub](https://www.metoffice.gov.uk/services/data/met-office-weather-datahub) as DataPoint is being retired in 2025\n\n## High level code logic:\n1. load configuration parameters from /config/config.json\n2. main loop\n3. get sensor readings\n4. post sensor data to MQTT \n5. post sensor data to Mongo \n6. get MetOffice readings \n7. post MetOffice data to MQTT \n8. post MetOffice data to Mongo \n9. sleep for configurable amount of seconds \u0026 loop to 2\n\u003eMongo, MQTT and MetOffice integrations can be enabled/disabled in config\n\n## Application structure\n\n`/main.py` main application \n\n`/src/sensor.py` BME280 integration code\n\n`/src/metoffice.py` MetOffice API integration code\n\n`/src/mongo.py` Mongo database integration code\n\n`/src/mqtt_handler` MQTT handler code (can be used for integration with Home Assistant)\n\n***\n\n## Installation\n\nEnsure that I2C interface is enabled on the Raspberry Pi. Check `raspi-config` (under `Interface Options` \u003e `I2C` = enabled):\n```bash\nsudo raspi-config\n```\n- Update OS packages\n```bash\nsudo apt-get update\n```\n\n- Install git\n```bash\nsudo apt-get install git\n```\n\n- Clone the repository\n```bash\ngit clone https://github.com/ajyounguk/bme280-sensor-logger\n```\n\n- Install Python\n```bash\nsudo apt install python3-pip\n```\n\n- move to bme280-sensor-logger directory\n```bash\ncd bme280-sensor-logger\n```\n\n\n-  Activate a Python Virtual Environment\n install venv if not available\n```bash\nsudo apt install python3-venv\n```\n\n```bash\npython3 -m venv venv\n```\n```bash\nsource venv/bin/activate\n```\n\n\n- Install Dependencies\n```bash\npip install -r requirements.txt\n```\n\n***\n\n## Configuration\n\n- Copy the example config file to config.json\n```bash\ncd config\n```\n```bash\ncp example-config.json config.json\n```\n- Customise the configuration filewith your MongoDB, MQTT, and Met Office credentials as per below:\n\nApplication:\n- `readingIntervalSeconds` - Application interval between sensor and MetOffice readings\n- `deviceName` - Name for this device, used to tag Mongo and MQTT records\n\n\nSensor:\n- `i2c_port`: Port number (typically 1).\n- `bme280_address`: The default I2C address for the BME280 sensor (varies between 0x76 and 0x77).\n\n\nMetOffice:\n- `enabled`: Enable or disable MetOffice data integration\n- `locationID`: Numeric value for the MetOffice location where data is collected (see notes below).\n- `APIKey`:  Your MetOffice API key (can be obtained [here](https://www.metoffice.gov.uk/services/data/datapoint/api)).\n\n\u003e The `locationID` denotes your weather data location. You may find success using 4-digit location IDs, often representing larger regions or Counties in the UK. See [this list](https://gist.github.com/ajyounguk/e05db10df74e0b86c7e6a0a39a95f1f4) for more.\n\n\nMongo:\n- `enabled`: Enable or disable MongoDB data integration\n- `uri`: Connection string for your Mongo database\n- `dbName`: Mongo database name\n- `collection`: collection name for storing readings.\n- `sensorSource`: source name for the bme280 documents stored in mongo\n- `metofficeSource`: source name for the MetOffice documents stored in mongo\n\n\nMQTT:\n- `enabled`: Enable or disable MQTT data integration\n- `broker`: IP address or FQDN for the MQTT Broker (For Home Assistant this is the address of your Home Assistant host)\n- `port`: 1833 is the default port\n- `mqttUsername`: MQTT username (for Home Assistant, use a dedicated user created for MQTT)\n- `mqttPassword`: The corresponding MQTT password.\n- `sensorTopic`: MQTT topic name to receive the bme280 sensor data \n- `metofficeTopic`: MQTT topic name to receive the MetOffice data\n\nExample configuration.json:\n```json\n{\n  \"app\": {\n      \"readingIntervalSeconds\": 600,\n      \"deviceName\": \"lounge_rpi\"\n  },\n  \"sensor\": {\n      \"i2c_port\": 1,\n      \"bme280_address\": \"0x76\"\n  },\n  \"metoffice\": {\n      \"enabled\": true,\n      \"locationID\": \"3414\",  \n      \"APIKey\": \"YOUR_METOFFICE_API_KEY\"           \n  },\n  \"mongo\": {\n      \"enabled\" : true,\n      \"uri\": \"mongodb+srv://MONGO_USERNAME:MONGO_PASSWORD@cluster0.1haxi.mongodb.net/DATABASE?retryWrites=true\u0026w=majority\",\n      \"dbName\": \"YOUR_DB_NAME\",\n      \"collection\": \"house_temperature_readings\",\n      \"sensorSource\": \"lounge\",\n      \"metofficeSource\": \"shropshire_outside\"\n  },\n  \"mqtt\": {\n      \"enabled\": true,\n      \"broker\": \"192.168.1.250\",\n      \"port\": 1883,\n      \"mqttUsername\": \"YOUR_MQTT_USERNAME\",\n      \"mqttPassword\": \"YOUR_MQTT_PASSWORD\",\n      \"sensorTopic\": \"homeassistant/bme280_lounge\",\n      \"metofficeTopic\": \"homeassistant/metoffice\"\n  }\n}\n```\n\n\n***\n## Run the Application:\n\n```bash\npython main.py\n```\n\n## Sample Output\n```\n[INFO 2024-10-19 16:29:47] - Connected to MQTT broker at 192.168.1.250:1883\n[INFO 2024-10-19 16:29:47] - I2C Port: 1, BME280 Address: 118\n[INFO 2024-10-19 16:29:47] - Reading Interval: 30 seconds\n[INFO 2024-10-19 16:29:47] - MongoDB Enabled: True\n[INFO 2024-10-19 16:29:47] - Met Office Enabled: True\n[INFO 2024-10-19 16:29:47] - MQTT Enabled: True\n[INFO 2024-10-19 16:29:47] - BME280 - Temperature: 23.44 °C, Pressure: 1001.85 hPa, Humidity: 47.63 %\n[INFO 2024-10-19 16:29:48] - MetOffice - Temperature: 15.7 °C, Humidity: 58.3 %, Pressure: 1011.0 hPa, Wind: 6.0 m/s\n[INFO 2024-10-19 16:30:19] - BME280 - Temperature: 23.44 °C, Pressure: 1001.87 hPa, Humidity: 47.45 %\n[INFO 2024-10-19 16:30:20] - MetOffice - Temperature: 15.7 °C, Humidity: 58.3 %, Pressure: 1011.0 hPa, Wind: 6.0 m/s\n```\n\n***\n# Additional Information\n\n#### MongoDB\n\nMongoDB can be hosted anywhere. The app works well with a free-tier Mongo Atlas cloud database. See [MongoDB Atlas](https://cloud.mongodb.com).\n\n- The `sensorSource` configuration is used to populate the `source` field for sensor reading data in Mongo, allowing multiple devices to store readings in the same collection.\n- The `metofficeSource` configuration is used to populate the `source` field for MetOffice data documents. \n- The MongoDB document structure looks like this:\n```json\n{\n  \"source\": \"lounge\",\n  \"temperature\": 22.5,\n  \"humidity\": 55.2,\n  \"pressure\": 1012.8,\n  \"wind\": 5.6,  // Only for Met Office data documents, Null for BME280 readings\n  \"timestamp\": \"2024-10-19T15:11:21.031+00:00\",\n}\n```\n\u003eYou can visualize data from different devices using MongoDB Charts. See [MongoDB Charts](https://www.mongodb.com/products/charts).\n\n\n---\n#### MQTT \n\nFor debugging purposes in Home Assitant you can use the inbuilt MQTT listener to monitor for messages from the clients by setting it to the relevant MQTT topic name:\n\n\u003e In Home Assistant go to Settings | Devices and Services | MQTT | Configure\n\n![MQTT](/screenshots/MQTT.png?raw=true)\n\n\n---\n#### MetOffice\n\n- MetOffice readings typically only updated once per hour.\n- In oder to avoid redundant MetOffice readings, the code checks if a MetOffice document already exists  in the Mongo database, if so it will skip creating another.\n- THis also ensures that if you are running multiple sensors, only a single MetOffice record is created in each hourly interval.\n\n---\n#### BME280 Sensor\n\n\n\n- The sensor typically operates on an I2C bus. To ensure proper communication, verify the bus number and address configuration. You can use `i2cdetect` to verify if your sensor is detected correctly.\n\nTo detect the port number for the BME280 sensor, use this command (note: the bus port may vary depending on the Pi model):\n\n```bash\n# install i2cdetect\nsudo apt-get update\nsudo apt-get install i2c-tools\n```\n\n```bash\ni2cdetect -y 1 # for newwer models\ni2cdetect -y 0  # for older 256MB Raspberry Pi\n```\n- In case of errors, double-check the wiring and bus settings. [There is a guide here](https://www.hackster.io/Shilleh/beginner-tutorial-how-to-connect-raspberry-pi-and-bme280-4fdbd5)\n\n---\n#### pm2\n\npm2 is a process manager for keeping applications running in the background (and autostart on boot).\nalthough it is mainly a NodeJS process manager, it works great with Python applications too. If you want to install it try:\n\n- nodejs installation\n```bash\nsudo apt update\n```\n```bash\nsudo apt install -y nodejs npm\n```\n\n- verify nodejs and npm installation\n```bash\nnode -v\n```\n```bash\nnpm -v \n```\n\n- install pm2 using npm\n```bash\nsudo npm install -g pm2\n```\n\n- verify pm2 nstallation\n```bash\npm2 -v\n```\n\n- set pm2 to start on boot\n```bash\npm2 startup\n```\n\n- it will generate a command specific to your system, which might look like this\n\u003e sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u pi --hp /home/pi\n- run the generated command to enable pm2 to start on boot\n\n\n- change to application directory \n\ne.g.\n```bash\ncd bme280-sensor-logger\n```\n- start the the application (main.py) with pm2\n```bash\npm2 start main.py\n```\n\n- save it into the pm2 process list\n```bash\npm2 save\n```\n\n- other pm2 commands (see `pm2 --help`) :\n\n```bash\npm2 status # will display process number for the application if runnign\npm2 logs # view logs\npm2 start 0 # or your process number from status\npm2 stop 0 # or your process number from status\npm2 restart 0  # or your process number from status\n```\n\n---\n#### Home Assistant Integration\n\nFor those using Home Assistant, you can use the MQTT integration to display temperature, pressure, and humidity data from your sensors. \n\nHome assistant integration is beyond the scope of this README, there are many better guides on the web for this. \nI recommend starting with the MQTT mosquito add-on for Home Assistant. Then configure the MQTT device integration. A separate credential (Home Assistant person) is recommended to provide access controls for the MQTT integration. Use the same credentials in the MQTT configuration for this application (`mqttUsername` and `mqttPassword`).\n\nFinally you will need to configure your Home Assistant `configuration.yaml` file to listen to the relevant MQTT topics and define the devices/entities for the sensor readings in line with the example below:\n\n\n```yaml\nmqtt:\n  sensor:\n    - name: \"BME280 Temperature\"\n      state_topic: \"homeassistant/bme280_lounge\"\n      unit_of_measurement: \"°C\"\n      value_template: \"{{ value_json.temperature }}\"\n  \n    - name: \"BME280 Pressure\"\n      state_topic: \"homeassistant/bme280_lounge\"\n      unit_of_measurement: \"hPa\"\n      value_template: \"{{ value_json.pressure }}\"\n  \n    - name: \"BME280 Humidity\"\n      state_topic: \"homeassistant/bme280_lounge\"\n      unit_of_measurement: \"%\"\n      value_template: \"{{ value_json.humidity }}\"\n  \n    - name: \"BME280 Wind Speed\"\n      state_topic: \"homeassistant/bme280_lounge\"\n      unit_of_measurement: \"m/s\"\n      value_template: \"{{ value_json.wind }}\"\n  \n    - name: \"Met Office Temperature\"\n      state_topic: \"homeassistant/metoffice\"\n      unit_of_measurement: \"°C\"\n      value_template: \"{{ value_json.temperature }}\"\n  \n    - name: \"Met Office Pressure\"\n      state_topic: \"homeassistant/metoffice\"\n      unit_of_measurement: \"hPa\"\n      value_template: \"{{ value_json.pressure }}\"\n  \n    - name: \"Met Office Humidity\"\n      state_topic: \"homeassistant/metoffice\"\n      unit_of_measurement: \"%\"\n      value_template: \"{{ value_json.humidity }}\"\n  \n    - name: \"Met Office Wind Speed\"\n      state_topic: \"homeassistant/metoffice\"\n      unit_of_measurement: \"m/s\"\n      value_template: \"{{ value_json.wind }}\"\n```\nHome Assistant Screenshots\n\n![HA](/screenshots/HA.png?raw=true)\n\n![HA2](/screenshots/HA2.png?raw=true)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajyounguk%2Fbme280-sensor-logger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fajyounguk%2Fbme280-sensor-logger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajyounguk%2Fbme280-sensor-logger/lists"}