-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_sensors.ino
67 lines (55 loc) · 1.54 KB
/
arduino_sensors.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
#include <SoftwareSerial.h>
/* Define receive and transmit pins */
#define rxPin 2
#define txPin 3
SoftwareSerial esp(rxPin , txPin);
/* Sends aggregated data every (DELAY * COUNTER) miliseconds */
const int DELAY = 1000;
const int COUNTER = 10;
/* Analog read pins */
const int lm35_pin = A0;
const int LDR_pin = A1;
void setup() {
Serial.begin(115200);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
esp.begin(9600);
}
int counter = 0;
float totalTemp = 0;
float totalLDR = 0;
void loop() {
int temp_adc_val;
float tempVal;
float LDRVal;
float aggrTemp;
float aggrLDR;
LDRVal = analogRead(LDR_pin); /* Read LDR */
temp_adc_val = analogRead(lm35_pin); /* Read Temperature */
tempVal = (temp_adc_val * 4.88); /* Convert adc value to equivalent voltage */
tempVal = (tempVal/10); /* LM35 gives output of 10mv/°C */
if (counter >= COUNTER) {
aggrTemp = totalTemp / float(COUNTER);
aggrLDR = totalLDR / float(COUNTER);
String tempString = String(aggrTemp);
String LDRString = String(aggrLDR);
String msg = String(tempString + "," + LDRString);
Serial.println("Sent message : " + msg);
esp.println(msg);
counter = 0;
totalTemp = 0;
totalLDR = 0;
}
else if (counter < COUNTER) {
totalTemp += tempVal;
totalLDR += LDRVal;
Serial.print("Read Values: Temperature = ");
Serial.print(totalTemp);
Serial.print(" Degree Celsius \n");
Serial.print("Read Values: LDR = ");
Serial.print(totalLDR);
Serial.print("\n");
counter += 1;
}
delay(DELAY);
}