Arduino IoT Shower

files.catbox.moe/jnhue9.mp4

Arduino MKR1000
(3) MG995 Servos
16X2 LCD Display
(2) Temperature Sensors
Tin Foil


  1. // Arduino IoT Shower
  2. // vgmlr
  3. // A1 Temp Head
  4. // 0 Cap 10M
  5. // 1 Cap 1K
  6. // 2 3 4 5 11 12 LCD
  7. // 6 LED
  8. // 7 Hot
  9. // 8 Valve
  10. // 9 Cold
  11. // Libraries
  12. #include "SPI.h"
  13. #include "WiFi101.h"
  14. #include "LiquidCrystal.h"
  15. #include "Servo.h"
  16. #include "CapacitiveSensor.h"
  17. // WiFi
  18. char ssid[] = "NETWORK";
  19. char pass[] = "PASSWORD";
  20. int keyIndex = 0;
  21. int status = WL_IDLE_STATUS;
  22. WiFiServer server(80);
  23. // ThingSpeak
  24. char thingSpeakAddress[] = "api.thingspeak.com";
  25. String APIKey = "XXXXXXXX"; // API
  26. const int updateThingSpeakInterval = 10 * 1000; // Interval
  27. long lastConnectionTime = 0;
  28. boolean lastConnected = false;
  29. WiFiClient client;
  30. // LCD
  31. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  32. // Servos
  33. Servo hot;
  34. Servo valve;
  35. Servo cold;
  36. int openturn = 0;
  37. int closeturn = 180;
  38. int noturn = 90;
  39. // Servo Position Delays
  40. int hotdelay = 450;
  41. int valvedelay = 700;
  42. int colddelay = 300;
  43. // Temperature
  44. int temptub = A0;
  45. int temphead = A1;
  46. int templow = 0;
  47. int temphigh = 0;
  48. int tempc = 0;
  49. int pretempf = 0;
  50. // Temperature Variable
  51. int settemp = 42;
  52. int preptemp = 0;
  53. int tempvarlow = 0;
  54. int tempvarhigh = 0;
  55. // Map Variables
  56. int lowvol = 400;
  57. int highvol = 1005;
  58. int lowc = 56;
  59. int highc = 131;
  60. // On Off Button
  61. CapacitiveSensor onoffbutton = CapacitiveSensor(0, 1);
  62. int touchpeak = 150;
  63. // State
  64. // 0 = Off
  65. // 1 = Turn On
  66. // 2 = On
  67. // 3 = Shut Off
  68. int state = 0;
  69. void setup() {
  70. Serial.begin(9600);
  71. // Servos
  72. hot.attach(7);
  73. valve.attach(8);
  74. cold.attach(9);
  75. hot.write(90);
  76. valve.write(90);
  77. cold.write(90);
  78. // Temperature
  79. pinMode(temptub, INPUT);
  80. pinMode(temphead, INPUT);
  81. // LCD
  82. lcd.begin(16, 2);
  83. lcd.clear();
  84. // WIFI Connection
  85. while ( status != WL_CONNECTED) {
  86. lcd.setCursor(0, 0);
  87. lcd.print("Connecting...");
  88. lcd.setCursor(0, 1);
  89. lcd.print(ssid);
  90. Serial.print("Connecting Network: ");
  91. Serial.println(ssid);
  92. status = WiFi.begin(ssid, pass);
  93. delay(10000);
  94. }
  95. server.begin();
  96. lcd.clear();
  97. printWifiStatus();
  98. delay(3000);
  99. lcd.clear();
  100. }
  101. void loop() {
  102. // WiFi Server Page
  103. if (state == 0) {
  104. WiFiClient client = server.available();
  105. if (client) {
  106. String currentLine = "";
  107. while (client.connected()) {
  108. if (client.available()) {
  109. char c = client.read();
  110. Serial.write(c);
  111. if (c == '\n') {
  112. if (currentLine.length() == 0) {
  113. client.println("HTTP/1.1 200 OK");
  114. client.println("Content-type:text/html");
  115. client.println();
  116. client.print("");
  117. client.print("
    ");
  118. client.print("
    ");
  119. client.print(settemp);
  120. client.print("
  121. "
    );
  122. client.print("");
  123. client.print("");
  124. client.print("");
  125. client.print("");
  126. client.println();
  127. break;
  128. }
  129. else {
  130. currentLine = "";
  131. }
  132. }
  133. else if (c != '\r') {
  134. currentLine += c;
  135. }
  136. if (currentLine.endsWith("GET /U")) {
  137. settemp = settemp + 1;
  138. }
  139. if (currentLine.endsWith("GET /D")) {
  140. settemp = settemp - 1;
  141. }
  142. if (currentLine.endsWith("GET /S")) {
  143. state = 1;
  144. }
  145. }
  146. }
  147. client.stop();
  148. }
  149. }
  150. // On Off Button
  151. long touch = onoffbutton.capacitiveSensor(30);
  152. if (touch > touchpeak) {
  153. if (state == 0) {
  154. state = 1;
  155. }
  156. else if (state == 2) {
  157. state = 3;
  158. }
  159. }
  160. // Update ThingSpeak
  161. if (state == 1 || state == 2) {
  162. temphigh = analogRead(temphead);
  163. pretempf = map(temphigh, lowvol, highvol, lowc, highc);
  164. tempc = (pretempf - 32) * 5 / 9;
  165. String headtemperature = String(tempc, DEC);
  166. String runtime = String(state, DEC);
  167. if (millis() - lastConnectionTime > updateThingSpeakInterval) {
  168. updateThingSpeak("field1=" + runtime + "&field2=" + headtemperature);
  169. }
  170. }
  171. // State Operation
  172. if (state == 1) {
  173. turnon();
  174. }
  175. else if (state == 2) {
  176. shower();
  177. }
  178. else if (state == 3) {
  179. turnoff();
  180. }
  181. }
  182. void printWifiStatus() {
  183. Serial.print("SSID: ");
  184. Serial.println(WiFi.SSID());
  185. IPAddress ip = WiFi.localIP();
  186. Serial.print("http://");
  187. Serial.println(ip);
  188. long rssi = WiFi.RSSI();
  189. Serial.print("RSSI: ");
  190. Serial.print(rssi);
  191. Serial.println(" dBm");
  192. lcd.setCursor(0, 0);
  193. lcd.print(WiFi.SSID());
  194. lcd.setCursor(0, 1);
  195. lcd.print(ip);
  196. }
  197. void updateThingSpeak(String tsData) {
  198. if (client.connect(thingSpeakAddress, 80)) {
  199. client.print("POST /update HTTP/1.1\n");
  200. client.print("Host: api.thingspeak.com\n");
  201. client.print("Connection: close\n");
  202. client.print("X-THINGSPEAKAPIKEY: " + APIKey + "\n");
  203. client.print("Content-Type: application/x-www-form-urlencoded\n");
  204. client.print("Content-Length: ");
  205. client.print(tsData.length());
  206. client.print("\n\n");
  207. client.print(tsData);
  208. lastConnectionTime = millis();
  209. }
  210. }
  211. void turnon() {
  212. // LCD Temperature Display
  213. lcd.clear();
  214. lcd.setCursor(0, 0);
  215. lcd.print("Set Temp:");
  216. lcd.setCursor(10, 0);
  217. lcd.print(settemp);
  218. lcd.setCursor(0, 1);
  219. lcd.print("Current:");
  220. lcd.setCursor(9, 1);
  221. lcd.print("0");
  222. // Hot Open Valve Tub Cold Closed
  223. hot.write(180);
  224. delay(hotdelay);
  225. hot.write(90);
  226. delay(2000);
  227. preptemp = settemp - 2;
  228. // Wait For Temp
  229. while (preptemp > tempc) {
  230. templow = analogRead(temptub);
  231. pretempf = map(templow, lowvol, highvol, lowc, highc);
  232. tempc = (pretempf - 32) * 5 / 9;
  233. lcd.setCursor(9, 1);
  234. lcd.print(tempc);
  235. String headtemperature = String(tempc, DEC);
  236. if (millis() - lastConnectionTime > updateThingSpeakInterval) {
  237. updateThingSpeak("&field2=" + headtemperature);
  238. }
  239. }
  240. // Hot Open Valve Tub Cold Open
  241. cold.write(180);
  242. delay(colddelay);
  243. cold.write(90);
  244. // Delay For While Statement
  245. delay(2000);
  246. // Wait For Temp
  247. while (preptemp > tempc) {
  248. templow = analogRead(temptub);
  249. pretempf = map(templow, lowvol, highvol, lowc, highc);
  250. tempc = (pretempf - 32) * 5 / 9;
  251. lcd.setCursor(9, 1);
  252. lcd.print(tempc);
  253. String headtemperature = String(tempc, DEC);
  254. if (millis() - lastConnectionTime > updateThingSpeakInterval) {
  255. updateThingSpeak("&field2=" + headtemperature);
  256. }
  257. }
  258. // Hot Open Valve Head Cold Open
  259. valve.write(180);
  260. delay(valvedelay);
  261. valve.write(90);
  262. // Set State On
  263. state = 2;
  264. // Update State ThingSpeak
  265. String runtime = String(state, DEC);
  266. updateThingSpeak("field1=" + runtime);
  267. }
  268. void shower() {
  269. // Get Temp Shower Head
  270. temphigh = analogRead(temphead);
  271. pretempf = map(temphigh, lowvol, highvol, lowc, highc);
  272. tempc = (pretempf - 32) * 5 / 9;
  273. // Print Shower Temp
  274. lcd.setCursor(9, 1);
  275. lcd.print(tempc);
  276. // Set Temperature Leeway
  277. tempvarlow = settemp - 2;
  278. tempvarhigh = settemp + 2;
  279. // Adjust Temperature
  280. if (tempc < tempvarlow) {
  281. cold.write(0);
  282. delay(100);
  283. cold.write(90);
  284. colddelay = colddelay - 100;
  285. delay(2000);
  286. }
  287. else if (tempc > tempvarhigh) {
  288. cold.write(180);
  289. delay(100);
  290. cold.write(90);
  291. colddelay = colddelay + 100;
  292. delay(2000);
  293. }
  294. }
  295. void turnoff() {
  296. // Get Temp Shower Head
  297. temphigh = analogRead(temphead);
  298. pretempf = map(temphigh, lowvol, highvol, lowc, highc);
  299. tempc = (pretempf - 32) * 5 / 9;
  300. // Print Shower Temp
  301. lcd.setCursor(9, 1);
  302. lcd.print(tempc);
  303. // Close Valve
  304. valve.write(0);
  305. delay(valvedelay);
  306. valve.write(90);
  307. // Close Hot
  308. hot.write(0);
  309. delay(hotdelay);
  310. hot.write(90);
  311. // Close Cold
  312. cold.write(0);
  313. delay(colddelay);
  314. cold.write(90);
  315. // Reset All States
  316. state = 0;
  317. templow = 0;
  318. temphigh = 0;
  319. tempc = 0;
  320. colddelay = 400;
  321. delay(1000);
  322. // Update ThingSpeak FINAL
  323. String headtemperature = String(tempc, DEC);
  324. String runtime = String(state, DEC);
  325. updateThingSpeak("field1=" + runtime + "&field2=" + headtemperature);
  326. delay(5000);
  327. // Clear LCD
  328. lcd.clear();
  329. }
Update(s)-
1. ptmr | 343025 [Changelog]
10:22 343 025
Dev-
1. TVShow (227) 'CSA'
2. Wedge (Miter) 1.0.0
3. TVShow (228) 'APT'
4. TVProgram (83) 'BXT'
11:51 339 025

Menu
Index
Engineering
Entertainment
Literature
Miscellaneous
Contact
Search
tiktok.com/@vgmlr
youtube.com/@vgmlr
Miter
@vgmlr
=SUM(parts)
86 miters
131 tenons
Subscribe
0.00107