BLE Lightning sensor for Nordic NRF51822 based module
Dependencies: AS3935 AS3935_ext BLE_API mbed nRF51822 nrf51_rtc
Diff: main.cpp
- Revision:
- 5:b8961da0d06a
- Parent:
- 4:8f815f8d804e
- Child:
- 6:273ddd00bd90
--- a/main.cpp Sun Aug 30 12:20:29 2015 +0000 +++ b/main.cpp Sat Sep 05 05:44:00 2015 +0000 @@ -7,6 +7,7 @@ #include "mbed.h" #include "nrf51_rtc.h" #include "AS3935_ext.h" +#include <string> #define NEED_CONSOLE_OUTPUT 1 // Set this if you need debug messages on the console; #define NEED_BLE_CONSOLE 1 @@ -56,6 +57,18 @@ time = 0; distance = 0; } + + char * events() { + if ( event & 0x4 ) { + return "Lightning interval"; + } else if ( event & 0x2 ) { + return "Disturber deteced"; + } else if ( event & 0x1 ) { + return "Noise level too high"; + } else { + return "Un known event"; + } + } } evrecord[s_evrecord]; time_t example_time() { @@ -77,7 +90,7 @@ char date[24]; strftime(date,sizeof(date),"%H:%M:%S on %m/%d/%G",init_timeinfo); - DEBUG("Initial time set is %s.\r\n",date); + DEBUG("Initial time set as %s.\r\n",date); // compute the proper value for time in time_t type rawtime = mktime(init_timeinfo); @@ -139,22 +152,86 @@ void onDataWritten(const GattWriteCallbackParams *params) { - char buff[21]; + char buff[24]; + int v; + int afe_gb, nf_lev, wdth, srej; + + int write_param=0; + if ((uart != NULL) && (params->handle == uart->getTXCharacteristicHandle())) { uint16_t bytesRead = params->len; - DEBUG("received %u bytes\n\r", bytesRead); strncpy((char*)buff,(char*)params->data,bytesRead); + buff[bytesRead] = 0x00; + + DEBUG("received %u bytes. String '%s'\n\r", bytesRead,buff); // ble.updateCharacteristicValue(uart->getRXCharacteristicHandle(), params->data, bytesRead); // ble.updateCharacteristicValue(uart->getRXCharacteristicHandle(), buff, bytesRead); if ( strncmp("get time",buff,bytesRead) == 0 ) { + char date[30]; + time_t rawtime=rtc.time(); - BLEC("Current Time:%d\n",rawtime); - } else if ( strncmp("get record ",buff,strlen("Get Record ")) == 0 ) { - int i=atoi(&buff[strlen("Get Record ")]); + struct tm * timeinfo= localtime(&rawtime); + strftime(date,sizeof(date),"%G/%m/%d %H:%M:%S",timeinfo); + DEBUG("Current Time:%d %s\r\n",rawtime,date); + + strftime(date,sizeof(date),"%G%m%d%H%M%S",timeinfo); + BLEC("%s\n",date); + + } else if ( strncmp("get record ",buff,strlen("get record ")) == 0 ) { + int i=atoi(&buff[strlen("get record ")]); if ( i >= 0 && i < s_evrecord ) { - BLEC("record %1d:%d:%1x:%2x\n",i,evrecord[i].time,evrecord[i].event,evrecord[i].distance); + char date[24]; + struct tm * timeinfo = localtime(&evrecord[i].time); + strftime(date,sizeof(date),"%G/%m/%d %H:%M:%S",timeinfo); + DEBUG("record %d at %s by %s (%dkm).\r\n",i,date,evrecord[i].events(),evrecord[i].distance); + BLEC("r%1d:%08x:%1x:%02x\n",i,evrecord[i].time,evrecord[i].event,evrecord[i].distance); } + } else if ( strncmp("st ",buff,strlen("st ")) == 0 ) { + struct tm * init_timeinfo; + time_t rawtime; + + // initialize time + init_timeinfo = localtime(&rawtime); // note: must initialize the struct with this before trying to set components + // ...else code goes into the weeds!! + sscanf(buff,"st %4d%2d%2d%2d%2d%2d",&init_timeinfo->tm_year,&init_timeinfo->tm_mon,&init_timeinfo->tm_mday,&init_timeinfo->tm_hour,&init_timeinfo->tm_min,&init_timeinfo->tm_sec); + init_timeinfo->tm_year -= 1900; + init_timeinfo->tm_mon --; + + char date[24]; + strftime(date,sizeof(date),"%G/%m/%d %H:%M:%S",init_timeinfo); + DEBUG("time set is %s.\r\n",date); + + rawtime = mktime(init_timeinfo); + + rtc.set_time(rawtime); + } else if ( strncmp("AFE_GB 0x",buff,strlen("AFE_GB 0x")) == 0 ) { + sscanf(buff,"AFE_GB 0x%2x",&v); + Lightning.registerWrite(AS3935_AFE_GB,v); + write_param = 1; + } else if ( strncmp("NF_LEV 0x",buff,strlen("NF_LEV 0x")) == 0 ) { + sscanf(buff,"NF_LEV 0x%2x",&v); + Lightning.registerWrite(AS3935_NF_LEV,v); + write_param = 1; + } else if ( strncmp("WDTH 0x",buff,strlen("WDTH 0x")) == 0 ) { + sscanf(buff,"WDTH 0x%2x",&v); + Lightning.registerWrite(AS3935_WDTH,v); + write_param = 1; + } else if ( strncmp("SREJ 0x",buff,strlen("SREJ 0x")) == 0 ) { + sscanf(buff,"SREJ 0x%2x",&v); + Lightning.registerWrite(AS3935_SREJ,v); + write_param = 1; + } else if ( strncmp("get param",buff,strlen("get param")) == 0 ) { + write_param = 1; + } + + if ( write_param != 0 ) { + afe_gb = Lightning.registerRead(AS3935_AFE_GB); + nf_lev = Lightning.registerRead(AS3935_NF_LEV); + wdth = Lightning.registerRead(AS3935_WDTH); + srej = Lightning.registerRead(AS3935_SREJ); + DEBUG("AFE_GB=0x%02x,NF_LEV=0x%02x,WDTH=0x%02x,SREJ=0x%02x\r\n",afe_gb, nf_lev, wdth, srej); + BLEC("%02x,%1x,%1x,%1x\n",afe_gb, nf_lev, wdth, srej); } } } @@ -167,17 +244,17 @@ time_t rawtime=rtc.time(); struct tm * timeinfo; timeinfo = localtime(&rawtime); - char date[24],sdate[9]; + char date[24];// ,sdate[9]; int distance; strftime(date,sizeof(date),"%H:%M:%S on %m/%d/%G",timeinfo); - strftime(sdate,sizeof(sdate),"%H%M%S",timeinfo); + // strftime(sdate,sizeof(sdate),"%H%M%S",timeinfo); wait_ms(2); //on attend 2ms préconisation constructeur OriginInt = Lightning.interruptSource(); distance = Lightning.lightningDistanceKm(); - for(int i=1;i<s_evrecord;i++) { + for(int i=s_evrecord-1;i>0;i--) { evrecord[i] = evrecord[i-1]; } evrecord[0].time = rawtime; @@ -226,9 +303,19 @@ Lightning.setTuneCap(5); // Tuning Parameter DEBUG("powerup\r\n"); Lightning.powerUp(); - + + // AFE Gain (default Inddor:0x12 Outdoor:0x0es) DEBUG("set Indoor Mode as 0x0d\r\n"); Lightning.registerWrite(AS3935_AFE_GB,0x0d); + + // Set NF_LEV (default = 2) (0..0x07) + DEBUG("set Noise Filter as 0x02\r\n"); + Lightning.registerWrite(AS3935_NF_LEV,0x02); + + // Set WDTH (default = 2) (0..0x0f) + DEBUG("set WDTH as 0x02\r\n"); + Lightning.registerWrite(AS3935_WDTH,0x02); + DEBUG("Auto Calibration Start\r\n"); float minerr = 100;