-
Notifications
You must be signed in to change notification settings - Fork 7
/
Thermometer_v2.ino
71 lines (57 loc) · 2.28 KB
/
Thermometer_v2.ino
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
// Broadcast the temperature Characteristic
// Note there is a bug on the Arduino 101 that prevents connections when broadcasting https://github.com/arduino/ArduinoCore-arc32/issues/583
// Use https://github.com/sandeepmistry/arduino-BLEPeripheral if you need broadcast & connection
#include <CurieBLE.h>
BLEPeripheral blePeripheral;
BLEService thermometerService = BLEService("BBB0");
BLEFloatCharacteristic temperatureCharacteristic = BLEFloatCharacteristic("BBB1", BLERead | BLENotify | BLEBroadcast);
BLEDescriptor temperatureDescriptor = BLEDescriptor("2901", "degrees C");
#define TEMPERATURE_PIN A0
unsigned long previousMillis = 0; // will store last time temperature was updated
unsigned short interval = 2000; // interval at which to read temperature (milliseconds)
void setup()
{
Serial.begin(9600);
Serial.println(F("Bluetooth Low Energy Thermometer"));
// set advertised name and service
blePeripheral.setLocalName("Thermometer");
blePeripheral.setDeviceName("Thermometer");
blePeripheral.setAdvertisedServiceUuid(thermometerService.uuid());
// add service and characteristic
blePeripheral.addAttribute(thermometerService);
blePeripheral.addAttribute(temperatureCharacteristic);
blePeripheral.addAttribute(temperatureDescriptor);
temperatureCharacteristic.broadcast();
blePeripheral.begin();
}
void loop()
{
// Tell the bluetooth radio to do whatever it should be working on
blePeripheral.poll();
// limit how often we read the sensor
if(millis() - previousMillis > interval) {
pollTemperatureSensor();
previousMillis = millis();
}
}
void pollTemperatureSensor()
{
float temperature = calculateTemperature();
// only set the characteristic value if the temperature has changed
if (temperatureCharacteristic.value() != temperature) {
temperatureCharacteristic.setValue(temperature);
Serial.println(temperature);
}
}
// calculate the temperature from the sensor reading
// https://learn.adafruit.com/tmp36-temperature-sensor/using-a-temp-sensor
float calculateTemperature()
{
// read the sensor value
int sensorValue = analogRead(TEMPERATURE_PIN);
// 3.3v logic, 10-bit ADC
float voltage = sensorValue * 3.3 / 1024.0;
// 100 degrees per volt with 0.5 volt offset
float temperature = (voltage - 0.5) * 100;
return temperature;
}