Javier Vargas
/
ControllerBLE
Test
source/main.cpp@74:12b9444a2fb4, 2019-01-23 (annotated)
- Committer:
- HelGast95
- Date:
- Wed Jan 23 10:54:17 2019 +0000
- Revision:
- 74:12b9444a2fb4
- Parent:
- 73:a91805f9e9f0
- Child:
- 75:6606a580ebc4
a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HelGast95 | 73:a91805f9e9f0 | 1 | #define END_OF_JSON 0xFE |
mbed_official | 1:72c60abef7e7 | 2 | |
HelGast95 | 73:a91805f9e9f0 | 3 | #include "mbed.h" |
HelGast95 | 73:a91805f9e9f0 | 4 | #include "picojson.h" |
HelGast95 | 73:a91805f9e9f0 | 5 | #include "stats_report.h" |
HelGast95 | 73:a91805f9e9f0 | 6 | |
HelGast95 | 73:a91805f9e9f0 | 7 | /* Librerías para BLE - Servicio GATT */ |
mbed_official | 1:72c60abef7e7 | 8 | #include "ble/BLE.h" |
mbed_official | 1:72c60abef7e7 | 9 | #include "ble/Gap.h" |
HelGast95 | 73:a91805f9e9f0 | 10 | #include "ble/DiscoveredCharacteristic.h" |
HelGast95 | 73:a91805f9e9f0 | 11 | #include "ble/DiscoveredService.h" |
HelGast95 | 73:a91805f9e9f0 | 12 | |
HelGast95 | 74:12b9444a2fb4 | 13 | bool serviceDiscovered = false; |
HelGast95 | 73:a91805f9e9f0 | 14 | |
HelGast95 | 74:12b9444a2fb4 | 15 | typedef struct { |
HelGast95 | 74:12b9444a2fb4 | 16 | string id; /* Id */ |
HelGast95 | 74:12b9444a2fb4 | 17 | float voltaje; /* Voltaje */ |
HelGast95 | 74:12b9444a2fb4 | 18 | } BLEdata_t; |
HelGast95 | 73:a91805f9e9f0 | 19 | |
HelGast95 | 73:a91805f9e9f0 | 20 | DigitalOut led3Test(LED3); |
HelGast95 | 73:a91805f9e9f0 | 21 | DigitalOut led4BLE(LED4); |
HelGast95 | 73:a91805f9e9f0 | 22 | Serial pcSerial(USBTX, USBRX); // Abrimos conexión serial con el puerto USB |
HelGast95 | 74:12b9444a2fb4 | 23 | DiscoveredCharacteristic testServiceptr; |
HelGast95 | 73:a91805f9e9f0 | 24 | |
HelGast95 | 73:a91805f9e9f0 | 25 | EventQueue eventQueue; |
HelGast95 | 74:12b9444a2fb4 | 26 | Queue<BLEdata_t, 32> BLEqueue; |
HelGast95 | 74:12b9444a2fb4 | 27 | MemoryPool<BLEdata_t, 32> BLEmpool; |
mbed_official | 1:72c60abef7e7 | 28 | |
HelGast95 | 73:a91805f9e9f0 | 29 | Thread threadLED(osPriorityAboveNormal1, 400); |
HelGast95 | 74:12b9444a2fb4 | 30 | Thread threadSerial(osPriorityAboveNormal2, 2500); |
HelGast95 | 74:12b9444a2fb4 | 31 | Thread threadBLE(osPriorityRealtime3, 1000); |
HelGast95 | 73:a91805f9e9f0 | 32 | |
HelGast95 | 73:a91805f9e9f0 | 33 | /** |
HelGast95 | 73:a91805f9e9f0 | 34 | * Tarea encargada de parpadear un LED continuamente |
HelGast95 | 73:a91805f9e9f0 | 35 | */ |
HelGast95 | 73:a91805f9e9f0 | 36 | void blinkLED3() { |
HelGast95 | 73:a91805f9e9f0 | 37 | while(true) { |
HelGast95 | 73:a91805f9e9f0 | 38 | led3Test = !led3Test; |
HelGast95 | 74:12b9444a2fb4 | 39 | wait(0.8); |
HelGast95 | 73:a91805f9e9f0 | 40 | } |
HelGast95 | 73:a91805f9e9f0 | 41 | } |
HelGast95 | 73:a91805f9e9f0 | 42 | |
HelGast95 | 73:a91805f9e9f0 | 43 | /** |
HelGast95 | 73:a91805f9e9f0 | 44 | * Método encargado de enviar un string por el puerto serie char a char |
HelGast95 | 73:a91805f9e9f0 | 45 | */ |
HelGast95 | 73:a91805f9e9f0 | 46 | void sendCharArrayToSerial(char const *array, Serial *serial) { |
HelGast95 | 74:12b9444a2fb4 | 47 | uint32_t i = 0; |
HelGast95 | 73:a91805f9e9f0 | 48 | while(array[i] != '\0') { |
HelGast95 | 73:a91805f9e9f0 | 49 | serial->putc(array[i]); |
HelGast95 | 73:a91805f9e9f0 | 50 | i++; |
HelGast95 | 73:a91805f9e9f0 | 51 | } |
HelGast95 | 73:a91805f9e9f0 | 52 | serial->putc('\0'); |
HelGast95 | 73:a91805f9e9f0 | 53 | } |
mbed_official | 1:72c60abef7e7 | 54 | |
HelGast95 | 73:a91805f9e9f0 | 55 | /** |
HelGast95 | 74:12b9444a2fb4 | 56 | * Thread encargado de enviar JSONs por el puerto serie |
HelGast95 | 73:a91805f9e9f0 | 57 | */ |
HelGast95 | 73:a91805f9e9f0 | 58 | void sendJsonOverSerial() { |
HelGast95 | 74:12b9444a2fb4 | 59 | picojson::object event; |
HelGast95 | 74:12b9444a2fb4 | 60 | char tmp[50]; // Vble auxiliar para el tratamiento de cadenas de char. |
HelGast95 | 74:12b9444a2fb4 | 61 | string str; |
HelGast95 | 74:12b9444a2fb4 | 62 | char final = END_OF_JSON; |
HelGast95 | 73:a91805f9e9f0 | 63 | |
HelGast95 | 74:12b9444a2fb4 | 64 | while(true) { |
HelGast95 | 74:12b9444a2fb4 | 65 | // Esperamos a un mensaje en la cola |
HelGast95 | 74:12b9444a2fb4 | 66 | osEvent evt = BLEqueue.get(); |
HelGast95 | 74:12b9444a2fb4 | 67 | if (evt.status == osEventMessage) { |
HelGast95 | 74:12b9444a2fb4 | 68 | BLEdata_t *BLEdata = (BLEdata_t*) evt.value.p; |
HelGast95 | 74:12b9444a2fb4 | 69 | |
HelGast95 | 74:12b9444a2fb4 | 70 | printf("\r\nDato recibido BLE\r\n"); |
HelGast95 | 74:12b9444a2fb4 | 71 | event["id"] = picojson::value(BLEdata->id); |
HelGast95 | 74:12b9444a2fb4 | 72 | printf("Id: %s\r\n", BLEdata->id.c_str()); |
HelGast95 | 74:12b9444a2fb4 | 73 | str = "Voltaje"; |
HelGast95 | 74:12b9444a2fb4 | 74 | event["type"] = picojson::value(str); |
HelGast95 | 74:12b9444a2fb4 | 75 | printf("Data: "); |
HelGast95 | 74:12b9444a2fb4 | 76 | event["data"] = picojson::value(BLEdata->voltaje); |
HelGast95 | 73:a91805f9e9f0 | 77 | |
HelGast95 | 74:12b9444a2fb4 | 78 | BLEmpool.free(BLEdata); |
HelGast95 | 74:12b9444a2fb4 | 79 | |
HelGast95 | 74:12b9444a2fb4 | 80 | printf("Se serializa el string\r\n"); |
HelGast95 | 74:12b9444a2fb4 | 81 | str = picojson::value(event).serialize(); |
HelGast95 | 74:12b9444a2fb4 | 82 | |
HelGast95 | 74:12b9444a2fb4 | 83 | // Convertimos el string a char * |
HelGast95 | 74:12b9444a2fb4 | 84 | strncpy(tmp, str.c_str(), sizeof(tmp)); |
HelGast95 | 74:12b9444a2fb4 | 85 | strncat(tmp, &final, sizeof(final)); // Añadimos el caracter al final |
HelGast95 | 74:12b9444a2fb4 | 86 | tmp[sizeof(tmp) - 1] = 0; |
HelGast95 | 74:12b9444a2fb4 | 87 | |
HelGast95 | 74:12b9444a2fb4 | 88 | printf("Se envia un evento por el puesto serie\r\n"); |
HelGast95 | 74:12b9444a2fb4 | 89 | sendCharArrayToSerial(tmp, &pcSerial); |
HelGast95 | 74:12b9444a2fb4 | 90 | } |
HelGast95 | 74:12b9444a2fb4 | 91 | } |
HelGast95 | 73:a91805f9e9f0 | 92 | } |
HelGast95 | 73:a91805f9e9f0 | 93 | |
HelGast95 | 74:12b9444a2fb4 | 94 | void scanCallback(const Gap::AdvertisementCallbackParams_t *params) { |
HelGast95 | 73:a91805f9e9f0 | 95 | if (params->peerAddr[0] != 0x8E) return; |
HelGast95 | 73:a91805f9e9f0 | 96 | printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", |
HelGast95 | 73:a91805f9e9f0 | 97 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
HelGast95 | 73:a91805f9e9f0 | 98 | params->rssi, params->isScanResponse, params->type); |
HelGast95 | 73:a91805f9e9f0 | 99 | |
HelGast95 | 73:a91805f9e9f0 | 100 | BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); |
HelGast95 | 73:a91805f9e9f0 | 101 | } |
mbed_official | 1:72c60abef7e7 | 102 | |
HelGast95 | 73:a91805f9e9f0 | 103 | void serviceDiscoveryCallback(const DiscoveredService *service) { |
HelGast95 | 73:a91805f9e9f0 | 104 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { |
HelGast95 | 73:a91805f9e9f0 | 105 | printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); |
HelGast95 | 73:a91805f9e9f0 | 106 | } else { |
HelGast95 | 73:a91805f9e9f0 | 107 | printf("S UUID-"); |
HelGast95 | 73:a91805f9e9f0 | 108 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
HelGast95 | 73:a91805f9e9f0 | 109 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
HelGast95 | 73:a91805f9e9f0 | 110 | printf("%02x", longUUIDBytes[i]); |
HelGast95 | 73:a91805f9e9f0 | 111 | } |
HelGast95 | 73:a91805f9e9f0 | 112 | printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); |
HelGast95 | 73:a91805f9e9f0 | 113 | } |
HelGast95 | 73:a91805f9e9f0 | 114 | } |
HelGast95 | 73:a91805f9e9f0 | 115 | |
HelGast95 | 73:a91805f9e9f0 | 116 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { |
HelGast95 | 73:a91805f9e9f0 | 117 | printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); |
HelGast95 | 74:12b9444a2fb4 | 118 | if (characteristicP->getUUID().getShortUUID() == 0xa001) { |
HelGast95 | 73:a91805f9e9f0 | 119 | testServiceptr = *characteristicP; |
HelGast95 | 73:a91805f9e9f0 | 120 | serviceDiscovered = true; |
HelGast95 | 73:a91805f9e9f0 | 121 | } |
HelGast95 | 73:a91805f9e9f0 | 122 | } |
HelGast95 | 73:a91805f9e9f0 | 123 | |
HelGast95 | 73:a91805f9e9f0 | 124 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { |
HelGast95 | 73:a91805f9e9f0 | 125 | printf("terminated SD for handle %u\r\n", connectionHandle); |
HelGast95 | 73:a91805f9e9f0 | 126 | } |
HelGast95 | 73:a91805f9e9f0 | 127 | |
HelGast95 | 73:a91805f9e9f0 | 128 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
HelGast95 | 73:a91805f9e9f0 | 129 | if (params->role == Gap::CENTRAL) { |
HelGast95 | 73:a91805f9e9f0 | 130 | BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
HelGast95 | 73:a91805f9e9f0 | 131 | BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xA000, 0xA001); |
HelGast95 | 73:a91805f9e9f0 | 132 | } |
HelGast95 | 73:a91805f9e9f0 | 133 | } |
mbed_official | 1:72c60abef7e7 | 134 | |
mbed_official | 1:72c60abef7e7 | 135 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
mbed_official | 1:72c60abef7e7 | 136 | { |
HelGast95 | 73:a91805f9e9f0 | 137 | /* Si se desconecta el dispositivo, volvemos a entrar en estado Advertising*/ |
HelGast95 | 73:a91805f9e9f0 | 138 | (void) params; |
HelGast95 | 73:a91805f9e9f0 | 139 | printf("Desconectado. Se comienza la fase de escaneo de nuevo\n\r"); |
HelGast95 | 73:a91805f9e9f0 | 140 | serviceDiscovered = false; |
HelGast95 | 74:12b9444a2fb4 | 141 | BLE::Instance().gap().startScan(scanCallback); |
mbed_official | 1:72c60abef7e7 | 142 | } |
mbed_official | 1:72c60abef7e7 | 143 | |
HelGast95 | 73:a91805f9e9f0 | 144 | void onDataReadClientCallback(const GattReadCallbackParams *response) { |
HelGast95 | 73:a91805f9e9f0 | 145 | if (response->handle == testServiceptr.getValueHandle()) { |
HelGast95 | 74:12b9444a2fb4 | 146 | printf("\r\n\r\nonDataReadClientCallback: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len); |
HelGast95 | 73:a91805f9e9f0 | 147 | for (unsigned index = 0; index < response->len; index++) { |
HelGast95 | 73:a91805f9e9f0 | 148 | printf("[%02x]", response->data[index]); |
HelGast95 | 73:a91805f9e9f0 | 149 | } |
HelGast95 | 73:a91805f9e9f0 | 150 | printf("\r\n"); |
HelGast95 | 74:12b9444a2fb4 | 151 | |
HelGast95 | 74:12b9444a2fb4 | 152 | // Enviamos datos al Mail para que los procese el thread del serial |
HelGast95 | 74:12b9444a2fb4 | 153 | BLEdata_t *BLEdata = BLEmpool.alloc(); |
HelGast95 | 74:12b9444a2fb4 | 154 | int r = rand() % 500; |
HelGast95 | 74:12b9444a2fb4 | 155 | char str[12]; |
HelGast95 | 74:12b9444a2fb4 | 156 | sprintf(str, "%d", r); |
HelGast95 | 74:12b9444a2fb4 | 157 | BLEdata->id = str; |
HelGast95 | 74:12b9444a2fb4 | 158 | uint16_t tensionaux; |
HelGast95 | 74:12b9444a2fb4 | 159 | tensionaux = ((response->data[1]) << 8) + (response->data[0]); |
HelGast95 | 74:12b9444a2fb4 | 160 | BLEdata->voltaje = (tensionaux * 1.0 )/100;; |
HelGast95 | 74:12b9444a2fb4 | 161 | BLEqueue.put(BLEdata); |
mbed_official | 1:72c60abef7e7 | 162 | } |
mbed_official | 1:72c60abef7e7 | 163 | } |
mbed_official | 1:72c60abef7e7 | 164 | |
HelGast95 | 73:a91805f9e9f0 | 165 | /** |
HelGast95 | 73:a91805f9e9f0 | 166 | * Esta función se llama si ha habido algún error en el proceso de inicialización del BLE |
HelGast95 | 73:a91805f9e9f0 | 167 | */ |
HelGast95 | 73:a91805f9e9f0 | 168 | void onBleInitError(BLE &ble, ble_error_t error) { |
HelGast95 | 73:a91805f9e9f0 | 169 | printf("Ha ocurrido un error al inicializar la configuracion del BLE\n"); |
mbed_official | 1:72c60abef7e7 | 170 | } |
mbed_official | 1:72c60abef7e7 | 171 | |
HelGast95 | 73:a91805f9e9f0 | 172 | void printMacAddress() { |
mbed_official | 43:fb2855f7754b | 173 | /* Print out device MAC address to the console*/ |
mbed_official | 43:fb2855f7754b | 174 | Gap::AddressType_t addr_type; |
mbed_official | 43:fb2855f7754b | 175 | Gap::Address_t address; |
mbed_official | 43:fb2855f7754b | 176 | BLE::Instance().gap().getAddress(&addr_type, address); |
mbed_official | 43:fb2855f7754b | 177 | printf("DEVICE MAC ADDRESS: "); |
mbed_official | 43:fb2855f7754b | 178 | for (int i = 5; i >= 1; i--){ |
mbed_official | 43:fb2855f7754b | 179 | printf("%02x:", address[i]); |
mbed_official | 43:fb2855f7754b | 180 | } |
mbed_official | 43:fb2855f7754b | 181 | printf("%02x\r\n", address[0]); |
mbed_official | 43:fb2855f7754b | 182 | } |
mbed_official | 43:fb2855f7754b | 183 | |
HelGast95 | 73:a91805f9e9f0 | 184 | /** |
HelGast95 | 73:a91805f9e9f0 | 185 | * Callback triggered when the ble initialization process has finished |
HelGast95 | 73:a91805f9e9f0 | 186 | */ |
mbed_official | 1:72c60abef7e7 | 187 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
mbed_official | 1:72c60abef7e7 | 188 | { |
mbed_official | 1:72c60abef7e7 | 189 | BLE& ble = params->ble; |
mbed_official | 1:72c60abef7e7 | 190 | ble_error_t error = params->error; |
mbed_official | 1:72c60abef7e7 | 191 | |
mbed_official | 1:72c60abef7e7 | 192 | if (error != BLE_ERROR_NONE) { |
HelGast95 | 73:a91805f9e9f0 | 193 | /* In case of error, forward the error handling to onBleInitError */ |
mbed_official | 1:72c60abef7e7 | 194 | onBleInitError(ble, error); |
mbed_official | 1:72c60abef7e7 | 195 | return; |
mbed_official | 1:72c60abef7e7 | 196 | } |
mbed_official | 1:72c60abef7e7 | 197 | |
HelGast95 | 73:a91805f9e9f0 | 198 | /* Ensure that it is the default instance of BLE */ |
HelGast95 | 73:a91805f9e9f0 | 199 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
mbed_official | 1:72c60abef7e7 | 200 | return; |
mbed_official | 1:72c60abef7e7 | 201 | } |
HelGast95 | 73:a91805f9e9f0 | 202 | |
HelGast95 | 73:a91805f9e9f0 | 203 | ble.gap().onConnection(connectionCallback); |
mbed_official | 1:72c60abef7e7 | 204 | ble.gap().onDisconnection(disconnectionCallback); |
mbed_official | 1:72c60abef7e7 | 205 | |
HelGast95 | 73:a91805f9e9f0 | 206 | ble.gattClient().onDataRead(onDataReadClientCallback); |
mbed_official | 1:72c60abef7e7 | 207 | |
HelGast95 | 73:a91805f9e9f0 | 208 | ble.gap().setScanParams(500, 400); |
HelGast95 | 74:12b9444a2fb4 | 209 | ble.gap().startScan(scanCallback); |
HelGast95 | 73:a91805f9e9f0 | 210 | |
HelGast95 | 73:a91805f9e9f0 | 211 | //printMacAddress(); |
mbed_official | 1:72c60abef7e7 | 212 | } |
mbed_official | 1:72c60abef7e7 | 213 | |
mbed_official | 1:72c60abef7e7 | 214 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
mbed_official | 1:72c60abef7e7 | 215 | BLE &ble = BLE::Instance(); |
mbed_official | 10:ac3615194d04 | 216 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
HelGast95 | 73:a91805f9e9f0 | 217 | } |
HelGast95 | 73:a91805f9e9f0 | 218 | |
HelGast95 | 73:a91805f9e9f0 | 219 | void readVoltageValue() { |
HelGast95 | 73:a91805f9e9f0 | 220 | BLE &ble = BLE::Instance(); |
HelGast95 | 73:a91805f9e9f0 | 221 | if (serviceDiscovered && !ble.gattClient().isServiceDiscoveryActive()) { |
HelGast95 | 73:a91805f9e9f0 | 222 | testServiceptr.read(); |
HelGast95 | 73:a91805f9e9f0 | 223 | } |
mbed_official | 1:72c60abef7e7 | 224 | } |
mbed_official | 1:72c60abef7e7 | 225 | |
HelGast95 | 73:a91805f9e9f0 | 226 | void BLEServiceManagment() { |
HelGast95 | 74:12b9444a2fb4 | 227 | eventQueue.call_every(1000, readVoltageValue); |
HelGast95 | 73:a91805f9e9f0 | 228 | |
mbed_official | 1:72c60abef7e7 | 229 | BLE &ble = BLE::Instance(); |
HelGast95 | 73:a91805f9e9f0 | 230 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
mbed_official | 1:72c60abef7e7 | 231 | ble.init(bleInitComplete); |
HelGast95 | 73:a91805f9e9f0 | 232 | |
mbed_official | 10:ac3615194d04 | 233 | eventQueue.dispatch_forever(); |
HelGast95 | 73:a91805f9e9f0 | 234 | } |
mbed_official | 1:72c60abef7e7 | 235 | |
HelGast95 | 73:a91805f9e9f0 | 236 | int main() { |
HelGast95 | 74:12b9444a2fb4 | 237 | srand(time(NULL)); |
HelGast95 | 73:a91805f9e9f0 | 238 | threadLED.start(blinkLED3); |
HelGast95 | 73:a91805f9e9f0 | 239 | threadBLE.start(BLEServiceManagment); |
HelGast95 | 74:12b9444a2fb4 | 240 | threadSerial.start(sendJsonOverSerial); |
HelGast95 | 74:12b9444a2fb4 | 241 | |
HelGast95 | 73:a91805f9e9f0 | 242 | threadLED.join(); |
HelGast95 | 73:a91805f9e9f0 | 243 | threadBLE.join(); |
HelGast95 | 74:12b9444a2fb4 | 244 | threadSerial.join(); |
HelGast95 | 73:a91805f9e9f0 | 245 | |
mbed_official | 1:72c60abef7e7 | 246 | return 0; |
HelGast95 | 73:a91805f9e9f0 | 247 | } |