Skip to content

Commit 3a2c276

Browse files
committed
Corr. of function converting hex digit to binary
1 parent d43bbf5 commit 3a2c276

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

examples/7_distance_and_light_sensor/7_distance_and_light_sensor.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ uint32_t messageRate = 5000; // stores the current data message rate in Millis
1212
unsigned long lastMessageTime = 0; // stores the time when last data message was sent
1313

1414
VL6180x sensor(VL6180X_ADDRESS);
15-
char ambient_light[20]; // ambient light level [lx]
1615
u_int8_t distance; // distance from obstruction [mm]
16+
float ambient_light; // ambient light [lx]
1717

1818
/******************************************************************************
1919
USER PROGRAM
@@ -27,7 +27,7 @@ void setup() {
2727
Serial.print(SW_REVISION);
2828
Serial.println(" ***");
2929
lo.setSecurity(TLS);
30-
lo.begin(MQTT, TEXT, true);
30+
lo.begin(MQTT, BINARY, true);
3131
lo.connect(); // connects to the network + Live Objects
3232
}
3333

@@ -37,10 +37,10 @@ void loop() {
3737
Serial.println("Sampling data");
3838

3939
distance = sensor.getDistance();
40-
lo.addToPayload("distance", distance); // adding 'distance' value to the current payload
41-
sprintf(ambient_light, "%.2f", sensor.getAmbientLight(GAIN_1));
42-
lo.addToPayload("ambient_light", ambient_light); // adding 'ambient_light' value to the current payload
43-
40+
lo.addToPayload(distance); // adding 'distance' value to the current payload
41+
ambient_light = (static_cast<int>(sensor.getAmbientLight(GAIN_1) * 100.0)) / 100.0;
42+
lo.addToPayload(ambient_light);
43+
4444
Serial.println("Sending data to Live Objects");
4545
lo.sendData(); // send the data to Live Objects
4646
lastMessageTime = millis();

src/Utils.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,18 @@ String ToHex(String x)
4646

4747
uint8_t hexBinary(char msb, char lsb)
4848
{
49+
auto capitalizeChar = [](char x)
50+
{
51+
if((x >= 'a') && (x <= 'f')) return (x - ' '); // eg. 'a'(0x61) - ' ' (0x20) -> 'A'(0x41)
52+
else return x - static_cast<char>(0);
53+
};
4954
auto hexToDec = [](char x)
5055
{
51-
if(x>='a' && x<='f') return 10 + (x-'a');
52-
else return x-'0';
56+
if((x >= 'A') && (x <= 'F')) return 10 + (x - 'A'); // only capital hex digits
57+
else return x - '0';
5358
};
5459

55-
return (hexToDec(msb)<<4)|hexToDec(lsb);
60+
return (hexToDec(capitalizeChar(msb)) << 4) | hexToDec(capitalizeChar(lsb));
5661
}
5762

5863
String to7bit(String inputString)

0 commit comments

Comments
 (0)