Components and supplies
ESP8266 ESP-01
Grove - Water Sensor
DHT11 Temperature & Humidity Sensor (3 pins)
Buzzer
ICStation Mega2560
Breadboard (generic)
9V 1A Switching Wall Power Supply
Apps and platforms
Arduino IDE
Project description
Code
Water_Leakage_Detector_v1
c_cpp
1/* 2 3 Cyber Physical Systems Project 4 Author: Aanchal Chaturvedi 5 Water Leak Detection System 6 created by using - UCTronics Ultimate Starter Kit for Arduino 7 8 Design Specifications for this sketch - 9 This sketch is designed to determin water leaks in building and detect humidity and temperature at the time of leakage. 10 The data recorded from the sensors is sent to Adafruit Cloud 11 12 Parts required: 13 - 1 Mega2560 R3 14 - 1 ESP8266 Module 15 - 1 DHT-11 Sensor Module 16 - 1 Grove Water Level Sensor Module 17 - Male to Male Jumper Wires 18 - Male to Female Jumper Wires 19 - 1 830 Tie Point Breadboard 20 21 22 Library Used: 23 Wifi Esp 24 https://www.arduino.cc/reference/en/libraries/wifiesp/ 25 Adafruit MQTT 26 https://www.arduino.cc/reference/en/libraries/adafruit-mqtt-library/ 27 DHT Sensor Library 28 https://www.arduino.cc/reference/en/libraries/dht-sensor-library/ 29 30 References: 31 https://github.com/UCTRONICS/uctronics_arduino_kits/blob/master/Code/Lesson_25_water_level_detection_sensor_module/Lesson_25_water_level_detection_sensor_module.ino 32 Arduino Examples > Adafruit MQTT Library > mqtt_esp8266 https://github.com/esp8266/Arduino 33 34 35*/ 36 37#include "WiFiEsp.h" 38#include "Adafruit_MQTT.h" 39#include "Adafruit_MQTT_Client.h" 40#include <DHT.h> 41#include <DHT_U.h> 42 43 44/********************** DHT Pin Settings *******************************/ 45//Input pin for DHT Sensor is Pin 7 46#define DHTPIN A1 47 48//We are using is DHT 11 type of DHT sensor Module 49#define DHTTYPE DHT11 50//create a DHT object/instance 51//DHTPIN - source pin for DHT sensor on the microcontroller 52//DHTTYPE - type of DHT Sensor Module 53 54DHT dht(DHTPIN, DHTTYPE); 55 56/******************Water Level Sensor Settings **************************/ 57 58// Configuration for the Water Level Sensor analog input and output 59#define WL_POWER_PIN 3 60#define WL_SIGNAL_PIN A0 61 62/******************Active Buzzer pin ***********************************/ 63int buzzerPin = 4; //definition digital 8 pins as pin to control the buzzer 64 65/************************* WiFi Settings *********************************/ 66 67char ssid[] = "**********"; // your network SSID (name) 68char password[] = "********"; // your network password 69int status = WL_IDLE_STATUS; // the Wifi radio's status 70 71/************************* Adafruit.io Setup *********************************/ 72 73#define AIO_SERVER "io.adafruit.com" 74#define AIO_SERVERPORT 1883 // use 8883 for SSL 75#define AIO_USERNAME "************" 76#define AIO_KEY "***************" 77 78WiFiEspClient client; 79 80// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 81Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); 82 83//publish to temperature feed located at aanchal0431/feeds/WaterLevel 84Adafruit_MQTT_Publish inputWaterLevel = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/WaterLevel"); 85//publish to humidity feed located at aanchal0431/feeds/humidity 86Adafruit_MQTT_Publish inputHumidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humidity"); 87//publish to temperature feed located at aanchal0431/feeds/temperature 88Adafruit_MQTT_Publish inputTemperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperature"); 89//publish to temperature feed located at aanchal0431/feeds/temperature 90Adafruit_MQTT_Publish inputonoff = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/onoff"); 91 92 93void setup() { 94 95 //baud rate for mega board 96 Serial.begin(115200); 97 //The serial connection at RX1, TX1 is open and 9600 is the baud rate for ESP8266 98 Serial1.begin(9600); 99 100 Serial.println("Cyber Physical Systems Project - Water Leakage Detection Sensor"); 101 delay(2000); 102 103 // Connect to WiFi 104 Serial.println(); 105 WiFi.init(&Serial1); 106 // check for the presence of the shield 107 if (WiFi.status() == WL_NO_SHIELD) { 108 Serial.println("WiFi shield not present"); 109 // don't continue 110 } 111 112 if (status != WL_CONNECTED) { 113 // attempt to connect to WiFi network 114 Serial.print("Attempting to connect to WPA SSID: "); 115 Serial.println(ssid); 116 // Connect to WPA/WPA2 network// 117 status = WiFi.begin(ssid, password); 118 while (WiFi.status() != WL_CONNECTED) { 119 delay(500); 120 } 121 122 Serial.println("You're connected to the network"); 123 } 124 125 //setup for Water Level Sensor 126 pinMode(WL_POWER_PIN, OUTPUT); // configure pin as an OUTPUT 127 128 //initialization for DHT sensor 129 dht.begin(); 130 131 //buzzer pin mode 132 pinMode(buzzerPin, OUTPUT); 133} 134 135void loop() { 136 137 MQTT_connect(); 138 139 delay(10000);//1 seconds delay between each run 140 141 //Humidity and Temperature Sensor code 142 float humidity = dht.readHumidity(); 143 float temperature = dht.readTemperature(true); 144 145 // Water Level Sensor Code 146 long waterLevel = getWaterLevel(); 147 Serial.println("Water Level is: " + waterLevel); 148 149 //calibrations for humidity and temperature reading is based on the average of 150 //the readings before 151 humidity = humidity + 14; 152 temperature = temperature - 2; 153 154 if (! inputWaterLevel.publish(waterLevel)) { 155 Serial.println(F("Failed")); 156 } else { 157 Serial.println(F("OK!")); 158 } 159 if (! inputHumidity.publish(humidity)) { 160 Serial.println(F("Failed")); 161 } else { 162 Serial.println(F("OK!")); 163 } 164 165 if (! inputTemperature.publish(temperature)) { 166 Serial.println(F("Failed")); 167 } else { 168 Serial.println(F("OK!")); 169 } 170 171 // validate water level values 172 if (isnan(waterLevel)) { 173 Serial.println("Unable to read from the Water Level sensor!"); 174 //end loop without executing the remaining code 175 return; 176 } 177 178 //validate temperature and humidity values 179 if (isnan(humidity) || isnan(temperature)) { 180 Serial.println("Unable to read from the DHT sensor!"); 181 //end loop without executing the remaining code 182 return; 183 } 184 185 if (waterLevel > 0 && waterLevel <= 100) { 186 long onStatus = 10; 187 soundAlarm(); 188 if (! inputonoff.publish(onStatus)) { 189 Serial.println(F("Failed")); 190 } else { 191 Serial.println(F("OK!")); 192 } 193 } else { 194 long onStatus = 0; 195 if (! inputonoff.publish(onStatus)) { 196 Serial.println(F("Failed")); 197 } else { 198 Serial.println(F("OK!")); 199 } 200 } 201 202 //Print Humidity and Temperature sensor readings and Water Level Sensor in serial monitor 203 Serial.print("Water Level = "); 204 Serial.print(waterLevel); 205 Serial.print(" "); 206 Serial.print("Humidity = "); 207 Serial.print(humidity); 208 Serial.print("% "); 209 Serial.print("Temperature = "); 210 Serial.print(temperature); 211 Serial.println("F"); 212 213} 214 215void MQTT_connect() { 216 int8_t ret; 217 // Stop if already connected. 218 if (mqtt.connected()) { 219 return; 220 } 221 Serial.print("Connecting to MQTT... "); 222 uint8_t retries = 3; 223 while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected 224 Serial.println(mqtt.connectErrorString(ret)); 225 Serial.println("Retrying MQTT connection in 5 seconds..."); 226 mqtt.disconnect(); 227 delay(5000); // wait 5 seconds 228 retries--; 229 if (retries == 0) { 230 // basically die and wait for reset 231 while (1); 232 } 233 } 234 Serial.println("MQTT Connected!"); 235} 236 237void soundAlarm(){ 238 digitalWrite(buzzerPin, HIGH); //Set PIN 8 feet as HIG67890]\\H = 5 v 239 delay(2000); //Set the delay time2000ms 240 digitalWrite(buzzerPin, LOW); //Set PIN 8 feet for LOW = 0 v 241 delay(2000); //Set the delay time2000ms 242} 243 244 245 246long getWaterLevel(){ 247 digitalWrite(WL_POWER_PIN, HIGH); // turn the sensor ON 248 delay(10); // wait 10 milliseconds 249 long waterLevel = analogRead(WL_SIGNAL_PIN); // read the analog value from sensor 250 digitalWrite(WL_POWER_PIN, LOW); // turn the sensor OFF 251 return waterLevel; 252} 253
Water_Leakage_Detector_v1
c_cpp
1/* 2 3 Cyber Physical Systems Project 4 Author: Aanchal Chaturvedi 5 6 Water Leak Detection System 7 created by using - UCTronics Ultimate Starter 8 Kit for Arduino 9 10 Design Specifications for this sketch - 11 This sketch 12 is designed to determin water leaks in building and detect humidity and temperature 13 at the time of leakage. 14 The data recorded from the sensors is sent to Adafruit 15 Cloud 16 17 Parts required: 18 - 1 Mega2560 R3 19 - 1 ESP8266 Module 20 21 - 1 DHT-11 Sensor Module 22 - 1 Grove Water Level Sensor Module 23 - Male 24 to Male Jumper Wires 25 - Male to Female Jumper Wires 26 - 1 830 Tie Point Breadboard 27 28 29 30 Library Used: 31 Wifi Esp 32 https://www.arduino.cc/reference/en/libraries/wifiesp/ 33 34 Adafruit MQTT 35 https://www.arduino.cc/reference/en/libraries/adafruit-mqtt-library/ 36 37 DHT Sensor Library 38 https://www.arduino.cc/reference/en/libraries/dht-sensor-library/ 39 40 41 References: 42 https://github.com/UCTRONICS/uctronics_arduino_kits/blob/master/Code/Lesson_25_water_level_detection_sensor_module/Lesson_25_water_level_detection_sensor_module.ino 43 44 Arduino Examples > Adafruit MQTT Library > mqtt_esp8266 https://github.com/esp8266/Arduino 45 46 47 48*/ 49 50#include "WiFiEsp.h" 51#include "Adafruit_MQTT.h" 52#include 53 "Adafruit_MQTT_Client.h" 54#include <DHT.h> 55#include <DHT_U.h> 56 57 58/********************** 59 DHT Pin Settings *******************************/ 60//Input pin for DHT Sensor 61 is Pin 7 62#define DHTPIN A1 63 64//We are using is DHT 11 type of DHT sensor 65 Module 66#define DHTTYPE DHT11 67//create a DHT object/instance 68//DHTPIN - 69 source pin for DHT sensor on the microcontroller 70//DHTTYPE - type of DHT Sensor 71 Module 72 73DHT dht(DHTPIN, DHTTYPE); 74 75/******************Water Level Sensor 76 Settings **************************/ 77 78// Configuration for the Water Level 79 Sensor analog input and output 80#define WL_POWER_PIN 3 81#define WL_SIGNAL_PIN 82 A0 83 84/******************Active Buzzer pin ***********************************/ 85int 86 buzzerPin = 4; //definition digital 8 pins as pin to control the buzzer 87 88/************************* 89 WiFi Settings *********************************/ 90 91char ssid[] = "**********"; 92 // your network SSID (name) 93char password[] = "********"; // 94 your network password 95int status = WL_IDLE_STATUS; // the Wifi radio's status 96 97/************************* 98 Adafruit.io Setup *********************************/ 99 100#define AIO_SERVER "io.adafruit.com" 101#define 102 AIO_SERVERPORT 1883 // use 8883 for SSL 103#define AIO_USERNAME 104 "************" 105#define AIO_KEY "***************" 106 107WiFiEspClient 108 client; 109 110// Setup the MQTT client class by passing in the WiFi client and 111 MQTT server and login details. 112Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, 113 AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); 114 115//publish to temperature feed located 116 at aanchal0431/feeds/WaterLevel 117Adafruit_MQTT_Publish inputWaterLevel = Adafruit_MQTT_Publish(&mqtt, 118 AIO_USERNAME "/feeds/WaterLevel"); 119//publish to humidity feed located at aanchal0431/feeds/humidity 120Adafruit_MQTT_Publish 121 inputHumidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humidity"); 122//publish 123 to temperature feed located at aanchal0431/feeds/temperature 124Adafruit_MQTT_Publish 125 inputTemperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperature"); 126//publish 127 to temperature feed located at aanchal0431/feeds/temperature 128Adafruit_MQTT_Publish 129 inputonoff = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/onoff"); 130 131 132void 133 setup() { 134 135 //baud rate for mega board 136 Serial.begin(115200); 137 138 //The serial connection at RX1, TX1 is open and 9600 is the baud rate for ESP8266 139 140 Serial1.begin(9600); 141 142 Serial.println("Cyber Physical Systems Project 143 - Water Leakage Detection Sensor"); 144 delay(2000); 145 146 // Connect to WiFi 147 148 Serial.println(); 149 WiFi.init(&Serial1); 150 // check for the presence of 151 the shield 152 if (WiFi.status() == WL_NO_SHIELD) { 153 Serial.println("WiFi 154 shield not present"); 155 // don't continue 156 } 157 158 if (status != WL_CONNECTED) 159 { 160 // attempt to connect to WiFi network 161 Serial.print("Attempting 162 to connect to WPA SSID: "); 163 Serial.println(ssid); 164 // Connect to WPA/WPA2 165 network// 166 status = WiFi.begin(ssid, password); 167 while (WiFi.status() 168 != WL_CONNECTED) { 169 delay(500); 170 } 171 172 Serial.println("You're 173 connected to the network"); 174 } 175 176 //setup for Water Level Sensor 177 178 pinMode(WL_POWER_PIN, OUTPUT); // configure pin as an OUTPUT 179 180 //initialization 181 for DHT sensor 182 dht.begin(); 183 184 //buzzer pin mode 185 pinMode(buzzerPin, 186 OUTPUT); 187} 188 189void loop() { 190 191 MQTT_connect(); 192 193 delay(10000);//1 194 seconds delay between each run 195 196 //Humidity and Temperature Sensor code 197 198 float humidity = dht.readHumidity(); 199 float temperature = dht.readTemperature(true); 200 201 202 // Water Level Sensor Code 203 long waterLevel = getWaterLevel(); 204 Serial.println("Water 205 Level is: " + waterLevel); 206 207 //calibrations for humidity and temperature 208 reading is based on the average of 209 //the readings before 210 humidity = humidity 211 + 14; 212 temperature = temperature - 2; 213 214 if (! inputWaterLevel.publish(waterLevel)) 215 { 216 Serial.println(F("Failed")); 217 } else { 218 Serial.println(F("OK!")); 219 220 } 221 if (! inputHumidity.publish(humidity)) { 222 Serial.println(F("Failed")); 223 224 } else { 225 Serial.println(F("OK!")); 226 } 227 228 if (! inputTemperature.publish(temperature)) 229 { 230 Serial.println(F("Failed")); 231 } else { 232 Serial.println(F("OK!")); 233 234 } 235 236 // validate water level values 237 if (isnan(waterLevel)) { 238 Serial.println("Unable 239 to read from the Water Level sensor!"); 240 //end loop without executing the 241 remaining code 242 return; 243 } 244 245 //validate temperature and humidity 246 values 247 if (isnan(humidity) || isnan(temperature)) { 248 Serial.println("Unable 249 to read from the DHT sensor!"); 250 //end loop without executing the remaining 251 code 252 return; 253 } 254 255 if (waterLevel > 0 && waterLevel <= 100) { 256 257 long onStatus = 10; 258 soundAlarm(); 259 if (! inputonoff.publish(onStatus)) 260 { 261 Serial.println(F("Failed")); 262 } else { 263 Serial.println(F("OK!")); 264 265 } 266 } else { 267 long onStatus = 0; 268 if (! inputonoff.publish(onStatus)) 269 { 270 Serial.println(F("Failed")); 271 } else { 272 Serial.println(F("OK!")); 273 274 } 275 } 276 277 //Print Humidity and Temperature sensor readings and Water 278 Level Sensor in serial monitor 279 Serial.print("Water Level = "); 280 Serial.print(waterLevel); 281 282 Serial.print(" "); 283 Serial.print("Humidity = "); 284 Serial.print(humidity); 285 286 Serial.print("% "); 287 Serial.print("Temperature = "); 288 Serial.print(temperature); 289 290 Serial.println("F"); 291 292} 293 294void MQTT_connect() { 295 int8_t ret; 296 297 // Stop if already connected. 298 if (mqtt.connected()) { 299 return; 300 301 } 302 Serial.print("Connecting to MQTT... "); 303 uint8_t retries = 3; 304 305 while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected 306 307 Serial.println(mqtt.connectErrorString(ret)); 308 Serial.println("Retrying 309 MQTT connection in 5 seconds..."); 310 mqtt.disconnect(); 311 delay(5000); 312 // wait 5 seconds 313 retries--; 314 if (retries == 0) { 315 // basically 316 die and wait for reset 317 while (1); 318 } 319 } 320 Serial.println("MQTT 321 Connected!"); 322} 323 324void soundAlarm(){ 325 digitalWrite(buzzerPin, HIGH); 326 //Set PIN 8 feet as HIG67890]\\H = 5 v 327 delay(2000); //Set 328 the delay time2000ms 329 digitalWrite(buzzerPin, LOW); //Set PIN 8 feet for LOW 330 = 0 v 331 delay(2000); //Set the delay time2000ms 332} 333 334 335 336long 337 getWaterLevel(){ 338 digitalWrite(WL_POWER_PIN, HIGH); // turn the sensor ON 339 340 delay(10); // wait 10 milliseconds 341 long waterLevel 342 = analogRead(WL_SIGNAL_PIN); // read the analog value from sensor 343 digitalWrite(WL_POWER_PIN, 344 LOW); // turn the sensor OFF 345 return waterLevel; 346} 347
Downloadable files
Schematics of the CPS
It is a reference to how the components are created but not reflective of the exact analog or digital pins to be included in the project.
Schematics of the CPS
Comments
Only logged in users can leave comments