Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- const char* ssid = "XXXXXXXXXXXXX";
- const char* password = "XXXXXXXXXXXXX";
- const int SENSOR_PIN = D4; // Arduino pin connected to DS18B20 sensor's DQ pin
- OneWire oneWire(SENSOR_PIN); // setup a oneWire instance
- DallasTemperature tempSensor(&oneWire); // pass oneWire to DallasTemperature library
- int status = WL_IDLE_STATUS;
- WiFiServer server(80);
- float getTemperature() {
- tempSensor.requestTemperatures(); // send the command to get temperatures
- float tempCelsius = tempSensor.getTempCByIndex(0); // read temperature in Celsius
- return tempCelsius;
- }
- void setup() {
- //Initialize serial and wait for port to open:
- Serial.begin(115200);
- tempSensor.begin(); // initialize the temperature sensor
- WiFi.begin(ssid, password);
- //vérifie que le wi-fi est connecté au réseau wi-fi
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connecte!");
- Serial.print("Votre adresse IP: "); Serial.println(WiFi.localIP());
- server.begin();
- Serial.println("Serveur HTTP demarre");
- }
- //server.begin();
- // you're connected now, so print out the status:
- //printWifiStatus();
- void loop() {
- // listen for incoming clients
- //float temperature = getTemperature();
- //Serial.println(temperature, 2);
- WiFiClient client = server.available();
- if (client) {
- // read the HTTP request header line by line
- while (client.connected()) {
- if (client.available()) {
- String HTTP_header = client.readStringUntil('\n'); // read the header line of HTTP request
- if (HTTP_header.equals("\r")) // the end of HTTP request
- break;
- Serial.print("<< ");
- Serial.println(HTTP_header); // print HTTP request to Serial Monitor
- }
- }
- // send the HTTP response
- // send the HTTP response header
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: close"); // the connection will be closed after completion of the response
- client.println(); // the separator between HTTP header and body
- // send the HTTP response body
- float temperature = getTemperature();
- client.print(temperature, 2);
- client.flush();
- delay(10);
- client.stop();
- printWifiStatus();
- }
- }
- void printWifiStatus() {
- // print your board's IP address:
- Serial.print("IP Address: ");
- Serial.println(WiFi.localIP());
- // print the received signal strength:
- Serial.print("signal strength (RSSI):");
- Serial.print(WiFi.RSSI());
- Serial.println(" dBm");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement