Workaround für HM3301 to Get Valid Data After Sleeping
This commit is contained in:
parent
3979ecd988
commit
efd81746a8
1 changed files with 38 additions and 23 deletions
|
@ -46,8 +46,11 @@ void HM330x::initialize(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for Sensor to get Ready
|
// Wait for Sensor to get Ready
|
||||||
|
if (sleep_pin == 0) {
|
||||||
DEBUG_PRINTLN("HM330x::initialize Waiting for Sensor Startup");
|
DEBUG_PRINTLN("HM330x::initialize Waiting for Sensor Startup");
|
||||||
delay(30000);
|
delay(30000);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
DEBUG_PRINTLN("HM330x::initialize Trying to send Select");
|
DEBUG_PRINTLN("HM330x::initialize Trying to send Select");
|
||||||
// Send select command
|
// Send select command
|
||||||
|
@ -65,46 +68,58 @@ void HM330x::initialize(void) {
|
||||||
// Read Data From Sensor and Put Them Into the Payload Array
|
// Read Data From Sensor and Put Them Into the Payload Array
|
||||||
uint8_t HM330x::getSensorData(char *payload, uint8_t startbyte) {
|
uint8_t HM330x::getSensorData(char *payload, uint8_t startbyte) {
|
||||||
uint8_t data[HM330x_DATA_LEN] = {0};
|
uint8_t data[HM330x_DATA_LEN] = {0};
|
||||||
uint16_t value = 0;
|
uint16_t values[3] = { 0xEEEE, 0xEEEE, 0xEEEE };
|
||||||
|
|
||||||
DEBUG_PRINTLN("HM330x::getSensorData");
|
DEBUG_PRINTLN("HM330x::getSensorData");
|
||||||
|
|
||||||
// Enable Sensor and Wait for it to Settle
|
// Enable Sensor and Wait for it to Settle
|
||||||
if (sleep_pin > 0) {
|
if (sleep_pin > 0) {
|
||||||
digitalWrite(sleep_pin, HIGH);
|
digitalWrite(sleep_pin, HIGH);
|
||||||
|
DEBUG_PRINTLN("HM330x::getSensorData Waiting for Stable Values after Sleep");
|
||||||
delay(30000);
|
delay(30000);
|
||||||
|
sendCmd(HM330x_SELECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize Payload with 0s
|
// Initialize Payload with 0s
|
||||||
for (uint8_t i=startbyte; i < startbyte+6; i++)
|
for (uint8_t i=startbyte; i < startbyte+6; i++)
|
||||||
payload[i] = 0xFF;
|
payload[i] = 0xFF;
|
||||||
|
|
||||||
|
bool dataok = false;
|
||||||
|
uint8_t tries = 5;
|
||||||
|
|
||||||
|
while (!dataok && tries > 0) {
|
||||||
|
DEBUG_PRINT("HM330x::getSensorData reading I2C Try ");
|
||||||
|
DEBUG_PRINTLN(6-tries);
|
||||||
// Get Data from the Sensors
|
// Get Data from the Sensors
|
||||||
Wire.requestFrom(HM330x_ADDR, HM330x_DATA_LEN);
|
Wire.requestFrom(HM330x_ADDR, HM330x_DATA_LEN);
|
||||||
if (Wire.available()) {
|
if (Wire.available()) {
|
||||||
DEBUG_PRINT("HM330x::getSensorData reading I2C:")
|
DEBUG_PRINT("HM330x::getSensorData I2C data: 0x")
|
||||||
for (uint8_t i = 0; i<HM330x_DATA_LEN; i++){
|
for (uint8_t i = 0; i<HM330x_DATA_LEN; i++){
|
||||||
data[i] = Wire.read();
|
data[i] = Wire.read();
|
||||||
DEBUG_PRINT(" 0x");
|
|
||||||
DEBUG_PRINT(data[i], HEX);
|
DEBUG_PRINT(data[i], HEX);
|
||||||
|
DEBUG_PRINT(" ");
|
||||||
}
|
}
|
||||||
DEBUG_PRINTLN("");
|
DEBUG_PRINTLN("");
|
||||||
|
|
||||||
if (calcSum(data) == data[HM330x_DATA_LEN-1]) {
|
if (calcSum(data) == data[HM330x_DATA_LEN-1]) {
|
||||||
for (uint8_t pos = 5; pos<8; pos++) {
|
for (uint8_t pos = 5; pos<8; pos++) {
|
||||||
value = bytesToUint16(data, pos);
|
values[pos-5]= bytesToUint16(data, pos);
|
||||||
uint16ToPayload(value, payload, startbyte);
|
|
||||||
startbyte += 2;
|
|
||||||
}
|
}
|
||||||
if (sleep_pin > 0) {
|
dataok = true;
|
||||||
digitalWrite(sleep_pin, LOW);
|
|
||||||
}
|
|
||||||
return startbyte;
|
|
||||||
} else {
|
} else {
|
||||||
for (uint8_t i=startbyte; i < startbyte+6; i++)
|
DEBUG_PRINTLN("HM330x::getSensorData Checksum Error")
|
||||||
payload[i] = 0xEE;
|
delay(2000);
|
||||||
|
tries--;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
DEBUG_PRINTLN("HM330x::getSensorData I2C Not Ready")
|
||||||
|
delay(2000);
|
||||||
|
tries--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
uint16ToPayload(values[0], payload, startbyte);
|
||||||
|
uint16ToPayload(values[1], payload, startbyte+2);
|
||||||
|
uint16ToPayload(values[2], payload, startbyte+4);
|
||||||
if (sleep_pin > 0) {
|
if (sleep_pin > 0) {
|
||||||
digitalWrite(sleep_pin, LOW);
|
digitalWrite(sleep_pin, LOW);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue