-
Notifications
You must be signed in to change notification settings - Fork 0
/
WateringSystem_Arduino.ino
53 lines (48 loc) · 1.15 KB
/
WateringSystem_Arduino.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
#include<SoftwareSerial.h>
SoftwareSerial BLE_DEVICE(2, 3);
#define relayPin 7
#define moistureSensorPin A4
#define lightmoistureSensorPin A1
int moisture = 0;
int light = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Started");
BLE_DEVICE.begin(9600);
pinMode(relayPin, OUTPUT);
// Make sure relay is off initially
digitalWrite(relayPin, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
sendMoistureData();
sendLightData();
relayControl();
delay(5000);
}
// Normalize to 8bit data at receiver
int normalize(int val) {
return 256 * val / 1024;
}
void sendMoistureData() {
moisture = analogRead(moistureSensorPin);
BLE_DEVICE.write(normalize(moisture));
}
void sendLightData() {
light = analogRead(lightmoistureSensorPin);
BLE_DEVICE.write(normalize(light));
}
void relayControl() {
if (BLE_DEVICE.available() > 0) {
Serial.println(BLE_DEVICE.available());
char rec = char(BLE_DEVICE.read());
if (rec == '1') {
digitalWrite(relayPin, LOW);
} else{
digitalWrite(relayPin, HIGH);
}
} else {
Serial.println("Not Available");
}
}