Browse Source

Added storage way to write files. Fixed channel settings

master
Englebert 3 years ago
parent
commit
80f56bf04c
  1. 25
      src/Storage.cpp
  2. 4
      src/Storage.h
  3. 91
      src/Web.cpp
  4. 9
      src/Web.h
  5. 63
      src/main.cpp
  6. 9
      src/main.h
  7. 8
      src/sbus.cpp

25
src/Storage.cpp

@ -112,20 +112,23 @@ void Storage::listDir(fs::FS &fs, const char * dirname, uint8_t levels){
}
void Storage::readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\r\n", path);
String Storage::readFile(fs::FS &fs, String path){
// Serial.printf("Reading file: %s\r\n", path);
String return_str = "";
File file = fs.open(path, "r");
if(!file || file.isDirectory()){
Serial.println(F("- failed to open file for reading"));
return;
//// Serial.println(F("- failed to open file for reading"));
return "";
}
Serial.println(F("- read from file:"));
// Serial.println(F("- read from file:"));
while(file.available()){
Serial.write(file.read());
return_str = return_str + String((char)file.read());
}
file.close();
return return_str;
}
@ -156,20 +159,24 @@ void Storage::deleteFile(fs::FS &fs, const char * path){
}
void Storage::writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\r\n", path);
/// void Storage::writeFile(fs::FS &fs, const char * path, const char * message){
void Storage::writeFile(fs::FS &fs, String path, String message){
//// Serial.printf("Writing file: %s\r\n", path);
File file = fs.open(path, "w+");
if(!file){
Serial.println("- failed to open file for writing");
//// Serial.println("- failed to open file for writing");
return;
}
file.println(message);
/****
if(file.print(message)){
Serial.println("- file written");
} else {
Serial.println("- write failed");
}
****/
file.close();
}

4
src/Storage.h

@ -22,10 +22,10 @@ class Storage {
Storage(void);
bool begin(void);
void listDir(fs::FS &fs, const char * dirname, uint8_t levels);
void readFile(fs::FS &fs, const char * path);
String readFile(fs::FS &fs, String path);
void appendFile(fs::FS &fs, const char * path, const char * message);
void deleteFile(fs::FS &fs, const char * path);
void writeFile(fs::FS &fs, const char * path, const char * message);
void writeFile(fs::FS &fs, String path, String message);
// Emulate EEPROM write to LITTLEFS
// void write_block(uint8_t * data, const char * path, uint32_t len);

91
src/Web.cpp

@ -6,6 +6,8 @@ bool update_status = false;
bool opened = false;
File root;
String header_html_str = "<html><meta charset=\"UTF-8\"><title>SimpleRX</title><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"><body><pre>";
WEB::WEB(void){
}
@ -18,50 +20,69 @@ void WEB::begin(void) {
// File html_handler = LITTLEFS.open("/index.html.gz", FILE_READ);
// server.streamFile(html_handler, "text/html");
// html_handler.close();
server.send(200, "text/html", "SimpleRX 0.1");
String index_html_str = header_html_str + "<b>SimpleRX v" + String(VERSION_MAJOR) + "." + String(VERSION_MINOR) + String(VERSION_BUILD) + "\n\n[ MAIN ]\n</b><hr>CPU: " + String(ESP.getCpuFreqMHz()) + "MHz\n";
index_html_str = index_html_str + "NRF Channel: " + String(rx.getChannel());
server.send(200, "text/html", index_html_str);
server.sendHeader("Connection", "close");
});
/*** Temporary disable update for ESP8266
// Update
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK. Device is rebooting");
// Multiple configuration
server.on("/set", HTTP_GET, []() {
bool update_channel = false;
uint8_t channel_raw = 0;
String msg = "";
if(Update.hasError()) {
update_status = false;
// web.set_update_status(false);
} else {
delay(1000);
ESP.restart();
// Server parameters
for(uint8_t i = 0; i < server.args(); i++) {
String value = server.arg(i);
if(server.argName(i) == "channel") {
channel_raw = value.toInt();
update_channel = true;
}
}
}, []() {
HTTPUpload& upload = server.upload();
if(!update_status)
update_status = true;
if(upload.status == UPLOAD_FILE_START) {
Serial.printf("Update: %s\n", upload.filename.c_str());
if(update_channel) {
// Making sure is channel 1 ~ 125.
if(channel_raw > 0 && channel_raw < 126) {
String filename = "/NRF_CHANNEL";
msg = "CHANNEL SET: " + String(channel_raw);
storage.writeFile(LittleFS, filename, String(channel_raw));
//// File file = LittleFS.open(filename, "w+");
//// file.println(channel_raw);
//// file.close();
channel = channel_raw;
rx.setChannel(channel); // Instant Change
} else {
msg = "ERROR UPDATE CHANNEL";
}
}
// Start with maximum available size
if(!Update.begin(UPDATE_SIZE_UNKNOWN))
Update.printError(Serial);
} else if(upload.status == UPLOAD_FILE_WRITE) {
// Flashing firmware to ESP
if(
Update.write(upload.buf, upload.currentSize) != upload.currentSize
)
Update.printError(Serial);
String index_html_str = header_html_str + msg;
server.sendHeader("Connection", "close");
server.send(200, "text/html", index_html_str);
});
} else if(upload.status == UPLOAD_FILE_END) {
if(Update.end(true))
// True to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
else
Update.printError(Serial);
// Getting configuration
server.on("/get", HTTP_GET, []() {
bool update_channel = false;
uint8_t channel_raw = 0;
String msg = "";
// Server parameters
for(uint8_t i = 0; i < server.args(); i++) {
String value = server.arg(i);
if(server.argName(i) == "channel") {
String filename = "/NRF_CHANNEL";
msg = storage.readFile(LittleFS, filename);
msg = "Channel (From FILE): " + msg;
}
}
String index_html_str = header_html_str + msg;
server.sendHeader("Connection", "close");
server.send(200, "text/html", index_html_str);
});
***/
// Reset
server.on("/reset", HTTP_GET, []() {
@ -76,7 +97,7 @@ void WEB::begin(void) {
// Reboot
server.on("/reboot", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", "1");
server.send(200, "text/html", "Rebooting...");
delay(1000);
ESP.restart();
});

9
src/Web.h

@ -4,12 +4,8 @@
#include <Arduino.h>
#include <ESP8266WebServer.h>
#include "Storage.h"
// #include <LittleFS.h>
// // #include <WebServer.h>
// #include <ESP8266WebServer.h>
// // #include <Update.h>
// #include <ArduinoOTA.h>
//
#include "main.h"
#define PORT_HTTP 80
class WEB {
@ -22,4 +18,5 @@ class WEB {
};
extern WEB web;
extern String header_html_str;
#endif

63
src/main.cpp

@ -23,13 +23,17 @@ int16_t sbus_packets_sent_raw = 0;
int16_t nrf_packets_received = 0;
int16_t nrf_packets_received_raw = 0;
bool sbus_busy = false;
bool first_load = true;
uint16_t last_receive = 0;
uint32_t uptime_total = 0;
uint8_t channel = 8;
Scheduler scheduler;
RXMessage rxMessage;
// ####### TASK SETUP ######### BEGIN
// Every 5 miliseconds
Task task_sbus_generator(4, TASK_FOREVER, &send_sbus);
Task task_main(4, TASK_FOREVER, &main_processor);
// Every 10 millseconds
// Task task_rx(10, TASK_FOREVER, &nrf_rx);
@ -44,6 +48,9 @@ Task task_heartbeat(500, TASK_FOREVER, &status_heartbeat);
// Every second
Task task_uptime(1000, TASK_FOREVER, &uptime_tracker);
// ####### TASK SETUP ######### END
void setup() {
Serial.begin(115200);
@ -51,7 +58,7 @@ void setup() {
delay(100);
// Checking CPU Speed
Serial.printf("CPU: %d MHz\r\n", ESP.getCpuFreqMHz());
//// Serial.printf("CPU: %d MHz\r\n", ESP.getCpuFreqMHz());
// Setting up status led
pinMode(LED_HEARTBEAT, OUTPUT);
@ -62,34 +69,26 @@ void setup() {
// Starting up SBUS
sbus.begin();
// One wire as SBUS (TX)
// Serial2.begin(100000, SERIAL_8E2);
// swSer1.begin(100000, SWSERIAL_8N2, D5, D5, false, 256);
// Start up NRF24L01
rx.begin();
// Setup Receiver
// setup_rx();
// For OTA
customwifi.begin();
ota.begin();
web.begin();
scheduler.init();
Serial.println("Enabling Tasks...");
//// Serial.println("Enabling Tasks...");
scheduler.init();
// Add task to scheduler
scheduler.addTask(task_sbus_generator);
//// scheduler.addTask(task_rx);
scheduler.addTask(task_main);
scheduler.addTask(task_heartbeat);
scheduler.addTask(task_web);
scheduler.addTask(task_ota);
scheduler.addTask(task_uptime);
// Enabling Tasks
task_sbus_generator.enable();
task_main.enable();
///// task_rx.enable();
task_heartbeat.enable();
task_web.enable();
@ -97,7 +96,7 @@ void setup() {
task_uptime.enable();
Serial.println("SimpleRX OK");
//// Serial.println("SimpleRX OK");
}
void loop() {
@ -125,16 +124,13 @@ void status_heartbeat(void) {
}
}
// Keep sending SBUS
void send_sbus(void) {
sbus_busy = true;
sbus.writePackets();
nrf_rx();
sbus_busy = false;
// Main Process
void main_processor(void) {
nrf_rx(); // Receving data
sbus.writePackets(); // Write Packets to SBUS port
}
uint32_t uptime_total = 0;
void uptime_tracker(void) {
uptime_total++;
sbus_packets_sent = sbus_packets_sent_raw;
@ -144,11 +140,6 @@ void uptime_tracker(void) {
}
// DEBUGGING...
uint32_t printagain = 0;
bool first_load = true;
uint16_t last_receive = 0;
// Processing received data
void nrf_rx(void) {
if(first_load == true) {
@ -161,7 +152,7 @@ void nrf_rx(void) {
first_load = false;
setup_rx();
Serial.println("NRF READY TO LISTEN...");
//// Serial.println("NRF READY TO LISTEN...");
}
if(nrf_packets_received == 0) {
@ -235,7 +226,19 @@ void nrf_rx(void) {
void setup_rx(void) {
rx.stopListening();
rx.setAutoAck(false);
rx.setChannel(0x08); // Temporary at 8 Later read from LittleFS
// Read from LITTLEFS, if not exists create it default to channel 8
String filename = "/NRF_CHANNEL";
String channel_str = "";
if(!LittleFS.exists(filename)) {
storage.writeFile(LittleFS, filename, String(channel));
} else {
channel_str = storage.readFile(LittleFS, filename);
channel = channel_str.toInt();
}
rx.setChannel(channel); // Temporary at 8 Later read from LittleFS
rx.setPayloadSize(sizeof(RXMessage));
rx.setDataRate(RF24_250KBPS);
rx.setPALevel(RF24_PA_MAX);

9
src/main.h

@ -1,6 +1,11 @@
#ifndef MAIN_H
#define MAIN_H
// Configurations
#define VERSION_MAJOR 1
#define VERSION_MINOR 1
#define VERSION_BUILD 3
// For NRF24L01P
#define MAX_CHANNELS 125
#define RX_CE_PIN 5
@ -9,7 +14,6 @@
const uint64_t pipeIn = 0xE8E8F0F0E1LL; // Must be same as the transmission
// Enable type of microcontroller for the nrf24l01 library to compile.
// #define ESP32
#define ESP12E
@ -21,7 +25,7 @@ const uint64_t pipeIn = 0xE8E8F0F0E1LL; // Must be same as the transmission
#include "Storage.h"
#include "Web.h"
void send_sbus(void);
void main_processor(void);
void setup_nrf_rx(void);
void setup_rx(void);
void nrf_rx(void);
@ -34,6 +38,7 @@ void uptime_tracker(void);
extern int16_t sbus_packets_sent;
extern int16_t sbus_packets_sent_raw;
extern int16_t nrf_packets_received;
extern uint8_t channel;
#endif

8
src/sbus.cpp

@ -64,10 +64,10 @@ void SBUS::writePackets(void) {
}
// DEBUG ONLY
if((uint32_t)(millis() - last_profile) > 1000) {
Serial.printf("Cmd: %d Signal: %d Sent: %d NRF: %d\r\n", rcCommand[2], signalNotOK, sbus_packets_sent, nrf_packets_received);
last_profile = millis();
}
// if((uint32_t)(millis() - last_profile) > 1000) {
// Serial.printf("Cmd: %d Signal: %d Sent: %d NRF: %d\r\n", rcCommand[2], signalNotOK, sbus_packets_sent, nrf_packets_received);
// last_profile = millis();
// }
}

Loading…
Cancel
Save