Skip to content

Commit 6112253

Browse files
committed
Float values sent in binary format over the air
Move base radio id to MyConfig.h
1 parent ca1c975 commit 6112253

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

libraries/MySensors/MyConfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#define RF24_DATARATE RF24_250KBPS //RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
99
#define RF24_PA_LEVEL RF24_PA_MAX //Sensor PA Level == RF24_PA_MIN=-18dBm, RF24_PA_LOW=-12dBm, RF24_PA_HIGH=-6dBM, and RF24_PA_MAX=0dBm
1010
#define RF24_PA_LEVEL_GW RF24_PA_LEVEL //Gateway PA Level, defaults to Sensor net PA Level. Tune here if using an amplified nRF2401+ in your gateway.
11+
#define BASE_RADIO_ID ((uint64_t)0xA8A8E1FC00LL) // This is also act as base value for sensor nodeId addresses. Change this (or channel) if you have more than one sensor network.
12+
1113

1214
/***
1315
* Enable/Disable debug logging

libraries/MySensors/MyMessage.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ char* MyMessage::getString(char *buffer) const {
8080
case P_ULONG32:
8181
ultoa(ulValue, buffer, 10);
8282
break;
83+
case P_FLOAT32:
84+
dtostrf(fValue,2,fPrecision,buffer);
85+
break;
8386
case P_CUSTOM:
8487
return getStream(buffer);
8588
break;
@@ -105,8 +108,15 @@ bool MyMessage::getBool() const {
105108
return getInt();
106109
}
107110

108-
double MyMessage::getDouble() const {
109-
return strtod(data, NULL);
111+
float MyMessage::getFloat() const {
112+
switch(miGetPayloadType()) {
113+
case P_FLOAT32:
114+
return fValue;
115+
case P_STRING:
116+
return atof(data);
117+
default:
118+
return 0;
119+
}
110120
}
111121

112122
long MyMessage::getLong() const {
@@ -195,10 +205,11 @@ MyMessage& MyMessage::set(uint8_t value) {
195205
}
196206

197207

198-
MyMessage& MyMessage::set(double value, uint8_t decimals) {
199-
dtostrf(value,2,decimals,data);
200-
miSetLength(strlen(data));
201-
miSetPayloadType(P_STRING);
208+
MyMessage& MyMessage::set(float value, uint8_t decimals) {
209+
miSetLength(5); // 32 bit float + persi
210+
miSetPayloadType(P_FLOAT32);
211+
fValue=value;
212+
fPrecision = decimals;
202213
return *this;
203214
}
204215

libraries/MySensors/MyMessage.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ typedef enum {
6363
} stream;
6464

6565
typedef enum {
66-
P_STRING, P_BYTE, P_INT16, P_UINT16, P_LONG32, P_ULONG32, P_CUSTOM
66+
P_STRING, P_BYTE, P_INT16, P_UINT16, P_LONG32, P_ULONG32, P_CUSTOM, P_FLOAT32
6767
} payload;
6868

6969

@@ -139,7 +139,7 @@ class MyMessage
139139
void* getCustom() const;
140140
uint8_t getByte() const;
141141
bool getBool() const;
142-
double getDouble() const;
142+
float getFloat() const;
143143
long getLong() const;
144144
unsigned long getULong() const;
145145
int getInt() const;
@@ -157,7 +157,7 @@ class MyMessage
157157
MyMessage& set(void* payload, uint8_t length);
158158
MyMessage& set(const char* value);
159159
MyMessage& set(uint8_t value);
160-
MyMessage& set(double value, uint8_t decimals);
160+
MyMessage& set(float value, uint8_t decimals);
161161
MyMessage& set(unsigned long value);
162162
MyMessage& set(long value);
163163
MyMessage& set(unsigned int value);
@@ -192,6 +192,10 @@ struct
192192
long lValue;
193193
unsigned int uiValue;
194194
int iValue;
195+
struct {
196+
float fValue;
197+
uint8_t fPrecision; // Number of decimals when serializing
198+
};
195199
char data[MAX_PAYLOAD + 1];
196200
} __attribute__((packed));
197201
#ifdef __cplusplus

libraries/MySensors/MySensor.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@
5757
#define EEPROM_LOCAL_CONFIG_ADDRESS (EEPROM_FIRMWARE_CRC_ADDRESS+2) // First free address for sketch static configuration
5858

5959
// This is the nodeId for sensor net gateway receiver sketch (where all sensors should send their data).
60-
// This is also act as base value for sensor nodeId
61-
#define BASE_RADIO_ID ((uint64_t)0xA8A8E1FC00LL)
6260
#define GATEWAY_ADDRESS ((uint8_t)0)
6361
#define BROADCAST_ADDRESS ((uint8_t)0xFF)
6462
#define TO_ADDR(x) (BASE_RADIO_ID + x)

0 commit comments

Comments
 (0)