Skip to content

Commit

Permalink
Merge pull request #7 from JvPeek/LittleFS
Browse files Browse the repository at this point in the history
Migrated to LittleFS
  • Loading branch information
cin3m authored Sep 24, 2023
2 parents aa58c8c + ddd5269 commit 77b80c5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ lib_deps =
tzapu/WiFiManager@^0.15.0
ArduinoOTA
plerup/EspSoftwareSerial@^8.1.0

board_build.filesystem = littlefs
[env:d1_mini_OTA]
platform = espressif8266
board = d1_mini
Expand Down
5 changes: 0 additions & 5 deletions src/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ VISCACommand movement(uint8_t cam) {

byte focusValues[4];
convertValues(focus, focusValues);
Serial.println();
Serial.println(focusValues[0]);
Serial.println(focusValues[1]);
Serial.println(focusValues[2]);
Serial.println(focusValues[3]);
byte cmd[] = {0x01, 0x04,
0x38, (focus == -1 ? 0x02 : 0x03),
0xff, (0x81 + cam),
Expand Down
37 changes: 19 additions & 18 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <ESP8266mDNS.h>
#include <FS.h> //this needs to be first, or it all crashes and burns...
//#include <FS.h> //this needs to be first, or it all crashes and burns...
#include "LittleFS.h"
// needed for library

#include <DNSServer.h>
Expand Down Expand Up @@ -60,12 +61,12 @@ void setup() {
// read configuration from FS json
debugPrintln("mounting FS...");

if (SPIFFS.begin()) {
if (LittleFS.begin()) {
debugPrintln("mounted file system");
if (SPIFFS.exists("/config.json")) {
if (LittleFS.exists("/config.json")) {
// file exists, reading and loading
debugPrintln("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
File configFile = LittleFS.open("/config.json", "r");
if (configFile) {
debugPrintln("opened config file");
size_t size = configFile.size();
Expand All @@ -74,11 +75,7 @@ void setup() {

configFile.readBytes(buf.get(), size);
DynamicJsonDocument jsonBuffer(1024);
//JsonObject& json = jsonBuffer.parseObject(buf.get());
DeserializationError deserializeError = deserializeJson(jsonBuffer,buf.get());
//Old dump into Serial
//json.printTo(Serial);
Serial.println(String(jsonBuffer["mqtt_server"]).c_str());
if (!deserializeError) {
debugPrintln("\nparsed json");
mqtt_server = String(jsonBuffer["mqtt_server"]);
Expand All @@ -99,11 +96,16 @@ void setup() {
// The extra parameters to be configured (can be either global or just in
// the setup) After connecting, parameter.getValue() will get you the
// configured value id/name placeholder/prompt default length
if(mqtt_basetopic == "") {
const char* mqtt_basetopic = "VISCA";
Serial.println(mqtt_basetopic);
}

WiFiManagerParameter custom_mqtt_server("server", "mqtt server",mqtt_server.c_str(), 40);
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", String(mqtt_port).c_str(), 6);
WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user.c_str(), 40);
WiFiManagerParameter custom_mqtt_password("password", "mqtt password", mqtt_password.c_str(), 40);
WiFiManagerParameter custom_mqtt_basetopic("basetopic", "mqtt basetopic", "VISCA", 128);
WiFiManagerParameter custom_mqtt_basetopic("basetopic", "mqtt basetopic", mqtt_basetopic.c_str(), 128);

// WiFiManager
// Local intialization. Once its business is done, there is no need to keep
Expand Down Expand Up @@ -190,7 +192,7 @@ void setup() {
debugPrintln("saving config");
//DynamicJsonBuffer jsonBuffer;
//JsonObject& json = jsonBuffer.createObject();
DynamicJsonDocument jsonBuffer(1024);
StaticJsonDocument<256> jsonBuffer;
//JsonObject& json = jsonBuffer.parseObject(buf.get());
//DeserializationError jsonError =
//deserializeJson(jsonBuffer,buf.get());
Expand All @@ -201,14 +203,14 @@ void setup() {
jsonBuffer["mqtt_password"] = custom_mqtt_password.getValue();
jsonBuffer["mqtt_basetopic"] = custom_mqtt_basetopic.getValue();

File configFile = SPIFFS.open("/config.json", "w");
File configFile = LittleFS.open("/config.json", "w");
if (!configFile) {
debugPrintln("failed to open config file for writing");
}

//json.printTo(Serial);
//json.printTo(configFile);
serializeJsonPretty(jsonBuffer, Serial);
//serializeJsonPretty(jsonBuffer, Serial);
serializeJson(jsonBuffer, configFile);
configFile.close();
// end save
Expand Down Expand Up @@ -284,7 +286,6 @@ void handleSerial() {
Serial.println(receivedByte);
client.publish("VISCA/return/camera/mqtt/", "Reading Data:");
client.publish("VISCA/return/camera/mqtt/", String(receivedByte, HEX).c_str());
//Serial.println(receivedByte, HEX);
switch (state) {
case IDLE:
if (receivedByte == 0x90) {
Expand Down Expand Up @@ -450,8 +451,8 @@ void callback(char* topic, byte* payload, unsigned int length) {
}
if (strcmp(topic, buildTopic("command/system/updateConfig").c_str()) == 0) {

File existingConfigFile = SPIFFS.open("/config.json", "r");
File newConfigFile = SPIFFS.open("/config.json", "r+");
File existingConfigFile = LittleFS.open("/config.json", "r");
File newConfigFile = LittleFS.open("/config.json", "r+");
//JSON-Object from storage
size_t size = existingConfigFile.size();
std::unique_ptr<char[]> buf(new char[size]);
Expand Down Expand Up @@ -490,9 +491,10 @@ void callback(char* topic, byte* payload, unsigned int length) {
ESP.restart();
}
if (strcmp(topic, buildTopic("command/system/getConfig").c_str()) == 0) {
if (SPIFFS.exists("/config.json")) {

if (LittleFS.exists("/config.json")) {
// file exists, reading and loading
File configFile = SPIFFS.open("/config.json", "r");
File configFile = LittleFS.open("/config.json", "r");
if (configFile) {
size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]);
Expand All @@ -501,7 +503,6 @@ void callback(char* topic, byte* payload, unsigned int length) {
deserializeJson(existingBuffer,buf.get());
char mqttResponse[256];
serializeJson(existingBuffer, mqttResponse);
serializeJsonPretty(existingBuffer, Serial);
client.publish(buildTopic("return/system").c_str(),mqttResponse);
}
}
Expand Down

0 comments on commit 77b80c5

Please sign in to comment.