From 8223661c6cae063d413c5f634702b2041104619e Mon Sep 17 00:00:00 2001 From: jstjst Date: Sun, 17 Jun 2018 09:27:41 +0200 Subject: [PATCH 1/4] add support for two BME280 sensors --- tracker/bme280.c | 82 +++++++++++++++++++++++++++++++----------------- tracker/bmp085.c | 33 ++++++++++--------- tracker/gps.h | 6 ++-- tracker/misc.c | 8 ++--- 4 files changed, 76 insertions(+), 53 deletions(-) diff --git a/tracker/bme280.c b/tracker/bme280.c index 011e9ce..b181f5c 100755 --- a/tracker/bme280.c +++ b/tracker/bme280.c @@ -85,9 +85,8 @@ struct TBME void bme280Calibration(struct TBME *bme); int bme280ReadInt(struct TBME *bme, unsigned char address); // double bme280ReadUP(short fd); -int BMEAddress; - -#define BME280_ADDRESS 0x76 // Possible I2C address of BME280 pressure sensor (could also be on ox77) //maybe needs to be changed +int BMEAddress1; +int BMEAddress2; int BMEPresent(struct TBME *bme, int Address) @@ -401,54 +400,79 @@ int bme280ReadInt(struct TBME *bme, unsigned char address) void *BME280Loop(void *some_void_ptr) { - struct TBME bme; + struct TBME bme1; + struct TBME bme2; struct TGPS *GPS; GPS = (struct TGPS *)some_void_ptr; - if (BMEPresent(&bme, BME280_ADDRESS)) - { - BMEAddress = BME280_ADDRESS; - } - else if (BMEPresent(&bme, BME280_ADDRESS+1)) - { - BMEAddress = BME280_ADDRESS+1; - } + if (BMEPresent(&bme1, 0x76)) + {BMEAddress1 = 0x76;} else - { - BMEAddress = 0; - } + {BMEAddress1 = 0;} - if (BMEAddress) - { - printf("BME280 Found At Address %02xh\n", BMEAddress); - } + if (BMEPresent(&bme2, 0x77)) + {BMEAddress2 = 0x77;} + else + {BMEAddress2 = 0;} + + if (BMEAddress1) + {printf("BME280 1 Found At Address %02xh\n", BMEAddress1);} else + {printf("BME280 1 Not Found (nothing at addresses 76h)\n");} + + if (BMEAddress2) + {printf("BME280 2 Found At Address %02xh\n", BMEAddress2);} + else + {printf("BME280 2 Not Found (nothing at addresses 77h)\n");} + + while (BMEAddress1) { - printf("BME280 Not Found (nothing at addresses 76/77h)\n"); + if ((bme1.fd = open_i2c(BMEAddress1)) >= 0) + { + bme280StartMeasurement(&bme1); + + sleep(1); // Wait (ample time) for measurement + + bme280ReadDataRegisters(&bme1); + + bme280GetRawValues(&bme1); + + GPS->BMP180Temperature[0] = bme280Temperature(&bme1); + GPS->Pressure[0] = bme280Pressure(&bme1); + GPS->Humidity[0] = bme280Humidity(&bme1); + + // printf("Temperature is %5.2lf\n", GPS->BMP180Temperature); + // printf("Pressure is %5.2lf\n", GPS->Pressure); + // printf("Humidity is %5.2lf\n", GPS->Humidity); + + close(bme1.fd); + } + + sleep(10); } - while (BMEAddress) + while (BMEAddress2) { - if ((bme.fd = open_i2c(BMEAddress)) >= 0) + if ((bme2.fd = open_i2c(BMEAddress2)) >= 0) { - bme280StartMeasurement(&bme); + bme280StartMeasurement(&bme2); sleep(1); // Wait (ample time) for measurement - bme280ReadDataRegisters(&bme); + bme280ReadDataRegisters(&bme2); - bme280GetRawValues(&bme); + bme280GetRawValues(&bme2); - GPS->BMP180Temperature = bme280Temperature(&bme); - GPS->Pressure = bme280Pressure(&bme); - GPS->Humidity = bme280Humidity(&bme); + GPS->BMP180Temperature[1] = bme280Temperature(&bme2); + GPS->Pressure[1] = bme280Pressure(&bme2); + GPS->Humidity[1] = bme280Humidity(&bme2); // printf("Temperature is %5.2lf\n", GPS->BMP180Temperature); // printf("Pressure is %5.2lf\n", GPS->Pressure); // printf("Humidity is %5.2lf\n", GPS->Humidity); - close(bme.fd); + close(bme2.fd); } sleep(10); diff --git a/tracker/bmp085.c b/tracker/bmp085.c index bbd7e86..c2ffb0d 100755 --- a/tracker/bmp085.c +++ b/tracker/bmp085.c @@ -19,12 +19,12 @@ struct TBMP { short fd; short ac1; - short ac2; - short ac3; + short ac2; + short ac3; unsigned short ac4; unsigned short ac5; unsigned short ac6; - short B1; + short B1; short B2; short Mb; short Mc; @@ -52,10 +52,10 @@ void *BMP085Loop(void *some_void_ptr) if ((bmp.fd = open_i2c(BMP085_ADDRESS)) >= 0) { int NoBMP; - + NoBMP = bmp085Calibration(&bmp); close(bmp.fd); - + if (NoBMP) { return 0; @@ -65,13 +65,13 @@ void *BMP085Loop(void *some_void_ptr) { return 0; } - + while (1) { if ((bmp.fd = open_i2c(BMP085_ADDRESS)) >= 0) { - GPS->BMP180Temperature = bmp085GetTemperature(&bmp); - GPS->Pressure = bmp085GetPressure(&bmp, GPS->BMP180Temperature); + GPS->BMP180Temperature[0] = bmp085GetTemperature(&bmp); + GPS->Pressure[0] = bmp085GetPressure(&bmp, GPS->BMP180Temperature[0]); // printf("Temperature is %5.2lf\n", GPS->BMP180Temperature); // printf("Pressure is %5.2lf\n", GPS->Pressure); @@ -91,7 +91,7 @@ int bmp085Calibration(struct TBMP *bmp) { return 1; } - + bmp->ac1 = bmp085ReadInt(bmp->fd, 0xAA); bmp->ac2 = bmp085ReadInt(bmp->fd, 0xAC); bmp->ac3 = bmp085ReadInt(bmp->fd, 0xAE); @@ -103,7 +103,7 @@ int bmp085Calibration(struct TBMP *bmp) bmp->Mb = bmp085ReadInt(bmp->fd, 0xBA); bmp->Mc = bmp085ReadInt(bmp->fd, 0xBC); bmp->Md = bmp085ReadInt(bmp->fd, 0xBE); - + return 0; // printf ("Values are %d %d %d %u %u %u %d %d %d %d %d\n", ac1, ac2, ac3, ac4, ac5, ac6, B1, B2, Mb, Mc, Md); @@ -171,11 +171,11 @@ int bmp085ReadInt(short fd, unsigned char address) buf[0] = address; - if ((write(fd, buf, 1)) != 1) { // Send register we want to read from + if ((write(fd, buf, 1)) != 1) { // Send register we want to read from printf("Error writing to i2c slave\n"); return -1; } - + if (read(fd, buf, 2) != 2) { // Read back data into buf[] printf("Unable to read from slave\n"); return -1; @@ -189,7 +189,7 @@ unsigned short bmp085ReadUT(short fd) { unsigned short ut; unsigned char buf[10]; - + // Write 0x2E into Register 0xF4 // This requests a temperature reading @@ -203,7 +203,7 @@ unsigned short bmp085ReadUT(short fd) } usleep(5000); - + ut = bmp085ReadInt(fd, 0xF6); // printf("ut = %u\n", ut); @@ -217,7 +217,7 @@ double bmp085ReadUP(short fd) unsigned char msb, lsb, xlsb; double up; unsigned char buf[10]; - + // Write 0x34 into register 0xF4 // Request a pressure reading w/ oversampling setting @@ -239,7 +239,7 @@ double bmp085ReadUP(short fd) printf("Error writing to i2c slave\n"); return 0; } - + if (read(fd, buf, 3) != 3) { printf("Unable to read from slave\n"); @@ -257,4 +257,3 @@ double bmp085ReadUP(short fd) return up; } - diff --git a/tracker/gps.h b/tracker/gps.h index 6b20a3a..fa13c04 100755 --- a/tracker/gps.h +++ b/tracker/gps.h @@ -21,9 +21,9 @@ struct TGPS // Sensors float DS18B20Temperature[10]; float BatteryVoltage; - float BMP180Temperature; - float Humidity; - float Pressure; + float BMP180Temperature[2]; + float Humidity[2]; + float Pressure[2]; float BoardCurrent; int DS18B20Count; diff --git a/tracker/misc.c b/tracker/misc.c index 7c77993..3fea83a 100755 --- a/tracker/misc.c +++ b/tracker/misc.c @@ -759,7 +759,7 @@ int BuildSentence(unsigned char *TxLine, int Channel, struct TGPS *GPS) int LoRaChannel; int ShowFields; int TSensor, TLength; - char TimeBuffer[12], ExtraFields1[20], ExtraFields2[20], ExtraFields3[40], ExtraFields4[64], ExtraFields5[32], ExtraFields6[32], *ExtraFields7; + char TimeBuffer[12], ExtraFields1[20], ExtraFields2[40], ExtraFields3[40], ExtraFields4[64], ExtraFields5[32], ExtraFields6[32], *ExtraFields7; if (FirstTime) { @@ -811,12 +811,12 @@ int BuildSentence(unsigned char *TxLine, int Channel, struct TGPS *GPS) // BMP Pressure/Temperature/Humidity, if available if (Config.EnableBME280) { - sprintf(ExtraFields2, ",%.1f,%.0f,%0.1f", GPS->BMP180Temperature, GPS->Pressure, GPS->Humidity); - if (ShowFields) printf(",BME.Temp,Pressure,Humidity"); + sprintf(ExtraFields2, ",%.1f,%.0f,%0.1f,%.1f,%.0f,%0.1f", GPS->BMP180Temperature[0], GPS->Pressure[0], GPS->Humidity[0], GPS->BMP180Temperature[1], GPS->Pressure[1], GPS->Humidity[1]); + if (ShowFields) printf(",BME.Temp1,Pressure1,Humidity1,BME.Temp2,Pressure2,Humidity2"); } else if (Config.EnableBMP085) { - sprintf(ExtraFields2, ",%.1f,%.0f", GPS->BMP180Temperature, GPS->Pressure); + sprintf(ExtraFields2, ",%.1f,%.0f", GPS->BMP180Temperature[0], GPS->Pressure[0]); if (ShowFields) printf(",BMP.Temp,Pressure"); } From 324df868c4004c14a26bb1a3f3c1df315c083b4e Mon Sep 17 00:00:00 2001 From: jstjst Date: Mon, 18 Jun 2018 12:07:34 +0200 Subject: [PATCH 2/4] debug output --- tracker/bme280.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tracker/bme280.c b/tracker/bme280.c index b181f5c..d7f8746 100755 --- a/tracker/bme280.c +++ b/tracker/bme280.c @@ -442,9 +442,9 @@ void *BME280Loop(void *some_void_ptr) GPS->Pressure[0] = bme280Pressure(&bme1); GPS->Humidity[0] = bme280Humidity(&bme1); - // printf("Temperature is %5.2lf\n", GPS->BMP180Temperature); - // printf("Pressure is %5.2lf\n", GPS->Pressure); - // printf("Humidity is %5.2lf\n", GPS->Humidity); + printf("Temperature 1 is %5.2lf\n", GPS->BMP180Temperature[0]); + printf("Pressure 1 is %5.2lf\n", GPS->Pressure[0]); + printf("Humidity 1 is %5.2lf\n", GPS->Humidity[0]); close(bme1.fd); } @@ -468,9 +468,9 @@ void *BME280Loop(void *some_void_ptr) GPS->Pressure[1] = bme280Pressure(&bme2); GPS->Humidity[1] = bme280Humidity(&bme2); - // printf("Temperature is %5.2lf\n", GPS->BMP180Temperature); - // printf("Pressure is %5.2lf\n", GPS->Pressure); - // printf("Humidity is %5.2lf\n", GPS->Humidity); + printf("Temperature 2 is %5.2lf\n", GPS->BMP180Temperature[1]); + printf("Pressure 2 is %5.2lf\n", GPS->Pressure[1]); + printf("Humidity 2 is %5.2lf\n", GPS->Humidity[1]); close(bme2.fd); } From 74858309775e66344ce370b01681f100a893e9cc Mon Sep 17 00:00:00 2001 From: jstjst Date: Mon, 18 Jun 2018 12:15:44 +0200 Subject: [PATCH 3/4] Fixed a thing --- tracker/bme280.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tracker/bme280.c b/tracker/bme280.c index d7f8746..bb78ac1 100755 --- a/tracker/bme280.c +++ b/tracker/bme280.c @@ -426,7 +426,7 @@ void *BME280Loop(void *some_void_ptr) else {printf("BME280 2 Not Found (nothing at addresses 77h)\n");} - while (BMEAddress1) + while (BMEAddress1 + BMEAddress2) { if ((bme1.fd = open_i2c(BMEAddress1)) >= 0) { @@ -449,11 +449,6 @@ void *BME280Loop(void *some_void_ptr) close(bme1.fd); } - sleep(10); - } - - while (BMEAddress2) - { if ((bme2.fd = open_i2c(BMEAddress2)) >= 0) { bme280StartMeasurement(&bme2); From 566da5b52925e7c348cfb52030bbc4477ec43056 Mon Sep 17 00:00:00 2001 From: jstjst Date: Mon, 18 Jun 2018 12:19:20 +0200 Subject: [PATCH 4/4] removed debug output --- tracker/bme280.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tracker/bme280.c b/tracker/bme280.c index bb78ac1..50bdaa3 100755 --- a/tracker/bme280.c +++ b/tracker/bme280.c @@ -442,9 +442,9 @@ void *BME280Loop(void *some_void_ptr) GPS->Pressure[0] = bme280Pressure(&bme1); GPS->Humidity[0] = bme280Humidity(&bme1); - printf("Temperature 1 is %5.2lf\n", GPS->BMP180Temperature[0]); - printf("Pressure 1 is %5.2lf\n", GPS->Pressure[0]); - printf("Humidity 1 is %5.2lf\n", GPS->Humidity[0]); + //printf("Temperature 1 is %5.2lf\n", GPS->BMP180Temperature[0]); + //printf("Pressure 1 is %5.2lf\n", GPS->Pressure[0]); + //printf("Humidity 1 is %5.2lf\n", GPS->Humidity[0]); close(bme1.fd); } @@ -463,9 +463,9 @@ void *BME280Loop(void *some_void_ptr) GPS->Pressure[1] = bme280Pressure(&bme2); GPS->Humidity[1] = bme280Humidity(&bme2); - printf("Temperature 2 is %5.2lf\n", GPS->BMP180Temperature[1]); - printf("Pressure 2 is %5.2lf\n", GPS->Pressure[1]); - printf("Humidity 2 is %5.2lf\n", GPS->Humidity[1]); + //printf("Temperature 2 is %5.2lf\n", GPS->BMP180Temperature[1]); + //printf("Pressure 2 is %5.2lf\n", GPS->Pressure[1]); + //printf("Humidity 2 is %5.2lf\n", GPS->Humidity[1]); close(bme2.fd); }