I've just got into Arduino and I'm exploring the built-in Bluetooth feature, I'm working on a little project to track a plant's requirements; light levels, soil moisture, temperature, and humidity.
I've got the sensors to work simultaneously (DHT11, GY-30[BH1750], Soil Moisture sensor) - now I'd like to send the data to my iPhone.
Right now, I would like some help sending a reading from my DHT11 sensor to my phone via BLE - I think once I get this done I'll be able to implement this with the other sensors.
Currently using nRF Connect to communicate with my arduino using Bluetooth, I've got it to connect before for another project - I was able to turn the built-in LED on and off by writing '0' or '1'.
I have little experience with BLE but I'm very willing to learn, please attach any resources you'd think will help!!
Here's what I have so far:
//-------------------------------------------------------------------
//Sensor
//-------------------------------------------------------------------
#include "DHT.h"
#define DHTPIN 14
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
//-------------------------------------------------------------------
//BLE
//-------------------------------------------------------------------
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLECharacteristic.h>
#include <BLE2902.h>
//-------------------------------------------------------------------
//BLE UUIDs
//-------------------------------------------------------------------
#define SERVICE_UUID "0782bff8-5782-11ec-bf63-0242ac130002"
#define CHARACTERISTIC_UUID_TEMP_HUM "1aa1635a-5782-11ec-bf63-0242ac130002"
bool deviceConnected = false;
String readings;
float h; //humidity value
float t; //temperature value
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
//Rewrite the write function to show temperature or humidity
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
if(CHARACTERISTIC_UUID_TEMP_HUM.equals(pCharacteristic->getUUID())) {
t = dht.readTemperature();
h = dht.readHumidity();
char *value = pCharacteristic->getData(); //get the data associated with it
switch(value){
case 'T':
Serial.print(F("Temperature: "));
Serial.print(t);
Serial.print(F("°C"));
break;
case 'H':
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("%"));
break;
default:
break;
}
}
}
};
void setup() {
Serial.begin(115200);
Serial.println("Welcome to Gaia!");
BLEDevice::init("Gaia");
//create server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
//create service
BLEService *pService = pServer->createService(SERVICE_UUID);
// tempHumCharacteristic = pServer->createCharacteristic(CHARACTERISTIC_UUID_TEMP_HUM, BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
tempHumCharacteristic->setCallbacks(new MyCallbacks());
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
dht.begin();
}
void loop() {
delay(10000);
}
// if (isnan(h) || isnan(t)) {
// Serial.println(F("Failed to read from DHT sensor!"));
// return;
// }