-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8638 from rjaakke/HP303B
HP303B Sensor support
- Loading branch information
Showing
12 changed files
with
2,593 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Arduino library for the HP303B | ||
### Installation | ||
- Clone this repository or download&unzip [zip file](https://github.com/wemos/LOLIN_HP303B_Library/archive/master.zip) into Arduino/libraries | ||
|
98 changes: 98 additions & 0 deletions
98
lib/LOLIN_HP303B/examples/i2c_background/i2c_background.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include <LOLIN_HP303B.h> | ||
|
||
// HP303B Opject | ||
LOLIN_HP303B HP303BPressureSensor = LOLIN_HP303B(); | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
while (!Serial); | ||
|
||
//Call begin to initialize HP303BPressureSensor | ||
//The parameter 0x76 is the bus address. The default address is 0x77 and does not need to be given. | ||
//HP303BPressureSensor.begin(Wire, 0x76); | ||
//Use the commented line below instead to use the default I2C address. | ||
HP303BPressureSensor.begin(); | ||
|
||
//temperature measure rate (value from 0 to 7) | ||
//2^temp_mr temperature measurement results per second | ||
int16_t temp_mr = 2; | ||
//temperature oversampling rate (value from 0 to 7) | ||
//2^temp_osr internal temperature measurements per result | ||
//A higher value increases precision | ||
int16_t temp_osr = 2; | ||
//pressure measure rate (value from 0 to 7) | ||
//2^prs_mr pressure measurement results per second | ||
int16_t prs_mr = 2; | ||
//pressure oversampling rate (value from 0 to 7) | ||
//2^prs_osr internal pressure measurements per result | ||
//A higher value increases precision | ||
int16_t prs_osr = 2; | ||
//startMeasureBothCont enables background mode | ||
//temperature and pressure ar measured automatically | ||
//High precision and hgh measure rates at the same time are not available. | ||
//Consult Datasheet (or trial and error) for more information | ||
int16_t ret = HP303BPressureSensor.startMeasureBothCont(temp_mr, temp_osr, prs_mr, prs_osr); | ||
//Use one of the commented lines below instead to measure only temperature or pressure | ||
//int16_t ret = HP303BPressureSensor.startMeasureTempCont(temp_mr, temp_osr); | ||
//int16_t ret = HP303BPressureSensor.startMeasurePressureCont(prs_mr, prs_osr); | ||
|
||
|
||
if (ret != 0) | ||
{ | ||
Serial.print("Init FAILED! ret = "); | ||
Serial.println(ret); | ||
} | ||
else | ||
{ | ||
Serial.println("Init complete!"); | ||
} | ||
} | ||
|
||
|
||
|
||
void loop() | ||
{ | ||
unsigned char pressureCount = 20; | ||
int32_t pressure[pressureCount]; | ||
unsigned char temperatureCount = 20; | ||
int32_t temperature[temperatureCount]; | ||
|
||
//This function writes the results of continuous measurements to the arrays given as parameters | ||
//The parameters temperatureCount and pressureCount should hold the sizes of the arrays temperature and pressure when the function is called | ||
//After the end of the function, temperatureCount and pressureCount hold the numbers of values written to the arrays | ||
//Note: The HP303B cannot save more than 32 results. When its result buffer is full, it won't save any new measurement results | ||
int16_t ret = HP303BPressureSensor.getContResults(temperature, temperatureCount, pressure, pressureCount); | ||
|
||
if (ret != 0) | ||
{ | ||
Serial.println(); | ||
Serial.println(); | ||
Serial.print("FAIL! ret = "); | ||
Serial.println(ret); | ||
} | ||
else | ||
{ | ||
Serial.println(); | ||
Serial.println(); | ||
Serial.print(temperatureCount); | ||
Serial.println(" temperature values found: "); | ||
for (int16_t i = 0; i < temperatureCount; i++) | ||
{ | ||
Serial.print(temperature[i]); | ||
Serial.println(" degrees of Celsius"); | ||
} | ||
|
||
Serial.println(); | ||
Serial.print(pressureCount); | ||
Serial.println(" pressure values found: "); | ||
for (int16_t i = 0; i < pressureCount; i++) | ||
{ | ||
Serial.print(pressure[i]); | ||
Serial.println(" Pascal"); | ||
} | ||
} | ||
|
||
//Wait some time, so that the HP303B can refill its buffer | ||
delay(10000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <LOLIN_HP303B.h> | ||
|
||
// HP303B Opject | ||
LOLIN_HP303B HP303BPressureSensor; | ||
|
||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
while (!Serial); | ||
|
||
|
||
//Call begin to initialize HP303BPressureSensor | ||
//The parameter 0x76 is the bus address. The default address is 0x77 and does not need to be given. | ||
//HP303BPressureSensor.begin(Wire, 0x76); | ||
//Use the commented line below instead of the one above to use the default I2C address. | ||
//if you are using the Pressure 3 click Board, you need 0x76 | ||
HP303BPressureSensor.begin(); | ||
|
||
Serial.println("Init complete!"); | ||
} | ||
|
||
|
||
|
||
void loop() | ||
{ | ||
int32_t temperature; | ||
int32_t pressure; | ||
int16_t oversampling = 7; | ||
int16_t ret; | ||
Serial.println(); | ||
|
||
//lets the HP303B perform a Single temperature measurement with the last (or standard) configuration | ||
//The result will be written to the paramerter temperature | ||
//ret = HP303BPressureSensor.measureTempOnce(temperature); | ||
//the commented line below does exactly the same as the one above, but you can also config the precision | ||
//oversampling can be a value from 0 to 7 | ||
//the HP303B will perform 2^oversampling internal temperature measurements and combine them to one result with higher precision | ||
//measurements with higher precision take more time, consult datasheet for more information | ||
ret = HP303BPressureSensor.measureTempOnce(temperature, oversampling); | ||
|
||
if (ret != 0) | ||
{ | ||
//Something went wrong. | ||
//Look at the library code for more information about return codes | ||
Serial.print("FAIL! ret = "); | ||
Serial.println(ret); | ||
} | ||
else | ||
{ | ||
Serial.print("Temperature: "); | ||
Serial.print(temperature); | ||
Serial.println(" degrees of Celsius"); | ||
} | ||
|
||
//Pressure measurement behaves like temperature measurement | ||
//ret = HP303BPressureSensor.measurePressureOnce(pressure); | ||
ret = HP303BPressureSensor.measurePressureOnce(pressure, oversampling); | ||
if (ret != 0) | ||
{ | ||
//Something went wrong. | ||
//Look at the library code for more information about return codes | ||
Serial.print("FAIL! ret = "); | ||
Serial.println(ret); | ||
} | ||
else | ||
{ | ||
Serial.print("Pressure: "); | ||
Serial.print(pressure); | ||
Serial.println(" Pascal"); | ||
} | ||
|
||
//Wait some time | ||
delay(500); | ||
} |
112 changes: 112 additions & 0 deletions
112
lib/LOLIN_HP303B/examples/i2c_interrupt/i2c_interrupt.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#include <LOLIN_HP303B.h> | ||
|
||
// HP303B Opject | ||
LOLIN_HP303B HP303BPressureSensor = LOLIN_HP303B(); | ||
|
||
void onFifoFull(); | ||
|
||
const unsigned char pressureLength = 50; | ||
unsigned char pressureCount = 0; | ||
int32_t pressure[pressureLength]; | ||
unsigned char temperatureCount = 0; | ||
const unsigned char temperatureLength = 50; | ||
int32_t temperature[temperatureLength]; | ||
|
||
|
||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
while (!Serial); | ||
|
||
//Call begin to initialize HP303BPressureSensor | ||
//The parameter 0x76 is the bus address. The default address is 0x77 and does not need to be given. | ||
//HP303BPressureSensor.begin(Wire, 0x76); | ||
//Use the commented line below instead to use the default I2C address. | ||
HP303BPressureSensor.begin(); | ||
|
||
int16_t ret = HP303BPressureSensor.setInterruptPolarity(1); | ||
ret = HP303BPressureSensor.setInterruptSources(1, 0, 0); | ||
//clear interrupt flag by reading | ||
HP303BPressureSensor.getIntStatusFifoFull(); | ||
|
||
//initialization of Interrupt for Controller unit | ||
//SDO pin of HP303B has to be connected with interrupt pin | ||
int16_t interruptPin = 3; | ||
pinMode(interruptPin, INPUT); | ||
attachInterrupt(digitalPinToInterrupt(interruptPin), onFifoFull, RISING); | ||
|
||
//start of a continuous measurement just like before | ||
int16_t temp_mr = 3; | ||
int16_t temp_osr = 2; | ||
int16_t prs_mr = 1; | ||
int16_t prs_osr = 3; | ||
ret = HP303BPressureSensor.startMeasureBothCont(temp_mr, temp_osr, prs_mr, prs_osr); | ||
if (ret != 0) | ||
{ | ||
Serial.print("Init FAILED! ret = "); | ||
Serial.println(ret); | ||
} | ||
else | ||
{ | ||
Serial.println("Init complete!"); | ||
} | ||
} | ||
|
||
|
||
void loop() | ||
{ | ||
//do other stuff | ||
Serial.println("loop running"); | ||
delay(500); | ||
|
||
|
||
//if result arrays are full | ||
//This could also be in the interrupt handler, but it would take too much time for a proper ISR | ||
if (pressureCount == pressureLength && temperatureCount == temperatureLength) | ||
{ | ||
//print results | ||
Serial.println(); | ||
Serial.println(); | ||
Serial.print(temperatureCount); | ||
Serial.println(" temperature values found: "); | ||
for (int16_t i = 0; i < temperatureCount; i++) | ||
{ | ||
Serial.print(temperature[i]); | ||
Serial.println(" degrees of Celsius"); | ||
} | ||
Serial.println(); | ||
Serial.print(pressureCount); | ||
Serial.println(" pressure values found: "); | ||
for (int16_t i = 0; i < pressureCount; i++) | ||
{ | ||
Serial.print(pressure[i]); | ||
Serial.println(" Pascal"); | ||
} | ||
Serial.println(); | ||
Serial.println(); | ||
//reset result counters | ||
pressureCount = 0; | ||
temperatureCount = 0; | ||
} | ||
} | ||
|
||
|
||
//interrupt handler | ||
void onFifoFull() | ||
{ | ||
//message for debugging | ||
Serial.println("Interrupt handler called"); | ||
|
||
//clear interrupt flag by reading | ||
HP303BPressureSensor.getIntStatusFifoFull(); | ||
|
||
//calculate the number of free indexes in the result arrays | ||
unsigned char prs_freespace = pressureLength - pressureCount; | ||
unsigned char temp_freespace = temperatureLength - temperatureCount; | ||
//read the results from HP303B, new results will be added at the end of the arrays | ||
HP303BPressureSensor.getContResults(&temperature[temperatureCount], temp_freespace, &pressure[pressureCount], prs_freespace); | ||
//after reading the result counters are increased by the amount of new results | ||
pressureCount += prs_freespace; | ||
temperatureCount += temp_freespace; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
####################################### | ||
# Syntax Coloring Map For DHT12 | ||
####################################### | ||
|
||
####################################### | ||
# Datatypes (KEYWORD1) | ||
####################################### | ||
|
||
DHT12 KEYWORD1 | ||
|
||
####################################### | ||
# Methods and Functions (KEYWORD2) | ||
####################################### | ||
|
||
get KEYWORD2 | ||
|
||
####################################### | ||
# Constants (LITERAL1) | ||
####################################### | ||
|
||
cTemp LITERAL1 | ||
fTemp LITERAL1 | ||
humidity LITERAL1 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name=LOLIN_HP303B | ||
version=1.0.0 | ||
author=WEMOS.CC <[email protected]> | ||
maintainer=WEMOS.CC | ||
sentence=Library for the <a href="https://www.wemos.cc">HP303B.</a>. | ||
paragraph=LOLIN HP303B | ||
category=Device Control | ||
url=https://github.com/wemos/LOLIN_HP303B_Library | ||
architectures=* |
Oops, something went wrong.