Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Rui Santos
- Complete project details at https://RandomNerdTutorials.com
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files.
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- */
- // Import required libraries
- #include <ESP8266WiFi.h>
- #include <ESPAsyncTCP.h>
- #include <ESPAsyncWebServer.h>
- #include <FS.h>
- #include <Wire.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include "LittleFS.h"
- // #include <Adafruit_Sensor.h>
- // #include <Adafruit_BME280.h>
- OneWire oneWire(0); //oneWire instance to communicate with any OneWire devices
- DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
- // Adafruit_BME280 bme; // I2C
- //Adafruit_BME280 bme(BME_CS); // hardware SPI
- //Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
- // Replace with your network credentials
- const char* ssid = "xxxx"; // wifi ssid
- const char* password = "xxx"; // wifi password
- // Set LED GPIO
- const int ledPin = 2;
- // Stores LED state
- String ledState;
- // Create AsyncWebServer object on port 80
- AsyncWebServer server(80);
- String getTemperature() {
- float temperature = get_temperature_from_ds18b20(0);
- // Read temperature as Fahrenheit (isFahrenheit = true)
- //float t = dht.readTemperature(true);
- Serial.println(temperature);
- return String(temperature);
- }
- float get_temperature_from_ds18b20(int sensorIndex){
- sensors.requestTemperatures();
- float temp = sensors.getTempCByIndex(sensorIndex);
- Serial.print("ds18b20 Temp= ");
- Serial.println(temp);
- return temp;
- }
- String getHumidity() {
- return String(1);
- }
- String getPressure() {
- return String(1);
- }
- // Replaces placeholder with LED state value
- String processor(const String& var){
- Serial.println(var);
- if(var == "STATE"){
- if(digitalRead(ledPin)){
- ledState = "ON";
- }
- else{
- ledState = "OFF";
- }
- Serial.print(ledState);
- return ledState;
- }
- else if (var == "TEMPERATURE"){
- return getTemperature();
- }
- else if (var == "HUMIDITY"){
- return getHumidity();
- }
- else if (var == "PRESSURE"){
- return getPressure();
- }
- }
- void setup(){
- // Serial port for debugging purposes
- Serial.begin(115200);
- pinMode(ledPin, OUTPUT);
- sensors.begin(); // Start up the Dallas Temperature library
- // Initialize the sensor
- // if (!bme.begin(0x76)) {
- // Serial.println("Could not find a valid BME280 sensor, check wiring!");
- // while (1);
- // }
- initialize_SPIFFS();
- if(!LittleFS.begin()){
- Serial.println("An Error has occurred while mounting LittleFS");
- return;
- }
- // Connect to Wi-Fi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi..");
- }
- // Print ESP32 Local IP Address
- Serial.println(WiFi.localIP());
- // Route for root / web page
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send(SPIFFS, "/index.html", String(), false, processor);
- });
- // Route to load style.css file
- server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send(SPIFFS, "/style.css", "text/css");
- });
- // Route to set GPIO to HIGH
- server.on("/on", HTTP_GET, [](AsyncWebServerRequest *request){
- digitalWrite(ledPin, HIGH);
- request->send(SPIFFS, "/index.html", String(), false, processor);
- });
- // Route to set GPIO to LOW
- server.on("/off", HTTP_GET, [](AsyncWebServerRequest *request){
- digitalWrite(ledPin, LOW);
- request->send(SPIFFS, "/index.html", String(), false, processor);
- });
- server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/plain", getTemperature().c_str());
- });
- server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/plain", getHumidity().c_str());
- });
- server.on("/pressure", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/plain", getPressure().c_str());
- });
- Serial.println("Starting server ...");
- // Start server
- server.begin();
- Serial.println("Done.");
- }
- void loop(){
- }
- void initialize_SPIFFS(){
- // FUNCION PARA INICIAR Y COMPROBAR FICHEROS EN SPI Flash File System
- Serial.printf("\nInitializing spiffs...");
- LittleFS.begin();
- if(!LittleFS.begin()){
- Serial.println("An Error has occurred while mounting LittleFS");
- return;
- }
- Serial.printf("\nFound following files in SPIFFS:\n");
- for (auto folder : { "/", "/data" }){
- Dir root = LittleFS.openDir(folder);
- while (root.next()) {
- File file = root.openFile("r");
- Serial.printf("\t%s%s (size=%i)\n", folder, root.fileName(), file.size());
- // Serial.println(" CONTENT(start): ============================================= ");
- // while(file.available()){
- // Serial.write(file.read());
- // }
- // Serial.println("");
- // Serial.println(" CONTENT(end): ============================================= ");
- file.close();
- }
- }
- Serial.println("End of initializing spiffs.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement