Guest User

Untitled

a guest
Apr 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. //while connected print this
  2. Serial.println("Wifi connected");
  3.  
  4. //starting the server
  5. server.begin();
  6. Serial.println("Server started.");
  7.  
  8. //get the ip address and print it
  9. Serial.print("This is your ip address: ");
  10. Serial.print("http://");
  11. Serial.print(WiFi.localIP());
  12. Serial.println("/");
  13.  
  14. //wait for the data to be sent from client
  15. Serial.println("New client connection");
  16. while(!client.available())
  17. {
  18. delay(1);
  19. }
  20.  
  21. //Read the first line of the request
  22. String request = client.readStringUntil('r');
  23. Serial.println(request);
  24. client.flush();
  25.  
  26. //check the index of the browser and act accordingly
  27. if (request.indexOf("/Thermo=ON") != -1)
  28. {
  29. digitalWrite(ledPin, HIGH);
  30. value = HIGH;
  31. sensor = analogRead(A0);
  32. temperature = (refvoltage * sensor * 100) / 1023; //converting degree to celcius
  33.  
  34. //save the data to mysql, access the php file to write
  35. HTTPClient http;
  36. String url = "http://localhost/iotproject/add.php?temp="+String(temp);
  37. Serial.println(url);
  38. http.begin(url);
  39.  
  40. //GET method
  41. int httpCode = http.GET();
  42. if(httpCode > 0)
  43. {
  44. Serial.printf("[HTTP] GET...code: %dn", httpCode);
  45. if(httpCode == HTTP_CODE_OK)
  46. {
  47. String payload = http.getString();
  48. Serial.println(payload);
  49. }
  50. }
  51. else
  52. {
  53. Serial.printf("[HTTP] GET... failed, error: %sn", http.errorToString(httpCode).c_str());
  54. }
  55. http.end();
  56. delay(3000);
  57.  
  58. if (request.indexOf("/Thermo=OFF") != -1)
  59. {
  60. digitalWrite(ledPin, LOW);
  61. value = LOW;
  62. }
  63. temp = temperature;
  64.  
  65. //Return the response
  66. client.println("HTTP/1.1 200 OK");
  67. client.println("Content-Type: text/html");
  68.  
  69. if (request.indexOf("/AlwaysOn") != -1)
  70. {
  71. digitalWrite(ledPin, HIGH);
  72. value = HIGH;
  73. sensor = analogRead(A0);
  74. temperature = (refvoltage * sensor * 100) / 1023; //converting degree to celcius
  75. //temp = temperature;
  76. client.println("Refresh: 5");
  77. }
  78.  
  79. client.println("");
  80. client.println("<!DOCTYPE HTML>");
  81. client.println("<html>");
  82. client.println("<body style=background-color:skyblue> </body>");
  83. client.println("<style> h1 {text-align: center}</style>");
  84. client.println("<style> h3 {text-align: center}</style>");
  85. client.println("<style> b {text-align: center}</style>");
  86. client.println("<style> p {text-align: center}</style>");
  87. client.println("<head><style>div.relative{position:relative; left:200px; height:20px; width:350px; border:2px solid #73AD21;}</style></head>");
  88. client.println();
  89.  
  90. client.println("<div class=relative> <b> UNIVERSITY OF TURKU </b> </div>");
  91. client.println("<div class=relative> Ashish Shrestha </div>");
  92. client.println("<div class = relative> Rituraj Nepal </div>");
  93.  
  94. //client.println("<br>");
  95. client.println("<br><br><h1>IOT Final Project: THERMOMETER</h1><br><br>");
  96. //client.println("<h3>Probable LM35 analog reading: <h3>");
  97. //client.print(sensor); //analog reading of the sensor
  98. //client.print("<br>");
  99.  
  100. client.print("<h3>Thermometer's current status: </h3>");
  101. if(request.indexOf("/AlwaysOn") != -1)
  102. {
  103. client.print("<h3>Always On<h3>");
  104. client.print("(Temperature updated every 5 seconds.)");
  105. client.println("<h3>Current Temperature: <h3>");
  106. client.print(temp);
  107. client.print("<b> deg C.</b>");
  108.  
  109. //save the data to mysql, with http client
  110. HTTPClient http;
  111. String url = "http://localhost/iotproject/add.php?temp="+String(temp); //access the php file to write data
  112. Serial.println(url);
  113. http.begin(url);
  114.  
  115. //using GET method to write to sql
  116. int httpCode = http.GET();
  117. if(httpCode > 0)
  118. {
  119. Serial.printf("[HTTP] GET...code: %dn", httpCode);
  120. if(httpCode == HTTP_CODE_OK)
  121. {
  122. String payload = http.getString();
  123. Serial.println(payload);
  124. }
  125. }
  126. else
  127. {
  128. Serial.printf("[HTTP] GET... failed, error: %sn", http.errorToString(httpCode).c_str());
  129. }
  130. http.end();
  131. //delay(3000);
  132. }
  133.  
  134. else if(value == HIGH)
  135. {
  136. client.print("<h3>On Once<h3>");
  137. //client.println("<br>");
  138. client.println("<h3>Current Temperature: <h3>");
  139. client.print(temp);
  140. client.print("<b> deg C.</b>");
  141. //delay(100);
  142. }
  143. else if(value == LOW)
  144. {
  145. client.print("<h3>Off<h3>");
  146. // client.println("<br>");
  147. client.println("<h3>Last measured temperature: <h3>"); //print the last value of temp
  148. client.print(temp);
  149. client.print("<b> deg C.</b>");
  150. }
  151.  
  152. client.println("<br><br>");
  153. //create buttons
  154. client.println("<a href="/Thermo=ON""><button>Turn On Once </button></a>");
  155. client.println("<a href="/AlwaysOn""><button>Keep It On </button></a>");
  156. client.println("<a href="/Thermo=OFF""><button>Turn Off </button></a><br />");
  157.  
  158. client.println("<h3>Check the Temperature records by following the below link: <h3>");
  159. client.println("<p>http://localhost/iotproject/show.php</p>");
  160.  
  161. // if there are incoming bytes available from the server, read them and print them:
  162. if (client.available())
  163. {
  164. char c = client.read();
  165. client.print(c);
  166. }
  167.  
  168. client.println("</html>");
  169.  
  170. client.println();
  171. delay(1);
  172. Serial.println("Client disconnected!");
  173. Serial.println("");
Add Comment
Please, Sign In to add comment