-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOne_Click_Code.ino
159 lines (129 loc) · 3.69 KB
/
One_Click_Code.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <RBL_nRF8001.h>
#include <RBL_services.h>
#include <math.h>
/**
* One_Click Arudino Code
* @data 4/14/16
* @author Matthew Joyce, Ben Forman
*/
#include <SPI.h>
#define analogPin A0 //the thermistor attach to
#define photocellPin A1 //photoresistor attach to A0
#define beta 4090 //the beta of the thermistor
#define resistance 10 //the value of the pull-down resistor
#define keyPin 12 //Button Pin
#define ledPin 13 //LED PIN
#define BLE_NAME "BLE SHEILD"
int photocellData = 0; // value read from the sensor
int serviceStarted = 0;
char thermisterArray[6];
/**
* setup()
*
* First method called when microcontroller is powered up.
* Sets up serial communication and button pin and led pin
*/
void setup()
{
Serial.begin(57600);
Serial.print("In setup");
pinMode(keyPin,INPUT); //initialize the key pin as input
pinMode(ledPin,OUTPUT); //initialize the led pin as output
}
/**
* startBLESerivce()
*
* Starts the BLE service and sets the name to BLE Shield.
*/
void startBLEService()
{
ble_set_name("BLE Shield");
ble_begin();
serviceStarted = 1;
}
/**
* readFromPhotoCell()
*
* Reads a analog value from the photocell.
*/
void readFromPhotoCell()
{
Serial.println("In read from photo cell");
photocellData = analogRead(photocellPin); //read the value of A0
Serial.print("Photo Resister data: ");
Serial.println(photocellData);
}
/**
* readFromThermister()
*
* Reads a anolog values from the thermister. Converts it from C to F then to an ascii string.
*/
void readFromThermister()
{
Serial.println("in read from thermister");
long thermisterData = analogRead(analogPin);
//the calculating formula of temperature
float tempC = beta /(log((1025.0 * 10 / thermisterData - 10) / 10) + beta / 298.0) - 273.0;
float tempF = 1.8 * tempC + 32.0;
dtostrf(tempF, 4, 2, thermisterArray);
//Serial.print("TempC: ");//print" TempC: "
//Serial.print(tempC);//print Celsius temperature
//Serial.print(" C");//print the unit
//Serial.println();
Serial.print(" TempF: ");
Serial.print(tempF);
Serial.print(" ");
Serial.print(thermisterArray);
//Serial.print(" F");
Serial.println();
}
/**
* loop()
*
* Infinite loop that will check if button has been pressed. If so the BLE serivce is started. The microcontroller
* will wait for the iPhone to connect and received a one before dumping its data.
*/
void loop()
{
if (digitalRead(keyPin) == HIGH)
{
Serial.println("Button Pressed, Starting BLE Service");
digitalWrite(ledPin,HIGH); //turn on the led
delay(1000);
startBLEService();
}
if (ble_connected() && serviceStarted)
{
if (ble_available() > 0)
{
int value = ble_read();
Serial.println("Read value: ");
Serial.println(value);
if (value == 49)
{
readFromPhotoCell();
readFromThermister();
Serial.println("Connected to Iphone and received 1");
Serial.println("Dumping Data....");
Serial.print("thermisterArray ");
Serial.print(thermisterArray);
ble_write(thermisterArray[0]);
ble_write(thermisterArray[1]);
ble_write(thermisterArray[2]);
ble_write(thermisterArray[3]);
ble_write(thermisterArray[4]);
ble_do_events();
int temp = (photocellData >> 8 ) & 0xff;
Serial.print("Low bit: ");
Serial.print(temp);
ble_write(temp);
temp = (photocellData) & 0xff;
Serial.print(" High byte: ");
Serial.print(temp);
ble_write(temp);
// serviceStarted = 0;
}
}
}
ble_do_events();
}