Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3.  
  4. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  5. IPAddress ip(192, 168, 1, 177);
  6. EthernetServer server(80);
  7. String readString;
  8.  
  9. const int statusPin = 8;
  10. const int tempSensor = A0;
  11. const int lysSensor = A1;
  12. const int buzzerPin = 9;
  13. const int blaaLysPin = 7;
  14. const int rodLysPin = 6;
  15.  
  16. void setup() {
  17. Serial.begin(9600);
  18.  
  19. pinMode(statusPin, OUTPUT);
  20. pinMode(buzzerPin, OUTPUT);
  21. pinMode(blaaLysPin, OUTPUT);
  22. pinMode(rodLysPin, OUTPUT);
  23.  
  24. Ethernet.begin(mac);
  25. Serial.print("server is at ");
  26. Serial.println(Ethernet.localIP());
  27. }
  28.  
  29. int loggeData = 1;
  30.  
  31. void loop() {
  32. int tempSensorVerdi = analogRead(tempSensor);
  33. float volt = (tempSensorVerdi / 1024.0) * 5.0;
  34. float temperatur = (volt - .5) * 100;
  35.  
  36. float lysSensorVerdi = analogRead(lysSensor);
  37.  
  38. EthernetClient client = server.available();
  39. if (client) {
  40. while (client.connected()) {
  41. if (client.available()) {
  42. char c = client.read();
  43. Serial.print(c);
  44.  
  45. if (readString.length() < 100) {
  46. readString += c;
  47. }
  48.  
  49. if (c == '\n') {
  50. Serial.println(readString);
  51. client.println("HTTP/1.1 200 OK");
  52. client.println("Content-Type: text/html");
  53. client.println("Connection: close");
  54. client.println("Refresh: 5"); // refresh the page automatically every 5 sec
  55. client.println();
  56. client.println("<!DOCTYPE HTML>");
  57. client.println("<html>");
  58. client.println("<head>");
  59. client.println("<title>Lab-7</title>");
  60. client.println("<meta charset=\"utf-8\"/>");
  61. client.println("</head>");
  62. client.println("<body>");
  63. if (loggeData == 1) {
  64. client.println("Temperatur: ");
  65. client.println(temperatur);
  66. client.println(" C");
  67. client.println("<br />");
  68. client.println("Lys: ");
  69. client.print(lysSensorVerdi);
  70. client.println("<br /><br />");
  71.  
  72. digitalWrite(statusPin, HIGH);
  73. }
  74. else {
  75. digitalWrite(statusPin, LOW);
  76. }
  77. client.println("<a href=\"/?monitoron\"\"><button>Skru på målinger</button></a>");
  78. client.println("<a href=\"/?monitoroff\"\"><button>Skru på målinger</button></a>");
  79. client.println("<br />");
  80. client.println("<button id=\"btnBlaa\">Blå LED</button>");
  81. client.println("</body>");
  82. client.println("</html>");
  83.  
  84. delay(1);
  85. client.stop();
  86.  
  87. if (readString.indexOf("?monitoron") > 0) {
  88. loggeData = 1;
  89. }
  90. if (readString.indexOf("?monitoroff") > 0) {
  91. loggeData = 0;
  92. }
  93. readString = "";
  94. }
  95. }
  96. }
  97.  
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement