Skip to content

Commit

Permalink
Read battery characteristic and publich battery value via mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
agvxov committed Apr 23, 2018
1 parent 5b190aa commit 5f02814
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions flora/config.h.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
#define MQTT_MOISTURE MQTT_DEV_BASE "moisture"
#define MQTT_LIGHT MQTT_DEV_BASE "light"
#define MQTT_CONDUCTIVITY MQTT_DEV_BASE "conductivity"
#define MQTT_BATTERY MQTT_DEV_BASE "battery"

#define SLEEP_DURATION 3600ll // duration of sleep between flora connection attempts in seconds (must be constant with "ll" suffix)
#define SLEEP_WAIT 30 // time until esp32 is put into deep sleep mode. must be sufficient to connect to wlan, connect to xiaomi flora device & push measurement data to MQTT

#define BATTERY_INTERVAL 12 // Retrieve battery level every n wakeups

const char* wifi_ssid = "ssid";
const char* wifi_password = "password";

Expand Down
35 changes: 33 additions & 2 deletions flora/flora.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include "config.h"

RTC_DATA_ATTR int bootCount = 0;

// The remote service we wish to connect to.
static BLEUUID serviceUUID("00001204-0000-1000-8000-00805f9b34fb");

Expand All @@ -48,7 +50,7 @@ WiFiClient espClient;
PubSubClient client(espClient);


bool getSensorData(BLEAddress pAddress) {
bool getSensorData(BLEAddress pAddress, bool getBattery) {
Serial.print("Forming a connection to Flora device at ");
Serial.println(pAddress.toString().c_str());

Expand Down Expand Up @@ -124,6 +126,34 @@ bool getSensorData(BLEAddress pAddress) {
snprintf(buffer, 64, "%d", conductivity);
client.publish(MQTT_CONDUCTIVITY, buffer);

if (getBattery) {
Serial.println("Trying to retrieve battery level...");
pRemoteCharacteristic = pRemoteService->getCharacteristic(uuid_version_battery);
if (pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(uuid_sensor_data.toString().c_str());
return false;
}
Serial.println(" - Found our characteristic");

// Read the value of the characteristic...
value = pRemoteCharacteristic->readValue();
Serial.print("The characteristic value was: ");
const char *val2 = value.c_str();
Serial.print("Hex: ");
for (int i = 0; i < 16; i++) {
Serial.print((int)val2[i], HEX);
Serial.print(" ");
}
Serial.println(" ");

int battery = val2[0];
Serial.print("Battery: ");
Serial.println(battery);
snprintf(buffer, 64, "%d", battery);
client.publish(MQTT_BATTERY, buffer);
}

pClient->disconnect();
}

Expand Down Expand Up @@ -179,7 +209,8 @@ void setup() {

delay(1000);

getSensorData(floraAddress);
getSensorData(floraAddress, ((bootCount % BATTERY_INTERVAL) == 0));
bootCount++;
} // End of setup.


Expand Down

0 comments on commit 5f02814

Please sign in to comment.