Components and supplies
ESP32S
Tools and machines
Hot glue gun (generic)
Apps and platforms
Arduino IDE
Project description
Code
esp32water.ino
c_cpp
1 #include <WiFi.h> 2 #include <HTTPClient.h> 3//IP or name of address root: ie: google.com 4//NOT google.com/nothing/after/the/dotcom.html 5const char* hostGet = "slogfy.com"; 6const char *ssid = "Robo"; // replace with your wifi ssid and wpa2 key 7const char *pass = "lopklopk"; 8#define LED_BUILTIN 2 9#define SENSOR 27 10 11long currentMillis = 0; 12long previousMillis = 0; 13int interval = 1000; 14boolean ledState = LOW; 15float calibrationFactor = 7.5; 16volatile byte pulseCount; 17byte pulse1Sec = 0; 18float flowRate; 19unsigned int flowMilliLitres; 20unsigned long totalMilliLitres; 21unsigned long water=99; 22 23void IRAM_ATTR pulseCounter() 24{ 25 pulseCount++; 26} 27void postData() { 28 29 WiFiClient clientGet; 30 const int httpGetPort = 80; 31 //the path and file to send the data to: 32 String urlGet = "/water/add1.php"; 33 34 // We now create and add parameters: 35 36 urlGet += "?water="; 37 urlGet += water; 38 Serial.print(">>> Connecting to host: "); 39 Serial.println(urlGet); 40 Serial.println(hostGet); 41 if (!clientGet.connect(hostGet, httpGetPort)) { 42 Serial.print("Connection failed: "); 43 Serial.print(hostGet); 44 } else { 45 clientGet.println("GET " + urlGet + " HTTP/1.1"); 46 clientGet.print("Host: "); 47 clientGet.println(hostGet); 48 clientGet.println("User-Agent: ESP8266/1.0"); 49 clientGet.println("Connection: close\ \ 50\ \ 51"); 52 53 unsigned long timeoutP = millis(); 54 while (clientGet.available() == 0) { 55 56 if (millis() - timeoutP > 10000) { 57 Serial.print(">>> Client Timeout: "); 58 Serial.println(hostGet); 59 clientGet.stop(); 60 return; 61 } 62 } 63 64 //just checks the 1st line of the server response. Could be expanded if needed. 65 while(clientGet.available()){ 66 String retLine = clientGet.readStringUntil('\r'); 67 Serial.println(retLine); 68 break; 69 } 70 71 } //end client connection if else 72 73 Serial.print(">>> Closing host: "); 74 Serial.println(hostGet); 75 76 clientGet.stop(); 77 78} 79 80void setup() { 81 Serial.begin(115200); 82 Serial.println("Connecting to "); 83 Serial.println(ssid); 84 85 86 WiFi.begin(ssid, pass); 87 88 while (WiFi.status() != WL_CONNECTED) 89 { 90 delay(500); 91 Serial.print("."); 92 } 93 Serial.println(""); 94 Serial.println("WiFi connected"); 95 96 pinMode(LED_BUILTIN, OUTPUT); 97 pinMode(SENSOR, INPUT_PULLUP); 98 99 pulseCount = 0; 100 flowRate = 0.0; 101 flowMilliLitres = 0; 102 totalMilliLitres = 0; 103 previousMillis = 0; 104 105 attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING); 106 107} 108 109void loop() { 110 currentMillis = millis(); 111 if (currentMillis - previousMillis > interval) { 112 113 pulse1Sec = pulseCount; 114 pulseCount = 0; 115 116 // Because this loop may not complete in exactly 1 second intervals we calculate 117 // the number of milliseconds that have passed since the last execution and use 118 // that to scale the output. We also apply the calibrationFactor to scale the output 119 // based on the number of pulses per second per units of measure (litres/minute in 120 // this case) coming from the sensor. 121 flowRate = ((1000.0 / (millis() - previousMillis)) * pulse1Sec) / calibrationFactor; 122 previousMillis = millis(); 123 124 // Divide the flow rate in litres/minute by 60 to determine how many litres have 125 // passed through the sensor in this 1 second interval, then multiply by 1000 to 126 // convert to millilitres. 127 flowMilliLitres = (flowRate / 60) * 1000; 128 129 // Add the millilitres passed in this second to the cumulative total 130 totalMilliLitres += flowMilliLitres; 131 132 // Print the flow rate for this second in litres / minute 133 Serial.print("Flow rate: "); 134 Serial.print(flowRate); // Print the integer part of the variable 135 Serial.print("L/min"); 136 Serial.print("\ "); // Print tab space 137 138 // Print the cumulative total of litres flowed since starting 139 Serial.print("Output Liquid Quantity: "); 140 Serial.print(totalMilliLitres); 141 142 Serial.print("mL / "); 143 Serial.print(totalMilliLitres / 1000); 144 Serial.println("L"); 145 } 146 147 water=totalMilliLitres; 148// postData(); 149 // delay(000); 150} 151
esp32water.ino
c_cpp
1 #include <WiFi.h> 2 #include <HTTPClient.h> 3//IP or name of address 4 root: ie: google.com 5//NOT google.com/nothing/after/the/dotcom.html 6const 7 char* hostGet = "slogfy.com"; 8const char *ssid = "Robo"; // replace 9 with your wifi ssid and wpa2 key 10const char *pass = "lopklopk"; 11#define 12 LED_BUILTIN 2 13#define SENSOR 27 14 15long currentMillis = 0; 16long previousMillis 17 = 0; 18int interval = 1000; 19boolean ledState = LOW; 20float calibrationFactor 21 = 7.5; 22volatile byte pulseCount; 23byte pulse1Sec = 0; 24float flowRate; 25unsigned 26 int flowMilliLitres; 27unsigned long totalMilliLitres; 28unsigned long water=99; 29 30 31void IRAM_ATTR pulseCounter() 32{ 33 pulseCount++; 34} 35void postData() 36 { 37 38 WiFiClient clientGet; 39 const int httpGetPort = 80; 40 //the 41 path and file to send the data to: 42 String urlGet = "/water/add1.php"; 43 44 45 // We now create and add parameters: 46 47 urlGet += "?water="; 48 49 urlGet += water; 50 Serial.print(">>> Connecting to host: "); 51 Serial.println(urlGet); 52 53 Serial.println(hostGet); 54 if (!clientGet.connect(hostGet, httpGetPort)) 55 { 56 Serial.print("Connection failed: "); 57 Serial.print(hostGet); 58 59 } else { 60 clientGet.println("GET " + urlGet + " HTTP/1.1"); 61 62 clientGet.print("Host: "); 63 clientGet.println(hostGet); 64 65 clientGet.println("User-Agent: ESP8266/1.0"); 66 clientGet.println("Connection: 67 close\ \ 68\ \ 69"); 70 71 unsigned long timeoutP = millis(); 72 73 while (clientGet.available() == 0) { 74 75 if 76 (millis() - timeoutP > 10000) { 77 Serial.print(">>> Client Timeout: 78 "); 79 Serial.println(hostGet); 80 clientGet.stop(); 81 82 return; 83 } 84 } 85 86 //just checks 87 the 1st line of the server response. Could be expanded if needed. 88 while(clientGet.available()){ 89 90 String retLine = clientGet.readStringUntil('\ '); 91 Serial.println(retLine); 92 93 break; 94 } 95 96 } //end client connection if else 97 98 99 Serial.print(">>> Closing host: "); 100 Serial.println(hostGet); 101 102 103 clientGet.stop(); 104 105} 106 107void setup() { 108 Serial.begin(115200); 109 110 Serial.println("Connecting to "); 111 Serial.println(ssid); 112 113 114 115 WiFi.begin(ssid, pass); 116 117 while (WiFi.status() != WL_CONNECTED) 118 119 { 120 delay(500); 121 Serial.print("."); 122 } 123 124 Serial.println(""); 125 Serial.println("WiFi connected"); 126 127 128 pinMode(LED_BUILTIN, OUTPUT); 129 pinMode(SENSOR, INPUT_PULLUP); 130 131 132 pulseCount = 0; 133 flowRate = 0.0; 134 flowMilliLitres = 0; 135 totalMilliLitres 136 = 0; 137 previousMillis = 0; 138 139 attachInterrupt(digitalPinToInterrupt(SENSOR), 140 pulseCounter, FALLING); 141 142} 143 144void loop() { 145 currentMillis = millis(); 146 147 if (currentMillis - previousMillis > interval) { 148 149 pulse1Sec = pulseCount; 150 151 pulseCount = 0; 152 153 // Because this loop may not complete in exactly 154 1 second intervals we calculate 155 // the number of milliseconds that have passed 156 since the last execution and use 157 // that to scale the output. We also apply 158 the calibrationFactor to scale the output 159 // based on the number of pulses 160 per second per units of measure (litres/minute in 161 // this case) coming from 162 the sensor. 163 flowRate = ((1000.0 / (millis() - previousMillis)) * pulse1Sec) 164 / calibrationFactor; 165 previousMillis = millis(); 166 167 // Divide the 168 flow rate in litres/minute by 60 to determine how many litres have 169 // passed 170 through the sensor in this 1 second interval, then multiply by 1000 to 171 // 172 convert to millilitres. 173 flowMilliLitres = (flowRate / 60) * 1000; 174 175 176 // Add the millilitres passed in this second to the cumulative total 177 totalMilliLitres 178 += flowMilliLitres; 179 180 // Print the flow rate for this second in litres 181 / minute 182 Serial.print("Flow rate: "); 183 Serial.print(flowRate); // 184 Print the integer part of the variable 185 Serial.print("L/min"); 186 Serial.print("\ "); 187 // Print tab space 188 189 // Print the cumulative total of litres flowed 190 since starting 191 Serial.print("Output Liquid Quantity: "); 192 Serial.print(totalMilliLitres); 193 194 195 Serial.print("mL / "); 196 Serial.print(totalMilliLitres / 1000); 197 198 Serial.println("L"); 199 } 200 201 water=totalMilliLitres; 202// postData(); 203 204 // delay(000); 205} 206
Downloadable files
water tap
water tap
Documentation
test
https://www.thingiverse.com/thing:4254641
test
https://www.thingiverse.com/thing:4254641
Comments
Only logged in users can leave comments