#include <DHT.h>
#include <DHT_U.h>
#include <ArduinoBLE.h>
#include "ArduinoTimer.h"
#define Type DHT22
ArduinoTimer Timer1;
int thsens= 4; //using D4 pin on NANO
DHT HT(thsens,Type); //connect to DHT22
float hum;
float tempC;
float tempF;
int delaytime=500; //delay time in milliseconds
int dt=1000;
const int ledPin = LED_BUILTIN; // pin to use for the LED
long previousMillis = 0; // last time the battery level was checked, in ms
BLEService SCGService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite | BLENotify);
//*************************************************************************************
void setup() {
Serial.begin(9600);
HT.begin();
while (Serial); //does not wait for PC to open serial port
// set LED pin to output mode
pinMode(thsens, INPUT); //set up pin for input or output this case
//we are receiving data from sensor so input
// set LED pin to output mode
pinMode(ledPin, OUTPUT);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("SCG");
BLE.setAdvertisedService(SCGService);
// add the characteristic to the service
SCGService.addCharacteristic(switchCharacteristic);
// SCGService.addCharacteristic(humidityChar);
// add service
BLE.addService(SCGService);
// set the initial value for the characeristic: this might need to be changes to read
switchCharacteristic.writeValue(0);
// humiditychar.writeValue[0];
// start advertising
BLE.advertise();
// Serial.println("BLE SCG Peripheral");
}
//****************************************************************************************
void loop() {
// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
// tempF=HT.readTemperature(true);
// Serial.print(tempF);
// Serial.println(" F");
// switchCharacteristic.writeValue(tempF);
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
long currentMillis = millis();
// if 200ms have passed
if (currentMillis - previousMillis >= 2000) {
previousMillis = currentMillis;
MeasureTemperature();
}
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
//Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
} else { // a 0 value
//Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
}
}
}
}
//if (Timer1.TimePassed_Hours(6, AutoReset - 0) || switchCharacteristic.value() ){ //If a certain time has passed or if the switch characteristic has some value. Call functions
//MeasureTemperature();
////Serial.print(soilsens());
//}
} //End void loop
void MeasureTemperature(){
hum=HT.readHumidity();
tempF=HT.readTemperature(true);
Serial.println((int)tempF);
switchCharacteristic.writeValue(tempF);
Serial.println((int)hum);
switchCharacteristic.writeValue(hum);
}
Sorry I couldn't upload just the program, so I had to sadly copy paste it. I tried to addcharacteristic humidityChar but ended up getting out of the scope when I tried to save humidityChar.writeValue(hum);