BLE Lightning sensor for Nordic NRF51822 based module
Dependencies: AS3935 AS3935_ext BLE_API mbed nRF51822 nrf51_rtc
main.cpp
- Committer:
- takafuminaka
- Date:
- 2015-08-30
- Revision:
- 4:8f815f8d804e
- Parent:
- 3:2ea547dab8a8
- Child:
- 5:b8961da0d06a
File content as of revision 4:8f815f8d804e:
/* */ #include "string.h" #include "mbed.h" #include "nrf51_rtc.h" #include "AS3935_ext.h" #define NEED_CONSOLE_OUTPUT 1 // Set this if you need debug messages on the console; #define NEED_BLE_CONSOLE 1 #if NEED_CONSOLE_OUTPUT Serial pc(USBTX, USBRX); #define DEBUG(...) { pc.printf(__VA_ARGS__); } #else #define DEBUG(...) /* nothing */ #endif /* #if NEED_CONSOLE_OUTPUT */ #if NEED_BLE_CONSOLE #include "BLE.h" #include "UARTService.h" #define BLEC(...) { char __blecstr[32]; sprintf(__blecstr,__VA_ARGS__); if (uart) uart->write(__blecstr, strlen(__blecstr)); } #else #define BLEC(...) /* nothing */ #endif /* #if NEED_BLE_CONSOLE */ #if NEED_BLE_CONSOLE // Prepare BLE device const uint8_t DEVICE_NAME[] = "BLE Ligttning Sensor"; #endif // #if NEED_BLE_CONSOLE // Prepare LED device DigitalOut led1(LED1); DigitalOut led2(LED2); AS3935_ext Lightning(I2C_SDA0,I2C_SCL0,0x00,P0_23); InterruptIn IntLightning(P0_23); //IRQ AS3935 // used for the example only, not required for rtc use DigitalIn button1(BUTTON1); // used to trigger the time report InterruptIn button1Press(BUTTON1); // event record buffer struct const int s_evrecord=10; struct t_evrecord { public: int event; time_t time; int distance; t_evrecord() { event = 0; time = 0; distance = 0; } } evrecord[s_evrecord]; time_t example_time() { // set an intial time // ...not really necessary for this example, but it beats setting it to 0 or some non-obvious large integer (# of seconds since 1/1/1970) time_t rawtime=0; struct tm * init_timeinfo; // initialize time init_timeinfo = localtime(&rawtime); // note: must initialize the struct with this before trying to set components // ...else code goes into the weeds!! init_timeinfo->tm_sec = 0; init_timeinfo->tm_min = 0; init_timeinfo->tm_hour = 0; init_timeinfo->tm_mon = 0; init_timeinfo->tm_mday = 1; init_timeinfo->tm_year = 70; 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); // compute the proper value for time in time_t type rawtime = mktime(init_timeinfo); return rawtime; } void print_time() { // called when a button is pushed, this prints the current time to the USB-connected console time_t rawtime=rtc.time(); // massage the time into a human-friendly format for printing struct tm * timeinfo; timeinfo = localtime(&rawtime); char date[24]; strftime(date,sizeof(date),"%H:%M:%S on %m/%d/%G",timeinfo); DEBUG("The current time is %s.(%d)\r\n",date,rawtime); } void periodic_update() { // for use as interrupt routine, to insure that RTC is updated periodically // ...if rtc is not read before the underlying counter rolls over (typically 512 seconds), the RTC value will be wrong // ...ideally this would be done as part of the nrf51_rtc method, but I couldn't get it to behave (see nrf51_rtc.cpp for details) rtc.time(); Lightning.lightningDistanceKm(); led1 = !led1; // print_time(); } #if NEED_BLE_CONSOLE BLEDevice ble; UARTService *uart; static Gap::ConnectionParams_t connectionParams; int buff_flash_flag; void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) // Mod { DEBUG("Disconnected handle %u!\n\r", handle); DEBUG("Restarting the advertising process\n\r"); led2 = 0; ble.gap().startAdvertising(); } void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params) { DEBUG("connected. Got handle %u\r\n", params->handle); connectionParams.slaveLatency = 1; led2 = 1; if (ble.gap().updateConnectionParams(params->handle, &connectionParams) != BLE_ERROR_NONE) { DEBUG("failed to update connection paramter\r\n"); } buff_flash_flag = 1; } void onDataWritten(const GattWriteCallbackParams *params) { char buff[21]; 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); // ble.updateCharacteristicValue(uart->getRXCharacteristicHandle(), params->data, bytesRead); // ble.updateCharacteristicValue(uart->getRXCharacteristicHandle(), buff, bytesRead); if ( strncmp("get time",buff,bytesRead) == 0 ) { 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 ")]); if ( i >= 0 && i < s_evrecord ) { BLEC("record %1d:%d:%1x:%2x\n",i,evrecord[i].time,evrecord[i].event,evrecord[i].distance); } } } } #endif // if NEED_BLE_CONSOLE void DetectLightning() { char OriginInt; time_t rawtime=rtc.time(); struct tm * timeinfo; timeinfo = localtime(&rawtime); 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); wait_ms(2); //on attend 2ms préconisation constructeur OriginInt = Lightning.interruptSource(); distance = Lightning.lightningDistanceKm(); for(int i=1;i<s_evrecord;i++) { evrecord[i] = evrecord[i-1]; } evrecord[0].time = rawtime; evrecord[0].event = OriginInt; evrecord[0].distance = distance; if (OriginInt == 1) { led2 = !led2; DEBUG("%24s : Noise level too high. %d km\r\n",date,distance); } if (OriginInt == 4) { led2 = !led2; DEBUG("%24s : Disturber detected. %d km\r\n",date,distance); } if (OriginInt == 8) { led2 = !led2; DEBUG("%24s : Lightning interrupt %d km\r\n",date,distance); } } int main(void) { #if NEED_BLE_CONSOLE ble.init(); ble.gap().onDisconnection(disconnectionCallback); ble.gap().onConnection(onConnectionCallback); ble.gattServer().onDataWritten(onDataWritten); ble.gap().getPreferredConnectionParams(&connectionParams); uart = new UARTService(ble); buff_flash_flag = 0; #endif // if NEED_BLE_CONSOLE led1=0; led2=0; int hz=0; //initialisations wait(1); DEBUG("reset\r\n"); Lightning.reset(); DEBUG("setTuneCap as 5\r\n"); Lightning.setTuneCap(5); // Tuning Parameter DEBUG("powerup\r\n"); Lightning.powerUp(); DEBUG("set Indoor Mode as 0x0d\r\n"); Lightning.registerWrite(AS3935_AFE_GB,0x0d); DEBUG("Auto Calibration Start\r\n"); float minerr = 100; int fincap = 7; for(int i=0;i<16;i++) { Lightning.setTuneCap(i); // Tuning Parameter hz = Lightning.MeasureLCOFreq(); float err = (hz-500000.)/500000.*100.; DEBUG("%d : hz=%10d Hz (%5.2f%%)\r\n",i,hz,err); if ( abs(err) < minerr ) { minerr = abs(err); fincap = i; } } Lightning.setTuneCap(fincap); // Tuning Parameter wait_ms(100); hz = Lightning.MeasureLCOFreq(); float err = (hz-500000.)/500000.*100.; DEBUG("Final %d : hz=%10d Hz (%5.2f%%)\r\n",fincap,hz,err); BLEC("%1x:%10dHz:%5.2f:final\n",fincap,hz,err); DEBUG("Auto Calibration finished\r\n"); // user selectable, any time < 512 seconds is OK #define PERIODIC_UPDATE 1 Ticker rtc_ticker; rtc_ticker.attach(&periodic_update, PERIODIC_UPDATE); time_t initial_time = example_time(); rtc.set_time(initial_time); button1Press.fall(&print_time); // when button1 is pressed, this calls rtc.time() and prints it IntLightning.rise(&DetectLightning); #if NEED_BLE_CONSOLE /* setup advertising */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, DEVICE_NAME, sizeof(DEVICE_NAME) - 1); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); ble.gap().setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ ble.gap().startAdvertising(); #endif // #if NEED_BLE_CONSOLE while (true) { #if NEED_BLE_CONSOLE ble.waitForEvent(); #endif // #if NEED_BLE_CONSOLE } }