Untitled
unknown
c_cpp
a year ago
3.8 kB
2
Indexable
Never
#include <OneWire.h> #include <DallasTemperature.h> #include <WiFi.h> #include <WebServer.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <SPIFFS.h> #define ONE_WIRE_BUS 10 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); const char* ssid = "taller"; const char* password = "fedecapo"; WebServer server(80); #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); File tempFile; volatile bool buttonState1 = false; volatile bool buttonState2 = false; volatile bool buttonState3 = false; bool loggingEnabled = false; void IRAM_ATTR handleButton1() { buttonState1 = !buttonState1; } void IRAM_ATTR handleButton2() { buttonState2 = !buttonState2; } void IRAM_ATTR handleButton3() { buttonState3 = !buttonState3; } void handleRoot() { String html = "<html><head><meta http-equiv=\"refresh\" content=\"5\"></head><body>"; html += "<h1>Temperatura</h1>"; sensors.requestTemperatures(); float tempC = sensors.getTempCByIndex(0); html += "<p>Temperatura: " + String(tempC) + " ℃</p>"; html += "</body></html>"; server.send(200, "text/html", html); } void handleDownload() { if (server.hasArg("download")) { tempFile = SPIFFS.open("/temperature.txt", "r"); if (tempFile) { server.sendHeader("Content-Type", "text/plain"); server.sendHeader("Content-Disposition", "attachment; filename=temperature.txt"); server.streamFile(tempFile, "application/octet-stream"); tempFile.close(); } else { server.send(404, "text/plain", "File not found"); } } else { String html = "<html><body>"; html += "<h1>Descargar archivo de temperatura</h1>"; html += "<p><a href=\"/download?download\">Descargar</a></p>"; html += "</body></html>"; server.send(200, "text/html", html); } } void setup() { Serial.begin(115200); if (!SPIFFS.begin(true)) { Serial.println("Error al inicializar SPIFFS"); } tempFile = SPIFFS.open("/temperature.txt", "w"); if (!tempFile) { Serial.println("Error al crear archivo de temperatura"); } sensors.begin(); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(0,0); display.println("Conectando"); display.display(); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Conectando a WiFi.."); } Serial.println("Conectado a WiFi"); display.clearDisplay(); display.setCursor(0,0); display.println("Conectado!"); display.println(WiFi.localIP()); display.display(); pinMode(25, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(25), handleButton1, FALLING); pinMode(26, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(26), handleButton2, FALLING); pinMode(27, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(27), handleButton3, FALLING); server.on("/", handleRoot); server.on("/download", handleDownload); server.begin(); } void loop() { server.handleClient(); if (buttonState1) { loggingEnabled = !loggingEnabled; buttonState1 = false; if (loggingEnabled) {Serial.println("Comienzo registro");} else {Serial.println("Detengo registro");} } sensors.requestTemperatures(); float tempC = sensors.getTempCByIndex(0); if (loggingEnabled) { tempFile.println(tempC); } display.clearDisplay(); display.setCursor(0,0); display.println(WiFi.localIP()); display.println(tempC); display.display(); }