Skip to content

Commit

Permalink
Fix build errors for esp8266 and esp32
Browse files Browse the repository at this point in the history
  • Loading branch information
Jojo-1000 committed Nov 21, 2024
1 parent db9abcd commit 1fc7249
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
with:
path: |
~/.cache/pip
~/.platforio/.cache
~/.platformio/.cache
key: ${{ runner.os }}-pio

- uses: actions/setup-python@v5
Expand Down
11 changes: 7 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ jobs:
test:
# Based on example in platformio docs
runs-on: ubuntu-latest
strategy:
matrix:
pio_env: [esp8266_d1_mini, esp32]

# List of steps this job will run
steps:
Expand All @@ -30,7 +33,7 @@ jobs:
with:
path: |
~/.cache/pip
~/.platforio/.cache
~/.platformio/.cache
key: ${{ runner.os }}-pio

- uses: actions/setup-python@v5
Expand All @@ -41,11 +44,11 @@ jobs:

# Install the platform and dependencies
- name: Build PlatformIO Project
run: pio run
run: pio run -e ${{ matrix.pio_env }}

# Upload binary files as artifacts
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: Firmware
path: .pio/build/d1_mini/firmware.bin
name: Firmware ${{ matrix.pio_env }}
path: .pio/build/${{ matrix.pio_env }}/firmware.bin
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ lib_deps =
mathieucarbou/ESPAsyncWebServer@^3.3.22

[env:esp8266_d1_mini]
platform = espressif8266
platform = espressif8266@^4.2.1
board = d1_mini

[env:esp32]
platform = espressif32
platform = espressif32@^5.4.0
board = wemos_d1_mini32
5 changes: 5 additions & 0 deletions src/Config.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "Config.h"

#ifdef ESP32
// Needs to be included separately
#include <SPIFFS.h>
#endif

constexpr int documentSizeConfig = 1024;
constexpr int documentSizeEffect = 128;

Expand Down
26 changes: 17 additions & 9 deletions src/Networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

#include "../webui/cpp/build.html.gz.h"


// ESP32 methods do not accept arduino strings
#if defined(ESP32)
#define ESP32_STR(s) s.c_str()
#else
#define ESP32_STR(s) s
#endif

void Networking::initWifi()
{
if (isInitialized)
Expand Down Expand Up @@ -79,7 +87,7 @@ void Networking::initServer(TreeLight& light)
void Networking::stop()
{
// server.end();
WiFi.mode(WIFI_SHUTDOWN, &savedState);
WiFi.mode(WIFI_OFF);
// Save off state for reboot
config.getNetworkConfig().wifiEnabled = false;
config.saveConfig();
Expand All @@ -88,7 +96,7 @@ void Networking::stop()

void Networking::resume()
{
WiFi.mode(WIFI_RESUME, &savedState);
WiFi.mode(config.getNetworkConfig().clientEnabled ? WIFI_STA : WIFI_AP);
config.getNetworkConfig().wifiEnabled = true;
config.saveConfig();
DEBUGLN("Resuming wifi");
Expand Down Expand Up @@ -126,13 +134,13 @@ void Networking::getStatusJsonString(JsonObject& output)

auto&& wifi_client = networking.createNestedObject("wifi_client");
wifi_client["status"] = client_enabled ? (WiFi.isConnected() ? "connected" : "enabled") : "disabled";
wifi_client["ip"] = WiFi.localIP();
wifi_client["netmask"] = WiFi.subnetMask();
wifi_client["dns"] = WiFi.dnsIP();
wifi_client["ip"] = WiFi.localIP().toString();
wifi_client["netmask"] = WiFi.subnetMask().toString();
wifi_client["dns"] = WiFi.dnsIP().toString();

auto&& wifi_ap = networking.createNestedObject("wifi_ap");
wifi_ap["status"] = client_enabled ? "disabled" : "enabled";
wifi_ap["ip"] = WiFi.softAPIP();
wifi_ap["ip"] = WiFi.softAPIP().toString();
}

void Networking::handleOTAUpload(
Expand Down Expand Up @@ -367,7 +375,7 @@ void Networking::startClient()

WiFi.persistent(true);
WiFi.mode(WIFI_STA);
WiFi.begin(wifi.clientSsid, wifi.clientPassword);
WiFi.begin(ESP32_STR(wifi.clientSsid), ESP32_STR(wifi.clientPassword));
DEBUG("Connecting to WiFi ..");

if (handleClientFailsafe())
Expand All @@ -393,12 +401,12 @@ void Networking::startAccessPoint(bool persistent)

if (wifi.apPassword.length() == 0)
{
WiFi.softAP(wifi.apSsid);
WiFi.softAP(ESP32_STR(wifi.apSsid));
DEBUGLN("Starting open AP");
}
else
{
WiFi.softAP(wifi.apSsid, wifi.apPassword);
WiFi.softAP(ESP32_STR(wifi.apSsid), ESP32_STR(wifi.apPassword));
DEBUGLN("Starting protected AP");
}

Expand Down
1 change: 0 additions & 1 deletion src/Networking.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ class Networking
const IPAddress AP_NETMASK = {255, 255, 255, 0};
DNSServer dnsServer; // DNS server for captive portal
AsyncWebServer server {80}; /// Webserver for OTA
WiFiState savedState;
bool isInitialized = false;
Config& config;
Mqtt mqtt;
Expand Down
27 changes: 18 additions & 9 deletions src/TreeLight.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
#include "TreeLight.h"

#ifdef ESP32
#include <bootloader_random.h>
#endif

void TreeLight::init(Menu& menu)
{
this->menu = &menu;

// Init random seed
#if defined(ESP32)
// Has to be called before using wifi, ADC or I2S, otherwise remove bootloader_random_enable
// FastLED may use I2S, so initialize seed before that
bootloader_random_enable();
randomSeed(esp_random());
bootloader_random_disable();
#elif defined(ESP8266)
randomSeed(RANDOM_REG32);
#else
randomSeed(analogRead(A0) * 17 + 23);
#endif

effectList = createEffects();
currentEffect = effectList[0];
currentEffect->reset(false);
Expand All @@ -19,15 +37,6 @@ void TreeLight::init(Menu& menu)
lastUpdate = millis();
ledBackup.fill_solid(CRGB::Black);

// Init random seed
#if defined(ESP32)
randomSeed(ESP.getVcc() * analogRead(A0));
#elif defined(ESP8266)
randomSeed(RANDOM_REG32);
#else
randomSeed(analogRead(A0) * 17 + 23);
#endif

colors.initRandomColors();
colors.setSelection(0);
}
Expand Down
25 changes: 21 additions & 4 deletions src/code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#define FASTLED_ALLOW_INTERRUPTS 0
#endif

#include <Arduino.h>
#include <AceButton.h>
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
#include <FastLED.h>

Expand All @@ -13,6 +13,11 @@
#include "Mqtt.h"
#include "Networking.h"
#include "TreeLight.h"

#if defined(ESP32)
#include <esp_wifi.h>
#endif

using namespace ace_button;

#if defined(ESP8266)
Expand All @@ -28,10 +33,19 @@ Config config;
Networking networking {config};
bool wifiEnabled = false;

void init_config()
void getMacAddress(uint8_t (&mac)[6])
{
uint8_t mac[6];
#if defined(ESP32)
esp_wifi_get_mac(WIFI_IF_STA, mac);
#else
wifi_get_macaddr(STATION_IF, mac);
#endif
}

void init_config()
{
uint8_t mac[6] = {};
getMacAddress(mac);
sniprintf(deviceMAC, sizeof(deviceMAC), "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

config.initConfig();
Expand Down Expand Up @@ -206,7 +220,10 @@ void loop()
delay(1);

#if defined(ESP8266) || defined(ESP32)
EVERY_N_SECONDS(1) { networking.update(); }
EVERY_N_SECONDS(1)
{
networking.update();
}
#endif

#ifdef DEBUG_PRINT
Expand Down

0 comments on commit 1fc7249

Please sign in to comment.