## Hardware Requirements
- Arduino board with BLE support
*(e.g. Arduino Nano 33 BLE, MKR WiFi 1010, etc.)*
- Capacitive moisture sensor (soil sensor)
- USB cable
- Arduino IDE
---
## The Script
```cpp
#include
const int sensorPin = A0; // Sensor pin
int minWert = 1023; // Minimum value for moisture
int maxWert = 0; // Maximum value for moisture
bool isAdvertising = false; // Advertising state
// UUIDs for the service and characteristic
const char* serviceUuid = "12345678-1234-1234-1234-123456789012";
const char* charUuid = "12345678-1234-1234-1234-123456789013";
BLEService customService(serviceUuid);
BLEIntCharacteristic humidityCharacteristic(charUuid, BLERead | BLENotify);
void setup() {
Serial.begin(9600);
while (!Serial);
// Initialize BLE
if (!BLE.begin()) {
Serial.println("Starting BLE failed!");
while (1);
}
BLE.setLocalName("Sensor01");
customService.addCharacteristic(humidityCharacteristic);
BLE.addService(customService);
BLE.advertise();
isAdvertising = true;
Serial.println("Bluetooth device is ready to send humidity");
}
void loop() {
int sensorValue = analogRead(sensorPin);
// Update maximum and minimum values
if (sensorValue > maxWert) maxWert = sensorValue;
if (sensorValue < minWert) minWert = sensorValue;
// Calculate humidity
float humidity = map(sensorValue, minWert, maxWert, 100, 0);
humidity = constrain(humidity, 0, 100);
humidityCharacteristic.writeValue((int)humidity);
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
// Check connection
if (BLE.connected()) {
if (isAdvertising) {
Serial.println("Device connected, stopping advertising.");
isAdvertising = false;
}
} else {
if (!isAdvertising) {
BLE.advertise();
Serial.println("Advertising restarted");
isAdvertising = true;
}
}
delay(2000);
}
Script Structure
The script consists of two main parts:
setup()Initialization of BLE, service, and characteristic, as well as starting advertisingloop()Reading the sensor, calculating moisture, transmitting data, and status checking
Reading Data
There are different ways to receive the data via Bluetooth.
The easiest is to use Bluetooth LE scanner apps, for example:
- BLE Scanner (iOS)
- BLE Scanner (Android)
There you will find your Arduino device under the name "Sensor01" and can read the moisture values live.
Use Cases
This script serves as a foundation for many projects:
- Home and garden automation
- Smart home systems
- Environmental monitoring
- Notifications on thresholds (e.g. too dry soil)
Conclusion
With this script, you have a solid starting point to reliably transmit sensor data via Bluetooth Low Energy.
The step from a simple moisture sensor to a comprehensive smart home solution is much smaller than you might initially think.