-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartWaterManagement.ino
157 lines (129 loc) · 3.84 KB
/
SmartWaterManagement.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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include<Servo.h>
Servo myservo;
#define trig D5
#define echo D6
void callback(char* topic, byte* payload, unsigned int payloadLength);
//-------- Customise these values -----------
const char* ssid = "";
const char* password = "";
#define ORG ""
#define DEVICE_TYPE ""
#define DEVICE_ID ""
#define TOKEN ""
//-------- Customise the above values --------
//include device credentials
char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char publictopic[] = "iot-2/evt/distance/fmt/json";
char subscribetopic[] = "iot-2/cmd/command/fmt/String";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;
WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);
String command;
void setup() {
myservo.attach(D4);
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
Serial.begin(9600);
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("."); }
Serial.println("");
Serial.print("WiFi connected, IP address: ");
Serial.println(WiFi.localIP());
mqttConnect();
}
void loop() {
float duration;
double distance;
double percent;
// put your main code here, to run repeatedly:
digitalWrite(trig,LOW);
delay(1000);
digitalWrite(trig,HIGH);
delay(8000);
digitalWrite(trig,LOW);
duration = pulseIn(echo,HIGH);
distance=(duration*0.034)/2;
percent=(100-distance);
Serial.print("distance level of unfilled water :");
Serial.println(distance);
Serial.print("percentage of filled water :");
Serial.println(percent);
PublishData(distance,percent);
delay(1000);
if (!client.loop()) {
mqttConnect(); }
delay(10);
}
void PublishData(float distance, float percent){
if (!!!client.connected()) {
Serial.print("Reconnecting client to ");
Serial.println(server);
while (!!!client.connect(clientId, authMethod, token)) {
Serial.print(".");
delay(50); }
Serial.println(); }
String payload = "{\"d\":{\"distance\":";
payload += distance;
payload += "," "\"percent\":";
payload += percent;
payload += "}}";
Serial.print("Sending payload: ");
Serial.println(payload);
if (client.publish(publictopic, (char*) payload.c_str())) {
Serial.println("Publish ok"); }
else {
Serial.println("Publish failed"); }
}
void mqttConnect() {
if (!client.connected()) {
Serial.print("Reconnecting MQTT client to "); Serial.println(server);
while (!client.connect(clientId, authMethod, token)) {
Serial.print(".");
delay(500);
}
initManagedDevice();
Serial.println();
}
}
void initManagedDevice() {
delay(100);
if (client.subscribe(subscribetopic)) {
Serial.println("subscribe to cmd OK");
} else {
Serial.println("subscribe to cmd FAILED");
}
}
void callback(char* topic, byte* payload, unsigned int payloadLength) {
Serial.print("callback invoked for topic: ");
Serial.println(subscribetopic);
for (int i = 0; i < payloadLength; i++) {
//Serial.println((char)payload[i]);
command += (char)payload[i];
}
Serial.println(command);
if(command=="turn_on"){
{
int pos;
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(360); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
else if(command=="turn_off")
{
myservo.write(0);
delay(15);
Serial.println("Motor is Turned OFF");
}
command ="";
}