Advertisement
Guest User

Untitled

a guest
Nov 6th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4.  
  5. const char* ssid = "XXXXXXXXXXXXX";
  6. const char* password = "XXXXXXXXXXXXX";
  7.  
  8. const int SENSOR_PIN = D4; // Arduino pin connected to DS18B20 sensor's DQ pin
  9.  
  10. OneWire oneWire(SENSOR_PIN); // setup a oneWire instance
  11. DallasTemperature tempSensor(&oneWire); // pass oneWire to DallasTemperature library
  12.  
  13. int status = WL_IDLE_STATUS;
  14.  
  15. WiFiServer server(80);
  16.  
  17. float getTemperature() {
  18. tempSensor.requestTemperatures(); // send the command to get temperatures
  19. float tempCelsius = tempSensor.getTempCByIndex(0); // read temperature in Celsius
  20. return tempCelsius;
  21. }
  22.  
  23. void setup() {
  24. //Initialize serial and wait for port to open:
  25. Serial.begin(115200);
  26. tempSensor.begin(); // initialize the temperature sensor
  27.  
  28. WiFi.begin(ssid, password);
  29.  
  30. //vérifie que le wi-fi est connecté au réseau wi-fi
  31. while (WiFi.status() != WL_CONNECTED) {
  32. delay(1000);
  33. Serial.print(".");
  34. }
  35. Serial.println("");
  36. Serial.println("WiFi connecte!");
  37. Serial.print("Votre adresse IP: "); Serial.println(WiFi.localIP());
  38.  
  39. server.begin();
  40. Serial.println("Serveur HTTP demarre");
  41. }
  42. //server.begin();
  43. // you're connected now, so print out the status:
  44. //printWifiStatus();
  45.  
  46. void loop() {
  47. // listen for incoming clients
  48. //float temperature = getTemperature();
  49. //Serial.println(temperature, 2);
  50. WiFiClient client = server.available();
  51. if (client) {
  52. // read the HTTP request header line by line
  53. while (client.connected()) {
  54. if (client.available()) {
  55. String HTTP_header = client.readStringUntil('\n'); // read the header line of HTTP request
  56.  
  57. if (HTTP_header.equals("\r")) // the end of HTTP request
  58. break;
  59.  
  60. Serial.print("<< ");
  61. Serial.println(HTTP_header); // print HTTP request to Serial Monitor
  62. }
  63. }
  64.  
  65. // send the HTTP response
  66. // send the HTTP response header
  67. client.println("HTTP/1.1 200 OK");
  68. client.println("Content-Type: text/html");
  69. client.println("Connection: close"); // the connection will be closed after completion of the response
  70. client.println(); // the separator between HTTP header and body
  71. // send the HTTP response body
  72. float temperature = getTemperature();
  73. client.print(temperature, 2);
  74. client.flush();
  75. delay(10);
  76. client.stop();
  77. printWifiStatus();
  78. }
  79.  
  80. }
  81.  
  82. void printWifiStatus() {
  83. // print your board's IP address:
  84. Serial.print("IP Address: ");
  85. Serial.println(WiFi.localIP());
  86.  
  87. // print the received signal strength:
  88. Serial.print("signal strength (RSSI):");
  89. Serial.print(WiFi.RSSI());
  90. Serial.println(" dBm");
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement