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

Committer:
f3d
Date:
Sat Aug 22 15:21:53 2020 +0000
Revision:
0:6d4b6c56a7ad
Child:
1:ab9f0e572c98
Initial commit with basic BLE running

Who changed what in which revision?

UserRevisionLine numberNew contents of line
f3d 0:6d4b6c56a7ad 1 /* mbed Microcontroller Library
f3d 0:6d4b6c56a7ad 2 * Copyright (c) 2006-2015 ARM Limited
f3d 0:6d4b6c56a7ad 3 *
f3d 0:6d4b6c56a7ad 4 * Licensed under the Apache License, Version 2.0 (the "License");
f3d 0:6d4b6c56a7ad 5 * you may not use this file except in compliance with the License.
f3d 0:6d4b6c56a7ad 6 * You may obtain a copy of the License at
f3d 0:6d4b6c56a7ad 7 *
f3d 0:6d4b6c56a7ad 8 * http://www.apache.org/licenses/LICENSE-2.0
f3d 0:6d4b6c56a7ad 9 *
f3d 0:6d4b6c56a7ad 10 * Unless required by applicable law or agreed to in writing, software
f3d 0:6d4b6c56a7ad 11 * distributed under the License is distributed on an "AS IS" BASIS,
f3d 0:6d4b6c56a7ad 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
f3d 0:6d4b6c56a7ad 13 * See the License for the specific language governing permissions and
f3d 0:6d4b6c56a7ad 14 * limitations under the License.
f3d 0:6d4b6c56a7ad 15 */
f3d 0:6d4b6c56a7ad 16
f3d 0:6d4b6c56a7ad 17 #include "mbed.h"
f3d 0:6d4b6c56a7ad 18
f3d 0:6d4b6c56a7ad 19 #include "ble/services/iBeacon.h"
f3d 0:6d4b6c56a7ad 20
f3d 0:6d4b6c56a7ad 21 BLE ble;
f3d 0:6d4b6c56a7ad 22 const static char DEVICE_NAME[] = "NRF52832";
f3d 0:6d4b6c56a7ad 23 static const uint16_t uuid16_list[] = {0xabcd,0x1234};
f3d 0:6d4b6c56a7ad 24 DigitalOut Led(P0_25);
f3d 0:6d4b6c56a7ad 25 Ticker ticker;
f3d 0:6d4b6c56a7ad 26 void periodicCallback(void)
f3d 0:6d4b6c56a7ad 27 {
f3d 0:6d4b6c56a7ad 28
f3d 0:6d4b6c56a7ad 29 if (Led == 0)
f3d 0:6d4b6c56a7ad 30 Led = 1;
f3d 0:6d4b6c56a7ad 31 else
f3d 0:6d4b6c56a7ad 32 Led = 0;
f3d 0:6d4b6c56a7ad 33
f3d 0:6d4b6c56a7ad 34 }
f3d 0:6d4b6c56a7ad 35
f3d 0:6d4b6c56a7ad 36 /**
f3d 0:6d4b6c56a7ad 37 * This callback allows the LEDService to receive updates to the ledState Characteristic.
f3d 0:6d4b6c56a7ad 38 *
f3d 0:6d4b6c56a7ad 39 * @param[in] params
f3d 0:6d4b6c56a7ad 40 * Information about the characterisitc being updated.
f3d 0:6d4b6c56a7ad 41 */
f3d 0:6d4b6c56a7ad 42 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
f3d 0:6d4b6c56a7ad 43
f3d 0:6d4b6c56a7ad 44 }
f3d 0:6d4b6c56a7ad 45
f3d 0:6d4b6c56a7ad 46 void onDataReadCallback(const GattReadCallbackParams *params) {
f3d 0:6d4b6c56a7ad 47
f3d 0:6d4b6c56a7ad 48
f3d 0:6d4b6c56a7ad 49 }
f3d 0:6d4b6c56a7ad 50 /**
f3d 0:6d4b6c56a7ad 51 * This function is called when the ble initialization process has failed
f3d 0:6d4b6c56a7ad 52 */
f3d 0:6d4b6c56a7ad 53 void onBleInitError(BLE &ble, ble_error_t error)
f3d 0:6d4b6c56a7ad 54 {
f3d 0:6d4b6c56a7ad 55 /* Initialization error handling should go here */
f3d 0:6d4b6c56a7ad 56 }
f3d 0:6d4b6c56a7ad 57
f3d 0:6d4b6c56a7ad 58 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
f3d 0:6d4b6c56a7ad 59 {
f3d 0:6d4b6c56a7ad 60 BLE::Instance().gap().startAdvertising();
f3d 0:6d4b6c56a7ad 61 }
f3d 0:6d4b6c56a7ad 62 /**
f3d 0:6d4b6c56a7ad 63 * Callback triggered when the ble initialization process has finished
f3d 0:6d4b6c56a7ad 64 */
f3d 0:6d4b6c56a7ad 65 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
f3d 0:6d4b6c56a7ad 66 {
f3d 0:6d4b6c56a7ad 67 BLE& ble = params->ble;
f3d 0:6d4b6c56a7ad 68 ble_error_t error = params->error;
f3d 0:6d4b6c56a7ad 69
f3d 0:6d4b6c56a7ad 70 if (error != BLE_ERROR_NONE) {
f3d 0:6d4b6c56a7ad 71 /* In case of error, forward the error handling to onBleInitError */
f3d 0:6d4b6c56a7ad 72 onBleInitError(ble, error);
f3d 0:6d4b6c56a7ad 73 return;
f3d 0:6d4b6c56a7ad 74 }
f3d 0:6d4b6c56a7ad 75
f3d 0:6d4b6c56a7ad 76 /* Ensure that it is the default instance of BLE */
f3d 0:6d4b6c56a7ad 77 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
f3d 0:6d4b6c56a7ad 78 return;
f3d 0:6d4b6c56a7ad 79 }
f3d 0:6d4b6c56a7ad 80
f3d 0:6d4b6c56a7ad 81 ble.gap().onDisconnection(disconnectionCallback);
f3d 0:6d4b6c56a7ad 82 ble.gattServer().onDataWritten(onDataWrittenCallback);
f3d 0:6d4b6c56a7ad 83 ble.gattServer().onDataRead(onDataReadCallback);
f3d 0:6d4b6c56a7ad 84
f3d 0:6d4b6c56a7ad 85 /* setup advertising */
f3d 0:6d4b6c56a7ad 86 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
f3d 0:6d4b6c56a7ad 87 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
f3d 0:6d4b6c56a7ad 88 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
f3d 0:6d4b6c56a7ad 89 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
f3d 0:6d4b6c56a7ad 90 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
f3d 0:6d4b6c56a7ad 91 ble.gap().startAdvertising();
f3d 0:6d4b6c56a7ad 92 }
f3d 0:6d4b6c56a7ad 93 int main(void)
f3d 0:6d4b6c56a7ad 94 {
f3d 0:6d4b6c56a7ad 95 ticker.attach(periodicCallback, 0.1); /* Poll devices every 100ms */
f3d 0:6d4b6c56a7ad 96 ble.init(bleInitComplete);
f3d 0:6d4b6c56a7ad 97
f3d 0:6d4b6c56a7ad 98 /* SpinWait for initialization to complete. This is necessary because the
f3d 0:6d4b6c56a7ad 99 * BLE object is used in the main loop below. */
f3d 0:6d4b6c56a7ad 100 while (!ble.hasInitialized()) { /* spin loop */ }
f3d 0:6d4b6c56a7ad 101 while (true) {
f3d 0:6d4b6c56a7ad 102 ble.waitForEvent(); // allows or low power operation
f3d 0:6d4b6c56a7ad 103 }
f3d 0:6d4b6c56a7ad 104 }