SMHOSY BLE - Petite Domotique ESP32 Commandé Via Une Application Android

Introduction: SMHOSY BLE - Petite Domotique ESP32 Commandé Via Une Application Android

Une petite base de domotique en bluetooth LE sur un ESP32 commander par une application android

voici comment réaliser simplement une petite domotique d'appartement à moindres frais

Supplies

  • 1 ESP32
  • 1 PCF8574AN
  • 2 résistance de 4,7 kΩ1
  • Module de 8 relais avec optocoupleur
  • un accès Thunkable
  • 1 smartphone ANDROID

Step 1: UN PEU DE PRATIQUE :

ayant envie de comprendre un peu le bluetooth LE je me suis dis quoi de mieux que de réaliser une petite domotique😁

j'ai donc ouvert l'exemple fourni dans arduino BLE_uart.ino puis fait plusieurs essai via Thunkable ce qui nous donne une petite application entièrement configurable

Coté ESP32 il fallait pouvoir commander des relais j'ai opté pour l'i2c avec le PCF8574AN et un module de 8 relais

Step 2: LE CODE ARDUINO :

Je suis partie sur l'exemple de l'esp32 Avec fournie uart_ble

TUTO_BLE_ESP32.ino

/*
=====================================================================================
========================== OPEN SOURCE LICENCE ==================================
=====================================================================================

Copyright 2021 David AUVRÉ https://smhosy.blogspot.com/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.

IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Video: https://www.youtube.com/watch?v=oCMOYS71NIU
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
Ported to Arduino ESP32 by Evandro Copercini

adaptation par SMHOSY DAVID https://smhosy.blogspot.com/
petit tuto permettant le pilotage de 8 relais via une aplication android

Librairie PCF8574 https://github.com/skywodd/pcf8574_arduino_library
*/

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>


#include "Arduino.h"
#include "PCF8574.h"
#include "config.h"

BLEServer *pServer = NULL;
BLECharacteristic * pTxCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint8_t txValue = 0;

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

//initialisation de l'I2C dans notre exemple @0x38
PCF8574 I2COutput(0x38);

// variable pour l'I2C
const int out_array[8] = {0,1,2,3,4,5,6,7};
int etat_out_array[8] = {1,1,1,1,1,1,1,1};

//fonction explode les donnée recu pour extraire l'API_KEY et la commande
String explode(String data, char separator, int index){
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;

for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};

void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};

class MyCallbacks: public BLECharacteristicCallbacks {
String api_key_uart = "";
int btn = -1;
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
String recv ="";
if (rxValue.length() > 0) {
for (int i = 0; i < rxValue.length(); i++){
recv += rxValue[i];
}
}
// on eclate la varible de reception
api_key_uart = explode(recv, ':', 0);
btn = (explode(recv, ':', 1)).toInt();
// si l'API_KEY correspond
if(API_KEY == api_key_uart){
// on inverse l'etat de la pin
etat_out_array[btn-1] = 1 - etat_out_array[btn-1];
// on envoi la commande
I2COutput.digitalWrite(btn-1, etat_out_array[btn-1]);
Serial.print("[SUCCES] -> COMMANDE BTN : ");Serial.println(btn);
}
else
{
Serial.println("[ERROR] -> API_KEY NO CORRECT");
}
}
};


void setup() {
Serial.begin(115200);
/*Déclaration des I/O I2C */
for (int i=0; i<8; i++) {
I2COutput.pinMode (out_array[i], OUTPUT);
I2COutput.digitalWrite(out_array[i], etat_out_array[out_array[i]]);
}
I2COutput.begin();
// Create the BLE Device
BLEDevice::init("ESP32_BLE_smhosy");

// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());

// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);

// Create a BLE Characteristic
pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);
pTxCharacteristic->addDescriptor(new BLE2902());

BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);

pRxCharacteristic->setCallbacks(new MyCallbacks());

// Start the service
pService->start();

// Start advertising
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
}



void loop() {

if (deviceConnected) {
pTxCharacteristic->setValue(&txValue, 1);
pTxCharacteristic->notify();
txValue++;
delay(10); // bluetooth stack will go into congestion, if too many packets are sent
}

// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
}
}

et le fichier config.h ou se trouve l'API_KEY


// varible api_key qui doit ètre identique a celle de l'application android par defaut 123456789
String API_KEY = "123456789";

retrouver les code sur mon git: Https://github.com/davi-domo/SMHOSY_BLE

Step 3: APPLICATION ANDROID :

Retrouver le projet Thunkable ici: Https://x.thunkable.com/projectPage/5ff277868aea05001145928e

n'hésitez pas à commenter c'est gratuit

Step 4: Liens Utiles

Be the First to Share

    Recommendations

    • Make it Real Student Design Challenge #3

      Make it Real Student Design Challenge #3
    • Toys & Games Contest

      Toys & Games Contest
    • Furniture Contest

      Furniture Contest

    Comments