1.5.20. fejezet, LittleFS

Kapcsolódó hivatkozások

Az ESP8266 vagy LOLIN(WEMOS) D1 R2 flash területén elhelyezett fájlrendszer kezelésére. A skatch könyvtárába helyezett data könyvtárból tölti fel a LittleFS root könyvtárba a fájlokat.
Alkalmas pl.: hálózati beállítások tárolására. Unix endline legyen minden sorvég jel (\n)

#include "LittleFS.h"
bool initialized = false;
 
void setup() {
  Serial.begin(9600);
  while(!Serial);
  Serial.println("Mounting LittleFS...");
 
  if(!LittleFS.begin()){
    Serial.println("An Error has occurred while mounting LittleFS");
    return;
  }
 
  File file = LittleFS.open("/config.ini", "r");
  if(!file){
    Serial.println("Failed to open file for reading");
    return;
  }
 
  Serial.println("File Content:");
  while(file.available()){
    Serial.write(file.read());
  }
  file.close();
}
 
void loop() {
 
}

IR távirányítóval és Weben keresztül vezérelhető világítás kapcsoló

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <IoAbstraction.h>
#include <IRremote.h>
#include "LittleFS.h"
 
String ssid = "";
String password = "";
String ip = "";
 
IoAbstractionRef ioDevice = ioUsingArduino();
FS fileSystem = LittleFS;
 
int buttonPin = 4;
int switchPin = 5;
int IR_RECEIVE_PIN = 2;
int ledPin = 0;
int switchState = LOW;
bool initStarted = true;
ESP8266WebServer server(80);
 
void onButtonClick(uint8_t pin, bool heldDown) {
  if (initStarted) {
    initStarted = false;
    Serial.println("Init state button click call. Switch state: " + String(switchState == HIGH ? "ON" : "OFF"));
    return;
  }
  if (!heldDown) {
    switchState = switchState == HIGH ? LOW : HIGH;
    Serial.println("Switch button pressed. Switch state: " + String(switchState == HIGH ? "ON" : "OFF"));
    ioDeviceDigitalWrite(ioDevice, switchPin, switchState);
    ioDeviceDigitalWrite(ioDevice, ledPin,!switchState);
  }
}
 
void handleNotFound() {
  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);  
}
 
void setup() {
  Serial.begin(9600);
  while(!Serial);
 
  ioDevicePinMode(ioDevice, switchPin, OUTPUT);
  ioDevicePinMode(ioDevice, ledPin, OUTPUT);
  ioDevicePinMode(ioDevice, buttonPin, INPUT);
 
  ioDeviceDigitalWrite(ioDevice, switchPin, LOW);
  ioDeviceDigitalWrite(ioDevice, ledPin, HIGH);
 
  switches.init(ioDevice, SWITCHES_POLL_EVERYTHING, true);
  switches.addSwitch(buttonPin, onButtonClick, NO_REPEAT);
 
  if(!LittleFS.begin()){
    Serial.println("An Error has occurred while mounting LittleFS");
    return;
  }
 
  File file = LittleFS.open("/config.ini", "r");
  if(!file){
    Serial.println("Failed to open file for reading");
    return;
  }
 
  String iniFileLine = "";
 
  Serial.println("File Content:");
  while(file.available()){
    iniFileLine = file.readStringUntil('\n');
    String propertyName = iniFileLine.substring(0,iniFileLine.indexOf("="));
    if (propertyName == "ssid") {
      ssid = iniFileLine.substring(iniFileLine.indexOf("=") + 1);
    } else if (propertyName == "password") {
      password = iniFileLine.substring(iniFileLine.indexOf("=") + 1);
    } else if (propertyName == "ip") {
      ip = iniFileLine.substring(iniFileLine.indexOf("=") + 1);
    }
  }
  file.close();
  Serial.println("ssid: [" + ssid + "]");
  Serial.println("password: [" + password + "]");
  Serial.println("ip: [" + ip + "]");
 
 
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid.c_str(), password.c_str());
  Serial.println("");
 
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
  Serial.println("Enabled IRin");
 
  server.on("/switch", []() {
    if (server.hasArg("light")) {
      switchState = server.arg("light") == "ON";
      digitalWrite(switchPin,switchState);
      digitalWrite(ledPin,!switchState);
      server.send(200, "text/plain", "{\"result\":\"ok\"}");
    } else {
      server.send(500, "text/plain", "{\"result\":\"no arg: light\"}");
    }
  });
 
  server.on("/getswitch", []() {
     String resp = String("{\"result\":\"") + (switchState ? "ON" : "OFF") + String("\"}");
      server.send(200, "text/plain", resp);
  });
 
  server.addHook([](const String & method, const String & url, WiFiClient * client, ESP8266WebServer::ContentTypeFunction contentType) {
    Serial.println(url);
    return ESP8266WebServer::CLIENT_REQUEST_CAN_CONTINUE;
  });
 
  server.onNotFound(handleNotFound);
 
  server.serveStatic("/index.html", fileSystem, "/index.html");
 
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  server.begin();
  Serial.println("HTTP server started");
}
 
void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
 
  server.handleClient();
  taskManager.runLoop();
  //IR v.3.9.0 !!!
  if (IrReceiver.decode()) {
    IrReceiver.printIRResultShort(&Serial);
    Serial.print("protocol: ");
    decode_type_t type = IrReceiver.decodedIRData.protocol;
    switch(type) {
        case PULSE_DISTANCE: Serial.println("PULSE_DISTANCE"); break ;
        case NEC: Serial.println("NEC"); break ;
        case NEC2: Serial.println("NEC2"); break;
        case SONY: Serial.println("SONY"); break ;
        case RC5: Serial.println("RC5"); break ;
        case RC6: Serial.println("RC6"); break ;
        case SHARP: Serial.println("SHARP"); break ;
        case JVC: Serial.println("JVC"); break ;
        case SAMSUNG: Serial.println("SAMSUNG"); break ;
        case SAMSUNG_LG: Serial.println("SAMSUNG_LG"); break;
        case LG: Serial.println("LG"); break ;
        case LG2: Serial.println("LG2"); break ;
        case PANASONIC: Serial.println("PANASONIC"); break ;
        case DENON: Serial.println("DENON"); break ;
        case KASEIKYO: Serial.println("KASEIKYO"); break ;
        case KASEIKYO_DENON: Serial.println("KASEIKYO_DENON"); break ;
        case KASEIKYO_SHARP: Serial.println("KASEIKYO_SHARP"); break;
        case KASEIKYO_JVC: Serial.println("KASEIKYO_JVC"); break;
        case KASEIKYO_MITSUBISHI: Serial.println("KASEIKYO_MITSUBISHI"); break;
        case ONKYO: Serial.println("ONKYO"); break;
        case APPLE: Serial.println("APPLE"); break;
        case BOSEWAVE: Serial.println("BOSEWAVE"); break;
        case LEGO_PF: Serial.println("LEGO_PF"); break;
        case MAGIQUEST: Serial.println("MAGIQUEST"); break;
        case WHYNTER: Serial.println("WHYNTER"); break;
      default:
        case UNKNOWN: Serial.println("UNKNOWN"); break ;
    }
    if (IrReceiver.decodedIRData.command==0xC) {
      switchState = switchState == HIGH ? LOW : HIGH;
      Serial.println("Switch button pressed. Switch state: " + String(switchState == HIGH ? "ON" : "OFF"));
      ioDeviceDigitalWrite(ioDevice, switchPin, switchState);
      ioDeviceDigitalWrite(ioDevice, ledPin, !switchState);
      delay(1000);    
    }
    IrReceiver.resume(); // Receive the next value  
  }
 
}