Menu
Index
Engineering
Entertainment
Literature
Miscellaneous
Contact
Search
tiktok.com/@pkvi.xyz
scored.co/u/pkvi_mv
Why Ayh?
Miter
Miter
@pkvi
"...may not meet professional standards."
1041 miters
122 tenons
Subscribe
0.00195
Arduino IoT Shower


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


  1. // Arduino IoT Shower
  2. // pkvi
  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("<body bgcolor=\"#9999FF\">");
  117. client.print("<div align=\"center\">");
  118. client.print("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\" border:1px solid #000000;background-color:#FFFFFF;\"><tr><td style=\"padding:5px;font:50px tahoma;\">");
  119. client.print(settemp);
  120. client.print("</td></tr></table>");
  121. client.print("<a href=\"/U\"><img src=\"http://yoursite.com/uparrow.png\" border=\"0\"></a><a href=\"/D\"><img src=\"http://yoursite.com/downarrow.png\" border=\"0\"></a>");
  122. client.print("");
  123. client.print("<a href=\"/S\"><img src=\"http://yoursite.com/showeron.png\" border=\"0\"></a>");
  124. client.print("</div></body></html>");
  125. client.println();
  126. break;
  127. }
  128. else {
  129. currentLine = "";
  130. }
  131. }
  132. else if (c != '\r') {
  133. currentLine += c;
  134. }
  135. if (currentLine.endsWith("GET /U")) {
  136. settemp = settemp + 1;
  137. }
  138. if (currentLine.endsWith("GET /D")) {
  139. settemp = settemp - 1;
  140. }
  141. if (currentLine.endsWith("GET /S")) {
  142. state = 1;
  143. }
  144. }
  145. }
  146. client.stop();
  147. }
  148. }
  149. // On Off Button
  150. long touch = onoffbutton.capacitiveSensor(30);
  151. if (touch > touchpeak) {
  152. if (state == 0) {
  153. state = 1;
  154. }
  155. else if (state == 2) {
  156. state = 3;
  157. }
  158. }
  159. // Update ThingSpeak
  160. if (state == 1 || state == 2) {
  161. temphigh = analogRead(temphead);
  162. pretempf = map(temphigh, lowvol, highvol, lowc, highc);
  163. tempc = (pretempf - 32) * 5 / 9;
  164. String headtemperature = String(tempc, DEC);
  165. String runtime = String(state, DEC);
  166. if (millis() - lastConnectionTime > updateThingSpeakInterval) {
  167. updateThingSpeak("field1=" + runtime + "&field2=" + headtemperature);
  168. }
  169. }
  170. // State Operation
  171. if (state == 1) {
  172. turnon();
  173. }
  174. else if (state == 2) {
  175. shower();
  176. }
  177. else if (state == 3) {
  178. turnoff();
  179. }
  180. }
  181. void printWifiStatus() {
  182. Serial.print("SSID: ");
  183. Serial.println(WiFi.SSID());
  184. IPAddress ip = WiFi.localIP();
  185. Serial.print("http://");
  186. Serial.println(ip);
  187. long rssi = WiFi.RSSI();
  188. Serial.print("RSSI: ");
  189. Serial.print(rssi);
  190. Serial.println(" dBm");
  191. lcd.setCursor(0, 0);
  192. lcd.print(WiFi.SSID());
  193. lcd.setCursor(0, 1);
  194. lcd.print(ip);
  195. }
  196. void updateThingSpeak(String tsData) {
  197. if (client.connect(thingSpeakAddress, 80)) {
  198. client.print("POST /update HTTP/1.1\n");
  199. client.print("Host: api.thingspeak.com\n");
  200. client.print("Connection: close\n");
  201. client.print("X-THINGSPEAKAPIKEY: " + APIKey + "\n");
  202. client.print("Content-Type: application/x-www-form-urlencoded\n");
  203. client.print("Content-Length: ");
  204. client.print(tsData.length());
  205. client.print("\n\n");
  206. client.print(tsData);
  207. lastConnectionTime = millis();
  208. }
  209. }
  210. void turnon() {
  211. // LCD Temperature Display
  212. lcd.clear();
  213. lcd.setCursor(0, 0);
  214. lcd.print("Set Temp:");
  215. lcd.setCursor(10, 0);
  216. lcd.print(settemp);
  217. lcd.setCursor(0, 1);
  218. lcd.print("Current:");
  219. lcd.setCursor(9, 1);
  220. lcd.print("0");
  221. // Hot Open Valve Tub Cold Closed
  222. hot.write(180);
  223. delay(hotdelay);
  224. hot.write(90);
  225. delay(2000);
  226. preptemp = settemp - 2;
  227. // Wait For Temp
  228. while (preptemp > tempc) {
  229. templow = analogRead(temptub);
  230. pretempf = map(templow, lowvol, highvol, lowc, highc);
  231. tempc = (pretempf - 32) * 5 / 9;
  232. lcd.setCursor(9, 1);
  233. lcd.print(tempc);
  234. String headtemperature = String(tempc, DEC);
  235. if (millis() - lastConnectionTime > updateThingSpeakInterval) {
  236. updateThingSpeak("&field2=" + headtemperature);
  237. }
  238. }
  239. // Hot Open Valve Tub Cold Open
  240. cold.write(180);
  241. delay(colddelay);
  242. cold.write(90);
  243. // Delay For While Statement
  244. delay(2000);
  245. // Wait For Temp
  246. while (preptemp > tempc) {
  247. templow = analogRead(temptub);
  248. pretempf = map(templow, lowvol, highvol, lowc, highc);
  249. tempc = (pretempf - 32) * 5 / 9;
  250. lcd.setCursor(9, 1);
  251. lcd.print(tempc);
  252. String headtemperature = String(tempc, DEC);
  253. if (millis() - lastConnectionTime > updateThingSpeakInterval) {
  254. updateThingSpeak("&field2=" + headtemperature);
  255. }
  256. }
  257. // Hot Open Valve Head Cold Open
  258. valve.write(180);
  259. delay(valvedelay);
  260. valve.write(90);
  261. // Set State On
  262. state = 2;
  263. // Update State ThingSpeak
  264. String runtime = String(state, DEC);
  265. updateThingSpeak("field1=" + runtime);
  266. }
  267. void shower() {
  268. // Get Temp Shower Head
  269. temphigh = analogRead(temphead);
  270. pretempf = map(temphigh, lowvol, highvol, lowc, highc);
  271. tempc = (pretempf - 32) * 5 / 9;
  272. // Print Shower Temp
  273. lcd.setCursor(9, 1);
  274. lcd.print(tempc);
  275. // Set Temperature Leeway
  276. tempvarlow = settemp - 2;
  277. tempvarhigh = settemp + 2;
  278. // Adjust Temperature
  279. if (tempc < tempvarlow) {
  280. cold.write(0);
  281. delay(100);
  282. cold.write(90);
  283. colddelay = colddelay - 100;
  284. delay(2000);
  285. }
  286. else if (tempc > tempvarhigh) {
  287. cold.write(180);
  288. delay(100);
  289. cold.write(90);
  290. colddelay = colddelay + 100;
  291. delay(2000);
  292. }
  293. }
  294. void turnoff() {
  295. // Get Temp Shower Head
  296. temphigh = analogRead(temphead);
  297. pretempf = map(temphigh, lowvol, highvol, lowc, highc);
  298. tempc = (pretempf - 32) * 5 / 9;
  299. // Print Shower Temp
  300. lcd.setCursor(9, 1);
  301. lcd.print(tempc);
  302. // Close Valve
  303. valve.write(0);
  304. delay(valvedelay);
  305. valve.write(90);
  306. // Close Hot
  307. hot.write(0);
  308. delay(hotdelay);
  309. hot.write(90);
  310. // Close Cold
  311. cold.write(0);
  312. delay(colddelay);
  313. cold.write(90);
  314. // Reset All States
  315. state = 0;
  316. templow = 0;
  317. temphigh = 0;
  318. tempc = 0;
  319. colddelay = 400;
  320. delay(1000);
  321. // Update ThingSpeak FINAL
  322. String headtemperature = String(tempc, DEC);
  323. String runtime = String(state, DEC);
  324. updateThingSpeak("field1=" + runtime + "&field2=" + headtemperature);
  325. delay(5000);
  326. // Clear LCD
  327. lcd.clear();
  328. }