This project can be used to measure UV light levels. It uses an ML8511 UV sensors and outputs its value over BLE in units of mW/cm^2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "UVSensor.h"
00019 
00020 #include <BLE.h>
00021 
00022 BLE ble;
00023 
00024 const static char     DEVICE_NAME[] = "NRF52832";
00025 static const uint16_t uuid16_list[] = {UVService::UV_SERVICE_UUID};    
00026 DigitalOut Led(P0_25);
00027 Ticker ticker;
00028 UVService *UVServicePtr;
00029 void periodicCallback(void)
00030 {
00031       
00032     UVServicePtr->poll();
00033     if (Led == 0)
00034         Led = 1;
00035     else
00036         Led = 0;
00037 }
00038 
00039 /**
00040  * This callback allows the LEDService to receive updates to the ledState Characteristic.
00041  *
00042  * @param[in] params
00043  *     Information about the characterisitc being updated.
00044  */
00045 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
00046     
00047 }
00048 
00049 void onDataReadCallback(const GattReadCallbackParams *params) {
00050   
00051 
00052 }
00053 /**
00054  * This function is called when the ble initialization process has failed
00055  */
00056 void onBleInitError(BLE &ble, ble_error_t error)
00057 {
00058     /* Initialization error handling should go here */
00059 }
00060 
00061 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00062 {
00063     BLE::Instance().gap().startAdvertising();
00064 }
00065 /**
00066  * Callback triggered when the ble initialization process has finished
00067  */
00068 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00069 {
00070     BLE&        ble   = params->ble;
00071     ble_error_t error = params->error;
00072 
00073     if (error != BLE_ERROR_NONE) {
00074         /* In case of error, forward the error handling to onBleInitError */
00075         onBleInitError(ble, error);
00076         return;
00077     }
00078 
00079     /* Ensure that it is the default instance of BLE */
00080     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00081         return;
00082     }
00083  
00084     ble.gap().onDisconnection(disconnectionCallback);
00085     ble.gattServer().onDataWritten(onDataWrittenCallback);
00086     ble.gattServer().onDataRead(onDataReadCallback); 
00087     uint16_t initial_value = 0;
00088     UVServicePtr = new UVService(ble,initial_value);
00089 
00090     /* setup advertising */
00091     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00092     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00093     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00094     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00095     ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
00096     ble.gap().startAdvertising();
00097 }
00098 int main(void)
00099 {
00100     ble.init(bleInitComplete);    
00101     /* SpinWait for initialization to complete. This is necessary because the
00102      * BLE object is used in the main loop below. */
00103     while (!ble.hasInitialized()) { /* spin loop */ }  
00104     ticker.attach(periodicCallback, 0.1); /* Poll devices every 100ms */
00105     while (true) {
00106         ble.waitForEvent(); // allows or low power operation            
00107     }
00108 }