1
+ /* ********************************************************************
2
+ Learn Guide: BLE Temperature Monitoring Armband
3
+
4
+ Adafruit invests time and resources providing this open source code,
5
+ please support Adafruit and open-source hardware by purchasing
6
+ products from Adafruit!
7
+
8
+ MIT license, check LICENSE for more information
9
+ All text above, and the splash screen below must be included in
10
+ any redistribution
11
+ *********************************************************************/
12
+ #include < bluefruit.h>
13
+ #include < Adafruit_LittleFS.h>
14
+ #include < InternalFileSystem.h>
15
+ #include < Wire.h>
16
+ #include " Adafruit_MCP9808.h"
17
+
18
+ // Read temperature in degrees Fahrenheit
19
+ #define TEMPERATURE_F
20
+ // uncomment the following line if you want to read temperature in degrees Celsius
21
+ // #define TEMPERATURE_C
22
+
23
+ // Sensor read timeout, in minutes
24
+ // NOTE: Measuring your armpit temperature for a minimum
25
+ // of 12 minutes is equivalent to measuring your core body temperature.
26
+ const long interval = 60000 ;
27
+
28
+ // last time the temperature was read
29
+ unsigned long prv_ms = 0 ;
30
+
31
+ // BLE Service
32
+ BLEDfu bledfu; // OTA DFU service
33
+ BLEDis bledis; // device information
34
+ BLEUart bleuart; // uart over ble
35
+ BLEBas blebas; // battery
36
+
37
+ // Create the MCP9808 temperature sensor object
38
+ Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
39
+
40
+
41
+ void setup () {
42
+ Serial.begin (115200 );
43
+ Serial.println (" Wearable BlueFruit Temperature Sensor" );
44
+ Serial.println (" -------------------------------------\n " );
45
+
46
+
47
+ if (!tempsensor.begin (0x18 )) {
48
+ Serial.println (" Couldn't find MCP9808! Check your connections and verify the address is correct." );
49
+ while (1 );
50
+ }
51
+ Serial.println (" Found MCP9808!" );
52
+
53
+ // Sets the resolution of reading
54
+ tempsensor.setResolution (3 );
55
+
56
+ // Configure BLE
57
+ // Setup the BLE LED to be enabled on CONNECT
58
+ // Note: This is actually the default behaviour, but provided
59
+ // here in case you want to control this LED manually via PIN 19
60
+ Bluefruit.autoConnLed (true );
61
+
62
+ // Config the peripheral connection with maximum bandwidth
63
+ Bluefruit.configPrphBandwidth (BANDWIDTH_MAX);
64
+
65
+ Bluefruit.begin ();
66
+ Bluefruit.setTxPower (4 ); // Check bluefruit.h for supported values
67
+ Bluefruit.setName (" Bluefruit52" );
68
+ Bluefruit.Periph .setConnectCallback (connect_callback);
69
+ Bluefruit.Periph .setDisconnectCallback (disconnect_callback);
70
+
71
+ // To be consistent OTA DFU should be added first if it exists
72
+ bledfu.begin ();
73
+
74
+ // Configure and Start Device Information Service
75
+ bledis.setManufacturer (" Adafruit Industries" );
76
+ bledis.setModel (" Bluefruit Feather52" );
77
+ bledis.begin ();
78
+
79
+ // Configure and Start BLE Uart Service
80
+ bleuart.begin ();
81
+
82
+ // Start BLE Battery Service
83
+ blebas.begin ();
84
+ blebas.write (100 );
85
+
86
+ // Set up and start advertising
87
+ startAdv ();
88
+
89
+ Serial.println (" Please use Adafruit's Bluefruit LE app to connect in UART mode" );
90
+ Serial.println (" Once connected, enter character(s) that you wish to send" );
91
+
92
+ }
93
+
94
+ void loop () {
95
+
96
+ unsigned long current_ms = millis ();
97
+
98
+ if (current_ms - prv_ms >= (1000 *5 )) {
99
+ prv_ms = current_ms;
100
+
101
+ // wakes up MCP9808 - power consumption ~200 mikro Ampere
102
+ Serial.println (" Wake up MCP9808" );
103
+ tempsensor.wake ();
104
+
105
+ // read and print the temperature
106
+ Serial.print (" Temp: " );
107
+ #ifdef TEMPERATURE_F
108
+ float temp = tempsensor.readTempF ();
109
+ Serial.print (temp);
110
+ Serial.println (" *F." );
111
+ #endif
112
+
113
+ #ifdef TEMPERATURE_C
114
+ float temp = tempsensor.readTempC ();
115
+ Serial.print (temp);
116
+ Serial.println (" *C." );
117
+ #endif
118
+
119
+ char buffer [8 ];
120
+ // itoa(temp, buffer, 10);
121
+ snprintf (buffer, sizeof (buffer) - 1 , " %0.*f" , 2 , temp);
122
+ bleuart.write (buffer);
123
+ bleuart.write (" \n " );
124
+
125
+ // shutdown MSP9808 - power consumption ~0.1 mikro Ampere
126
+ // stops temperature sampling
127
+ Serial.println (" Shutting down MCP9808" );
128
+ tempsensor.shutdown_wake (1 );
129
+ }
130
+
131
+ }
132
+
133
+
134
+ void startAdv (void )
135
+ {
136
+ // Advertising packet
137
+ Bluefruit.Advertising .addFlags (BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
138
+ Bluefruit.Advertising .addTxPower ();
139
+
140
+ // Include bleuart 128-bit uuid
141
+ Bluefruit.Advertising .addService (bleuart);
142
+
143
+ // Secondary Scan Response packet (optional)
144
+ // Since there is no room for 'Name' in Advertising packet
145
+ Bluefruit.ScanResponse .addName ();
146
+
147
+ /* Start Advertising
148
+ * - Enable auto advertising if disconnected
149
+ * - Interval: fast mode = 20 ms, slow mode = 152.5 ms
150
+ * - Timeout for fast mode is 30 seconds
151
+ * - Start(timeout) with timeout = 0 will advertise forever (until connected)
152
+ *
153
+ * For recommended advertising interval
154
+ * https://developer.apple.com/library/content/qa/qa1931/_index.html
155
+ */
156
+ Bluefruit.Advertising .restartOnDisconnect (true );
157
+ Bluefruit.Advertising .setInterval (32 , 244 ); // in unit of 0.625 ms
158
+ Bluefruit.Advertising .setFastTimeout (30 ); // number of seconds in fast mode
159
+ Bluefruit.Advertising .start (0 ); // 0 = Don't stop advertising after n seconds
160
+ }
161
+
162
+ // callback invoked when central connects
163
+ void connect_callback (uint16_t conn_handle)
164
+ {
165
+ // Get the reference to current connection
166
+ BLEConnection* connection = Bluefruit.Connection (conn_handle);
167
+
168
+ char central_name[32 ] = { 0 };
169
+ connection->getPeerName (central_name, sizeof (central_name));
170
+
171
+ Serial.print (" Connected to " );
172
+ Serial.println (central_name);
173
+ }
174
+
175
+ /* *
176
+ * Callback invoked when a connection is dropped
177
+ * @param conn_handle connection where this event happens
178
+ * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
179
+ */
180
+ void disconnect_callback (uint16_t conn_handle, uint8_t reason)
181
+ {
182
+ (void ) conn_handle;
183
+ (void ) reason;
184
+
185
+ Serial.println ();
186
+ Serial.print (" Disconnected, reason = 0x" ); Serial.println (reason, HEX);
187
+ }
0 commit comments