Chamal Ayesh Wickramanayaka
Published © MIT

Home Gardening with Thingy:53

The next battle we have to face as human is the food shortage. This has been already happened to some countries at alarming rates.

IntermediateWork in progress10 hours279
Home Gardening with Thingy:53

Things used in this project

Hardware components

Thingy:52
Nordic Semiconductor Thingy:52
×1
Espressif ESP32 Development Board - Developer Edition
Espressif ESP32 Development Board - Developer Edition
×1
MINI RELAY SPDT 5 PINS 12VDC 10A 120V CONTACT
TaydaElectronics MINI RELAY SPDT 5 PINS 12VDC 10A 120V CONTACT
×1
Water pump 6V
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

BLE controlled water pump schemantic

Code

BLE enabled water pump

Arduino
#include "BLEDevice.h"
#include <Wire.h>
#define bleServerName "Wio Micro Agro"
#define ONBOARD_LED 2
#define PUMP_PIN 3
static BLEUUID pumpServiceUUID("180f");
static BLEUUID pumpCharacteristicUUID("2a19");
static boolean doConnect = false;
static boolean connected = false;
static BLEAddress *pServerAddress;
static BLERemoteCharacteristic* pumpCharacteristic;
const uint8_t notificationOn[] = {0x1, 0x0};
const uint8_t notificationOff[] = {0x0, 0x0};
char* pumpChar;
boolean newPumpState = false;
bool connectToServer(BLEAddress pAddress) {
BLEClient* pClient = BLEDevice::createClient();
pClient->connect(pAddress);
Serial.println(" - Connected to server");
BLERemoteService* pRemoteService = pClient->getService(pumpServiceUUID);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(pumpServiceUUID.toString().c_str());
return (false);
}
pumpCharacteristic = pRemoteService->getCharacteristic(pumpCharacteristicUUID);
if (pumpCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID");
return false;
}
Serial.println(" - Found our characteristics");
pumpCharacteristic->registerForNotify(pumpNotifyCallback);
return true;
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
if (advertisedDevice.getName() == bleServerName) {
advertisedDevice.getScan()->stop();
pServerAddress = new BLEAddress(advertisedDevice.getAddress());
doConnect = true;
Serial.println("Device found. Connecting!");
}
}
};
static void pumpNotifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData, size_t length, bool isNotify) {
pumpChar = (char*)pData;
newPumpState = true;
}
void setup() {
Serial.begin(115200);
Serial.println("Starting Arduino BLE Client application...");
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->start(30);
pinMode(ONBOARD_LED, OUTPUT);
pinMode(PUMP_PIN, OUTPUT);
}
void loop() {
if (doConnect == true) {
if (connectToServer(*pServerAddress)) {
Serial.println("We are now connected to the BLE Server.");
pumpCharacteristic->getDescriptor(BLEUUID((uint16_t)0x4545))->writeValue((uint8_t*)notificationOn, 2, true);
connected = true;
} else {
Serial.println("We have failed to connect to the server; Restart your device to scan for nearby BLE server again.");
}
doConnect = false;
}
std::string value =   pumpCharacteristic->readValue();
Serial.print("Pump state by BLE server = ");
Serial.println(value.c_str());
if(value=="on"){
Serial.println("On");
digitalWrite(ONBOARD_LED, HIGH);
digitalWrite(PUMP_PIN, HIGH);
}
else{
Serial.println("Off");
digitalWrite(ONBOARD_LED, LOW);
digitalWrite(PUMP_PIN, LOW);
}
delay(1000);
}

Credits

Chamal Ayesh Wickramanayaka

Chamal Ayesh Wickramanayaka

21 projects • 25 followers
Experienced software engineer with a passion for AI, IoT, and innovation, continuously seeking to learn and embrace new technologies.

Comments