Guest User

Untitled

a guest
Feb 17th, 2019
91
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Load Wi-Fi library
  2. #include <WiFi.h>
  3.  
  4. //Load Temp Sensor Libraries
  5. #include <OneWire.h>
  6. #include <DallasTemperature.h>
  7.  
  8. //Load EEPROM Library
  9. #include <EEPROM.h>
  10.  
  11. // define the number of bytes you want to access
  12. #define EEPROM_SIZE 1
  13.  
  14. //Define temp probe pin
  15. #define ONE_WIRE_BUS 15
  16.  
  17. // set up 1-wire probes
  18. OneWire oneWire(ONE_WIRE_BUS);
  19. DallasTemperature sensors(&oneWire);
  20. DeviceAddress AIR_TEMP_SENSOR = {0x28, 0xFF, 0x16, 0x8D, 0x87, 0x16, 0x03, 0x50}; //Test sensor A
  21. DeviceAddress VAT_TEMP_SENSOR = {0x28, 0xFF, 0x0A, 0x2E, 0x68, 0x14, 0x04, 0xA6}; //Test sensor B
  22.  
  23. //variables for temp sensors
  24. float air_temp;
  25. float vat_temp;
  26.  
  27. //set temp variables
  28. float set_temp;
  29. float new_set_temp;
  30.  
  31. // Replace with your network credentials
  32. const char* ssid = "YOUR_WIFI_SSID";
  33. const char* password = "YOUR_PASSWORD";
  34.  
  35. // Set web server port number to 80
  36. WiFiServer server(80);
  37.  
  38. // Variable to store the HTTP request
  39. String header;
  40.  
  41. WiFiClient client = server.available();
  42.  
  43. void setup() {
  44. Serial.begin(115200);
  45.  
  46. //Start EEPROM at defined size (above).
  47. EEPROM.begin(EEPROM_SIZE);
  48.  
  49. // Set up temperature probes
  50. sensors.setResolution(AIR_TEMP_SENSOR, 11); //resolution of 0.125deg cels,
  51. sensors.setResolution(VAT_TEMP_SENSOR, 11); //takes approx 375ms
  52.  
  53. // Connect to Wi-Fi network with SSID and password
  54. Serial.print("Connecting to ");
  55. Serial.println(ssid);
  56. WiFi.begin(ssid, password);
  57. while (WiFi.status() != WL_CONNECTED) {
  58. delay(500);
  59. Serial.print(".");
  60. }
  61. // Print local IP address and start web server
  62. Serial.println("");
  63. Serial.println("WiFi connected.");
  64. Serial.println("IP address: ");
  65. Serial.println(WiFi.localIP());
  66. server.begin();
  67.  
  68. //set up set temp variables here.
  69. if (EEPROM.read(0) != 255) {
  70. set_temp = EEPROM.read(0);
  71. }
  72. if (EEPROM.read(0) == 255) {
  73. set_temp = 19.0;
  74. }
  75. new_set_temp = set_temp;
  76.  
  77.  
  78. }
  79.  
  80. void loop(){
  81.  
  82. //Get temps
  83. sensors.requestTemperaturesByAddress(AIR_TEMP_SENSOR);
  84. sensors.requestTemperaturesByAddress(VAT_TEMP_SENSOR);
  85. vat_temp = sensors.getTempC(VAT_TEMP_SENSOR);
  86. air_temp = sensors.getTempC(AIR_TEMP_SENSOR);
  87.  
  88. //run web server
  89. web_server();
  90.  
  91. //conditional statement to run webserver
  92. /*WiFiClient client = server.available(); // Listen for incoming clients
  93. if (client) {
  94. web_server();
  95. }*/
  96. }
  97.  
  98. void web_server(){
  99.  
  100. WiFiClient client = server.available(); // Listen for incoming clients
  101.  
  102. if (client) { // If a new client connects,
  103. Serial.println("New Client."); // print a message out in the serial port
  104. String currentLine = ""; // make a String to hold incoming data from the client
  105. while (client.connected()) { // loop while the client's connected
  106. if (client.available()) { // if there's bytes to read from the client,
  107. char c = client.read(); // read a byte, then
  108. Serial.write(c); // print it out the serial monitor
  109. header += c;
  110. if (c == 'n') { // if the byte is a newline character
  111. // if the current line is blank, you got two newline characters in a row.
  112. // that's the end of the client HTTP request, so send a response:
  113. if (currentLine.length() == 0) {
  114. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  115. // and a content-type so the client knows what's coming, then a blank line:
  116. client.println("HTTP/1.1 200 OK");
  117. client.println("Content-type:text/html");
  118. client.println("Connection: close");
  119. client.println();
  120.  
  121.  
  122. //Set Temp Changes By Button
  123. if (header.indexOf("GET /set/up") >= 0) {
  124. Serial.println("Increase New Set Temperature by 0.5");
  125. new_set_temp = new_set_temp + 0.5;
  126. }
  127. else if (header.indexOf("GET /set/down") >= 0) {
  128. Serial.println("Decrease New Set Temperature by 0.5");
  129. new_set_temp = new_set_temp - 0.5;
  130. }
  131. //Submit button for changing set temp
  132. if (header.indexOf("GET /set/submit") >= 0) {
  133. Serial.print("Change Set Temp To ");
  134. set_temp = new_set_temp;
  135. Serial.println(set_temp);
  136. EEPROM.write(0, set_temp);
  137. EEPROM.commit();
  138. Serial.println("EEPROM Write");
  139. }
  140.  
  141. // Display the HTML web page
  142. client.println("<!DOCTYPE html><html>");
  143. client.println("<head><meta name="viewport" content="width=device-width, initial-scale=1">");
  144. client.println("<link rel="icon" href="data:,">");
  145.  
  146. //CSS Styles
  147. client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center; font-size: 15px}");
  148. client.println(".button {background-color: lightgrey; color: black; padding: 10px 20px; text-decoration: none; font-size: 15px; margin: 2px; cursor: pointer; border-color: black; border-style: solid; border-radius: 10px;}");
  149. client.println(".temp {background-color: #4CAF50; color: white; padding: 10px 30px; text-decoration: none; font-size: 15px; margin: 2px; border-color: black; border-style: none; border-radius: 10px;}");
  150. client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
  151. client.println("</style></head>");
  152.  
  153. // Web Page Heading
  154. client.println("<body><h1>Laser Snake Temperature</h1>");
  155.  
  156. //Set Temp Line
  157. client.println("Set Temperature =");
  158. client.println("<inline-block class="temp">" );
  159. client.println(set_temp);
  160. client.println("&#8451</inline-block><br><br><br>");
  161.  
  162. //Change Set Temp Line
  163. client.println("New Set Temperature =");
  164. client.println("<inline-block class="temp">" );
  165. client.println(new_set_temp);
  166. client.println("&#8451</inline-block>");
  167. client.println("<a href="/set/up"><button class="button">+</button></a><a href="/set/down"><button class="button">-</button></a><a href="/set/submit"><button class="button">SUBMIT</button></a></p><br>");
  168.  
  169. //Fermenter Temp Line
  170. client.println("Fermenter Temperature =");
  171. client.println("<inline-block class="temp">" );
  172. client.println(vat_temp);
  173. client.println("&#8451</inline-block><br><br><br>");
  174.  
  175. //Fridge Temp Line
  176. client.println("Fridge Temperature =");
  177. client.println("<inline-block class="temp">" );
  178. client.println(air_temp);
  179. client.println("&#8451</inline-block><br><br><br>");
  180. client.println("</body></html>");
  181.  
  182. // The HTTP response ends with another blank line
  183. client.println();
  184. // Break out of the while loop
  185. break;
  186. } else { // if you got a newline, then clear currentLine
  187. currentLine = "";
  188. }
  189. } else if (c != 'r') { // if you got anything else but a carriage return character,
  190. currentLine += c; // add it to the end of the currentLine
  191. }
  192. }
  193. }
  194. // Clear the header variable
  195. header = "";
  196. // Close the connection
  197. client.stop();
  198. Serial.println("Client disconnected.");
  199. Serial.println("");
  200. }
  201. }
RAW Paste Data