Sure, here the code:
#include "InfluxDB.h"
// MAC addresses of sensors
char *DEVICES[] = {
"AA:BB:CC:AA:BB:CC",
"AA:BB:CC:AA:BB:CC"
};
#define HISTORY_DELTA 7890000 // 3 months
#define maxCharacteristics 30
static int deviceCount = sizeof DEVICES / sizeof DEVICES[0];
// BLE
BleCharacteristic sensorData;
BleCharacteristic writeMode;
BleCharacteristic writeHistoryMode;
BleCharacteristic batteryData;
BleCharacteristic timeData;
BleCharacteristic historyData;
BlePeerDevice peer;
void onDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context);
void onSensorDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context);
void onWriteModeDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context);
void onWriteHistoryModeDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context);
void onBatteryDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context);
void onTimeDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context);
void onHistoryDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context);
// Buffer for reading data values from sensor
byte char_data[32];
// Array to store historical data values from sensors and write to database in a second stage
byte history_data[10][32];
// Log handling
SerialLogHandler logHandler(LOG_LEVEL_INFO);
String str = "";
double temp_pub;
int moist_pub;
int lux_pub;
int soil_pub;
int batt_pub;
long int timestamp;
// InfluxDB credentials
InfluxDB idb = InfluxDB("user", "password");
void setup()
{
Log.info("Argon start");
sensorData.onDataReceived(onSensorDataReceived, NULL);
writeMode.onDataReceived(onWriteModeDataReceived, NULL);
writeHistoryMode.onDataReceived(onWriteHistoryModeDataReceived, NULL);
batteryData.onDataReceived(onBatteryDataReceived, NULL);
timeData.onDataReceived(onTimeDataReceived, NULL);
historyData.onDataReceived(onHistoryDataReceived, NULL);
Particle.variable("temp", temp_pub);
Particle.variable("moist", moist_pub);
Particle.variable("lux", lux_pub);
Particle.variable("soil", soil_pub);
Particle.variable("batt", batt_pub);
Particle.function("refresh",refresh);
Particle.function("update",update);
// InfluxDB details
idb.setDatabase("test");
idb.setDeviceName("particle");
idb.setDebug(true);
}
void loop()
{
// Do nothing
}
void onWriteModeDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context)
{
uint8_t flags = data[0];
//Log.info("Writemode Data: Length %d ", len);
}
void onWriteHistoryModeDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context)
{
uint8_t flags = data[0];
//Log.info("WriteHistorymode Data: Length %d ", len);
}
void onSensorDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context)
{
uint8_t flags = data[0];
//Log.info("Sensor Data: Length %d ", len);
}
void onBatteryDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context)
{
uint8_t flags = data[0];
//Log.info("Battery Data: Length %d ", len);
}
void onTimeDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context)
{
uint8_t flags = data[0];
//Log.info("Time Data: Length %d ", len);
}
void onHistoryDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context)
{
uint8_t flags = data[0];
//Log.info("History Data: Length %d ", len);
}
int refresh(String command) {
if (command=="reboot"){
Particle.publish("REBOOT", "from command", PRIVATE);
System.reset();
return 0;
}
else {
return -1;
}
}
int update(String command) {
if (command=="update"){
Particle.publish("UPDATE", "from command", PRIVATE);
readsensors();
return 0;
}
else {
return -1;
}
}
void readsensors()
{
for (int i = 0; i < deviceCount; i++)
{
for (int a = 1; a < 4; a++)
{
char *deviceMacAddress = DEVICES[i];
Log.info(" [*] Connecting to sensor %s - Attempt: %d of 3", deviceMacAddress, attempt);
BLE.on();
peer = BLE.connect(BleAddress(deviceMacAddress));
if (peer.connected())
{
// Successfull
//[...]
BLE.off()
}
else
{
Log.info("[FAIL] SENSOR READ: sensor not connected...");
delay(1500);
}
}
}
}