wiping everything to get exactly what is on the pi

This commit is contained in:
Brian 2024-12-19 09:43:50 -07:00
parent 041ba75d8a
commit ceca171b3c
Signed by: brian
GPG Key ID: 5032868E98A381FA
11 changed files with 0 additions and 2223 deletions

24
.gitignore vendored
View File

@ -1,24 +0,0 @@
### Project Specific
# Files
*.a
*.o
*.log
# Directories
### Editor Specific
# Files
temperatureRecorder.sublime-project
temperatureRecorder.sublime-workspace
# Directories
.idea
.vscode
### System Specific
# Files
.DS_Store
Thumbs.db
# Directories
__MACOSX

View File

@ -1,22 +0,0 @@
STANDARD := gnu99
DEBUG_DIR := build/Debug
BIN_DEBUG_DIR := bin/Debug
RELEASE_DIR := build/Release
BIN_RELEASE_DIR := bin/Release
debug:
gcc -Wall -fPIC -pg -g -c src/bme280/bme280.c -o $(DEBUG_DIR)/bme280.o -std=${STANDARD}
gcc -Wall -fPIC -pg -g -c src/main.c -o $(DEBUG_DIR)/main.o -std=${STANDARD}
gcc -Wall -fPIC -pg -g -o $(BIN_DEBUG_DIR)/tempRecord $(DEBUG_DIR)/main.o $(DEBUG_DIR)/bme280.o -std=${STANDARD}
release:
gcc -Wall -fPIC -O2 -c src/bme280/bme280.c -o $(RELEASE_DIR)/bme280.o -std=${STANDARD}
gcc -Wall -fPIC -O2 -c src/main.c -o $(RELEASE_DIR)/main.o -std=${STANDARD}
gcc -Wall -fPIC -O2 -o $(BIN_RELEASE_DIR)/tempRecord $(RELEASE_DIR)/main.o $(RELEASE_DIR)/bme280.o -std=${STANDARD}
.PHONY clean:
rm -rf -v !\(.gitignore\) $(DEBUG_DIR)/*
rm -rf -v !\(.gitignore\) $(BIN_DEBUG_DIR)/*
rm -rf -v !\(.gitignore\) $(RELEASE_DIR)/*
rm -rf -v !\(.gitignore\) $(BIN_RELEASE_DIR)/*

View File

@ -1,2 +0,0 @@
*
!.gitignore

View File

@ -1,2 +0,0 @@
*
!.gitignore

View File

@ -1,2 +0,0 @@
*
!.gitignore

View File

@ -1,2 +0,0 @@
*
!.gitignore

File diff suppressed because it is too large Load Diff

View File

@ -1,240 +0,0 @@
/**
* Copyright (C) 2016 - 2017 Bosch Sensortec GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the copyright holder nor the names of the
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
* OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*
* The information provided is believed to be accurate and reliable.
* The copyright holder assumes no responsibility
* for the consequences of use
* of such information nor for any infringement of patents or
* other rights of third parties which may result from its use.
* No license is granted by implication or otherwise under any patent or
* patent rights of the copyright holder.
*
* @file bme280.h
* @date 14 Feb 2018
* @version 3.3.4
* @brief
*
*/
/*! @file bme280.h
@brief Sensor driver for BME280 sensor */
/*!
* @defgroup BME280 SENSOR API
* @{*/
#ifndef BME280_H_
#define BME280_H_
/*! CPP guard */
#ifdef __cplusplus
extern "C" {
#endif
/* Header includes */
#include "bme280_defs.h"
/*!
* @brief This API is the entry point.
* It reads the chip-id and calibration data from the sensor.
*
* @param[in,out] dev : Structure instance of bme280_dev
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
int8_t bme280_init(struct bme280_dev *dev);
/*!
* @brief This API writes the given data to the register address
* of the sensor.
*
* @param[in] reg_addr : Register address from where the data to be written.
* @param[in] reg_data : Pointer to data buffer which is to be written
* in the sensor.
* @param[in] len : No of bytes of data to write..
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
int8_t bme280_set_regs(uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len, const struct bme280_dev *dev);
/*!
* @brief This API reads the data from the given register address of the sensor.
*
* @param[in] reg_addr : Register address from where the data to be read
* @param[out] reg_data : Pointer to data buffer to store the read data.
* @param[in] len : No of bytes of data to be read.
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, const struct bme280_dev *dev);
/*!
* @brief This API sets the oversampling, filter and standby duration
* (normal mode) settings in the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[in] desired_settings : Variable used to select the settings which
* are to be set in the sensor.
*
* @note : Below are the macros to be used by the user for selecting the
* desired settings. User can do OR operation of these macros for configuring
* multiple settings.
*
* Macros | Functionality
* -----------------------|----------------------------------------------
* BME280_OSR_PRESS_SEL | To set pressure oversampling.
* BME280_OSR_TEMP_SEL | To set temperature oversampling.
* BME280_OSR_HUM_SEL | To set humidity oversampling.
* BME280_FILTER_SEL | To set filter setting.
* BME280_STANDBY_SEL | To set standby duration setting.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error.
*/
int8_t bme280_set_sensor_settings(uint8_t desired_settings, const struct bme280_dev *dev);
/*!
* @brief This API gets the oversampling, filter and standby duration
* (normal mode) settings from the sensor.
*
* @param[in,out] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error.
*/
int8_t bme280_get_sensor_settings(struct bme280_dev *dev);
/*!
* @brief This API sets the power mode of the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[in] sensor_mode : Variable which contains the power mode to be set.
*
* sensor_mode | Macros
* ---------------------|-------------------
* 0 | BME280_SLEEP_MODE
* 1 | BME280_FORCED_MODE
* 3 | BME280_NORMAL_MODE
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
int8_t bme280_set_sensor_mode(uint8_t sensor_mode,
const struct bme280_dev *dev);
/*!
* @brief This API gets the power mode of the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[out] sensor_mode : Pointer variable to store the power mode.
*
* sensor_mode | Macros
* ---------------------|-------------------
* 0 | BME280_SLEEP_MODE
* 1 | BME280_FORCED_MODE
* 3 | BME280_NORMAL_MODE
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
int8_t bme280_get_sensor_mode(uint8_t *sensor_mode, const struct bme280_dev *dev);
/*!
* @brief This API performs the soft reset of the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error.
*/
int8_t bme280_soft_reset(const struct bme280_dev *dev);
/*!
* @brief This API reads the pressure, temperature and humidity data from the
* sensor, compensates the data and store it in the bme280_data structure
* instance passed by the user.
*
* @param[in] sensor_comp : Variable which selects which data to be read from
* the sensor.
*
* sensor_comp | Macros
* ------------|-------------------
* 1 | BME280_PRESS
* 2 | BME280_TEMP
* 4 | BME280_HUM
* 7 | BME280_ALL
*
* @param[out] comp_data : Structure instance of bme280_data.
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
int8_t bme280_get_sensor_data(uint8_t sensor_comp, struct bme280_data *comp_data, struct bme280_dev *dev);
/*!
* @brief This API is used to parse the pressure, temperature and
* humidity data and store it in the bme280_uncomp_data structure instance.
*
* @param[in] reg_data : Contains register data which needs to be parsed
* @param[out] uncomp_data : Contains the uncompensated pressure, temperature
* and humidity data.
*/
void bme280_parse_sensor_data(const uint8_t *reg_data, struct bme280_uncomp_data *uncomp_data);
/*!
* @brief This API is used to compensate the pressure and/or
* temperature and/or humidity data according to the component selected by the
* user.
*
* @param[in] sensor_comp : Used to select pressure and/or temperature and/or
* humidity.
* @param[in] uncomp_data : Contains the uncompensated pressure, temperature and
* humidity data.
* @param[out] comp_data : Contains the compensated pressure and/or temperature
* and/or humidity data.
* @param[in] calib_data : Pointer to the calibration data structure.
*
* @return Result of API execution status.
* @retval zero -> Success / -ve value -> Error
*/
int8_t bme280_compensate_data(uint8_t sensor_comp, const struct bme280_uncomp_data *uncomp_data,
struct bme280_data *comp_data, struct bme280_calib_data *calib_data);
#ifdef __cplusplus
}
#endif /* End of CPP guard */
#endif /* BME280_H_ */
/** @}*/

View File

@ -1,373 +0,0 @@
/**
* Copyright (C) 2016 - 2017 Bosch Sensortec GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the copyright holder nor the names of the
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
* OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*
* The information provided is believed to be accurate and reliable.
* The copyright holder assumes no responsibility
* for the consequences of use
* of such information nor for any infringement of patents or
* other rights of third parties which may result from its use.
* No license is granted by implication or otherwise under any patent or
* patent rights of the copyright holder.
*
* @file bme280_defs.h
* @date 14 Feb 2018
* @version 3.3.4
* @brief
*
*/
/*! @file bme280_defs.h
@brief Sensor driver for BME280 sensor */
/*!
* @defgroup BME280 SENSOR API
* @brief
* @{*/
#ifndef BME280_DEFS_H_
#define BME280_DEFS_H_
/********************************************************/
/* header includes */
#ifdef __KERNEL__
#include <linux/types.h>
#include <linux/kernel.h>
#else
#include <stdint.h>
#include <stddef.h>
#endif
/********************************************************/
/*! @name Common macros */
/********************************************************/
#if !defined(UINT8_C) && !defined(INT8_C)
#define INT8_C(x) S8_C(x)
#define UINT8_C(x) U8_C(x)
#endif
#if !defined(UINT16_C) && !defined(INT16_C)
#define INT16_C(x) S16_C(x)
#define UINT16_C(x) U16_C(x)
#endif
#if !defined(INT32_C) && !defined(UINT32_C)
#define INT32_C(x) S32_C(x)
#define UINT32_C(x) U32_C(x)
#endif
#if !defined(INT64_C) && !defined(UINT64_C)
#define INT64_C(x) S64_C(x)
#define UINT64_C(x) U64_C(x)
#endif
/**@}*/
/**\name C standard macros */
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *) 0)
#endif
#endif
/********************************************************/
#ifndef BME280_FLOAT_ENABLE
/*
Integer is used by default, uncommenting this means using floating-point type
*/
#define BME280_FLOAT_ENABLE
#endif
#ifndef BME280_FLOAT_ENABLE
#ifndef BME280_64BIT_ENABLE
/*
3264
Using a 32-bit system, uncommenting this means using a 64-bit system
*/
/*#define BME280_64BIT_ENABLE*/
#endif
#endif
#ifndef TRUE
#define TRUE UINT8_C(1)
#endif
#ifndef FALSE
#define FALSE UINT8_C(0)
#endif
/**\name I2C addresses */
#define BME280_I2C_ADDR_PRIM UINT8_C(0x76)
#define BME280_I2C_ADDR_SEC UINT8_C(0x77)
/**\name BME280 chip identifier */
#define BME280_CHIP_ID UINT8_C(0x60)
/**\name Register Address */
#define BME280_CHIP_ID_ADDR UINT8_C(0xD0)
#define BME280_RESET_ADDR UINT8_C(0xE0)
#define BME280_TEMP_PRESS_CALIB_DATA_ADDR UINT8_C(0x88)
#define BME280_HUMIDITY_CALIB_DATA_ADDR UINT8_C(0xE1)
#define BME280_PWR_CTRL_ADDR UINT8_C(0xF4)
#define BME280_CTRL_HUM_ADDR UINT8_C(0xF2)
#define BME280_CTRL_MEAS_ADDR UINT8_C(0xF4)
#define BME280_CONFIG_ADDR UINT8_C(0xF5)
#define BME280_DATA_ADDR UINT8_C(0xF7)
/**\name API success code */
#define BME280_OK INT8_C(0)
/**\name API error codes */
#define BME280_E_NULL_PTR INT8_C(-1)
#define BME280_E_DEV_NOT_FOUND INT8_C(-2)
#define BME280_E_INVALID_LEN INT8_C(-3)
#define BME280_E_COMM_FAIL INT8_C(-4)
#define BME280_E_SLEEP_MODE_FAIL INT8_C(-5)
/**\name API warning codes */
#define BME280_W_INVALID_OSR_MACRO INT8_C(1)
/**\name Macros related to size */
#define BME280_TEMP_PRESS_CALIB_DATA_LEN UINT8_C(26)
#define BME280_HUMIDITY_CALIB_DATA_LEN UINT8_C(7)
#define BME280_P_T_H_DATA_LEN UINT8_C(8)
/**\name Sensor power modes */
#define BME280_SLEEP_MODE UINT8_C(0x00)
#define BME280_FORCED_MODE UINT8_C(0x01)
#define BME280_NORMAL_MODE UINT8_C(0x03)
/**\name Macro to combine two 8 bit data's to form a 16 bit data */
#define BME280_CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb)
#define BME280_SET_BITS(reg_data, bitname, data) \
((reg_data & ~(bitname##_MSK)) | \
((data << bitname##_POS) & bitname##_MSK))
#define BME280_SET_BITS_POS_0(reg_data, bitname, data) \
((reg_data & ~(bitname##_MSK)) | \
(data & bitname##_MSK))
#define BME280_GET_BITS(reg_data, bitname) ((reg_data & (bitname##_MSK)) >> \
(bitname##_POS))
#define BME280_GET_BITS_POS_0(reg_data, bitname) (reg_data & (bitname##_MSK))
/**\name Macros for bit masking */
#define BME280_SENSOR_MODE_MSK UINT8_C(0x03)
#define BME280_SENSOR_MODE_POS UINT8_C(0x00)
#define BME280_CTRL_HUM_MSK UINT8_C(0x07)
#define BME280_CTRL_HUM_POS UINT8_C(0x00)
#define BME280_CTRL_PRESS_MSK UINT8_C(0x1C)
#define BME280_CTRL_PRESS_POS UINT8_C(0x02)
#define BME280_CTRL_TEMP_MSK UINT8_C(0xE0)
#define BME280_CTRL_TEMP_POS UINT8_C(0x05)
#define BME280_FILTER_MSK UINT8_C(0x1C)
#define BME280_FILTER_POS UINT8_C(0x02)
#define BME280_STANDBY_MSK UINT8_C(0xE0)
#define BME280_STANDBY_POS UINT8_C(0x05)
/**\name Sensor component selection macros
These values are internal for API implementation. Don't relate this to
data sheet.*/
#define BME280_PRESS UINT8_C(1)
#define BME280_TEMP UINT8_C(1 << 1)
#define BME280_HUM UINT8_C(1 << 2)
#define BME280_ALL UINT8_C(0x07)
/**\name Settings selection macros */
#define BME280_OSR_PRESS_SEL UINT8_C(1)
#define BME280_OSR_TEMP_SEL UINT8_C(1 << 1)
#define BME280_OSR_HUM_SEL UINT8_C(1 << 2)
#define BME280_FILTER_SEL UINT8_C(1 << 3)
#define BME280_STANDBY_SEL UINT8_C(1 << 4)
#define BME280_ALL_SETTINGS_SEL UINT8_C(0x1F)
/**\name Oversampling macros */
#define BME280_NO_OVERSAMPLING UINT8_C(0x00)
#define BME280_OVERSAMPLING_1X UINT8_C(0x01)
#define BME280_OVERSAMPLING_2X UINT8_C(0x02)
#define BME280_OVERSAMPLING_4X UINT8_C(0x03)
#define BME280_OVERSAMPLING_8X UINT8_C(0x04)
#define BME280_OVERSAMPLING_16X UINT8_C(0x05)
/**\name Standby duration selection macros */
#define BME280_STANDBY_TIME_1_MS (0x00)
#define BME280_STANDBY_TIME_62_5_MS (0x01)
#define BME280_STANDBY_TIME_125_MS (0x02)
#define BME280_STANDBY_TIME_250_MS (0x03)
#define BME280_STANDBY_TIME_500_MS (0x04)
#define BME280_STANDBY_TIME_1000_MS (0x05)
#define BME280_STANDBY_TIME_10_MS (0x06)
#define BME280_STANDBY_TIME_20_MS (0x07)
/**\name Filter coefficient selection macros */
#define BME280_FILTER_COEFF_OFF (0x00)
#define BME280_FILTER_COEFF_2 (0x01)
#define BME280_FILTER_COEFF_4 (0x02)
#define BME280_FILTER_COEFF_8 (0x03)
#define BME280_FILTER_COEFF_16 (0x04)
/*!
* @brief Interface selection Enums
*/
enum bme280_intf {
/*! SPI interface */
BME280_SPI_INTF,
/*! I2C interface */
BME280_I2C_INTF
};
/*!
* @brief Type definitions
*/
typedef int8_t (*bme280_com_fptr_t)(uint8_t dev_id, uint8_t reg_addr,
uint8_t *data, uint16_t len);
typedef void (*bme280_delay_fptr_t)(uint32_t period);
/*!
* @brief Calibration data
*/
struct bme280_calib_data {
/**
* @ Trim Variables
*/
/**@{*/
uint16_t dig_T1;
int16_t dig_T2;
int16_t dig_T3;
uint16_t dig_P1;
int16_t dig_P2;
int16_t dig_P3;
int16_t dig_P4;
int16_t dig_P5;
int16_t dig_P6;
int16_t dig_P7;
int16_t dig_P8;
int16_t dig_P9;
uint8_t dig_H1;
int16_t dig_H2;
uint8_t dig_H3;
int16_t dig_H4;
int16_t dig_H5;
int8_t dig_H6;
int32_t t_fine;
/**@}*/
};
/*!
* @brief bme280 sensor structure which comprises of temperature, pressure and
* humidity data
*/
#ifdef BME280_FLOAT_ENABLE
struct bme280_data {
/*! Compensated pressure */
double pressure;
/*! Compensated temperature */
double temperature;
/*! Compensated humidity */
double humidity;
};
#else
struct bme280_data {
/*! Compensated pressure */
uint32_t pressure;
/*! Compensated temperature */
int32_t temperature;
/*! Compensated humidity */
uint32_t humidity;
};
#endif /* BME280_USE_FLOATING_POINT */
/*!
* @brief bme280 sensor structure which comprises of uncompensated temperature,
* pressure and humidity data
*/
struct bme280_uncomp_data {
/*! un-compensated pressure */
uint32_t pressure;
/*! un-compensated temperature */
uint32_t temperature;
/*! un-compensated humidity */
uint32_t humidity;
};
/*!
* @brief bme280 sensor settings structure which comprises of mode,
* oversampling and filter settings.
*/
struct bme280_settings {
/*! pressure oversampling */
uint8_t osr_p;
/*! temperature oversampling */
uint8_t osr_t;
/*! humidity oversampling */
uint8_t osr_h;
/*! filter coefficient */
uint8_t filter;
/*! standby time */
uint8_t standby_time;
};
/*!
* @brief bme280 device structure
*/
struct bme280_dev {
/*! Chip Id */
uint8_t chip_id;
/*! Device Id */
uint8_t dev_id;
/*! SPI/I2C interface */
enum bme280_intf intf;
/*! Read function pointer */
bme280_com_fptr_t read;
/*! Write function pointer */
bme280_com_fptr_t write;
/*! Delay function pointer */
bme280_delay_fptr_t delay_ms;
/*! Trim data */
struct bme280_calib_data calib_data;
/*! Sensor settings */
struct bme280_settings settings;
};
#endif /* BME280_DEFS_H_ */
/** @}*/
/** @}*/

View File

@ -1,100 +0,0 @@
/**
*
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <json-c/json_object.h>
#include <json-c/json_tokener.h>
int8_t check_file_exists(const char *filename)
{
FILE *file;
file = fopen(filename, "r");
if (file != NULL)
{
fclose(file);
return 1;
}
return 0;
}
int main(int argc, char* argv[])
{
time_t rawtime = time(NULL);
struct tm *timeinfo;
char dateString[32];
char timeString[32];
char filename[100];
char jsonBuffer[1024];
//char jsonStrOutput[4096];
FILE *jsonFilePtr;
struct json_object *parsed_json;
struct json_object *latest_reading_object = json_object_new_object();
struct json_object *temperature_object = json_object_new_object();
//struct json_object *pressure_object = json_object_new_object();
timeinfo = localtime(&rawtime);
strftime(dateString, 32, "%F", timeinfo);
strftime(timeString, 32, "%F_%H-%M-%S", timeinfo);
snprintf(filename, sizeof(filename), "readings_%s.json", dateString);
if (!check_file_exists(filename))
{
printf("File for today does not exist! Creating...\n");
FILE *jsonFile = fopen(filename, "w");
fputs("{}", jsonFile);
fclose(jsonFile);
}
jsonFilePtr = fopen(filename, "r");
if (jsonFilePtr == NULL)
{
printf("Failed to open %s for reading...\n", filename);
exit(1);
}
fread(jsonBuffer, 1024, 1, jsonFilePtr);
fclose(jsonFilePtr);
printf("raw json read in: %s\n", jsonBuffer);
parsed_json = json_tokener_parse(jsonBuffer);
printf("json read in: %s\n", json_object_to_json_string_ext(parsed_json, JSON_C_TO_STRING_PRETTY));
double fahrenheit = (23 * 1.8) + 32;
double humidity = 100 - 80.82;
json_object_object_add(temperature_object, "fahrenheit", json_object_new_double(fahrenheit));
json_object_object_add(latest_reading_object, "temperature", temperature_object);
json_object_object_add(latest_reading_object, "humidity_percent", json_object_new_double(humidity));
json_object_object_add(parsed_json, timeString, latest_reading_object);
printf("json to write out: %s\n", json_object_to_json_string_ext(latest_reading_object, JSON_C_TO_STRING_PRETTY));
printf("new json to write out: %s\n", json_object_to_json_string_ext(parsed_json, JSON_C_TO_STRING_PRETTY));
//uint32_t jsonStrSize = sizeof(json_object_to_json_string_ext(parsed_json, JSON_C_TO_STRING_PRETTY));
//printf("jsonStrSize %i\n", jsonStrSize);
//char jsonStrOutput[] = json_object_to_json_string_ext(parsed_json, JSON_C_TO_STRING_PRETTY);
jsonFilePtr = fopen(filename, "w");
if (jsonFilePtr == NULL)
{
printf("Failed to open %s for writing...\n", filename);
exit(1);
}
fputs(json_object_to_json_string_ext(parsed_json, JSON_C_TO_STRING_PRETTY), jsonFilePtr);
//fwrite(json_object_to_json_string_ext(parsed_json), 1, jsonStrSize, jsonFilePtr);
fclose(jsonFilePtr);
return 0;
}

View File

@ -1,183 +0,0 @@
/**
*
*/
#include "bme280/bme280.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#define IIC_Dev "/dev/i2c-1"
int file_descriptor; // maybe file descriptor? example had it as 'fd'
int8_t check_file_exists(const char *filename)
{
FILE *file;
file = fopen(filename, "r");
if (file != NULL)
{
fclose(file);
return 1;
}
return 0;
}
void user_delay_ms(uint32_t period)
{
usleep(period*1000);
}
double celsius_to_fahrenheit(double celsius)
{
return (celsius * 1.8) + 32;
}
double pascals_to_mbar(double pascals)
{
return pascals / 100;
}
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
write(file_descriptor, &reg_addr, 1);
read(file_descriptor, data, len);
return 0;
}
int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
int8_t *buf;
buf = malloc(len +1);
buf[0] = reg_addr;
memcpy(buf +1, data, len);
write(file_descriptor, buf, len +1);
free(buf);
return 0;
}
void print_sensor_data(struct bme280_data *sensor_readings_data)
{
#ifdef BME280_FLOAT_ENABLE
double fahrenheit = celsius_to_fahrenheit(sensor_readings_data->temperature);
double celsius = sensor_readings_data->temperature;
double pascals = sensor_readings_data->pressure;
double millibars = pascals_to_mbar(sensor_readings_data->pressure);
double humidity = sensor_readings_data->humidity;
printf("%0.2f\xc2\xb0\x46 / %0.2f\xc2\xb0\x43, %0.2fPa, %0.2fmbar, %0.2f%% \r\n", fahrenheit, celsius, pascals, millibars, humidity);
#else
uint32_t fahrenheit = celsius_to_fahrenheit(sensor_readings_data->temperature);
uint32_t celsius = sensor_readings_data->temperature;
uint32_t pascals = sensor_readings_data->pressure;
uint32_t millibars = pascals_to_mbar(sensor_readings_data->pressure);
uint32_t humidity = sensor_readings_data->humidity;
printf("%ld\xc2\xb0\x46 / %ld\xc2\xb0\x43, %ldPa, %ldmbar, %ld%% \r\n", fahrenheit, celsius, pascals, millibars, humidity);
#endif
}
int8_t save_data_to_csv_file(struct bme280_data *sensor_readings_data)
{
time_t rawtime = time(NULL);
struct tm *timeinfo;
char dateString[32];
char datetimeString[32];
char filename[100];
FILE *csvFilePtr;
double celsius = sensor_readings_data->temperature;
double fahrenheit = celsius_to_fahrenheit(sensor_readings_data->temperature);
double pascals = sensor_readings_data->pressure;
double millibars = pascals_to_mbar(sensor_readings_data->pressure);
double humidity = sensor_readings_data->humidity;
timeinfo = localtime(&rawtime);
strftime(dateString, 32, "%F", timeinfo);
strftime(datetimeString, 32, "%F %H:%M:%S", timeinfo);
snprintf(filename, sizeof(filename), "/home/pi/readings/readings_%s.csv", dateString);
if (!check_file_exists(filename))
{
printf("File for today does not exist! Creating... \n");
FILE *csvFile = fopen(filename, "w");
fputs("Date,Celsius,Fahrenheit,Pascals,Millibars,Humidity Percent\n", csvFile);
fclose(csvFile);
}
csvFilePtr = fopen(filename, "a");
if (csvFilePtr == NULL)
{
printf("Failed to open %s for writing...\n", filename);
exit(1);
}
fprintf(csvFilePtr, "%s,%0.5f,%0.5f,%0.5f,%0.5f,%0.5f\n", datetimeString, celsius, fahrenheit, pascals, millibars, humidity);
fclose(csvFilePtr);
return 0;
}
int8_t read_sensor_data_normal_mode(struct bme280_dev *dev)
{
int8_t readings_result;
uint8_t settings_sel;
struct bme280_data sensor_readings_data;
/* Recommended mode of operation: Indoor navigation */
dev->settings.osr_h = BME280_OVERSAMPLING_1X;
dev->settings.osr_p = BME280_OVERSAMPLING_16X;
dev->settings.osr_t = BME280_OVERSAMPLING_2X;
dev->settings.filter = BME280_FILTER_COEFF_16;
dev->settings.standby_time = BME280_STANDBY_TIME_62_5_MS;
settings_sel = BME280_OSR_PRESS_SEL;
settings_sel |= BME280_OSR_TEMP_SEL;
settings_sel |= BME280_OSR_HUM_SEL;
settings_sel |= BME280_STANDBY_SEL;
settings_sel |= BME280_FILTER_SEL;
readings_result = bme280_set_sensor_settings(settings_sel, dev);
readings_result = bme280_set_sensor_mode(BME280_NORMAL_MODE, dev);
readings_result = bme280_get_sensor_data(BME280_ALL, &sensor_readings_data, dev);
//print_sensor_data(&sensor_readings_data);
save_data_to_csv_file(&sensor_readings_data);
return readings_result;
}
int main(int argc, char* argv[])
{
struct bme280_dev dev;
int8_t init_result = BME280_OK;
if ((file_descriptor = open(IIC_Dev, O_RDWR)) < 0) {
printf("Failed to open the i2c bus %s", argv[1]);
exit(1);
}
if (ioctl(file_descriptor, I2C_SLAVE, 0x76) < 0) {
printf("Failed to acquire bus access and/or talk to slave.\n");
exit(1);
}
dev.dev_id = BME280_I2C_ADDR_PRIM; //0x76
//dev.dev_id = BME280_I2C_ADDR_SEC; //0x77
dev.intf = BME280_I2C_INTF;
dev.read = user_i2c_read;
dev.write = user_i2c_write;
dev.delay_ms = user_delay_ms;
init_result = bme280_init(&dev);
printf("\n\nBME280 Init Result is: %d\n\n", init_result);
read_sensor_data_normal_mode(&dev);
printf("Recorded readings.\n\n");
return 0;
}