une 30aine passés sans pb entre les trames
Dependencies: ADS1015 ARNSRS_SENSORS DmTftLibrary MS5803_14BA SDFileSystem mbed
Fork of ARNSRS_SERVOS_USB_TFT by
Diff: main.cpp
- Revision:
- 13:aa620707126d
- Parent:
- 12:9ac5be447764
- Child:
- 14:e6686a44db84
--- a/main.cpp Mon May 08 12:51:00 2017 +0000 +++ b/main.cpp Mon May 08 13:15:35 2017 +0000 @@ -14,6 +14,17 @@ #include "mbed.h" #include "SDFileSystem.h" #include "ARNSRS_SENSORS.h" +#include "ble/BLE.h" + +#include "ble/services/UARTService.h" +#include <string> + +//Ecrit dans le moniteur série de l'ordi à 9600 bds +#define DEBUG(...) { printf(__VA_ARGS__); } + +//Init BLE +BLEDevice ble; +UARTService *uartServicePtr; //Init de la lib ARNSRS; ARNSRS arnsrs; @@ -40,6 +51,47 @@ char fileName[32]; int points = 1; +//Fonction pour créer et envoyer la chaine d'in en BLE +void build_send_Message(int co2, int ppo2, float press){ + char buf[50]; + sprintf(buf,"%d %d %f\r\n", ppo2, co2, press); + uartServicePtr->writeString(buf); + } + +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) +{ + DEBUG("Disconnected!\n\r"); + DEBUG("Restarting the advertising process\n\r"); + ble.startAdvertising(); +} + +void onDataWritten(const GattWriteCallbackParams *params) +{ + if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { + uint16_t bytesRead = params->len; + + // chaine reçue de l'app android (à dimensionner selon besoin) + char commande [bytesRead] ; + + // conversion de la commande uint8_t en chaine pour permettre comparaison avec commandes connues + for (int j=0;j<bytesRead;j++) { + commande [j] = (*((params->data)+j)); + } + + DEBUG("received %u bytes => ", bytesRead); + DEBUG("commande recue -%s-\r\n",commande); + DEBUG ( "comparaison a commande 'p' vaut %d\r\n",strcmp(commande, "p")); + DEBUG ( "comparaison a commande 'o' vaut %d\r\n",strcmp(commande, "o")); + DEBUG ( "comparaison a commande 'c' vaut %d\r\n",strcmp(commande, "c")); + + + DEBUG("Tous les capteurs = %d %d %d\r\n", ppO2, co2, pression); + build_send_Message(ppO2, co2, pression); + + } + +} + //Fonction qui change le nom du fichier ouvert pour le LOG FILE *nextLogFile(void) { @@ -115,6 +167,30 @@ { setup(); +DEBUG("Initialising....\n\r"); + ble.init(); + ble.onDisconnection(disconnectionCallback); + ble.onDataWritten(onDataWritten); + + /* setup advertising */ + ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); + ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); + ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, + (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); + ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, + (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); + + ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ + ble.startAdvertising(); + + UARTService uartService(ble); + uartServicePtr = &uartService; + + while (true) { + + ble.waitForEvent(); + + while (1) { wait(2); @@ -151,4 +227,6 @@ DATA_LOG(); } + + } }