forked from lennonrar/WaterLevel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototipo2.ino
274 lines (232 loc) · 5.88 KB
/
prototipo2.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <PubSubClient.h>
#include <WiFiEsp.h>
#include <SoftwareSerial.h>
#define MQTT_SERVER "test.mosca.io"
char ssid[] = "motog";
char passwd[] = "12345678";
char* lightTopic = "/rexproject/esp/01";
void callback(char* topic, byte* payload, unsigned int length);
WiFiEspClient wifiClient;
PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);
SoftwareSerial Serial1(13, 12);
#include <Servo.h>
Servo motor;
//sensores
#define echo 2
#define echo0 4
#define trig 3
//leds
#define led_wifi 5
#define led_r 7
#define led_g 8
//motor
int mtr = 6;
//buzzer
#define buz 9
int _reset = 1;
float raio, lado_a, lado_b, volume, maximo;
long anterior = 0;
int form;
void reset();
void calculate();
void button();
float lvl(int n);
void triPulse(const int a);
void move_motor();
void alarme();
void reconnect();
void setup(){
Serial.begin(9600);
delay(100);
Serial1.begin(9600);
WiFi.init(&Serial1);
//start wifi subsystem
WiFi.begin(ssid, passwd);
//attempt to connect to the WIFI network and then connect to the MQTT server
reconnect();
//wait a bit before starting the main loop
delay(2000);
motor.attach(mtr);
motor.write(0);
pinMode(echo0, INPUT);
pinMode(echo, INPUT);
pinMode(trig,OUTPUT);
pinMode(led_r, OUTPUT);
pinMode(led_g, OUTPUT);
pinMode(buz, OUTPUT);
pinMode(led_wifi, OUTPUT);
digitalWrite(trig, LOW);
Serial.begin(9600);
}
void loop(){
if (!client.connected() && WiFi.status() == 3) {reconnect();}
client.loop();
if (_reset == 1){
reset();
Serial.println("Iniciando...");
}
calculate();
alarme();
Serial.print("Volume: ");
Serial.println(volume);
if (millis() - anterior > 60000){
client.publish("/rexproject/esp/01", String(volume).c_str());
anterior = millis();
}
//client.publish("/rexproject/esp/01", String(lvl(1)).c_str(), true);
delay(500);
}
void reset(){
motor.write(0);
delay(200);
float l = lvl(0);
Serial.print("Form: ");
Serial.println(form);
while(1){
if (form == 1){
raio = l;
//raio = 10;
delay(1000);
calculate();
maximo = volume;
_reset = 0;
break;
}else if (form == 0) {
lado_a = l;
delay(1000);
move_motor();
calculate();
maximo = volume;
_reset = 0;
break;
}else if (form == 2){
lado_a = lvl(0);
delay(1000);
lado_b = lado_a;
calculate();
maximo = volume;
_reset = 0;
break;
}
}
tone(buz, 1000);
delay(2000);
noTone(buz);
}
void calculate(){
float h = lvl(1);
if (form == 1){
volume = 3.1415*(raio*raio)*h;
}else if(form == 0 || form == 2
){
volume = (lado_b*lado_a)*h;
}
}
float lvl(int sensor){
long pulse;
float ret = 0.0;
if (sensor == 0){
trigPulse(trig);
pulse = pulseIn(echo0, HIGH);
}else if (sensor == 1){
trigPulse(trig);
pulse = pulseIn(echo, HIGH);
}
return microsecondsToCentimeters(pulse);
}
float microsecondsToCentimeters(long microseconds)
{
return microseconds/58.82;
}
void move_motor(){
int pos;
for (pos = 0; pos <= 90; pos++) {
motor.write(pos);
delay(10);
}
lado_b = lvl(0);
delay(1000);
for (pos = 90; pos >= 0; pos--) { // goes from 180 degrees to 0 degrees
motor.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
}
void trigPulse(const int a)
{
digitalWrite(a, HIGH); //Pulso de trigge em nível baixo
delayMicroseconds(10);
digitalWrite(a, LOW);
}
void alarme(){
int i = 0;
if (volume > maximo*0.75 || volume < maximo*0.25){
for(i = 0; i<10; i++){
digitalWrite(led_g, LOW);
digitalWrite(led_r, HIGH);
tone(buz, 1000);
delay(100);
digitalWrite(led_r, LOW);
noTone(buz);
}
digitalWrite(led_g, HIGH);
noTone(buz);
}
}
void callback(char* topic, byte* payload, unsigned int length) {
//convert topic to string to make it easier to work with
String topicStr = topic;
//Print out some debugging info
Serial.println("\nCallback update.");
Serial.print("Topic: ");
Serial.print(topicStr);
Serial.print(":");
for(int i = 0; i < length; i++){
Serial.print((char)payload[i]);
}
//turn the light on if the payload is '1' and publish to the MQTT server a confirmation message
if(payload[0] == '1'){
form = 1;
}
//turn the light off if the payload is '0' and publish to the MQTT server a confirmation message
else if (payload[0] == '0'){
form = 0;
} else if (payload[0] == '2'){
form = 2;
}
}
void reconnect() {
//attempt to connect to the wifi if connection is lost
if(WiFi.status() != WL_CONNECTED){
//debug printing
Serial.print("Connecting to ");
Serial.println(ssid);
//loop while we wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//print out some more debug once connected
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//make sure we are connected to WIFI before attemping to reconnect to MQTT
if(WiFi.status() == WL_CONNECTED){
// Loop until we're reconnected to the MQTT server
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Generate client name based on MAC address and last 8 bits of microsecond counter
String clientName;
clientName += "esp8266-";
//if connected, subscribe to the topic(s) we want to be notified about
if (client.connect((char*) clientName.c_str())) {
Serial.print("\tMTQQ Connected");
digitalWrite(led_wifi, HIGH);
client.subscribe(lightTopic);
}
//otherwise print failed for debugging
else{Serial.println("\tFailed."); abort();digitalWrite(led_wifi, LOW);}
}
}
}