Python Forum
save my sensor data from the bme680 into a json or csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
save my sensor data from the bme680 into a json or csv file
#1
Hello,

I would like to save my sensor data from the bme680 into a json or csv file. For the last section I wrote myself. I've been trying to learn python for a few months now. Perhaps you can help me. I do not really know what I have to use for 'numbers'.

BME680 Example https://github.com/pimoroni/bme680-python

Many thanks

#!/usr/bin/env python

import bme680
import time
import json

print("""indoor-air-quality.py - Estimates indoor air quality.
Runs the sensor for a burn-in period, then uses a
combination of relative humidity and gas resistance
to estimate indoor air quality as a percentage.
Press Ctrl+C to exit!
""")

try:
    sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except IOError:
    sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)

# These oversampling settings can be tweaked to
# change the balance between accuracy and noise in
# the data.

sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680.OS_8X)
sensor.set_filter(bme680.FILTER_SIZE_3)
sensor.set_gas_status(bme680.ENABLE_GAS_MEAS)

sensor.set_gas_heater_temperature(320)
sensor.set_gas_heater_duration(150)
sensor.select_gas_heater_profile(0)

# start_time and curr_time ensure that the
# burn_in_time (in seconds) is kept track of.

start_time = time.time()
curr_time = time.time()
burn_in_time = 300

burn_in_data = []

try:
    # Collect gas resistance burn-in values, then use the average
    # of the last 50 values to set the upper limit for calculating
    # gas_baseline.
    print('Collecting gas resistance burn-in data for 5 mins\n')
    while curr_time - start_time < burn_in_time:
        curr_time = time.time()
        if sensor.get_sensor_data() and sensor.data.heat_stable:
            gas = sensor.data.gas_resistance
            burn_in_data.append(gas)
            print('Gas: {0} Ohms'.format(gas))
            time.sleep(10)

    gas_baseline = sum(burn_in_data[-50:]) / 50.0

    # Set the humidity baseline to 40%, an optimal indoor humidity.
    hum_baseline = 40.0

    # This sets the balance between humidity and gas reading in the
    # calculation of air_quality_score (25:75, humidity:gas)
    hum_weighting = 0.25

    print('Gas baseline: {0} Ohms, humidity baseline: {1:.2f} %RH\n'.format(
        gas_baseline,
        hum_baseline))

    while True:
        if sensor.get_sensor_data() and sensor.data.heat_stable:
            gas = sensor.data.gas_resistance
            gas_offset = gas_baseline - gas

            hum = sensor.data.humidity
            hum_offset = hum - hum_baseline

            # Calculate hum_score as the distance from the hum_baseline.
            if hum_offset > 0:
                hum_score = (100 - hum_baseline - hum_offset)
                hum_score /= (100 - hum_baseline)
                hum_score *= (hum_weighting * 100)

            else:
                hum_score = (hum_baseline + hum_offset)
                hum_score /= hum_baseline
                hum_score *= (hum_weighting * 100)

            # Calculate gas_score as the distance from the gas_baseline.
            if gas_offset > 0:
                gas_score = (gas / gas_baseline)
                gas_score *= (100 - (hum_weighting * 100))
            else:
                gas_score = 100 - (hum_weighting * 100)

            # Calculate air_quality_score.
            air_quality_score = hum_score + gas_score

            print('Gas: {0:.2f} Ohms,humidity: {1:.2f} %RH,air quality: {2:.2f}'.format(
                gas,
                hum,
                air_quality_score))

            time.sleep(10)

#import json

numbers = "Gas: {0:.2f} Ohms,humidity: {1:.2f} %RH,air quality: {2:.2f}" **think**  **think**  **think** 

filename = 'home/pi/bme680/log.json'

try:
    with open(filename, 'w', encoding='utf8') as f_obj:
        json.dump(numbers, f_obj)
except:
    msg= "Sorry, the file " + filename + " does not exist."
    print(msg)


except KeyboardInterrupt:
    pass
Reply
#2
The code works until line 102. From line 104 it was written by me.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] Save a matplotlib figure into hdf5 file paul18fr 1 2,472 Jun-08-2021, 05:58 PM
Last Post: paul18fr
  HELP! Importing json file into csv into jupyter notebook vilsef 2 2,533 Jan-22-2021, 11:06 AM
Last Post: snippsat
  Automating to save generated data Robotguy 3 2,229 Aug-12-2020, 03:32 PM
Last Post: Robotguy
  JSON file Loading issue punna111 4 8,493 Jun-29-2020, 08:07 AM
Last Post: buran
  Indirectlty convert string to float in JSON file WBPYTHON 6 5,831 May-06-2020, 12:09 PM
Last Post: WBPYTHON
  Read json array data by pandas vipinct 0 1,902 Apr-13-2020, 02:24 PM
Last Post: vipinct
  Help batch converting .json chosen file to MySQL BrandonKastning 2 2,287 Mar-14-2020, 09:19 PM
Last Post: BrandonKastning
  Corrupted numpy arrays when save to file. DreamingInsanity 2 3,187 Dec-14-2019, 12:12 PM
Last Post: DreamingInsanity
  Handling escape charters while converting data frame to JSON RahulShukla 0 1,668 Nov-11-2019, 11:22 AM
Last Post: RahulShukla
  Is there a way to save a CSV file as a python object amjass12 4 2,681 Jul-16-2019, 12:00 PM
Last Post: amjass12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020