Data Gator
Hardware and software documentation for the Data Gator project.
Loading...
Searching...
No Matches
KKM_K6P.hpp
Go to the documentation of this file.
8#ifndef KKMK6P_H
9#define KKMK6P_H
10
11#include <Arduino.h>
12#include <sstream>
13#include <string.h>
14
15#include <../../include/BLESensor.hpp>
16#include <NimBLEAdvertisedDevice.h>
17#include <MQTTMailer.hpp>
18
20#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8))
22#define ENDIAN_CHANGE_U32(x) ((((x)&0xFF000000)>>24) + (((x)&0x00FF0000)>>8)) + ((((x)&0xFF00)<<8) + (((x)&0xFF)<<24))
23
24extern const bool USB_DEBUG;
43typedef struct ufloat88 {
44 int8_t upper = 0x00;
45 uint8_t lower = 0x00;
52 std::string toString(){
53 std::stringstream ss;
54
55 ss << (int)upper << "." << (int)lower;
56 return ss.str();
57 }
58
59} __attribute__((packed)) ufloat88;
60
64class KKMK6P: public BLESensor{
65public:
66 KKMK6P(){}
67
68 static KKMK6P& getInstance(){
69 static KKMK6P instance;
70 return instance;
71 }
72
73 bool advertisedDeviceIsK6P(NimBLEAdvertisedDevice*); // true if the data in this payload is from S1
74 MQTTMail* parseAdvertisedData(NimBLEAdvertisedDevice* dev); // get the data from a scanned device
75
76 float getTemp();
77 float getHumidity();
78 int getVoltage();
79 uint32_t timeUp();
80 BLEAddress getMAC();
81 std::string toJSON(double temp, double humidity); // export all known information to json string object
82 std::string getSensorType();
83
84
85private:
86 int serviceCount = 3; // how many services are stored in the list below, needed for iterating over the list in checks
87 NimBLEUUID s1_services[3] = {NimBLEUUID("0000ffe1-0000-1000-8000-00805f9b34fb"), // HT
88 NimBLEUUID("0000fff1-0000-1000-8000-00805f9b34fb"), // Eddystone URL
89 NimBLEUUID("0000feaa-0000-1000-8000-00805f9b34fb")}; // Eddystone TLM
90
91 struct { // HT data
92 uint8_t id = 0x00; // payload id
93 uint16_t unknown = 0x0000; // 2 bytes of useless data
94 uint16_t temp = 0x0000; // temperature in Celsius as a float
95 uint16_t humidity = 0x0000; // relative humidity as a float
96 uint8_t address[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // S1 MAC address
97
98 // convert to a string of information
99 std::string toString(){
100
101 std::stringstream ss;
102 ss << ((float)ENDIAN_CHANGE_U16(temp))/256 << " C, "
103 << ((float)ENDIAN_CHANGE_U16(humidity))/256 << "%RH";
104
105 return ss.str();
106
107 }
108
109 } __attribute__((packed)) HT_Frame;
110
111 struct { // INFO data
112 uint8_t id = 0x00; // payload id
113 uint16_t unknown = 0x0000; // 2 bytes of useless data
114 uint8_t address[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // S1 MAC address
115 char name[2] = {0x00, 0x00};
116
117 // convert to a string of information
118 std::string toString(){
119
120 auto size = 18;
121 char *res = (char*)malloc(size);
122 snprintf(res, size, "%02x:%02x:%02x:%02x:%02x:%02x", address[5], address[4], address[3], address[2], address[1], address[0]);
123 std::string ret(res);
124 free(res);
125 std::stringstream ss;
126 ss << "MAC: " << ret << ", "
127 << "Name: "<< name[0] << name[1];
128
129 return ss.str();
130 }
131
132 } __attribute__((packed)) INFO_Frame;
133
134 struct { // Eddystone Telemetry data
135 uint8_t frameType = 0x00; // eddystone frame specifier
136 uint8_t version = 0x00; // version...
137 uint8_t sensor_mask = 0x00; // sensor mask
138 uint16_t volt = 0x0000; // voltage of sensor battery in mV
139 ufloat88 temp; // temperature reading as float in Celsius
140 ufloat88 humidity; // temperature reading as float in Celsius
141 ufloat88 accX; // temperature reading as float in Celsius
142 ufloat88 accY; // temperature reading as float in Celsius
143 ufloat88 accZ; // temperature reading as float in Celsius
144
145 std::string toString(){
146
147 std::stringstream ss;
148 ss << "V" << (int)version << ".0, "
149 << volt << "mV, "
150 << temp.toString() << " C, "
151 << humidity.toString() << "%, "
152 << accX.toString() << "mg X, "
153 << accY.toString() << "mg Y, "
154 << accZ.toString() << "mg Z, ";
155
156 return ss.str();
157
158 }
159
160 } __attribute__((packed)) TLM_Frame;
161
162 struct { // iBeacon UUID, Major, & Minor data
163 uint8_t manufacturerID[6];
164
165 std::string toString(){
166 return "";
167 }
168
169 } __attribute__((packed)) iBeacon_Frame;
170
171};
172
173#endif
const bool USB_DEBUG
USB serial debugging enabled.
Definition main.cpp:62
std::string toString()
Convert struct members upper and lower to a string double value.
Definition KKM_K6P.hpp:8
#define ENDIAN_CHANGE_U16(x)
< Converts endianess for 16 bit integers
Definition KKM_K6P.hpp:20
Defines a bluetooth low energy (BLE) interface for integrating new sensors into the DG firmware.
Definition BLESensor.hpp:20
Singleton used to identify and parse BLE transmissions from KKMK6P BLE Temperature and Humidity senso...
Definition KKM_K6P.hpp:65
int getVoltage()
Gets the voltage.
Definition KKM_K6P.cpp:116
std::string toJSON(double temp, double humidity)
convert data values to JSON string object
Definition KKM_K6P.cpp:141
bool advertisedDeviceIsK6P(NimBLEAdvertisedDevice *)
Check if the advertised device is a KKMK6P sensor.
Definition KKM_K6P.cpp:11
float getHumidity()
Convert uint16_t from HT_Frame to a float relative humidity value.
Definition KKM_K6P.cpp:99
MQTTMail * parseAdvertisedData(NimBLEAdvertisedDevice *dev)
Retrieve data advertised by the device and save it if of interest.
Definition KKM_K6P.cpp:35
float getTemp()
Convert the uint16_t in HT_Frame to a float temperature value in Celsius.
Definition KKM_K6P.cpp:81
std::string getSensorType()
get the sensor type as a string
Definition KKM_K6P.cpp:137
An object that stores data from which a topic and message can be extracted for publishing to an MQTT ...
Definition MQTTMailer.hpp:37
int reset_count
number of resets retrieved for NVS
Definition scheduler.hpp:31
Defines structure for floating point numbers in dual 8 bit upper/lower format.
Definition KKM_K6P.hpp:44
int8_t upper
the value to the left of the decimal point
Definition KKM_K6P.hpp:45
std::string toString()
Convert struct members upper and lower to a string double value.
Definition KKM_K6P.hpp:53
uint8_t lower
the value to the right of the decimal point
Definition KKM_K6P.hpp:46