Skip to content

Commit b47ab67

Browse files
committed
device provisioning refactor
1 parent 3f8fc44 commit b47ab67

File tree

10 files changed

+160
-62
lines changed

10 files changed

+160
-62
lines changed

examples/ESP32/SetupClient/SetupClient.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ void setup() {
6767
// Your other application setup stuff goes here
6868
//
6969

70-
// This method will initialize and start the bytebeam client
70+
// This method will initialize and start the bytebeam client
71+
// You can over-ride the default file system and file name options, if needed
72+
// eg. Bytebeam.begin(BytebeamArduino::LITTLEFS_FILE_SYSTEM, "/my_device_config.json")
7173
Bytebeam.begin();
7274

7375
//

examples/ESP32/SetupClient_GSM/SetupClient_GSM.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ void setup() {
137137
//
138138

139139
// This method will initialize and start the bytebeam client
140+
// You can over-ride the default file system and file name options, if needed
141+
// eg. Bytebeam.begin(&modem, BytebeamArduino::LITTLEFS_FILE_SYSTEM, "/my_device_config.json")
140142
Bytebeam.begin(&modem);
141143

142144
//
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Bytebeam FATFS Provisioning Example
2+
This example will show you how to provison your device with FATFS file system.
3+
4+
## Hardware specification
5+
1. Dev Board (ESP32 or ESP8266)
6+
7+
## Software Specification
8+
1. Bytebeam Arduino Library

examples/Provisioning/LITTLEFSProvisioning/LITTLEFSProvisioning.ino

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#include "FS.h"
2-
#include "LittleFS.h"
1+
#include <BytebeamArduino.h>
32

43
/* This macro is used to specify the base path of the littlefs partiiton */
54
#define LITTLEFS_BASE_PATH "/littlefs"
@@ -36,36 +35,53 @@ char deviceConfigWriteStr[DEVICE_CONFIG_STR_LENGTH] = R"(
3635
}
3736
)";
3837

39-
void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
40-
Serial.printf("Listing directory : %s\r\n", dirname);
38+
#if defined(BYTEBEAM_ARDUINO_ARCH_ESP32)
39+
void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
40+
Serial.printf("Listing directory : %s\r\n", dirname);
4141

42-
File root = fs.open(dirname);
43-
if (!root) {
44-
Serial.println("- failed to open directory");
45-
return;
46-
}
47-
if (!root.isDirectory()) {
48-
Serial.println(" - not a directory");
49-
return;
50-
}
42+
File root = fs.open(dirname);
43+
if (!root) {
44+
Serial.println("- failed to open directory");
45+
return;
46+
}
47+
if (!root.isDirectory()) {
48+
Serial.println(" - not a directory");
49+
return;
50+
}
5151

52-
File file = root.openNextFile();
53-
while (file) {
54-
if (file.isDirectory()) {
55-
Serial.print(" - DIR : ");
56-
Serial.println(file.name());
57-
if (levels) {
58-
listDir(fs, file.name(), levels - 1);
52+
File file = root.openNextFile();
53+
while (file) {
54+
if (file.isDirectory()) {
55+
Serial.print(" - DIR : ");
56+
Serial.println(file.name());
57+
if (levels) {
58+
listDir(fs, file.name(), levels - 1);
59+
}
60+
} else {
61+
Serial.print(" - FILE: ");
62+
Serial.print(file.name());
63+
Serial.print("\tSIZE: ");
64+
Serial.println(file.size());
5965
}
60-
} else {
61-
Serial.print(" - FILE: ");
62-
Serial.print(file.name());
63-
Serial.print("\tSIZE: ");
64-
Serial.println(file.size());
66+
file = root.openNextFile();
6567
}
66-
file = root.openNextFile();
68+
}
69+
#elif defined(BYTEBEAM_ARDUINO_ARCH_ESP8266)
70+
void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
71+
Serial.printf("Listing directory: %s\n", dirname);
72+
73+
Dir root = fs.openDir(dirname);
74+
75+
while (root.next()) {
76+
File file = root.openFile("r");
77+
Serial.print(" - FILE: ");
78+
Serial.print(root.fileName());
79+
Serial.print("\tSIZE: ");
80+
Serial.println(file.size());
81+
file.close();
6782
}
6883
}
84+
#endif
6985

7086
void readFile(fs::FS &fs, const char *path, char *message) {
7187
Serial.printf("Reading file : %s\r\n", path);
@@ -127,8 +143,16 @@ void setup() {
127143
Serial.begin(115200);
128144
Serial.println();
129145

130-
// initalize the littlefs file system
131-
if (!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED, LITTLEFS_BASE_PATH)) {
146+
bool beginStatus = false;
147+
148+
// initalize the spiffs file system
149+
#if defined(BYTEBEAM_ARDUINO_ARCH_ESP32)
150+
beginStatus = LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED, LITTLEFS_BASE_PATH);
151+
#elif defined(BYTEBEAM_ARDUINO_ARCH_ESP8266)
152+
beginStatus = LittleFS.begin();
153+
#endif
154+
155+
if (!beginStatus) {
132156
Serial.println("littlefs mount failed");
133157
return;
134158
} else {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Bytebeam LITTLEFS Provisioning Example
2+
This example will show you how to provison your device with LITTLEFS file system.
3+
4+
## Hardware specification
5+
1. Dev Board (ESP32 or ESP8266)
6+
7+
## Software Specification
8+
1. Bytebeam Arduino Library

examples/Provisioning/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Provisioning
2+
3+
Before you get started with the **BytebeamArduino** you need to provision your device. Each device have it's own config file which needs to be flased into it's file system. As long as the file system is supported by the device, you can pick any of the following file systems for device provisioning.
4+
5+
- SPIFFSProvisioning
6+
- LITTLEFSProvisioning
7+
- FATFSProvisioning
8+
9+
Default Configuration,
10+
11+
| Arch | File Sytem | File Name |
12+
| ------------- |:-------------:| -------------------:|
13+
| ESP32 | SPIFFS | /device_config.json |
14+
| ESP8266 | LITTLEFS | /device_config.json |
15+
16+
You can always override the default configurations and here are steps to do so,
17+
- Provision your device with the required configurations
18+
- Specify the same configurations in your sketch via `begin` method.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Bytebeam SPIFFS Provisioning Example
2+
This example will show you how to provison your device with SPIFFS file system.
3+
4+
## Hardware specification
5+
1. Dev Board (ESP32 or ESP8266)
6+
7+
## Software Specification
8+
1. Bytebeam Arduino Library

examples/Provisioning/SPIFFSProvisioning/SPIFFSProvisioning.ino

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#include "FS.h"
2-
#include "SPIFFS.h"
1+
#include <BytebeamArduino.h>
32

43
/* This macro is used to specify the base path of the spiffs partiiton */
54
#define SPIFFS_BASE_PATH "/spiffs"
@@ -8,7 +7,7 @@
87
#define FORMAT_SPIFFS_IF_FAILED true
98

109
/* This macro is used to format the spiffs in the beginnig i.e reset if not required */
11-
#define FORMAT_SPIFSS_IN_BEGINNING true
10+
#define FORMAT_SPIFSS_IN_BEGINNING true
1211

1312
/* This macro is used to specify the name of the device config file */
1413
#define DEVICE_CONFIG_FILE_NAME "/device_config.json"
@@ -36,36 +35,53 @@ char deviceConfigWriteStr[DEVICE_CONFIG_STR_LENGTH] = R"(
3635
}
3736
)";
3837

39-
void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
40-
Serial.printf("Listing directory : %s\r\n", dirname);
38+
#if defined(BYTEBEAM_ARDUINO_ARCH_ESP32)
39+
void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
40+
Serial.printf("Listing directory : %s\r\n", dirname);
4141

42-
File root = fs.open(dirname);
43-
if (!root) {
44-
Serial.println("- failed to open directory");
45-
return;
46-
}
47-
if (!root.isDirectory()) {
48-
Serial.println(" - not a directory");
49-
return;
50-
}
42+
File root = fs.open(dirname);
43+
if (!root) {
44+
Serial.println("- failed to open directory");
45+
return;
46+
}
47+
if (!root.isDirectory()) {
48+
Serial.println(" - not a directory");
49+
return;
50+
}
5151

52-
File file = root.openNextFile();
53-
while (file) {
54-
if (file.isDirectory()) {
55-
Serial.print(" - DIR : ");
56-
Serial.println(file.name());
57-
if (levels) {
58-
listDir(fs, file.name(), levels - 1);
52+
File file = root.openNextFile();
53+
while (file) {
54+
if (file.isDirectory()) {
55+
Serial.print(" - DIR : ");
56+
Serial.println(file.name());
57+
if (levels) {
58+
listDir(fs, file.name(), levels - 1);
59+
}
60+
} else {
61+
Serial.print(" - FILE: ");
62+
Serial.print(file.name());
63+
Serial.print("\tSIZE: ");
64+
Serial.println(file.size());
5965
}
60-
} else {
61-
Serial.print(" - FILE: ");
62-
Serial.print(file.name());
63-
Serial.print("\tSIZE: ");
64-
Serial.println(file.size());
66+
file = root.openNextFile();
6567
}
66-
file = root.openNextFile();
68+
}
69+
#elif defined(BYTEBEAM_ARDUINO_ARCH_ESP8266)
70+
void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
71+
Serial.printf("Listing directory: %s\n", dirname);
72+
73+
Dir root = fs.openDir(dirname);
74+
75+
while (root.next()) {
76+
File file = root.openFile("r");
77+
Serial.print(" - FILE: ");
78+
Serial.print(root.fileName());
79+
Serial.print("\tSIZE: ");
80+
Serial.println(file.size());
81+
file.close();
6782
}
6883
}
84+
#endif
6985

7086
void readFile(fs::FS &fs, const char *path, char *message) {
7187
Serial.printf("Reading file : %s\r\n", path);
@@ -117,8 +133,16 @@ void setup() {
117133
}
118134
#endif
119135

136+
bool beginStatus = false;
137+
120138
// initalize the spiffs file system
121-
if (!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED, SPIFFS_BASE_PATH)) {
139+
#if defined(BYTEBEAM_ARDUINO_ARCH_ESP32)
140+
beginStatus = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED, SPIFFS_BASE_PATH);
141+
#elif defined(BYTEBEAM_ARDUINO_ARCH_ESP8266)
142+
beginStatus = SPIFFS.begin();
143+
#endif
144+
145+
if (!beginStatus) {
122146
Serial.println("spiffs mount failed");
123147
return;
124148
} else {

src/BytebeamArduino.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#include "BytebeamArduino.h"
22

3-
#ifndef FILE_READ
4-
#define FILE_READ "r"
5-
#endif
6-
73
/* This object will represent the Time library, We will be exposing the necessary functionality and info
84
* for the usage, If you want to do any Timing related stuff this guy is for you.
95
*/
@@ -65,7 +61,7 @@ void BytebeamArduino::printArchitectureInfo() {
6561
// log esp8266 arch info to serial :)
6662
Serial.printf("*\t\t\t Architecture : ESP8266 \t\t\t*\n");
6763
Serial.printf("*\t\t\t Chip Id : %d \t\t\t*\n", ESP.getChipId());
68-
Serial.printf("*\t\t\t CPU Freq : %d MHz \t\t\t*\n", ESP.getCpuFreqMHz());
64+
Serial.printf("*\t\t\t CPU Freq : %d MHz \t\t\t\t*\n", ESP.getCpuFreqMHz());
6965
Serial.printf("*\t\t\t Flash Size : %d MB \t\t\t\t*\n", ((ESP.getFlashChipSize())/1024)/1024);
7066
Serial.printf("*\t\t\t Free Heap : %d KB \t\t\t\t*\n", (ESP.getFreeHeap())/1024);
7167
Serial.printf("*\t\t\t SDK Version : %s \t\t*\n", ESP.getSdkVersion());

src/BytebeamArduino.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
#include "BytebeamOTA.h"
1010
#include "BytebeamArduinoDefines.h"
1111

12+
#ifndef FILE_READ
13+
#define FILE_READ "r"
14+
#endif
15+
16+
#ifndef FILE_WRITE
17+
#define FILE_WRITE "w"
18+
#endif
19+
1220
/* This macro is used to debug the library, we will keep all the unnecessary print under this macro */
1321
#define DEBUG_BYTEBEAM_ARDUINO false
1422

0 commit comments

Comments
 (0)