-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqtt_water_bot.ino
More file actions
227 lines (184 loc) · 5.9 KB
/
mqtt_water_bot.ino
File metadata and controls
227 lines (184 loc) · 5.9 KB
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
#include <WiFi.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HTTPClient.h>
#include <AWS_IOT.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const char *ssid = "smartypants"; // replace with your wifi ssid and wpa2 key
const char *password = "123123123";
// AWS IoT Core params
#define CLIENT_ID "water_bot" // thing unique ID, this id should be unique among all things associated with your AWS account.
#define MQTT_TOPIC "$aws/things/water_bot/shadow/update" //topic for the MQTT data
#define AWS_HOST "123123123-ats.iot.us-east-2.amazonaws.com" // your host for uploading data to AWS,
AWS_IOT aws;
#define LED_BUILTIN 16
#define SENSOR 15 //2
long currentMillis = 0;
long previousMillis = 0;
long previousHeartBeat = 0;
int interval = 1000;
boolean ledState = LOW;
float calibrationFactor = 5.5;
volatile byte pulseCount;
byte pulse1Sec = 0;
float flowRate;
unsigned long flowMilliLitres;
unsigned int totalMilliLitres;
float flowLitres;
float totalLitres;
const int relay = 13;
bool waterFlowing = false;
void IRAM_ATTR pulseCounter()
{
pulseCount++;
}
void setup()
{
Serial.begin(115200);
// Connect to wifi
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Connect to AWS MQTT
if(aws.connect(AWS_HOST, CLIENT_ID) == 0){ // connects to host and returns 0 upon success
Serial.println(" Connected to AWS\n Done.");
}else {
Serial.println(" Connection failed!\n make sure your subscription to MQTT in the test page");
}
// Set up the display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
display.display();
delay(10);
// Set up the flow sensor
pinMode(LED_BUILTIN, OUTPUT);
pinMode(SENSOR, INPUT_PULLUP);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
previousMillis = 0;
previousHeartBeat = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
// Set up the relay
pinMode(relay, OUTPUT);
setRelayClosed(false);
}
void loop()
{
if(millis() > 7200000){
publishMqtt("{\"device_id\": \"water_bot\", \"restart\":" + (String)millis() + "}\n");
delay(3000);
ESP.restart();
}
currentMillis = millis();
if(currentMillis - previousHeartBeat > 30000){
Serial.println("heartbeat");
publishMqtt("{\"device_id\": \"water_bot\", \"heartbeat\":" + (String)millis() + "}\n");
previousHeartBeat = millis();
}
if (currentMillis - previousMillis > interval)
{
Serial.println("millis " + (String)millis());
pulse1Sec = pulseCount;
pulseCount = 0;
// scale the output
flowRate = ((1000.0 / (millis() - previousMillis)) * pulse1Sec) / calibrationFactor;
previousMillis = millis();
// convert to ml
flowMilliLitres = (flowRate / 60) * 1000;
flowLitres = (flowRate / 60);
// Add to the total
totalMilliLitres += flowMilliLitres;
totalLitres += flowLitres;
Serial.print("Flow rate: ");
Serial.print(float(flowRate));
Serial.print("L/min");
Serial.print("\t");
display.clearDisplay();
display.setCursor(10,0);
display.setTextSize(1);
display.setTextColor(WHITE);
display.print("MQTT Water Bot");
display.setCursor(0,20);
display.setTextSize(2);
display.setTextColor(WHITE);
display.print("R:");
display.print(float(flowRate));
display.setCursor(100,28);
display.setTextSize(1);
display.print("L/M");
Serial.print("Output Liquid Quantity: ");
Serial.print(totalMilliLitres);
Serial.print("mL / ");
Serial.print(totalLitres);
Serial.println("L");
display.setCursor(0,45);
display.setTextSize(2);
display.setTextColor(WHITE);
display.print("V:");
display.print(totalLitres);
display.setCursor(100,53);
display.setTextSize(1);
display.print("L");
display.display();
// open/close the relay
if( flowRate > 0 ){
setRelayClosed(true);
publishMqtt("{\"device_id\": \"water_bot\", \"flowRate\":" + (String)flowRate + ",\"total_volume\":" + (String)totalLitres + "}\n");
waterFlowing = true;
} else {
if( waterFlowing ){
setRelayClosed(false);
publishMqtt("{\"device_id\": \"water_bot\", \"flowRate\":" + (String)flowRate + ",\"total_volume\":" + (String)totalLitres + "}\n");
waterFlowing = false;
}
}
}
}
void setRelayClosed(bool close){
if(close){
digitalWrite(relay, HIGH);
Serial.println("Current Flowing");
publishMqtt("{\"device_id\": \"water_bot\", \"uv_status\":1}\n");
} else {
Serial.println("Current not Flowing");
publishMqtt("{\"device_id\": \"water_bot\", \"uv_status\":0}\n");
delay(3000);
digitalWrite(relay, LOW);
}
}
void publishMqtt(String msg){
//create string payload for publishing
int len = msg.length();
char payload[len];
msg.toCharArray(payload, len);
Serial.println("Publishing:- ");
Serial.println(payload);
if (aws.publish(MQTT_TOPIC, payload) == 0){// publishes payload and returns 0 upon success
Serial.println("Success\n");
}else{
Serial.println("Failed! waiting and then will try again....\n");
delay(1000);
Serial.println("trying again....");
if (aws.publish(MQTT_TOPIC, payload) == 0){// publishes payload and returns 0 upon success
Serial.println("Success\n");
}else{
Serial.println("Failed on second attempt! moving on...\n");
}
}
}