Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple function for measuring temperature #389

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,12 @@ byte b = LoRa.random();
```

Returns random byte.

### Temperature

Measures chip temperature in °C (blocking, ~150μs)
(uncalibrated accuracy ±10°C)

```
int temp = LoRa.temperature();
```
27 changes: 27 additions & 0 deletions src/LoRa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@
#define REG_DETECTION_THRESHOLD 0x37
#define REG_SYNC_WORD 0x39
#define REG_INVERTIQ2 0x3b
#define REG_IMAGECAL 0x3b
#define REG_TEMP 0x3c
#define REG_DIO_MAPPING_1 0x40
#define REG_VERSION 0x42
#define REG_PA_DAC 0x4d

// modes
#define MODE_LONG_RANGE_MODE 0x80
#define MODE_FSK_MODE 0x00

#define MODE_SLEEP 0x00
#define MODE_STDBY 0x01
#define MODE_TX 0x03
Expand Down Expand Up @@ -285,6 +289,29 @@ long LoRaClass::packetFrequencyError()
return static_cast<long>(fError);
}

int LoRaClass::temperature()
{
uint8_t opModeState = readRegister(REG_OP_MODE);
sleep(); //must be in sleep mode in order to switch between FSK and long range mode
writeRegister(REG_OP_MODE, MODE_FSK_MODE | MODE_SLEEP); //must be in FSK/OOK mode in order to access temp / imgage cal registers
writeRegister(REG_OP_MODE, MODE_FSK_MODE | MODE_RX_CONTINUOUS); //Temperature monitoring done in all modes except Sleep and Standby

uint8_t imgCalState = readRegister(REG_IMAGECAL);
writeRegister(REG_IMAGECAL,imgCalState &!0x01); //enable temperature reading
delayMicroseconds(140);
writeRegister(REG_IMAGECAL,imgCalState | 0x01); //stop temperature reading
uint8_t rawTemp = readRegister(REG_TEMP);

sleep(); //restore REG_OP_MODE trough sleep
writeRegister(REG_OP_MODE, opModeState);

if((rawTemp&0x80)==0x80){ //datasheet unclear, int8_t would make more sense but this was example code
return(0xff - rawTemp);
}
return(-rawTemp);

}

size_t LoRaClass::write(uint8_t byte)
{
return write(&byte, sizeof(byte));
Expand Down
2 changes: 2 additions & 0 deletions src/LoRa.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class LoRaClass : public Stream {
int packetRssi();
float packetSnr();
long packetFrequencyError();

int temperature();

// from Print
virtual size_t write(uint8_t byte);
Expand Down