Blame view

Mbed_compiler/Capteur_pollution/main.cpp 4.28 KB
1b3a0906   mbutaye   Ajout des fichier...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  /* mbed Microcontroller Library
   * Copyright (c) 2006-2013 ARM Limited
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  #include "mbed.h"
  #include "BLEDevice.h"
  #include "PollutionService.h"
  #include "EnvironmentalSensingService.h"
  #include "LocationAndNavigationService.h"
  #include "CurrentTimeService.h"
  #include "BatteryService.h"
  #include "DeviceInformationService.h"
  #include "Utils.h"
  
  BLEDevice  ble;
  DigitalOut led1(LED1);
  
  const static char     DEVICE_NAME[]        = "Pollution";
  static const uint16_t uuid16_list[]        = {0xFFFF,
                                                PollutionService::UUID_POLLUTION_SERVICE,
                                                EnvironmentalSensingService::UUID_ENVIRONMENTAL_SENSING_SERVICE,
                                                LocationAndNavigationService::UUID_LOCATION_AND_NAVIGATION_SERVICE,
                                                CurrentTimeService::UUID_CURRENT_TIME_SERVICE,
                                                GattService::UUID_BATTERY_SERVICE,
                                                GattService::UUID_DEVICE_INFORMATION_SERVICE};
  static volatile bool  triggerSensorPolling = false;
  
  
  void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
  {
      DEBUG("Disconnected!\n\r");
      DEBUG("Restarting the advertising process\n\r");
      ble.startAdvertising(); // restart advertising
  }
  
  void connectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *reason)
  {
      DEBUG("Connected\r\n");
  }
  
  void periodicCallback(void)
  {
      led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
      triggerSensorPolling = true;
  }
  
  int main(void)
  {
      led1 = 1;
      Ticker ticker;
      ticker.attach(periodicCallback, 1);
  
      DEBUG("Initialising \n\r");
      ble.init();
      ble.onDisconnection(disconnectionCallback);
      ble.onConnection(connectionCallback);
  
      /* Setup services. */
      PollutionService         pollutionService(ble, 76);
      EnvironmentalSensingService     environmentalSensingService(ble);
      LocationAndNavigationService locationAndNavigationService(ble, 0x0C0400);
      CurrentTimeService currentTimeService(ble);
      BatteryService           batteryService(ble, 61);
      DeviceInformationService deviceInfoService(ble, "ST", "Nucleo", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
  
      /* Setup advertising. */
      /* BREDR_NOT_SUPPORTED means classic bluetooth not supported;
      * LE_GENERAL_DISCOVERABLE means that this peripheral can be
      * discovered by any BLE scanner--i.e. any phone. */
      ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
      ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
      ble.accumulateAdvertisingPayload(GapAdvertisingData::POLLUTION_PARTICLES_SENSOR);
      ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
      ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
      ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
      ble.startAdvertising();
      
      while (true)
      {
          if (triggerSensorPolling)
          {
              triggerSensorPolling = false;
              pollutionService.updatePollutionLevel(73);
              pollutionService.updateTemperature(25.76);
              environmentalSensingService.updateHumidity(15.2);
              environmentalSensingService.updatePressure(3.5);
              locationAndNavigationService.updateLocationAndSpeed(0x0C00, 4836.5375, 00740.9373, 200.2);
              currentTimeService.updateDateTime(2018, 02, 02, 16, 29, 45);
              currentTimeService.updateDayOfWeek(5);
          }
          else
          {
              ble.waitForEvent();
          }
      }
  }