Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed BLE_API nRF51822 htu21d
Revision 14:0451ac0d8eb2, committed 2021-11-08
- Comitter:
- milen_iot
- Date:
- Mon Nov 08 18:20:34 2021 +0000
- Parent:
- 13:64601d952fe4
- Commit message:
- initial
Changed in this revision
htu21d.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/htu21d.lib Mon Nov 08 18:20:34 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/loopsva/code/htu21d/#5c0f8e91d319
--- a/main.cpp Sun Sep 24 06:42:25 2017 +0000 +++ b/main.cpp Mon Nov 08 18:20:34 2021 +0000 @@ -31,43 +31,29 @@ #include "mbed.h" #include "toolchain.h" -#include "ble/BLE.h" -#include "Si7021.h" //credit for library @irsanjul: https://os.mbed.com/users/irsanjul/code/Si7021/ - - +#include "ble/BLE.h"//credit for library @irsanjul: https://os.mbed.com/users/irsanjul/code/Si7021/ /******************************************************************************************* * START tochange: items that may need customization depending on sensors, hardware, and desired behavior *******************************************************************************************/ -const uint16_t Periodic_Update_Seconds = 15; //number of seconds between periodic I/O status re-transmits 900s =15 min. -#define MyDebugEnb 0 //enables serial output for debug, consumes ~1mA when idle -//uint8_t magnet_near=0; //this I/O, specifically for reed switch sensor - +const uint16_t Periodic_Update_Seconds = 10; //number of seconds between periodic I/O status re-transmits 900s =15 min. +#define MyDebugEnb 1 //enables serial output for debug, consumes ~1mA when idle //set I2C pins -Si7021 tempI2C(p9, p11); //sda=p0.9, scl=p.11, for bart boards -//Si7021 tempI2C(p0, p1); //sda=p0.0, scl=p0.1 - -DigitalOut sensorPwr(p0); //digital output on P0.00, for light sensor, bart board - //depending on how many sensor values you have, change this and examine Flag_Update_ADV_01 -const uint8_t Max_Sensor_Num = 3; //set to the number of advertisements (temp, humidity, lumins = 3) +const uint8_t Max_Sensor_Num = 2; //set to the number of advertisements (temp, humidity, lumins = 3) //Set beacon rate behavior. If 4 Seconds_Per_Advertisement @ 300ms ADV_Interval_ms // then you have about 4 / 0.3 = 13 advertisements for each sensor data. Probably excessive. const uint8_t Seconds_Per_Advertisement = 2; //for each sensor, adverise for this seconds -const uint16_t ADV_Interval_ms = 900; //ms between advertisements. Lower means more beacons. set >=10ms +const uint16_t ADV_Interval_ms = 1000; //ms between advertisements. Lower means more beacons. set >=10ms //sensor data. char json_char_01[6] = {0, 0, 0, 0, 0, 0}; //hold json for temperature char json_char_02[6] = {0, 0, 0, 0, 0, 0}; //hold json for humidity -char json_char_03[6] = {0, 0, 0, 0, 0, 0}; //hold json for next sensor uint8_t json_len_01; uint8_t json_len_02; -uint8_t json_len_03; -//ADC_CONFIG_PSEL_AnalogInput4 = AIN4 = P0.03 = nRF Pin 7 -//ADC_CONFIG_PSEL_AnalogInput2 = AIN2 = P0.01 = nRF Pin 5 #define MyAnalogPin ADC_CONFIG_PSEL_AnalogInput2 /****************************************************************************************** @@ -76,26 +62,23 @@ #if MyDebugEnb +//fail motherfucker // if you see ~1mA consumption during sleep, that's because MyDebugEnb==1, it's enabled. -Serial device(p9, p11); //nRF51822 uart : TX=p9. RX=p11 +Serial device(p1, p2); #endif +AnalogIn soilSensor(p3); // p9 static Ticker Tic_Stop_Adv; //used to stop advertising after X seconds static Ticker Tic_Debounce; //debounce I/O static Ticker Tic_Periodic; //transmit sensor data on a periodic basis outside I/O events -const uint16_t Periodicity = 1800; //birthday periodicity used for spoof checking, must match gateway. Should be 1800 seconds for 30minutes -static Timer Tmr_From_Birthday; //holds number of seconds since birthday, for spoof detection -static Ticker Tic_Birthday; //resets Tmr_From_Birthday every Periodicity seconds, for spoof detection - - static bool Flag_Update_IO = false; //flag to indicate event is hardware interrupt static bool Flag_Periodic_Call = false; //flag to indicate event is periodic callback static bool Flag_Detach_Adv_Tic = false; //flag to stop advertising static uint8_t Flag_Update_ADV_01 = 1; //flag for which sensor data to advertise; 1, 2, 3, etc... /* Optional: Device Name, add for human read-ability */ -const static char DEVICE_NAME[] = "LOL"; +const static char DEVICE_NAME[] = "SL0"; //Advertisement Data @@ -160,69 +143,22 @@ uint8_t Xmit_Cnt = 1; - - -/* **** NOT USED **** */ -//16byte UUID loading happens here -//Look at <GapAdvertisingData.h> for rest of definition -struct ApplicationData_t { - //Byte 0: AppID High Byte - //Byte 1: AppID Low Byte - //Byte 2: sensor High Word - //Byte 3: - //Byte 4: - //Byte 5: sensor Low Byte - - - //app ID is 16 bit, (0xFEFE) - uint16_t applicationSpecificId; /* An ID used to identify temperature value in the manufacture specific AD data field */ - - //TMP_nrf51::TempSensorValue_t tmpSensorValue; /* this is a float (32-bit), user data */ -} PACKED; - - - -void debounce_Callback(void) -{ - Tic_Debounce.detach(); - Flag_Update_IO = true; //start advertising - /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access - * BLE device API from the main thread. */ - -} - +/* **************************************** + * Decides what actions need to be performed on periodic basis +*******************************************/ void stop_adv_Callback(void) -{ - /*stops advertising after X seconds or after all sensor data advertisements iterated through - * flow: - * set Flag_Update_ADV_01 = 1, 1st advertisement sent - * set Flag_Update_ADV_01 = 2, 2nd advertisement sent - * set Flag_Update_ADV_01 = 3, 3rd advertisement sent - * now Flag_Update_ADV_01 is 3, stop. - */ - - if (Flag_Update_ADV_01 >= Max_Sensor_Num) //set to the number of advertisements (temp, humidity, lumins = 3) - { - Flag_Detach_Adv_Tic = true; //flag to stop advertising - Flag_Update_ADV_01=0; //reset so we start at the first sensor in the group on next periodic update trigger - Flag_Periodic_Call = false; - } - else - { - Flag_Update_ADV_01++; - Xmit_Cnt++; //increment transmit counter when updating I/O - } +{ + Flag_Detach_Adv_Tic = true; //flag to stop advertising + Flag_Periodic_Call = false; } - - -/* **************************************** - * Decides what actions need to be performed on periodic basis -*******************************************/ void periodic_Callback(void) { + #if MyDebugEnb + device.printf("periodic callback."); + #endif Flag_Update_IO = true; Flag_Update_ADV_01 = 1; //flag for advertising first sensor Flag_Periodic_Call = true; //adds the "p" in topic name @@ -230,31 +166,6 @@ Xmit_Cnt++; //increment transmit counter when updating I/O } - -/* **************************************** - * No RTC available, tickers only have a 35 minute range. - * So periodicity for spoof avoidance is set to 30 minutes -*******************************************/ -void clock_reset_Callback(void) -{ -#if MyDebugEnb - device.printf("===== reset timer ====="); - device.printf("\r\n"); -#endif - Tmr_From_Birthday.reset(); -}; - - -void setupApplicationData(ApplicationData_t &appData) -{ - // two byte ID: 0xFEFE - static const uint16_t APP_SPECIFIC_ID_TEST = 0xFEFE; //2 byte application ID - - appData.applicationSpecificId = APP_SPECIFIC_ID_TEST; -} - - - /** * This function is called when the ble initialization process has failled */ @@ -264,7 +175,6 @@ } - /** * Callback triggered when the ble initialization process has finished */ @@ -291,7 +201,6 @@ //set modes "no EDR", "discoverable" for beacon type advertisements ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); - //from GAP example /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData)); @@ -299,22 +208,6 @@ /* Setup advertising parameters: not connectable */ ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); ble.gap().setAdvertisingInterval(ADV_Interval_ms); //one advertisment every X ms. Self tickers, so you don't have to worry. - -} - - -//not needed anymore -void my_analogin_init(void) -{ - - NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | - (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | - //(ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | - (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | - (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | - //(ADC_CONFIG_PSEL_AnalogInput4 << ADC_CONFIG_PSEL_Pos) | - (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); - NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; } @@ -347,60 +240,17 @@ wait_ms(1); //save off RESULT before disabling. - //uint16_t myresult = (uint16_t)NRF_ADC->RESULT; + uint16_t myresult = (uint16_t)NRF_ADC->RESULT; //disable ADC to lower bat consumption NRF_ADC->TASKS_STOP = 1; - //NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled; //disable to shutdown ADC & lower bat consumption + NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled; //disable to shutdown ADC & lower bat consumption - return (uint16_t)NRF_ADC->RESULT; // 10 bit - //return myresult; +// return (uint16_t)NRF_ADC->RESULT; // 10 bit + return myresult; } //end read_bat_volt - -/* **************************************** - * Read battery voltage using bandgap reference - * shunt analog pin to ADC, from API here - * https://developer.mbed.org/users/mbed_official/code/mbed-src/file/cb4253f91ada/targets/hal/TARGET_NORDIC/TARGET_NRF51822/analogin_api.c -*******************************************/ -uint16_t read_ADC_pin(void) -{ - - //10 bit resolution, route PSEL pin as 1/3 input sel, - //set ADC ref to VBG band gap - //set AnalogInput4 as input pin (this is P0.03) - //no external voltage reference - NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | - //(ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | - (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | - (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | - //ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | - (MyAnalogPin << ADC_CONFIG_PSEL_Pos) | - (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); - //set pin select to AnalogInput4 = pin 7 = p0.03 = AIN4 - //NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk; - //NRF_ADC->CONFIG |= ADC_CONFIG_PSEL_AnalogInput4 << ADC_CONFIG_PSEL_Pos; - NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; - NRF_ADC->TASKS_START = 1; - - - //while loop doesn't actually loop until reading comlete, use a wait. - while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}; - wait_ms(1); //needed because busy while loop doesn't run. - - //save off RESULT before disabling. - //uint16_t myresult = (uint16_t)NRF_ADC->RESULT; - - //disable ADC to lower bat consumption - //NRF_ADC->TASKS_STOP = 1; - //NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled; //disable to shutdown ADC & lower bat consumption - - return (uint16_t)NRF_ADC->RESULT; // 10 bit - //return myresult; -} //end read_ADC_pin - - /* **************************************** * Pattern scheme indicating "one of ours" * generate first part of ADV data so that observer can recognize it as "one of ours". @@ -444,25 +294,17 @@ device.printf("\r\n"); #endif - - Tmr_From_Birthday.start(); //tracks # sec since birthday - - BLE &ble = BLE::Instance(); ble.init(bleInitComplete); float bat_reading; //hold battery voltage reading (Vbg/Vcc) - my_analogin_init();//routes band-gap to analog input - /* SpinWait for initialization to complete. This is necessary because the * BLE object is used in the main loop below. */ while (ble.hasInitialized() == false) { /* spin loop */ } //every X seconds, sends period update, up to 1800 (30 minutes) Tic_Periodic.attach(periodic_Callback, Periodic_Update_Seconds); //send updated I/O every x seconds - Tic_Birthday.attach(clock_reset_Callback, Periodicity); //clock algorithm periodicity - //ble.getAddress(0,mac_reverse); //last byte of MAC (as shown on phone app) is at mac[0], not mac[6]; ble.gap().getAddress(0,mac_reverse); //last byte of MAC (as shown on phone app) is at mac[0], not mac[6]; @@ -474,16 +316,10 @@ } device.printf("\r\n"); #endif + + while (true) { //Main Loop - - uint16_t seconds_Old =(uint16_t)(Tmr_From_Birthday.read_ms()/1000); // 0-1800 seconds (30 minutes) - - #if MyDebugEnb - device.printf("current time in seconds: %d \r\n", seconds_Old); - #endif - - //Update variables holding sensor readings, in preparation for advertisement. if (Flag_Update_IO) { /* Do blocking calls or whatever hardware-specific action is @@ -494,8 +330,6 @@ Tic_Periodic.attach(periodic_Callback, Periodic_Update_Seconds); //Xmit_Cnt++; //increment transmit counter when updating I/O - - //read and convert battery voltage bat_reading = (float)read_bat_volt(); bat_reading = (bat_reading * 3.6) / 1024.0; @@ -507,31 +341,32 @@ memset(&bat_volt_char[0], 0, sizeof(bat_volt_char)); //clear out buffer //convert battery voltage float value to string reprsentation to 2 decimal places, and save the size of string. total_chars = sprintf (bat_volt_char, "%.2f", bat_reading); - - sensorPwr = 1; //turn on power to light sensor - //read and convert analog voltage. Comment out this section if note needed, saves some battery - NRF_ADC->TASKS_STOP = 1; - float analogreading; - analogreading = (float)read_ADC_pin(); - analogreading = (analogreading * 3.6) / 1024.0; - memset(&json_char_03[0], 0, sizeof(json_char_03)); //clear out buffer - json_len_03 = sprintf (json_char_03, "%.2f", analogreading); - - #if MyDebugEnb - device.printf("separate analog reading: %.02f \r\n", analogreading); - #endif - + //disable ADC to save power NRF_ADC->TASKS_STOP = 1; NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled; //disable to shutdown ADC & lower bat consumption - sensorPwr = 0; //turn off power to light sensor + + float temptemp = 20; + + float temphum = 7; + + int humint = -1; - //read temperature of Si7021 - tempI2C.measure(); - float temptemp = (tempI2C.get_temperature()/1000.00); - float temphum = (tempI2C.get_humidity()/1000.00); - temptemp = temptemp*((float)9.0/5.0) + 32; + temphum = soilSensor.read(); + humint = soilSensor.read_u16(); + #if MyDebugEnb + device.printf("tempasd: %f \r\n", temptemp); + device.printf("hum0: %f \r\n", temphum); + device.printf("asd: %f \r\n", 3.14f); + device.printf("hum: %f \r\n", temphum); + device.printf("hum2: %d \r\n", humint); + device.printf("this is the end \r\n"); + + + + #endif + memset(&json_char_01[0], 0, sizeof(json_char_01)); //clear out buffer memset(&json_char_02[0], 0, sizeof(json_char_02)); //clear out buffer @@ -546,249 +381,77 @@ Flag_Update_IO = false; //all needed variables for advertisement of sensor data has been updated in memory }//end Flag_Update_IO - - //compose the correct JSON portion of advertisement sensor data - if (Flag_Update_ADV_01 > 0) - { - //Generate "First Section" for ADV_Data so gateway will recognize our advertisement pattern - hash_first_section(Adv_First_Section, mac_reverse, bat_volt_char); + + //Generate "First Section" for ADV_Data so gateway will recognize our advertisement pattern + hash_first_section(Adv_First_Section, mac_reverse, bat_volt_char); - /* **************************************** - * start writing out ADVData array - * todo: this is easy to write but hard to read. Maybe make it easy to read and hard to write? - ******************************************/ - memset(&AdvData[0], 0, sizeof(AdvData)); //put nulls into adv data - uint8_t JSON_loc=0; //AdvData[0] + /* **************************************** + * start writing out ADVData array + * todo: this is easy to write but hard to read. Maybe make it easy to read and hard to write? + ******************************************/ + memset(&AdvData[0], 0, sizeof(AdvData)); //put nulls into adv data + uint8_t JSON_loc=0; //AdvData[0] - AdvData[0] = Adv_First_Section[0]; //"our device" flag, MAC[3] - JSON_loc++; //JSON_loc == 1 - AdvData[1] = Adv_First_Section[1]; //"out device" flag, MAC[2]... - JSON_loc++; //JSON_loc == 2 - AdvData[2] = Adv_First_Section[2]; - JSON_loc++; //JSON_loc == 3 - AdvData[3] = Adv_First_Section[3]; - JSON_loc++; //JSON_loc == 4 - AdvData[4] = Adv_First_Section[4]; - JSON_loc++; //JSON_loc == 5 - AdvData[5] = Adv_First_Section[5]; - JSON_loc++; //JSON_loc == 6 - AdvData[6] = Adv_First_Section[6]; - JSON_loc++; - AdvData[7] = Adv_First_Section[7]; - JSON_loc++; - AdvData[8] = Adv_First_Section[8]; - JSON_loc++; - AdvData[9] = Adv_First_Section[9]; - JSON_loc++; + for(int i = 0; i < 10; i++){ + AdvData[i] = Adv_First_Section[i]; //"our device" flag, MAC[3] + } - #if MyDebugEnb - device.printf("ADV first 10 array: "); - for (int i=0; i<10; i++) - { - device.printf("%x ", AdvData[i]); - } - device.printf("\r\n"); - #endif + #if MyDebugEnb + device.printf("ADV first 10 array: "); + for (int i=0; i<10; i++) + { + device.printf("%x ", AdvData[i]); + } + device.printf("\r\n"); + #endif - JSON_loc = 10; - //Start of encrypted user data - - //[10] and [11] hold 2 bytes for how many seconds since birthday, little endian - AdvData[10] = seconds_Old & 0xFF; + JSON_loc = 10; + //convert sensor data to string + for (int i=0; i < json_len_01; i++) + { + AdvData[JSON_loc] = json_char_01[i]; JSON_loc++; - AdvData[11] = (seconds_Old >> 8) & 0xFF; - JSON_loc++; - - AdvData[12] = Xmit_Cnt; - JSON_loc++; - - - + } //JSON_loc left at location of next character - //------ START JASON Flag_Update_ADV_01 - //start of jason data - if (Flag_Update_ADV_01 == 1) //temperature - { - JSON_loc = 13; - AdvData[JSON_loc] = 0x22; //ADV_Data[13] = " - JSON_loc++; //14 - - AdvData[JSON_loc] = 0x54; //ADV_Data[14] = t - JSON_loc++; //15 - - AdvData[JSON_loc] = 0x4D; //ADV_Data[15] = m - JSON_loc++; //16 - - AdvData[JSON_loc] = 0x50; //ADV_Data[16] = p - JSON_loc++; //17 - - //for periodic calls, we want to add an extra mqtt level "p", using "/p" - //to delineate between MQTT publishes from real world I/O interrupts vs timer interrupts - if (Flag_Periodic_Call) - { - AdvData[JSON_loc] = 0x2f; // ADV_Data[17] = / - JSON_loc++; //18 - AdvData[JSON_loc] = 0x70; // ADV_Data[18] =p - JSON_loc++; //19 - } - - AdvData[JSON_loc] = 0x22; //ADV_Data[17 or 19] = " - JSON_loc++; //20 - AdvData[JSON_loc] = 0x3a; //ADV_Data[18 or 20] = : - JSON_loc++; //21 - - //convert magnet variable to string, for magnet sensor, this is easy - //since we only have 1 or 0, but this also works for analog values - /* - memset(&buffer[0], 0, sizeof(buffer)); //clear out buffer - total_chars = sprintf (buffer, "%d", magnet_near); //returns total number of characters, which is 1 character. - for (int i=0; i < total_chars; i++) - { - AdvData[JSON_loc] = buffer[i]; - JSON_loc++; //23 - } //JSON_loc left at location of next character - */ - - //convert sensor data to string - for (int i=0; i < json_len_01; i++) - { - AdvData[JSON_loc] = json_char_01[i]; - JSON_loc++; - } //JSON_loc left at location of next character - - //AdvData[JSON_loc] = 0x0; //since AdvData was cleared to start with, we don't need to null term - }//end Flag_Update_ADV_01==1 - if (Flag_Update_ADV_01 == 2) //humidity + AdvData[JSON_loc] = 0x2c; //since AdvData was cleared to start with, we don't need to null term + JSON_loc++; + + for (int i=0; i < json_len_02; i++) + { + AdvData[JSON_loc] = json_char_02[i]; + JSON_loc++; + } //JSON_loc left at location of next character + + AdvData[JSON_loc] = 0x2c; //since AdvData was cleared to start with, we don't need to null term + + #if MyDebugEnb + device.printf("AdvData: "); + for (int i=10; i<JSON_loc+1; i++) //replace last 16 bytes with encrypted 16 bytes { - JSON_loc = 13; - AdvData[JSON_loc] = 0x22; //ADV_Data[13] = " - JSON_loc++; //14 - - AdvData[JSON_loc] = 0x48; //ADV_Data[14] = H = 0x48 - JSON_loc++; //15 - - AdvData[JSON_loc] = 0x55; //ADV_Data[15] = U = 0x55 - JSON_loc++; //16 - - AdvData[JSON_loc] = 0x4d; //ADV_Data[16] = M = 0x4d - JSON_loc++; //17 - - //for periodic calls, we want to add an extra mqtt level "p", using "/p" - //to delineate between MQTT publishes from real world I/O interrupts vs timer interrupts - if (Flag_Periodic_Call) - { - AdvData[JSON_loc] = 0x2f; // ADV_Data[17] = / - JSON_loc++; //18 - AdvData[JSON_loc] = 0x70; // ADV_Data[18] =p - JSON_loc++; //19 - } - - AdvData[JSON_loc] = 0x22; //ADV_Data[17 or 19] = " - JSON_loc++; //20 - AdvData[JSON_loc] = 0x3a; //ADV_Data[18 or 20] = : - JSON_loc++; //21 - - - //convert sensor data to string - //memset(&buffer[0], 0, sizeof(buffer)); //clear out buffer - for (int i=0; i < json_len_02; i++) - { - AdvData[JSON_loc] = json_char_02[i]; - JSON_loc++; - } //JSON_loc left at location of next character + device.printf("%x", AdvData[i]); + } + #endif - //AdvData[JSON_loc] = 0x0; //since AdvData was cleared to start with, we don't need to null term - - } //end if (Flag_Update_ADV_01 == 2) - if (Flag_Update_ADV_01 == 3) //light sensor - { - JSON_loc = 13; - AdvData[JSON_loc] = 0x22; //ADV_Data[13] = " - JSON_loc++; //14 - - AdvData[JSON_loc] = 0x4C; //ADV_Data[14] = L = 0x4C, as in lumin - JSON_loc++; //15 - - AdvData[JSON_loc] = 0x55; //ADV_Data[15] = U = 0x55 - JSON_loc++; //16 - - AdvData[JSON_loc] = 0x4d; //ADV_Data[16] = M = 0x4d - JSON_loc++; //17 - - //for periodic calls, we want to add an extra mqtt level "p", using "/p" - //to delineate between MQTT publishes from real world I/O interrupts vs timer interrupts - if (Flag_Periodic_Call) - { - AdvData[JSON_loc] = 0x2f; // ADV_Data[17] = / - JSON_loc++; //18 - AdvData[JSON_loc] = 0x70; // ADV_Data[18] =p - JSON_loc++; //19 - } - - AdvData[JSON_loc] = 0x22; //ADV_Data[17 or 19] = " - JSON_loc++; //20 - AdvData[JSON_loc] = 0x3a; //ADV_Data[18 or 20] = : - JSON_loc++; //21 - - - //convert sensor data to string - //memset(&buffer[0], 0, sizeof(buffer)); //clear out buffer - for (int i=0; i < json_len_03; i++) - { - AdvData[JSON_loc] = json_char_03[i]; - JSON_loc++; - } //JSON_loc left at location of next character - - //AdvData[JSON_loc] = 0x0; //since AdvData was cleared to start with, we don't need to null term - - } //end if (Flag_Update_ADV_01 == 3) - - - ApplicationData_t appData; - setupApplicationData(appData); - - /********************* - * start encrypting last 16 bytes of ADV_Data - *********************/ - for (int i=0; i<16; i++) - { - src_buf[i] = AdvData[i+10]; //start of encrypted section is at AdvData[10] - } - nrf_ecb_init(); - nrf_ecb_set_key(key_buf); - bool successful_ecb = nrf_ecb_crypt(des_buf, src_buf); - #if MyDebugEnb - device.printf("success ecb = %d \r\n", successful_ecb); - device.printf("src_buf: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \r\n", src_buf[0], src_buf[1], src_buf[2], src_buf[3], src_buf[4], src_buf[5], src_buf[6], src_buf[7], src_buf[8], src_buf[9], src_buf[10], src_buf[11], src_buf[12], src_buf[13], src_buf[14], src_buf[15]); - device.printf("des_buf: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \r\n", des_buf[0], des_buf[1], des_buf[2], des_buf[3], des_buf[4], des_buf[5], des_buf[6], des_buf[7], des_buf[8], des_buf[9], des_buf[10], des_buf[11], des_buf[12], des_buf[13], des_buf[14], des_buf[15]); - #endif - for (int i=0; i<16; i++) //replace last 16 bytes with encrypted 16 bytes - { - AdvData[i+10] = des_buf[i]; - } - - //set payload for advertisement to our custom manufactured data. First 5 bytes is BLE standard, last 26 bytes is our array - //ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *) &appData, sizeof(ApplicationData_t)); - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData)); - - - ble.gap().startAdvertising(); - - //Advertise THIS data for X seconds, then advertisement the next sensor, etc... - Tic_Stop_Adv.attach(stop_adv_Callback, Seconds_Per_Advertisement); - }//end Flag_Update_ADV_## - + //set payload for advertisement to our custom manufactured data. First 5 bytes is BLE standard, last 26 bytes is our array + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData)); + ble.gap().startAdvertising(); + Tic_Stop_Adv.attach(stop_adv_Callback, Seconds_Per_Advertisement); if (Flag_Detach_Adv_Tic == true) //ticker callback flag to stop advertising { + #if MyDebugEnb + device.printf("stop advertising"); + #endif ble.gap().stopAdvertising(); //may be safer to execute BLE operations in main Tic_Stop_Adv.detach(); Flag_Detach_Adv_Tic = false; } - + #if MyDebugEnb + device.printf("Calling wait for event"); + #endif ble.waitForEvent(); //sleeps until interrupt form ticker or I/O - }//end forever while -}//end main + } +}