Skip to content

Commit

Permalink
ESP32 support (#1)
Browse files Browse the repository at this point in the history
Easy ESP32 support
  • Loading branch information
sblantipodi authored Jun 19, 2020
1 parent b1843b1 commit dd7f801
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 64 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Arduino Bootstrapper

Utility classes for bootstrapping arduino projects with Wifi management, OTA upload management, memory management,
MQTT and queue management. Espressif ESP8266 is the default platform used but it can be easily configured for other boards.
MQTT and queue management.
Espressif **`ESP8266/ESP32`** are the default platforms used but it can be easily configured for other platforms and boards.
_Written for Arduino IDE and PlatformIO._

[![GitHub Actions CI](https://github.com/sblantipodi/arduino_bootstrapper/workflows/GitHub%20Actions%20CI/badge.svg)](https://github.com/sblantipodi/arduino_bootstrapper/actions)
Expand Down
7 changes: 4 additions & 3 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"name": "ArduinoBootstrapper",
"keywords": "bootstrapper, mqtt, wifi, iot",
"description": "Utility classes for bootstrapping arduino projects with Wifi management, OTA upload management, memory management, MQTT and queue management.",
"description": "Utility classes for bootstrapping arduino projects with Wifi management, OTA upload management, memory management, MQTT and queue management. (ESP8266/ESP32 ready)",
"repository": {
"type": "git",
"url": "https://github.com/sblantipodi/arduino_bootstrapper.git"
},
"version": "1.4.5",
"version": "1.5.0",
"examples": "examples/*.cpp",
"exclude": "tests",
"frameworks": "arduino",
"platforms": [
"espressif8266"
"espressif8266",
"espressif32"
]
}
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=Bootstrapper
version=1.4.5
version=1.5.0
author=Davide Perini <[email protected]>
maintainer=Davide Perini <[email protected]>
sentence=A client library for MQTT messaging.
paragraph=Utility classes for bootstrapping arduino projects with Wifi management, OTA upload management, memory management, MQTT and queue management.
paragraph=Utility classes for bootstrapping arduino projects with Wifi management, OTA upload management, memory management, MQTT and queue management. (ESP8266/ESP32 ready)
category=Other
url=https://github.com/sblantipodi/arduino_bootstrapper
architectures=*
Expand Down
14 changes: 6 additions & 8 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
extra_configs = secrets.ini

[env:bootstrapper]
platform = espressif8266
board = d1_mini
platform = espressif8266 ; for ESP32 use: espressif32
board = d1_mini ; for ESP32 Lolin D32 board use: lolin_d32
framework = arduino

build_flags =
Expand All @@ -40,17 +40,15 @@ build_flags =

monitor_port = COM4
monitor_speed = 115200
monitor_filters = esp8266_exception_decoder
board_build.f_cpu = 160000000L
monitor_filters = esp8266_exception_decoder ; for ESP32 use: esp32_exception_decoder
board_build.f_cpu = 80000000L ; for ESP32 use: 240000000L, ESP8266 can run @160000000L if you need it
; upload_protocol = espota
; upload_port = 192.168.1.99
; upload_flags =
; --port=8268
; --auth= ${secrets.ota_password}
; --auth=${secrets.ota_password} ; configure secrets.ini for OTA Upload, first upload using COM port is needed
upload_port = COM4
lib_deps =
ArduinoJson
PubSubClient
Adafruit SSD1306
; extra_scripts = pre:platformio_version_increment/version_increment.py

Adafruit SSD1306
100 changes: 92 additions & 8 deletions src/BootstrapManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
/********************************** BOOTSTRAP FUNCTIONS FOR SETUP() *****************************************/
void BootstrapManager::bootstrapSetup(void (*manageDisconnections)(), void (*manageHardwareButton)(), void (*callback)(char*, byte*, unsigned int)) {

if (!LittleFS.begin()) {
Serial.println("LittleFS mount failed");
return;
}
#if defined(ESP8266)
if (!LittleFS.begin()) {
Serial.println("LittleFS mount failed");
return;
}
#endif

if (isWifiConfigured()) {
isConfigFileOk = true;
Expand Down Expand Up @@ -180,7 +182,7 @@ void BootstrapManager::getMicrocontrollerInfo() {
helper.smartPrint(F("IP: ")); helper.smartPrintln(IP);
helper.smartPrintln(F("MAC: ")); helper.smartPrintln(WiFi.macAddress());
helper.smartPrint(F("SDK: ")); helper.smartPrintln(ESP.getSdkVersion());
helper.smartPrint(F("Arduino Core: ")); helper.smartPrintln(ESP.getCoreVersion());
// helper.smartPrint(F("Arduino Core: ")); helper.smartPrintln(ESP.getCoreVersion());
helper.smartPrintln(F("Last Boot: ")); helper.smartPrintln(lastBoot);
helper.smartPrintln(F("Last WiFi connection:")); helper.smartPrintln(lastWIFiConnection);
helper.smartPrintln(F("Last MQTT connection:")); helper.smartPrintln(lastMQTTConnection);
Expand Down Expand Up @@ -259,6 +261,7 @@ void BootstrapManager::sendState(const char *topic, JsonObject objectToSend, Str
}

// write json file to storage
#if defined(ESP8266)
void BootstrapManager::writeToLittleFS(DynamicJsonDocument jsonDoc, String filename) {

File jsonFile = LittleFS.open("/" + filename, "w");
Expand All @@ -272,8 +275,31 @@ void BootstrapManager::writeToLittleFS(DynamicJsonDocument jsonDoc, String filen
}

}
#endif

// write json file to storage
#if defined(ESP32)
void BootstrapManager::writeToSPIFFS(DynamicJsonDocument jsonDoc, String filename) {

if (SPIFFS.begin()) {
// SPIFFS.format();
File configFile = SPIFFS.open("/" + filename, "w");
if (!configFile) {
helper.smartPrintln("Failed to open [" + filename + "] file for writing");
}
serializeJsonPretty(jsonDoc, Serial);
serializeJson(jsonDoc, configFile);
configFile.close();
helper.smartPrintln("[" + filename + "] written correctly");
} else {
helper.smartPrintln(F("Failed to mount FS for write"));
}

}
#endif

// read json file from storage
#if defined(ESP8266)
DynamicJsonDocument BootstrapManager::readLittleFS(String filename) {

// Helpers classes
Expand Down Expand Up @@ -309,7 +335,7 @@ DynamicJsonDocument BootstrapManager::readLittleFS(String filename) {

DynamicJsonDocument jsonDoc(1024);
auto error = deserializeJson(jsonDoc, buf.get());
serializeJsonPretty(jsonDoc, Serial);
if (filename != "setup.json") serializeJsonPretty(jsonDoc, Serial);
jsonFile.close();
if (error) {
helper.smartPrintln("Failed to parse [" + filename + "] file");
Expand All @@ -327,6 +353,60 @@ DynamicJsonDocument BootstrapManager::readLittleFS(String filename) {
return jsonDoc;

}
#endif

// read json file from storage
#if defined(ESP32)
DynamicJsonDocument BootstrapManager::readSPIFFS(String filename) {

// Helpers classes
Helpers helper;

if (PRINT_TO_DISPLAY) {
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
}
helper.smartPrintln(F("Mounting SPIFSS..."));
helper.smartDisplay();
// SPIFFS.remove("/config.json");
if (SPIFFS.begin()) {
helper.smartPrintln(F("FS mounted"));
if (SPIFFS.exists("/" + filename)) {
//file exists, reading and loading
helper.smartPrintln("Reading " + filename + " file...");
File configFile = SPIFFS.open("/" + filename, "r");
if (configFile) {
helper.smartPrintln(F("Config OK"));
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);

configFile.readBytes(buf.get(), size);
DeserializationError deserializeError = deserializeJson(jsonDoc, buf.get());
Serial.println("\nReading " + filename);
if (filename != "setup.json") serializeJsonPretty(jsonDoc, Serial);
if (!deserializeError) {
helper.smartPrintln(F("JSON parsed"));
} else {
jsonDoc[VALUE] = ERROR;
helper.smartPrintln(F("Failed to load json file"));
}
}
} else {
jsonDoc[VALUE] = ERROR;
helper.smartPrintln("Error reading " + filename + " file...");
}
} else {
jsonDoc[VALUE] = ERROR;
helper.smartPrintln(F("failed to mount FS"));
}
helper.smartDisplay();
delay(DELAY_4000);
return jsonDoc;

}
#endif

// check if wifi is correctly configured
bool BootstrapManager::isWifiConfigured() {
Expand All @@ -339,9 +419,13 @@ bool BootstrapManager::isWifiConfigured() {
mqttpass = MQTT_PASSWORD;
return true;
} else {
DynamicJsonDocument mydoc = readLittleFS("setup.json");
#if defined(ESP8266)
DynamicJsonDocument mydoc = readLittleFS("setup.json");
#elif defined(ESP32)
DynamicJsonDocument mydoc = readSPIFFS("setup.json");
#endif
if (mydoc.containsKey("qsid")) {
Serial.println("LittleFS OK, restoring WiFi and MQTT config.");
Serial.println("Storage OK, restoring WiFi and MQTT config.");
qsid = helper.getValue(mydoc["qsid"]);
qpass = helper.getValue(mydoc["qpass"]);
OTApass = helper.getValue(mydoc["OTApass"]);
Expand Down
16 changes: 12 additions & 4 deletions src/BootstrapManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <FS.h>
#include <LittleFS.h>
#include "Configuration.h"
#if defined(ESP8266)
#include <LittleFS.h>
#elif defined(ESP32)
#include "SPIFFS.h"
#endif #include "Configuration.h"
#include "Helpers.h"
#include "WifiManager.h"
#include "QueueManager.h"
Expand Down Expand Up @@ -56,8 +59,13 @@ class BootstrapManager {
void drawInfoPage(String softwareVersion, String author); // draw a page with all the microcontroller's info
void drawScreenSaver(String txt); // useful for OLED displays
void sendState(const char *topic, JsonObject objectToSend, String version); // send microcontroller's info on the queue
void writeToLittleFS(DynamicJsonDocument jsonDoc, String filename); // write json file to storage
DynamicJsonDocument readLittleFS(String filename); // read json file from storage
#if defined(ESP8266)
void writeToLittleFS(DynamicJsonDocument jsonDoc, String filename); // write json file to storage
DynamicJsonDocument readLittleFS(String filename); // read json file from storage
#elif defined(ESP32)
void writeToSPIFFS(DynamicJsonDocument jsonDoc, String filename); // write json file to storage
DynamicJsonDocument readSPIFFS(String filename); // read json file from storage
#endif
bool isWifiConfigured(); // check if wifi is correctly configured
void launchWebServerForOTAConfig(); // if no ssid available, launch web server to get config params via browser

Expand Down
10 changes: 9 additions & 1 deletion src/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@
#ifndef _DPSOFTWARE_CONFIG_H
#define _DPSOFTWARE_CONFIG_H

#include <ESP8266WiFi.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <WebServer.h>
#endif
#include <Adafruit_SSD1306.h>

#ifndef AUTHOR
Expand Down
Loading

0 comments on commit dd7f801

Please sign in to comment.