Lösung: Messung mit ESP32 und Anzeige im Browser
Completion requirements
Lösung vorgestellt von Florian Meier, Patrick Maul und Johannes Schrage (FI0F, 2022)
#include <WiFi.h> #include <WiFiClient.h> #include <WebServer.h> #include <ESPmDNS.h> #include <OneWire.h> #include <DallasTemperature.h> // Data wire is plugged TO GPIO 4 #define ONE_WIRE_BUS 4 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS); // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); // Number of temperature devices found int numberOfDevices; // We'll use this variable to store a found device address DeviceAddress tempDeviceAddress; const char* ssid = "iPhone von Patrick"; const char* password = "Eick2.0/"; WebServer server(80); const int led = 13; int tempUpdate = 3002; float tempC = 0; float tempF = 0; void handleRoot() { digitalWrite(led, 1); server.send(200, "text/plain", "hello from esp32!"); digitalWrite(led, 0); } void handleNotFound() { digitalWrite(led, 1); String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET) ? "GET" : "POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i = 0; i < server.args(); i++) { message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); digitalWrite(led, 0); } void setup(void) { pinMode(led, OUTPUT); digitalWrite(led, 0); Serial.begin(115200); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println(""); setupTemperature(); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); if (MDNS.begin("esp32")) { Serial.println("MDNS responder started"); } server.on("/", handleRoot); server.on("/temperature", []() { server.send(200, "text/html", "" "<html style='height: 100%;width: 100%;'>" "<body style='display: grid; place-items: center;font-size: 26px;height: 100%;width: 100%;'>" "<h1>" + String(tempC) + " Grad</h1>" "<script>setInterval(() => {fetch('/api/temperature').then(r => r.json()).then(r => document.querySelector('h1').innerText = r.tempC + ' Grad')}, 500)</script>" "</body>" "</html>"); }); server.on("/api/temperature", []() { server.send(200, "text/plain", "{\"tempC\":\"" + String(tempC) + "\",\"tempF\":\"" + String(tempF) + "\"}" ); }); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); } void loop(void) { server.handleClient(); delay(2);//allow the cpu to switch to other tasks if(tempUpdate > 1000) { tempUpdate = 0; updateTemperature(); delay(2); } else { tempUpdate += 8; } } /********* Rui Santos Complete project details at https://RandomNerdTutorials.com *********/ void setupTemperature() { // Start up the library sensors.begin(); // Grab a count of devices on the wire numberOfDevices = sensors.getDeviceCount(); // locate devices on the bus Serial.print("Locating temperature devices..."); Serial.print("Found "); Serial.print(numberOfDevices, DEC); Serial.println(" devices."); // Loop through each device, print out address for(int i=0;i<numberOfDevices; i++){ // Search the wire for address if(sensors.getAddress(tempDeviceAddress, i)){ Serial.print("Found device "); Serial.print(i, DEC); Serial.print(" with address: "); printAddress(tempDeviceAddress); Serial.println(); } else { Serial.print("Found ghost device at "); Serial.print(i, DEC); Serial.print(" but could not detect address. Check power and cabling"); } } } void updateTemperature() { sensors.requestTemperatures(); // Send the command to get temperatures // Loop through each device, print out temperature data for(int i=0;i<numberOfDevices; i++){ // Search the wire for address if(sensors.getAddress(tempDeviceAddress, i)){ // Output the device ID Serial.print("Temperature for device: "); Serial.println(i,DEC); // Print the data tempC = sensors.getTempC(tempDeviceAddress); tempF = DallasTemperature::toFahrenheit(tempC); Serial.print("Temp C: "); Serial.print(tempC); Serial.print(" Temp F: "); Serial.println(tempF); // Converts tempC to Fahrenheit Serial.print("IP address: "); Serial.println(WiFi.localIP()); } } } // function to print a device address void printAddress(DeviceAddress deviceAddress) { for (uint8_t i = 0; i < 8; i++){ if (deviceAddress[i] < 16) Serial.print("0"); Serial.print(deviceAddress[i], HEX); } }
Last modified: Thursday, 7 July 2022, 9:31 AM