-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTankGaugit.ino
162 lines (140 loc) · 5.14 KB
/
TankGaugit.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
160
161
162
/**
* Copyright (C) 2015 Jeeva Kandasamy ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Jeeva Kandasamy (jkandasa)
* @since 0.0.1
*/
/**
* Water level sensor based on ultrasonic sensor.
*/
#include <SPI.h>
#include <MySensor.h>
#include <NewPing.h>
// Application details
#define APPLICATION_NAME "Water Level Sensor Node"
#define APPLICATION_VERSION "0.0.1"
//My node Id
#define NODE_ID AUTO
//This node Sensors
#define S_WATER_DISTANCE_ID (int8_t) 1
#define DIST_CHILD_ID (int8_t) 2
#define PRESS_CHILD_ID (int8_t) 3
//Tank water Level
#define LEVEL_TANK_FULL (int8_t) 22 // Set your tank water FULL LEVEL
#define LEVEL_TANK_EMPTY (int8_t) 165 // Set your tank water EMPTY LEVEL
//^^^^ which is calculated as 0.7 (165-22 -> 100/142) ~= 0.7
//EEPROM Address
#define E_ADR_LEVEL_CHECK_TIME (int8_t) 15 //in seconds
#define TRIGGER_PIN 7 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 6 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 350 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define SLEEP_TIME (unsigned long)1000l*15 // Sleep time between reads (in milliseconds)
int sensorPin = A1; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void incomingMessage(const MyMessage &message);
void sendWaterLevel();
MySensor gw;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
MyMessage msg(S_WATER_DISTANCE_ID, V_VOLUME);
MyMessage distmsg(DIST_CHILD_ID, V_DISTANCE);
MyMessage pressmsg(PRESS_CHILD_ID, V_PRESSURE);
uint8_t lastDist = 0;
uint8_t sendLevelDelayTime = 120;
unsigned long lastSent;
bool isLevelIncreasing = false;
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
//Serial.begin(115200);
//Serial.println("TankGaugit");
gw.begin(incomingMessage, NODE_ID, true);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo(APPLICATION_NAME, APPLICATION_VERSION);
// Register all sensors to gw (they will be created as child devices)
gw.present(S_WATER_DISTANCE_ID, S_WATER, "Tank");
gw.present(DIST_CHILD_ID, S_DISTANCE, "Distance");
gw.present(PRESS_CHILD_ID, S_BARO, "Pressure");
MyMessage commandMsg(S_WATER, V_VAR1);
sendLevelDelayTime = gw.loadState(E_ADR_LEVEL_CHECK_TIME);
if (sendLevelDelayTime <= 14) {
sendLevelDelayTime = 15;
}
gw.send(commandMsg.set(sendLevelDelayTime)); //Send current state of delay time
lastSent = millis();
}
void loop() {
gw.process();
//This is to avoid infinite loop, This number will overflow (go back to zero), after approximately 50 days.
//https://www.arduino.cc/en/reference/millis
if (millis() <= 3 * 1000l) {
lastSent = millis();
} else if ((lastSent + (sendLevelDelayTime * 1000l)) <= millis()) {
lastSent = millis();
sendWaterLevel();
sendPressureLevel();
}
}
void sendPressureLevel() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
//Serial.print("Pressure is: ");
//Serial.print(sensorValue);
gw.send(pressmsg.set(sensorValue));
}
void sendWaterLevel() {
uint8_t dist = sonar.ping_cm();
if (lastDist != 0) {
if (lastDist > dist) {
if (!isLevelIncreasing) {
dist = lastDist;
isLevelIncreasing = true;
}
} else if (lastDist < dist) {
if (isLevelIncreasing) {
dist = lastDist;
isLevelIncreasing = false;
}
}
}
lastDist = dist;
float percentage;
//Serial.print(" Distance is: ");
//Serial.print(dist);
//convert it in to percentage 0~100 %
float level = 0.7f * (dist - LEVEL_TANK_FULL );
if (level < 0) {
percentage = 100.0f + (level * -1);
} else if (level > 100.0f) {
percentage = 0.0f;
} else {
percentage = 100.0f - level;
}
gw.send(msg.set(percentage, 2));
gw.send(distmsg.set(dist));
//Serial.print(" Percentage is: ");
//Serial.println(percentage);
}
void incomingMessage(const MyMessage &message) {
if (message.type == V_VAR1) {
if (message.getInt() == 0) {
sendWaterLevel();
} else if ((message.getInt() >= 15) && (message.getInt() <= 250)) {
gw.saveState(E_ADR_LEVEL_CHECK_TIME, (uint8_t) message.getInt());
sendLevelDelayTime = gw.loadState(E_ADR_LEVEL_CHECK_TIME);
}
}
}