Advertisement
Guest User

Untitled

a guest
Nov 10th, 2022
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.36 KB | None | 0 0
  1. /*
  2.   Rui Santos
  3.   Complete project details at https://RandomNerdTutorials.com
  4.  
  5.   Permission is hereby granted, free of charge, to any person obtaining a copy
  6.   of this software and associated documentation files.
  7.  
  8.   The above copyright notice and this permission notice shall be included in all
  9.   copies or substantial portions of the Software.
  10. */
  11.  
  12. // Import required libraries
  13. #include <ESP8266WiFi.h>
  14. #include <ESPAsyncTCP.h>
  15. #include <ESPAsyncWebServer.h>
  16. #include <FS.h>
  17. #include <Wire.h>
  18. #include <OneWire.h>
  19. #include <DallasTemperature.h>
  20. #include "LittleFS.h"
  21.  
  22.  
  23. // #include <Adafruit_Sensor.h>
  24. // #include <Adafruit_BME280.h>
  25. OneWire oneWire(0);  //oneWire instance to communicate with any OneWire devices
  26. DallasTemperature sensors(&oneWire);  // Pass our oneWire reference to Dallas Temperature.
  27.  
  28. // Adafruit_BME280 bme; // I2C
  29. //Adafruit_BME280 bme(BME_CS); // hardware SPI
  30. //Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
  31.  
  32. // Replace with your network credentials
  33. const char* ssid = "xxxx";  // wifi ssid
  34. const char* password = "xxx";  // wifi password
  35.  
  36. // Set LED GPIO
  37. const int ledPin = 2;
  38. // Stores LED state
  39. String ledState;
  40.  
  41. // Create AsyncWebServer object on port 80
  42. AsyncWebServer server(80);
  43.  
  44. String getTemperature() {
  45.   float temperature = get_temperature_from_ds18b20(0);
  46.   // Read temperature as Fahrenheit (isFahrenheit = true)
  47.   //float t = dht.readTemperature(true);
  48.   Serial.println(temperature);
  49.   return String(temperature);
  50. }
  51. float get_temperature_from_ds18b20(int sensorIndex){
  52.   sensors.requestTemperatures();
  53.   float temp = sensors.getTempCByIndex(sensorIndex);  
  54.   Serial.print("ds18b20 Temp= ");
  55.   Serial.println(temp);
  56.   return temp;
  57. }
  58. String getHumidity() {
  59.   return String(1);
  60. }
  61.  
  62. String getPressure() {
  63.   return String(1);
  64. }
  65.  
  66. // Replaces placeholder with LED state value
  67. String processor(const String& var){
  68.   Serial.println(var);
  69.   if(var == "STATE"){
  70.     if(digitalRead(ledPin)){
  71.       ledState = "ON";
  72.     }
  73.     else{
  74.       ledState = "OFF";
  75.     }
  76.     Serial.print(ledState);
  77.     return ledState;
  78.   }
  79.   else if (var == "TEMPERATURE"){
  80.     return getTemperature();
  81.   }
  82.   else if (var == "HUMIDITY"){
  83.     return getHumidity();
  84.   }
  85.   else if (var == "PRESSURE"){
  86.     return getPressure();
  87.   }  
  88. }
  89.  
  90. void setup(){
  91.   // Serial port for debugging purposes
  92.   Serial.begin(115200);
  93.   pinMode(ledPin, OUTPUT);
  94.   sensors.begin(); // Start up the Dallas Temperature library
  95.  
  96.   // Initialize the sensor
  97.   // if (!bme.begin(0x76)) {
  98.   //   Serial.println("Could not find a valid BME280 sensor, check wiring!");
  99.   //   while (1);
  100.   // }
  101.   initialize_SPIFFS();
  102.     if(!LittleFS.begin()){
  103.     Serial.println("An Error has occurred while mounting LittleFS");
  104.     return;
  105.     }
  106.   // Connect to Wi-Fi
  107.   WiFi.begin(ssid, password);
  108.   while (WiFi.status() != WL_CONNECTED) {
  109.     delay(1000);
  110.     Serial.println("Connecting to WiFi..");
  111.   }
  112.  
  113.   // Print ESP32 Local IP Address
  114.   Serial.println(WiFi.localIP());
  115.  
  116.   // Route for root / web page
  117.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  118.     request->send(SPIFFS, "/index.html", String(), false, processor);
  119.   });
  120.  
  121.   // Route to load style.css file
  122.   server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){
  123.     request->send(SPIFFS, "/style.css", "text/css");
  124.   });
  125.  
  126.   // Route to set GPIO to HIGH
  127.   server.on("/on", HTTP_GET, [](AsyncWebServerRequest *request){
  128.     digitalWrite(ledPin, HIGH);    
  129.     request->send(SPIFFS, "/index.html", String(), false, processor);
  130.   });
  131.  
  132.   // Route to set GPIO to LOW
  133.   server.on("/off", HTTP_GET, [](AsyncWebServerRequest *request){
  134.     digitalWrite(ledPin, LOW);    
  135.     request->send(SPIFFS, "/index.html", String(), false, processor);
  136.   });
  137.  
  138.   server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
  139.     request->send_P(200, "text/plain", getTemperature().c_str());
  140.   });
  141.  
  142.   server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
  143.     request->send_P(200, "text/plain", getHumidity().c_str());
  144.   });
  145.  
  146.   server.on("/pressure", HTTP_GET, [](AsyncWebServerRequest *request){
  147.     request->send_P(200, "text/plain", getPressure().c_str());
  148.   });
  149.  
  150.     Serial.println("Starting server ...");
  151.   // Start server
  152.   server.begin();
  153.   Serial.println("Done.");
  154. }
  155.  
  156. void loop(){
  157.  
  158. }
  159. void initialize_SPIFFS(){
  160.     // FUNCION PARA INICIAR Y COMPROBAR FICHEROS EN SPI Flash File System
  161.     Serial.printf("\nInitializing spiffs...");
  162.     LittleFS.begin();
  163.     if(!LittleFS.begin()){
  164.     Serial.println("An Error has occurred while mounting LittleFS");
  165.     return;
  166.     }
  167.     Serial.printf("\nFound following files in SPIFFS:\n");
  168.     for (auto folder :  { "/", "/data" }){
  169.         Dir root = LittleFS.openDir(folder);
  170.         while (root.next()) {
  171.             File file = root.openFile("r");
  172.             Serial.printf("\t%s%s (size=%i)\n", folder, root.fileName(), file.size());
  173.             // Serial.println(" CONTENT(start): ============================================= ");
  174.             // while(file.available()){
  175.             //     Serial.write(file.read());
  176.             // }
  177.             // Serial.println("");
  178.             // Serial.println(" CONTENT(end):   ============================================= ");  
  179.             file.close();
  180.         }
  181.     }
  182.     Serial.println("End of initializing spiffs.");
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement