Float values sent in binary format over the air

Move base radio id to MyConfig.h
This commit is contained in:
Henrik Ekblad
2014-08-13 09:39:45 +02:00
parent ca1c975cc5
commit 61122532ad
4 changed files with 26 additions and 11 deletions
+2
View File
@@ -8,6 +8,8 @@
#define RF24_DATARATE RF24_250KBPS //RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
#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
#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.
#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.
/***
* Enable/Disable debug logging
+17 -6
View File
@@ -80,6 +80,9 @@ char* MyMessage::getString(char *buffer) const {
case P_ULONG32:
ultoa(ulValue, buffer, 10);
break;
case P_FLOAT32:
dtostrf(fValue,2,fPrecision,buffer);
break;
case P_CUSTOM:
return getStream(buffer);
break;
@@ -105,8 +108,15 @@ bool MyMessage::getBool() const {
return getInt();
}
double MyMessage::getDouble() const {
return strtod(data, NULL);
float MyMessage::getFloat() const {
switch(miGetPayloadType()) {
case P_FLOAT32:
return fValue;
case P_STRING:
return atof(data);
default:
return 0;
}
}
long MyMessage::getLong() const {
@@ -195,10 +205,11 @@ MyMessage& MyMessage::set(uint8_t value) {
}
MyMessage& MyMessage::set(double value, uint8_t decimals) {
dtostrf(value,2,decimals,data);
miSetLength(strlen(data));
miSetPayloadType(P_STRING);
MyMessage& MyMessage::set(float value, uint8_t decimals) {
miSetLength(5); // 32 bit float + persi
miSetPayloadType(P_FLOAT32);
fValue=value;
fPrecision = decimals;
return *this;
}
+7 -3
View File
@@ -63,7 +63,7 @@ typedef enum {
} stream;
typedef enum {
P_STRING, P_BYTE, P_INT16, P_UINT16, P_LONG32, P_ULONG32, P_CUSTOM
P_STRING, P_BYTE, P_INT16, P_UINT16, P_LONG32, P_ULONG32, P_CUSTOM, P_FLOAT32
} payload;
@@ -139,7 +139,7 @@ public:
void* getCustom() const;
uint8_t getByte() const;
bool getBool() const;
double getDouble() const;
float getFloat() const;
long getLong() const;
unsigned long getULong() const;
int getInt() const;
@@ -157,7 +157,7 @@ public:
MyMessage& set(void* payload, uint8_t length);
MyMessage& set(const char* value);
MyMessage& set(uint8_t value);
MyMessage& set(double value, uint8_t decimals);
MyMessage& set(float value, uint8_t decimals);
MyMessage& set(unsigned long value);
MyMessage& set(long value);
MyMessage& set(unsigned int value);
@@ -192,6 +192,10 @@ struct
long lValue;
unsigned int uiValue;
int iValue;
struct {
float fValue;
uint8_t fPrecision; // Number of decimals when serializing
};
char data[MAX_PAYLOAD + 1];
} __attribute__((packed));
#ifdef __cplusplus
-2
View File
@@ -57,8 +57,6 @@
#define EEPROM_LOCAL_CONFIG_ADDRESS (EEPROM_FIRMWARE_CRC_ADDRESS+2) // First free address for sketch static configuration
// This is the nodeId for sensor net gateway receiver sketch (where all sensors should send their data).
// This is also act as base value for sensor nodeId
#define BASE_RADIO_ID ((uint64_t)0xA8A8E1FC00LL)
#define GATEWAY_ADDRESS ((uint8_t)0)
#define BROADCAST_ADDRESS ((uint8_t)0xFF)
#define TO_ADDR(x) (BASE_RADIO_ID + x)