-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBluetoothBLEMoistureLevelSensor.ino
61 lines (43 loc) · 1.63 KB
/
BluetoothBLEMoistureLevelSensor.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <Mouse.h>
#include <ArduinoBLE.h>
BLEService environmentalService("181A"); // Environmental
BLEUnsignedCharCharacteristic humidityCharacteristic("2A6F", BLERead); // Humidity
int moistureLevelSensor = A0;
const int AirValue = 884; // Calibrated
const int WaterValue = 422; // Calibrated
void setup() {
Serial.begin(9600);
if (!BLE.begin()) {
Serial.println("Starting BLE failed");
while (1);
}
// Set a name that will appear when scanning for bluetooth devices
BLE.setLocalName("Moisture Sensor");
BLE.setAdvertisedService(environmentalService);
environmentalService.addCharacteristic(humidityCharacteristic);
BLE.addService(environmentalService);
// Set initial value for the characteristic
humidityCharacteristic.writeValue(0);
// Start advertising the service
BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections...");
}
void loop() {
// Wait for a BLE central
BLEDevice central = BLE.central();
if (central) {
Serial.print("Connected to central: ");
Serial.println(central.address());
// Check the moisture level when a central is connected
while (central.connected()) {
int soilMoistureValue = analogRead(moistureLevelSensor);
int moisureLevelPercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
humidityCharacteristic.writeValue(moisureLevelPercent);
Serial.print("Written moisture level percent to characteristic. Moisture level percent: ");
Serial.println(moisureLevelPercent);
delay(1000);
}
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}