Test
source/main.cpp@76:596c9924e51b, 2019-01-24 (annotated)
- Committer:
- HelGast95
- Date:
- Thu Jan 24 11:47:57 2019 +0000
- Revision:
- 76:596c9924e51b
- Parent:
- 75:6606a580ebc4
- Child:
- 77:48c622bf903f
Se comenta la parte de BLE para que envie JSON con la nueva ontologia
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 | 76:596c9924e51b | 59 | char tmp[512]; // Vble auxiliar para el tratamiento de cadenas de char. |
| HelGast95 | 74:12b9444a2fb4 | 60 | string str; |
| HelGast95 | 74:12b9444a2fb4 | 61 | char final = END_OF_JSON; |
| HelGast95 | 74:12b9444a2fb4 | 62 | while(true) { |
| HelGast95 | 74:12b9444a2fb4 | 63 | // Esperamos a un mensaje en la cola |
| HelGast95 | 76:596c9924e51b | 64 | picojson::object event; |
| HelGast95 | 76:596c9924e51b | 65 | picojson::object extraInfo; |
| HelGast95 | 76:596c9924e51b | 66 | int type = rand() % 3; |
| HelGast95 | 76:596c9924e51b | 67 | int idEvent = rand() % 500; |
| HelGast95 | 76:596c9924e51b | 68 | event["idEvent"] = picojson::value((double) idEvent); |
| HelGast95 | 76:596c9924e51b | 69 | event["type"] = picojson::value((double) type); |
| HelGast95 | 76:596c9924e51b | 70 | str = "E9:ED:F4:AC:BF:8E"; |
| HelGast95 | 76:596c9924e51b | 71 | extraInfo["hazardousDevice"] = picojson::value(str); |
| HelGast95 | 76:596c9924e51b | 72 | str = "D5:62:12:BF:B8:45"; |
| HelGast95 | 76:596c9924e51b | 73 | if(type != 0) extraInfo["affectedDevice"] = picojson::value(str); |
| HelGast95 | 76:596c9924e51b | 74 | event["extraInfo"] = picojson::value(extraInfo); |
| HelGast95 | 75:6606a580ebc4 | 75 | |
| HelGast95 | 76:596c9924e51b | 76 | //printf("Se serializa el string\r\n"); |
| HelGast95 | 76:596c9924e51b | 77 | str = picojson::value(event).serialize(); |
| HelGast95 | 76:596c9924e51b | 78 | |
| HelGast95 | 76:596c9924e51b | 79 | // Convertimos el string a char * |
| HelGast95 | 76:596c9924e51b | 80 | strncpy(tmp, str.c_str(), sizeof(tmp)); |
| HelGast95 | 76:596c9924e51b | 81 | strncat(tmp, &final, sizeof(final)); // Añadimos el caracter al final |
| HelGast95 | 76:596c9924e51b | 82 | tmp[sizeof(tmp) - 1] = 0; |
| HelGast95 | 76:596c9924e51b | 83 | |
| HelGast95 | 76:596c9924e51b | 84 | //printf("Se envia un evento por el puesto serie\r\n"); |
| HelGast95 | 76:596c9924e51b | 85 | sendCharArrayToSerial(tmp, &pcSerial); |
| HelGast95 | 76:596c9924e51b | 86 | |
| HelGast95 | 76:596c9924e51b | 87 | wait(2); |
| HelGast95 | 74:12b9444a2fb4 | 88 | } |
| HelGast95 | 73:a91805f9e9f0 | 89 | } |
| HelGast95 | 73:a91805f9e9f0 | 90 | |
| HelGast95 | 74:12b9444a2fb4 | 91 | void scanCallback(const Gap::AdvertisementCallbackParams_t *params) { |
| HelGast95 | 73:a91805f9e9f0 | 92 | if (params->peerAddr[0] != 0x8E) return; |
| HelGast95 | 76:596c9924e51b | 93 | /* |
| HelGast95 | 73:a91805f9e9f0 | 94 | printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", |
| HelGast95 | 73:a91805f9e9f0 | 95 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
| HelGast95 | 73:a91805f9e9f0 | 96 | params->rssi, params->isScanResponse, params->type); |
| HelGast95 | 76:596c9924e51b | 97 | */ |
| HelGast95 | 73:a91805f9e9f0 | 98 | |
| HelGast95 | 73:a91805f9e9f0 | 99 | BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); |
| HelGast95 | 73:a91805f9e9f0 | 100 | } |
| mbed_official | 1:72c60abef7e7 | 101 | |
| HelGast95 | 73:a91805f9e9f0 | 102 | void serviceDiscoveryCallback(const DiscoveredService *service) { |
| HelGast95 | 73:a91805f9e9f0 | 103 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { |
| HelGast95 | 76:596c9924e51b | 104 | //printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); |
| HelGast95 | 73:a91805f9e9f0 | 105 | } else { |
| HelGast95 | 76:596c9924e51b | 106 | //printf("S UUID-"); |
| HelGast95 | 73:a91805f9e9f0 | 107 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
| HelGast95 | 73:a91805f9e9f0 | 108 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
| HelGast95 | 76:596c9924e51b | 109 | //printf("%02x", longUUIDBytes[i]); |
| HelGast95 | 73:a91805f9e9f0 | 110 | } |
| HelGast95 | 76:596c9924e51b | 111 | //printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); |
| HelGast95 | 73:a91805f9e9f0 | 112 | } |
| HelGast95 | 73:a91805f9e9f0 | 113 | } |
| HelGast95 | 73:a91805f9e9f0 | 114 | |
| HelGast95 | 73:a91805f9e9f0 | 115 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { |
| HelGast95 | 76:596c9924e51b | 116 | //printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); |
| HelGast95 | 74:12b9444a2fb4 | 117 | if (characteristicP->getUUID().getShortUUID() == 0xa001) { |
| HelGast95 | 73:a91805f9e9f0 | 118 | testServiceptr = *characteristicP; |
| HelGast95 | 73:a91805f9e9f0 | 119 | serviceDiscovered = true; |
| HelGast95 | 73:a91805f9e9f0 | 120 | } |
| HelGast95 | 73:a91805f9e9f0 | 121 | } |
| HelGast95 | 73:a91805f9e9f0 | 122 | |
| HelGast95 | 73:a91805f9e9f0 | 123 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { |
| HelGast95 | 76:596c9924e51b | 124 | //printf("terminated SD for handle %u\r\n", connectionHandle); |
| HelGast95 | 73:a91805f9e9f0 | 125 | } |
| HelGast95 | 73:a91805f9e9f0 | 126 | |
| HelGast95 | 73:a91805f9e9f0 | 127 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
| HelGast95 | 73:a91805f9e9f0 | 128 | if (params->role == Gap::CENTRAL) { |
| HelGast95 | 73:a91805f9e9f0 | 129 | BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
| HelGast95 | 73:a91805f9e9f0 | 130 | BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xA000, 0xA001); |
| HelGast95 | 73:a91805f9e9f0 | 131 | } |
| HelGast95 | 73:a91805f9e9f0 | 132 | } |
| mbed_official | 1:72c60abef7e7 | 133 | |
| mbed_official | 1:72c60abef7e7 | 134 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
| mbed_official | 1:72c60abef7e7 | 135 | { |
| HelGast95 | 73:a91805f9e9f0 | 136 | /* Si se desconecta el dispositivo, volvemos a entrar en estado Advertising*/ |
| HelGast95 | 76:596c9924e51b | 137 | //printf("Desconectado. Se comienza la fase de escaneo de nuevo\n\r"); |
| HelGast95 | 73:a91805f9e9f0 | 138 | serviceDiscovered = false; |
| HelGast95 | 74:12b9444a2fb4 | 139 | BLE::Instance().gap().startScan(scanCallback); |
| mbed_official | 1:72c60abef7e7 | 140 | } |
| HelGast95 | 75:6606a580ebc4 | 141 | |
| HelGast95 | 73:a91805f9e9f0 | 142 | void onDataReadClientCallback(const GattReadCallbackParams *response) { |
| HelGast95 | 73:a91805f9e9f0 | 143 | if (response->handle == testServiceptr.getValueHandle()) { |
| HelGast95 | 76:596c9924e51b | 144 | //printf("\r\n\r\nonDataReadClientCallback: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len); |
| HelGast95 | 73:a91805f9e9f0 | 145 | for (unsigned index = 0; index < response->len; index++) { |
| HelGast95 | 76:596c9924e51b | 146 | //printf("[%02x]", response->data[index]); |
| HelGast95 | 73:a91805f9e9f0 | 147 | } |
| HelGast95 | 76:596c9924e51b | 148 | //printf("\r\n"); |
| HelGast95 | 75:6606a580ebc4 | 149 | |
| HelGast95 | 74:12b9444a2fb4 | 150 | BLEdata_t *BLEdata = BLEmpool.alloc(); |
| HelGast95 | 74:12b9444a2fb4 | 151 | int r = rand() % 500; |
| HelGast95 | 74:12b9444a2fb4 | 152 | char str[12]; |
| HelGast95 | 74:12b9444a2fb4 | 153 | sprintf(str, "%d", r); |
| HelGast95 | 74:12b9444a2fb4 | 154 | BLEdata->id = str; |
| HelGast95 | 74:12b9444a2fb4 | 155 | uint16_t tensionaux; |
| HelGast95 | 74:12b9444a2fb4 | 156 | tensionaux = ((response->data[1]) << 8) + (response->data[0]); |
| HelGast95 | 75:6606a580ebc4 | 157 | BLEdata->voltaje = (tensionaux * 1.0 )/100; |
| HelGast95 | 76:596c9924e51b | 158 | BLEqueue.put(BLEdata); |
| mbed_official | 1:72c60abef7e7 | 159 | } |
| mbed_official | 1:72c60abef7e7 | 160 | } |
| mbed_official | 1:72c60abef7e7 | 161 | |
| HelGast95 | 73:a91805f9e9f0 | 162 | /** |
| HelGast95 | 73:a91805f9e9f0 | 163 | * Esta función se llama si ha habido algún error en el proceso de inicialización del BLE |
| HelGast95 | 73:a91805f9e9f0 | 164 | */ |
| HelGast95 | 73:a91805f9e9f0 | 165 | void onBleInitError(BLE &ble, ble_error_t error) { |
| HelGast95 | 76:596c9924e51b | 166 | //printf("Ha ocurrido un error al inicializar la configuracion del BLE\n"); |
| mbed_official | 1:72c60abef7e7 | 167 | } |
| mbed_official | 1:72c60abef7e7 | 168 | |
| HelGast95 | 73:a91805f9e9f0 | 169 | void printMacAddress() { |
| mbed_official | 43:fb2855f7754b | 170 | /* Print out device MAC address to the console*/ |
| mbed_official | 43:fb2855f7754b | 171 | Gap::AddressType_t addr_type; |
| mbed_official | 43:fb2855f7754b | 172 | Gap::Address_t address; |
| mbed_official | 43:fb2855f7754b | 173 | BLE::Instance().gap().getAddress(&addr_type, address); |
| mbed_official | 43:fb2855f7754b | 174 | printf("DEVICE MAC ADDRESS: "); |
| mbed_official | 43:fb2855f7754b | 175 | for (int i = 5; i >= 1; i--){ |
| mbed_official | 43:fb2855f7754b | 176 | printf("%02x:", address[i]); |
| mbed_official | 43:fb2855f7754b | 177 | } |
| mbed_official | 43:fb2855f7754b | 178 | printf("%02x\r\n", address[0]); |
| mbed_official | 43:fb2855f7754b | 179 | } |
| mbed_official | 43:fb2855f7754b | 180 | |
| HelGast95 | 73:a91805f9e9f0 | 181 | /** |
| HelGast95 | 73:a91805f9e9f0 | 182 | * Callback triggered when the ble initialization process has finished |
| HelGast95 | 73:a91805f9e9f0 | 183 | */ |
| mbed_official | 1:72c60abef7e7 | 184 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
| mbed_official | 1:72c60abef7e7 | 185 | { |
| mbed_official | 1:72c60abef7e7 | 186 | BLE& ble = params->ble; |
| mbed_official | 1:72c60abef7e7 | 187 | ble_error_t error = params->error; |
| mbed_official | 1:72c60abef7e7 | 188 | |
| mbed_official | 1:72c60abef7e7 | 189 | if (error != BLE_ERROR_NONE) { |
| HelGast95 | 73:a91805f9e9f0 | 190 | /* In case of error, forward the error handling to onBleInitError */ |
| mbed_official | 1:72c60abef7e7 | 191 | onBleInitError(ble, error); |
| mbed_official | 1:72c60abef7e7 | 192 | return; |
| mbed_official | 1:72c60abef7e7 | 193 | } |
| mbed_official | 1:72c60abef7e7 | 194 | |
| HelGast95 | 73:a91805f9e9f0 | 195 | /* Ensure that it is the default instance of BLE */ |
| HelGast95 | 73:a91805f9e9f0 | 196 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
| mbed_official | 1:72c60abef7e7 | 197 | return; |
| mbed_official | 1:72c60abef7e7 | 198 | } |
| HelGast95 | 73:a91805f9e9f0 | 199 | |
| HelGast95 | 73:a91805f9e9f0 | 200 | ble.gap().onConnection(connectionCallback); |
| mbed_official | 1:72c60abef7e7 | 201 | ble.gap().onDisconnection(disconnectionCallback); |
| mbed_official | 1:72c60abef7e7 | 202 | |
| HelGast95 | 73:a91805f9e9f0 | 203 | ble.gattClient().onDataRead(onDataReadClientCallback); |
| mbed_official | 1:72c60abef7e7 | 204 | |
| HelGast95 | 73:a91805f9e9f0 | 205 | ble.gap().setScanParams(500, 400); |
| HelGast95 | 75:6606a580ebc4 | 206 | ble.gap().startScan(scanCallback); |
| mbed_official | 1:72c60abef7e7 | 207 | } |
| mbed_official | 1:72c60abef7e7 | 208 | |
| mbed_official | 1:72c60abef7e7 | 209 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
| mbed_official | 1:72c60abef7e7 | 210 | BLE &ble = BLE::Instance(); |
| mbed_official | 10:ac3615194d04 | 211 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
| HelGast95 | 73:a91805f9e9f0 | 212 | } |
| HelGast95 | 73:a91805f9e9f0 | 213 | |
| HelGast95 | 73:a91805f9e9f0 | 214 | void readVoltageValue() { |
| HelGast95 | 73:a91805f9e9f0 | 215 | BLE &ble = BLE::Instance(); |
| HelGast95 | 73:a91805f9e9f0 | 216 | if (serviceDiscovered && !ble.gattClient().isServiceDiscoveryActive()) { |
| HelGast95 | 73:a91805f9e9f0 | 217 | testServiceptr.read(); |
| HelGast95 | 73:a91805f9e9f0 | 218 | } |
| mbed_official | 1:72c60abef7e7 | 219 | } |
| mbed_official | 1:72c60abef7e7 | 220 | |
| HelGast95 | 73:a91805f9e9f0 | 221 | void BLEServiceManagment() { |
| HelGast95 | 75:6606a580ebc4 | 222 | eventQueue.call_every(2000, readVoltageValue); |
| HelGast95 | 73:a91805f9e9f0 | 223 | |
| mbed_official | 1:72c60abef7e7 | 224 | BLE &ble = BLE::Instance(); |
| HelGast95 | 73:a91805f9e9f0 | 225 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
| mbed_official | 1:72c60abef7e7 | 226 | ble.init(bleInitComplete); |
| HelGast95 | 73:a91805f9e9f0 | 227 | |
| mbed_official | 10:ac3615194d04 | 228 | eventQueue.dispatch_forever(); |
| HelGast95 | 73:a91805f9e9f0 | 229 | } |
| mbed_official | 1:72c60abef7e7 | 230 | |
| HelGast95 | 73:a91805f9e9f0 | 231 | int main() { |
| HelGast95 | 74:12b9444a2fb4 | 232 | srand(time(NULL)); |
| HelGast95 | 73:a91805f9e9f0 | 233 | threadLED.start(blinkLED3); |
| HelGast95 | 76:596c9924e51b | 234 | //threadBLE.start(BLEServiceManagment); |
| HelGast95 | 74:12b9444a2fb4 | 235 | threadSerial.start(sendJsonOverSerial); |
| HelGast95 | 74:12b9444a2fb4 | 236 | |
| HelGast95 | 73:a91805f9e9f0 | 237 | threadLED.join(); |
| HelGast95 | 73:a91805f9e9f0 | 238 | threadBLE.join(); |
| HelGast95 | 74:12b9444a2fb4 | 239 | threadSerial.join(); |
| HelGast95 | 73:a91805f9e9f0 | 240 | } |