Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. /*
  2. Web Server DEZE WERKT
  3.  
  4. A simple web server that shows the value of the analog input pins.
  5. using an Arduino Wiznet Ethernet shield.
  6.  
  7. Circuit:
  8. * Ethernet shield attached to pins 10, 11, 12, 13
  9. * Analog inputs attached to pins A0 through A5 (optional)
  10.  
  11. created 18 Dec 2009
  12. by David A. Mellis
  13. modified 9 Apr 2012
  14. by Tom Igoe
  15. modified 02 Sept 2015
  16. by Arturo Guadalupi
  17.  
  18. */
  19.  
  20. #include <SPI.h>
  21. #include <Ethernet.h>
  22. #include "Seeed_BME280.h"
  23. #include <Wire.h>
  24.  
  25. BME280 bme280;
  26.  
  27. // Enter a MAC address and IP address for your controller below.
  28. // The IP address will be dependent on your local network:
  29. byte mac[] = {
  30. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  31. };
  32. IPAddress ip(89, 98, 44, 110);
  33.  
  34. // Initialize the Ethernet server library
  35. // with the IP address and port you want to use
  36. // (port 80 is default for HTTP):
  37. EthernetServer server(80);
  38.  
  39. void setup() {
  40. // You can use Ethernet.init(pin) to configure the CS pin
  41. //Ethernet.init(10); // Most Arduino shields
  42. //Ethernet.init(5); // MKR ETH shield
  43. //Ethernet.init(0); // Teensy 2.0
  44. //Ethernet.init(20); // Teensy++ 2.0
  45. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  46. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  47.  
  48. // Open serial communications and wait for port to open:
  49. Serial.begin(9600);
  50. while (!Serial) {
  51. ; // wait for serial port to connect. Needed for native USB port only
  52. }
  53. Serial.println("Ethernet WebServer Example");
  54.  
  55. // start the Ethernet connection and the server:
  56. Ethernet.begin(mac, ip);
  57.  
  58. // Check for Ethernet hardware present
  59. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  60. Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  61. while (true) {
  62. delay(1); // do nothing, no point running without Ethernet hardware
  63. }
  64. }
  65. if (Ethernet.linkStatus() == LinkOFF) {
  66. Serial.println("Ethernet cable is not connected.");
  67. }
  68.  
  69. // start the server
  70. server.begin();
  71. Serial.print("server is at ");
  72. Serial.println(Ethernet.localIP());
  73. {
  74. Serial.begin(9600);
  75. if(!bme280.init()){
  76. Serial.println("Device error!");
  77. }
  78. }
  79.  
  80. }
  81.  
  82.  
  83.  
  84. void loop() {
  85. // listen for incoming clients
  86. EthernetClient client = server.available();
  87. if (client) {
  88. Serial.println("new client");
  89. // an http request ends with a blank line
  90. boolean currentLineIsBlank = true;
  91. while (client.connected()) {
  92. if (client.available()) {
  93. char c = client.read();
  94. Serial.write(c);
  95. // if you've gotten to the end of the line (received a newline
  96. // character) and the line is blank, the http request has ended,
  97. // so you can send a reply
  98. if (c == '\n' && currentLineIsBlank) {
  99. // send a standard http response header
  100. client.println("HTTP/1.1 200 OK");
  101. client.println("Content-Type: text/html");
  102. client.println("Connection: close"); // the connection will be closed after completion of the response
  103. client.println("Refresh: 5"); // refresh the page automatically every 5 sec
  104. client.println();
  105. client.println("{");
  106. client.println("}");
  107.  
  108. float pressure;
  109.  
  110. //get and print temperatures
  111. client.print("\"Temp\": ");
  112. client.print("\"" + bme280.getTemperature() + "\", ");
  113.  
  114. //get and print atmospheric pressure data
  115. client.print("\"Pressure\": ");
  116. client.print("\"" + pressure = bme280.getPressure() + "\", ");
  117.  
  118. //get and print altitude data
  119. client.print("\"Altitude\": ");
  120. client.print("\"" + bme280.calcAltitude(pressure) + "\", ");
  121.  
  122. //get and print humidity data
  123. client.print("\"Humidity\": ");
  124. client.print("\"" + bme280.getHumidity() + "\"");
  125.  
  126. delay(1000);
  127. break;
  128. }
  129. if (c == '\n') {
  130. // you're starting a new line
  131. currentLineIsBlank = true;
  132. } else if (c != '\r') {
  133. // you've gotten a character on the current line
  134. currentLineIsBlank = false;
  135. }
  136. }
  137. }
  138. // give the web browser time to receive the data
  139. delay(1);
  140. // close the connection:
  141. client.stop();
  142. Serial.println("client disconnected");
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement