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: BLE_API mbed nRF51822
main.cpp
00001 #include "mbed.h" 00002 #include "BLE.h" 00003 #include "Gap.h" 00004 #include "stdio.h" 00005 00006 typedef struct{ 00007 BLEProtocol::AddressBytes_t address; 00008 uint8_t frameId; 00009 uint16_t age; 00010 } DeviceEntry; 00011 00012 typedef struct{ 00013 BLEProtocol::AddressBytes_t address; 00014 uint8_t data[31]; 00015 uint8_t dataLen; 00016 } TransmitData; 00017 00018 BLE ble; 00019 Serial pc(USBTX, USBRX); 00020 DeviceEntry *deviceArray; 00021 TransmitData txData; 00022 bool txEnabled = false; 00023 uint8_t devicesCount = 0; 00024 char tmp[18]; 00025 00026 void dumpBytes(const uint8_t *data, uint8_t len){ 00027 for(uint8_t i=0;i<len; i++){ 00028 pc.printf("%02X ", data[i]); 00029 } 00030 } 00031 00032 bool isAddressEqual(const uint8_t *addr1, const uint8_t *addr2){ 00033 uint8_t n; 00034 for(n=0;n<6;n++){ 00035 if(addr1[n] != addr2[n]){ 00036 return false; 00037 } 00038 } 00039 return true; 00040 } 00041 00042 void getAddressString(char* addressString, const uint8_t *address){ 00043 sprintf(addressString, "%02X%02X%02X%02X%02X%02X", address[5], address[4], address[3], address[2], address[1], address[0]); 00044 } 00045 00046 void sendToWifi(BLEProtocol::AddressBytes_t *address, const uint8_t *data, uint8_t dataLen){ 00047 if(txEnabled==false){ 00048 txEnabled = true; 00049 getAddressString(tmp, (uint8_t*)address); 00050 memcpy(txData.address, address, sizeof(BLEProtocol::AddressBytes_t)); 00051 memcpy(txData.data, data, dataLen); 00052 txData.dataLen = dataLen; 00053 } 00054 } 00055 00056 void sendToWifi(){ 00057 if(txData.data[3] == 0xFB){ 00058 //beescale 00059 }else 00060 if(txData.data[3] == 0xFA){ 00061 //weatherbeacon 00062 float temp = (float)(txData.data[11]|txData.data[12]<<8)/100; 00063 int16_t pressure = txData.data[14]|txData.data[15]<<8; 00064 uint16_t battery = txData.data[16]|txData.data[17]<<8; 00065 00066 pc.printf("\r\ntemperature is %.1f C\r\n", temp); 00067 pc.printf("humidity %d%%\r\n", txData.data[13]); 00068 if(pressure>0){ 00069 pc.printf("pressure %d hPa\r\n", pressure); 00070 } 00071 pc.printf("battery %dmV\r\n", battery); 00072 }else 00073 if(txData.data[3] == 0xFC){ 00074 //weatherbeacon 00075 float temp = (float)(txData.data[11]|txData.data[12]<<8)/10; 00076 int16_t pressure = txData.data[14]|txData.data[15]<<8; 00077 uint16_t battery = txData.data[16]|txData.data[17]<<8; 00078 /* 00079 pc.printf("\r\ntemperature is %.1f C\r\n", temp); 00080 pc.printf("humidity %d%%\r\n", txData.data[13]); 00081 if(pressure>0){ 00082 pc.printf("pressure %d hPa\r\n", pressure); 00083 } 00084 */ 00085 pc.printf("START\r\n"); 00086 pc.printf("api.thingspeak.com\r\n"); 00087 pc.printf("/update.json\r\n"); 00088 pc.printf("key=WL5U725HQ30KWELM&field1=%.1f&field2=%d&field3=%d&field4=%d\r\n", temp, txData.data[13], pressure, battery); 00089 pc.printf("END\r\n"); 00090 //START 00091 //HOST 00092 //QUERY 00093 //POST DATA 00094 //END 00095 } 00096 00097 getAddressString(tmp, (uint8_t*)txData.address); 00098 dumpBytes(txData.data, txData.dataLen); 00099 pc.printf("\r\nuploading %d bytes, frame type %02X from %s to the cloud...", txData.dataLen, txData.data[3], tmp); 00100 wait(2); 00101 pc.printf(" OK\r\n"); 00102 } 00103 00104 void updateDevice(const uint8_t *address, uint8_t frameId, const uint8_t *data, uint8_t dataLen){ 00105 00106 //search if there are some devices, find if this already exists 00107 DeviceEntry *_devArr = deviceArray; 00108 for(uint8_t n=0;n<devicesCount;n++){ 00109 if(isAddressEqual(_devArr->address, address)){ 00110 if(_devArr->frameId == frameId){ 00111 //check if frameId is equal 00112 //pc.printf("frameId is equal, skipping\r\n"); 00113 }else{ 00114 _devArr->frameId = frameId; 00115 getAddressString(tmp, address); 00116 pc.printf("\r\nnew frameId %d on %s\r\n", frameId, tmp); 00117 sendToWifi((BLEProtocol::AddressBytes_t*)address, data, dataLen); 00118 } 00119 return; 00120 } 00121 _devArr+=sizeof(DeviceEntry); 00122 } 00123 00124 //create new device (existing device not found or no devices yet) 00125 DeviceEntry *_newDevArr = (DeviceEntry *)malloc(sizeof(DeviceEntry)*(devicesCount+1)); 00126 if(_newDevArr!=NULL){ 00127 if(deviceArray!=NULL){ //resizing 00128 pc.printf("\r\nextending devices array, now size %d, old %d, new %d\r\n", devicesCount+1, deviceArray, _newDevArr); 00129 memcpy(_newDevArr, deviceArray, sizeof(DeviceEntry)*devicesCount); 00130 free(deviceArray); //release old array 00131 00132 }else{ 00133 //initializing 00134 pc.printf("\r\ninitializing dev array\r\n"); 00135 } 00136 00137 devicesCount++; 00138 deviceArray = _newDevArr; //remember new resized array 00139 00140 //put to array 00141 DeviceEntry *newDevEntry = deviceArray + ((devicesCount-1)*sizeof(DeviceEntry)); 00142 memcpy(newDevEntry->address, address, sizeof(BLEProtocol::AddressBytes_t)); 00143 newDevEntry->frameId = frameId; 00144 getAddressString(tmp, address); 00145 pc.printf("address %s and frameId %d registered\r\n", tmp, frameId); 00146 sendToWifi((BLEProtocol::AddressBytes_t*)address, data, dataLen); 00147 00148 }else{ 00149 pc.printf("\r\nmalloc failed\r\n"); 00150 } 00151 } 00152 00153 void onScanResult(const Gap::AdvertisementCallbackParams_t *params){ 00154 uint8_t n; 00155 const uint8_t *data = params->advertisingData; 00156 for(n=0;n<params->advertisingDataLen-1;n++){ 00157 const uint8_t len = data[n]; 00158 const uint8_t tpe = data[n+1]; 00159 //printf("Type %02X, l=%d\r\n", tpe, len); 00160 if(tpe==0xFF){//search for manufacturer-data 00161 if(data[n+2] == 0xFF && data[n+3] == 0xFF && (data[n+4] == 0xFA || data[n+4] == 0xFC || data[n+4] == 0xFC)){ 00162 updateDevice(params->peerAddr, data[n+5], data+n+1, len); 00163 } 00164 } 00165 n+=len; 00166 } 00167 } 00168 00169 int main(void) 00170 { 00171 pc.baud(9600); 00172 ble.init(); 00173 while (ble.hasInitialized() == false) { /* spin loop */ } 00174 ble.gap().setScanParams(1000 /* scan interval */, 1000 /* scan window */); 00175 ble.gap().startScan(onScanResult); 00176 00177 pc.printf("Scan Start \r\n"); 00178 while(1){ 00179 ble.waitForEvent(); 00180 00181 if(txEnabled){ 00182 ble.gap().stopScan(); 00183 sendToWifi(); 00184 txEnabled = false; 00185 ble.gap().startScan(onScanResult); 00186 } 00187 } 00188 } 00189 00190 00191 00192 00193 00194 00195 00196 00197 00198 00199 00200 00201 00202 00203 00204
Generated on Mon Jul 18 2022 19:14:49 by
1.7.2