Test
Revision 74:12b9444a2fb4, committed 2019-01-23
- Comitter:
- HelGast95
- Date:
- Wed Jan 23 10:54:17 2019 +0000
- Parent:
- 73:a91805f9e9f0
- Child:
- 75:6606a580ebc4
- Commit message:
- a
Changed in this revision
| source/main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/source/main.cpp Tue Jan 22 08:36:23 2019 +0000
+++ b/source/main.cpp Wed Jan 23 10:54:17 2019 +0000
@@ -10,54 +10,25 @@
#include "ble/DiscoveredCharacteristic.h"
#include "ble/DiscoveredService.h"
-DiscoveredCharacteristic testServiceptr;
+bool serviceDiscovered = false;
-bool serviceDiscovered = false;
+typedef struct {
+ string id; /* Id */
+ float voltaje; /* Voltaje */
+} BLEdata_t;
DigitalOut led3Test(LED3);
DigitalOut led4BLE(LED4);
Serial pcSerial(USBTX, USBRX); // Abrimos conexión serial con el puerto USB
+DiscoveredCharacteristic testServiceptr;
EventQueue eventQueue;
+Queue<BLEdata_t, 32> BLEqueue;
+MemoryPool<BLEdata_t, 32> BLEmpool;
Thread threadLED(osPriorityAboveNormal1, 400);
-//Thread threadSerial(osPriorityAboveNormal2, 2000);
-Thread threadBLE(osPriorityRealtime3, 2000);
-
-int getNum(char ch)
-{
- int num=0;
- if(ch>='0' && ch<='9')
- {
- num=ch-0x30;
- }
- else
- {
- switch(ch)
- {
- case 'A': case 'a': num=10; break;
- case 'B': case 'b': num=11; break;
- case 'C': case 'c': num=12; break;
- case 'D': case 'd': num=13; break;
- case 'E': case 'e': num=14; break;
- case 'F': case 'f': num=15; break;
- default: num=0;
- }
- }
- return num;
-}
-
-//function : hex2int
-//this function will return integer value against
-//hexValue - which is in string format
-
-unsigned int hex2int(unsigned char hex[])
-{
- unsigned int x=0;
- x=(getNum(hex[0]))*16+(getNum(hex[1]));
- return x;
-
-}
+Thread threadSerial(osPriorityAboveNormal2, 2500);
+Thread threadBLE(osPriorityRealtime3, 1000);
/**
* Tarea encargada de parpadear un LED continuamente
@@ -65,7 +36,7 @@
void blinkLED3() {
while(true) {
led3Test = !led3Test;
- wait(0.2);
+ wait(0.8);
}
}
@@ -73,7 +44,7 @@
* Método encargado de enviar un string por el puerto serie char a char
*/
void sendCharArrayToSerial(char const *array, Serial *serial) {
- int i = 0;
+ uint32_t i = 0;
while(array[i] != '\0') {
serial->putc(array[i]);
i++;
@@ -82,69 +53,45 @@
}
/**
- * Tarea encragada de enviar JSONs por el puerto serie
+ * Thread encargado de enviar JSONs por el puerto serie
*/
void sendJsonOverSerial() {
- /* Contruimos el objeto JSON */
- picojson::object worker;
- picojson::object vehicle;
- picojson::object device;
- picojson::object event;
- picojson::object data;
-
- string tmpValue = "1234";
- char tmp[1024];
- string str;
- char final = END_OF_JSON; // Caracter que indica el final del JSON
-
- while(true) {
- worker["idDevice"] = picojson::value(tmpValue);
- tmpValue = "12-12-2019";
- worker["tsLastSeen"] = picojson::value(tmpValue);
- tmpValue = "Baby Driver";
- worker["role"] = picojson::value(tmpValue);
- tmpValue = "12345";
- worker["idVehicle"] = picojson::value(tmpValue);
-
- tmpValue = "123456";
- vehicle["idDevice"] = picojson::value(tmpValue);
- tmpValue = "18-12-2019";
- vehicle["tsLastSeen"] = picojson::value(tmpValue);
+ picojson::object event;
+ char tmp[50]; // Vble auxiliar para el tratamiento de cadenas de char.
+ string str;
+ char final = END_OF_JSON;
- tmpValue = "11111";
- device["idDevice"] = picojson::value(tmpValue);
- device["battLevel"] = picojson::value(45.0);
- tmpValue = "Pulsera";
- device["deviceType"] = picojson::value(tmpValue);
-
- tmpValue = "256745";
- event["idEvent"] = picojson::value(tmpValue);
- event["hazardousDevice"] = picojson::value(false);
- event["affectedDevice"] = picojson::value(true);
- tmpValue = "Critical";
- event["eventType"] = picojson::value(tmpValue);
- tmpValue = "19-12-2018";
- event["tsEvent"] = picojson::value(tmpValue);
+ while(true) {
+ // Esperamos a un mensaje en la cola
+ osEvent evt = BLEqueue.get();
+ if (evt.status == osEventMessage) {
+ BLEdata_t *BLEdata = (BLEdata_t*) evt.value.p;
+
+ printf("\r\nDato recibido BLE\r\n");
+ event["id"] = picojson::value(BLEdata->id);
+ printf("Id: %s\r\n", BLEdata->id.c_str());
+ str = "Voltaje";
+ event["type"] = picojson::value(str);
+ printf("Data: ");
+ event["data"] = picojson::value(BLEdata->voltaje);
- data["worker"] = picojson::value(worker);
- data["vehicle"] = picojson::value(vehicle);
- data["device"] = picojson::value(device);
- data["event"] = picojson::value(event);
-
- str = picojson::value(data).serialize();
-
- // Convertimos el string a char *
- strncpy(tmp, str.c_str(), sizeof(tmp));
- strncat(tmp, &final, sizeof(final)); // Añadimos el caracter al final
- tmp[sizeof(tmp) - 1] = 0;
-
- //sendCharArrayToSerial(tmp, &pcSerial);
-
- wait(0.5);
- }
+ BLEmpool.free(BLEdata);
+
+ printf("Se serializa el string\r\n");
+ str = picojson::value(event).serialize();
+
+ // Convertimos el string a char *
+ strncpy(tmp, str.c_str(), sizeof(tmp));
+ strncat(tmp, &final, sizeof(final)); // Añadimos el caracter al final
+ tmp[sizeof(tmp) - 1] = 0;
+
+ printf("Se envia un evento por el puesto serie\r\n");
+ sendCharArrayToSerial(tmp, &pcSerial);
+ }
+ }
}
-void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
+void scanCallback(const Gap::AdvertisementCallbackParams_t *params) {
if (params->peerAddr[0] != 0x8E) return;
printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
@@ -168,7 +115,7 @@
void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
- if (characteristicP->getUUID().getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */
+ if (characteristicP->getUUID().getShortUUID() == 0xa001) {
testServiceptr = *characteristicP;
serviceDiscovered = true;
}
@@ -191,16 +138,27 @@
(void) params;
printf("Desconectado. Se comienza la fase de escaneo de nuevo\n\r");
serviceDiscovered = false;
- BLE::Instance().gap().startScan(advertisementCallback);
+ BLE::Instance().gap().startScan(scanCallback);
}
void onDataReadClientCallback(const GattReadCallbackParams *response) {
if (response->handle == testServiceptr.getValueHandle()) {
- printf("onDataReadClientCallback: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len);
+ printf("\r\n\r\nonDataReadClientCallback: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len);
for (unsigned index = 0; index < response->len; index++) {
printf("[%02x]", response->data[index]);
}
printf("\r\n");
+
+ // Enviamos datos al Mail para que los procese el thread del serial
+ BLEdata_t *BLEdata = BLEmpool.alloc();
+ int r = rand() % 500;
+ char str[12];
+ sprintf(str, "%d", r);
+ BLEdata->id = str;
+ uint16_t tensionaux;
+ tensionaux = ((response->data[1]) << 8) + (response->data[0]);
+ BLEdata->voltaje = (tensionaux * 1.0 )/100;;
+ BLEqueue.put(BLEdata);
}
}
@@ -248,7 +206,7 @@
ble.gattClient().onDataRead(onDataReadClientCallback);
ble.gap().setScanParams(500, 400);
- ble.gap().startScan(advertisementCallback);
+ ble.gap().startScan(scanCallback);
//printMacAddress();
}
@@ -266,7 +224,7 @@
}
void BLEServiceManagment() {
- eventQueue.call_every(2000, readVoltageValue);
+ eventQueue.call_every(1000, readVoltageValue);
BLE &ble = BLE::Instance();
ble.onEventsToProcess(scheduleBleEventsProcessing);
@@ -276,13 +234,14 @@
}
int main() {
+ srand(time(NULL));
threadLED.start(blinkLED3);
threadBLE.start(BLEServiceManagment);
- //threadSerial.start(sendJsonOverSerial);
-
+ threadSerial.start(sendJsonOverSerial);
+
threadLED.join();
threadBLE.join();
- //threadSerial.join();
+ threadSerial.join();
return 0;
}
\ No newline at end of file