Compare commits

...

2 Commits

Author SHA1 Message Date
f4587886ac
adding stuff from pi 2024-12-19 09:44:35 -07:00
ceca171b3c
wiping everything to get exactly what is on the pi 2024-12-19 09:43:50 -07:00
11 changed files with 222 additions and 160 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

@ -3,20 +3,20 @@ DEBUG_DIR := build/Debug
BIN_DEBUG_DIR := bin/Debug
RELEASE_DIR := build/Release
BIN_RELEASE_DIR := bin/Release
CFLAGS += $(shell pkg-config --cflags json-c)
LDFLAGS += $(shell pkg-config --libs json-c)
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}
gcc -Wall -fPIC -pg -g -c src/bme280/bme280.c -o $(DEBUG_DIR)/bme280.o -std=${STANDARD} ${LDFLAGS}
gcc -Wall -fPIC -pg -g -c src/main.c -o $(DEBUG_DIR)/main.o -std=${STANDARD} ${LDFLAGS}
gcc -Wall -fPIC -pg -g -o $(BIN_DEBUG_DIR)/tempRecord $(DEBUG_DIR)/main.o $(DEBUG_DIR)/bme280.o -std=${STANDARD} ${LDFLAGS}
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}
gcc -Wall -fPIC -O2 -c src/bme280/bme280.c -o $(RELEASE_DIR)/bme280.o -std=${STANDARD} ${LDFLAGS}
gcc -Wall -fPIC -O2 -c src/main.c -o $(RELEASE_DIR)/main.o -std=${STANDARD} ${LDFLAGS}
gcc -Wall -fPIC -O2 -o $(BIN_RELEASE_DIR)/tempRecord $(RELEASE_DIR)/main.o $(RELEASE_DIR)/bme280.o -std=${STANDARD} ${LDFLAGS}
.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

BIN
bin/Debug/gmon.out Normal file

Binary file not shown.

BIN
bin/Debug/tempRecord Executable file

Binary file not shown.

View File

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

BIN
gmon.out Normal file

Binary file not shown.

1
readings_2024-02-26.json Normal file
View File

@ -0,0 +1 @@
{}{ "09_49_44": { "temperature": { "celsius": 23.462168825045229, "fahrenheit": 74.231903885081408 }, "pressure": { "pascals": 68096.059937644299, "millibars": 680.96059937644304 }, "humidity_percent": 87.599591275460014 } }{ "10_59_25": { "temperature": { "celsius": 23.462168825045229, "fahrenheit": 74.231903885081408 }, "pressure": { "pascals": 68096.059937644299, "millibars": 680.96059937644304 }, "humidity_percent": 87.599591275460014 } }

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

@ -13,6 +13,8 @@
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <json-c/json_object.h>
#include <json-c/json_tokener.h>
#define IIC_Dev "/dev/i2c-1"
@ -36,6 +38,7 @@ void user_delay_ms(uint32_t period)
usleep(period*1000);
}
#ifdef BME280_FLOAT_ENABLE
double celsius_to_fahrenheit(double celsius)
{
return (celsius * 1.8) + 32;
@ -45,6 +48,17 @@ double pascals_to_mbar(double pascals)
{
return pascals / 100;
}
#else
uint32_t celsius_to_fahrenheit(uint32_t celsius)
{
return (celsius * 1.8) + 32;
}
uint32_t pascals_to_mbar(uint32_t pascals)
{
return pascals / 100;
}
#endif
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
@ -83,44 +97,96 @@ void print_sensor_data(struct bme280_data *sensor_readings_data)
#endif
}
int8_t save_data_to_csv_file(struct bme280_data *sensor_readings_data)
int8_t save_data_to_json_file(struct bme280_data *sensor_readings_data)
{
time_t rawtime = time(NULL);
struct tm *timeinfo;
char dateString[32];
char datetimeString[32];
char timeString[32];
char filename[100];
FILE *csvFilePtr;
char jsonBuffer[1024];
FILE *jsonFilePtr;
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;
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(datetimeString, 32, "%F %H:%M:%S", timeinfo);
strftime(timeString, 32, "%H_%M_%S", timeinfo);
//puts(dateString);
//time(&rawtime);
snprintf(filename, sizeof(filename), "/home/pi/readings/readings_%s.csv", dateString);
snprintf(filename, sizeof(filename), "readings_%s.json", 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);
FILE *jsonFile = fopen(filename, "w");
fputs("{}", jsonFile);
fclose(jsonFile);
}
csvFilePtr = fopen(filename, "a");
if (csvFilePtr == NULL)
jsonFilePtr = fopen(filename, "r+");
if (jsonFilePtr == NULL)
{
printf("Failed to open %s for writing...\n", filename);
sprintf("Failed to open %s", 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);
fread(jsonBuffer, 1024, 1, 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(parsed_json));
#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;
json_object_object_add(temperature_object, "celsius", json_object_new_double(celsius));
json_object_object_add(temperature_object, "fahrenheit", json_object_new_double(fahrenheit));
json_object_object_add(pressure_object, "pascals", json_object_new_double(pascals));
json_object_object_add(pressure_object, "millibars", json_object_new_double(millibars));
json_object_object_add(latest_reading_object, "temperature", temperature_object);
json_object_object_add(latest_reading_object, "pressure", pressure_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);
//fprintf(jsonFilePtr, "{\"temperature\":{\"celsius\":%0.2f,\"fahrenheit\":%0.2f}}", celsius, fahrenheit);
#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;
json_object_object_add(temperature_object, "celsius", json_object_new_int(celsius));
json_object_object_add(temperature_object, "fahrenheit", json_object_new_int(fahrenheit));
json_object_object_add(pressure_object, "pascals", json_object_new_int(pascals));
json_object_object_add(pressure_object, "millibars", json_object_new_int(millibars));
json_object_object_add(latest_reading_object, "temperature", temperature_object);
json_object_object_add(latest_reading_object, "pressure", pressure_object);
json_object_object_add(latest_reading_object, "humidity_percent", json_object_new_int(humidity));
//json_object_object_add(parsed_json, "timeString", latest_reading_object);
//fprintf(jsonFilePtr, "{\"temperature\":{\"celsius\":%ld,\"fahrenheit\":%ld}}", celsius, fahrenheit);
#endif
json_object_object_add(parsed_json, timeString, latest_reading_object);
//fprintf(jsonFilePtr, json_object_to_json_string(parsed_json));
fputs(json_object_to_json_string(parsed_json), jsonFilePtr);
printf("json to write out: %s \n", json_object_to_json_string(latest_reading_object));
printf("new json to write out: %s \n", json_object_to_json_string(parsed_json));
fclose(jsonFilePtr);
return 0;
}
@ -145,8 +211,8 @@ int8_t read_sensor_data_normal_mode(struct bme280_dev *dev)
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);
print_sensor_data(&sensor_readings_data);
//save_data_to_json_file(&sensor_readings_data);
return readings_result;
}
@ -173,11 +239,9 @@ int main(int argc, char* argv[])
dev.delay_ms = user_delay_ms;
init_result = bme280_init(&dev);
printf("\n\nBME280 Init Result is: %d\n\n", init_result);
printf("\r\n BME280 Init Result is:%d \r\n", init_result);
read_sensor_data_normal_mode(&dev);
printf("Recorded readings.\n\n");
return 0;
}

125
src/second.c Normal file
View File

@ -0,0 +1,125 @@
/**
*
*/
#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'
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)
{
printf("print_sensor_data\n\n");
#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 read_sensor_data_normal_mode(struct bme280_dev *dev)
{
int8_t readings_result;
uint8_t settings_sel;
struct bme280_data sensor_readings_data;
printf("read_sensor_data_normal_mode\n\n");
/* 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;
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;
printf("starting up\n\n");
init_result = bme280_init(&dev);
//printf("got init_result\n");
//if (init_result != 0) {
// printf("\n\nBME280 Init Result is: %d\n\n", init_result);
// return 1;
//}
//printf("before read sensor data\n\n");
// read_sensor_data_normal_mode(&dev);
printf("Recorded readings.\n\n");
return 0;
}