commit 2ecf05c0245f49eb7903f325277a2da2471a2189 Author: Larry Bank Date: Fri Feb 13 21:27:38 2026 +0100 initial commit diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..4671124 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +CFLAGS= -D__LINUX__ -c -Wall -O2 +LIBS = -lm -lbb_scd41 -lpthread + +all: sensor2json + +sensor2json: main.o + g++ main.o $(LIBS) -o sensor2json + +main.o: main.cpp + g++ $(CFLAGS) main.cpp + +clean: + rm *.o sensor2json diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..2fcec1c --- /dev/null +++ b/main.cpp @@ -0,0 +1,113 @@ +// +// Write SCD4x sensor values into a JSON file +// written by Larry Bank Feb 13, 2026 +// +#include +#include +#include +#include +#include +#include +#include +#include + +SCD41 co2; + +void CreateJSON(const char *filename, int iCO2, int iTemp, int iHumidity) +{ +FILE *f; +char szTemp[256], szOut[1024]; +time_t tt; + + f = fopen(filename, "w"); + if (f) { + tt = time(NULL); // get the current system time + strcpy(szOut, "{\n \"data\": [\n {\n"); +// CO2 value + strcat(szOut, " \"make\": \"Sensirion\",\n \"model\": \"SCD41\",\n"); + strcat(szOut, " \"kind\": \"co2\",\n"); + sprintf(szTemp, " \"value\": %d,\n", iCO2); + strcat(szOut, szTemp); + strcat(szOut, " \"unit\": \"ppm\",\n"); + sprintf(szTemp, " \"updated_at\": %lu\n },\n {\n", (unsigned long)tt); + strcat(szOut, szTemp); +// Temperature value + strcat(szOut, " \"make\": \"Sensirion\",\n \"model\": \"SCD41\",\n"); + strcat(szOut, " \"kind\": \"temperature\",\n"); + sprintf(szTemp, " \"value\": %.2f,\n", ((float)iTemp)/10.0f); + strcat(szOut, szTemp); + strcat(szOut, " \"unit\": \"celcius\",\n"); + sprintf(szTemp, " \"updated_at\": %lu\n },\n {\n", (unsigned long)tt); + strcat(szOut, szTemp); +// Humidity value + strcat(szOut, " \"make\": \"Sensirion\",\n \"model\": \"SCD41\",\n"); + strcat(szOut, " \"kind\": \"humidity\",\n"); + sprintf(szTemp, " \"value\": %d,\n", iHumidity); + strcat(szOut, szTemp); + strcat(szOut, " \"unit\": \"percent\",\n"); + sprintf(szTemp, " \"updated_at\": %lu\n }\n ]\n}\n", (unsigned long)tt); + strcat(szOut, szTemp); + fwrite(szOut, 1, strlen(szOut), f); + fflush(f); + fclose(f); + } +} /* CreateJSON() */ + +int main(int argc, char *argv[]) +{ +int i, iBus; +DIR *pDir; +struct dirent *pDE; +uint32_t u32Buses = 0; // available I2C bus numbers (0-31) + + + printf("sensor2json example\n"); + printf("Finds which I2C bus has the supported sensor, then\ninitializes, configures, and loops updating the json with the latest sensor values.\n"); + if (argc != 2) { + printf("usage: sensor2json \n"); + return -1; + } + // I2C buses in Linux are defined as a file in the /dev directory + pDir = opendir("/dev"); + if (!pDir) { + printf("Error searching /dev directory; aborting.\n"); + return -1; + } + // Search all names in the /dev directory for those starting with i2c- + while ((pDE = readdir(pDir)) != NULL) { + if (memcmp(pDE->d_name, "i2c-", 4) == 0) { // found one! + iBus = atoi(&pDE->d_name[4]); + u32Buses |= (1 << iBus); // collect the bus numbers + } + } + closedir(pDir); + if (u32Buses == 0) { // Something went wrong; no I2C buses + printf("No I2C buses found!\n"); + printf("Check your system configuration (e.g. raspi-config)\n"); + printf("to ensure that I2C is enabled.\n"); + return -1; + } + // Search each I2C bus for a supported proximited sensor + for (iBus=0; iBus<32; iBus++) { + if (u32Buses & (1<