Skip to content

Commit

Permalink
Merge pull request #463 from dbambus/ESP32
Browse files Browse the repository at this point in the history
Added ESP32 as an optional build option
  • Loading branch information
dbambus authored Oct 18, 2024
2 parents 56de7fa + fdca644 commit 8c7475b
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 20 deletions.
51 changes: 40 additions & 11 deletions include/Wifi.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#pragma once

#ifdef ESP8266
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif

//---------------------------------------------------------
// WLAN-Status
//---------------------------------------------------------
char wstatus[7][25] = {

"WL_IDLE_STATUS", "WL_NO_SSID_AVAIL", "WL_SCAN_COMPLETED",
"WL_CONNECTED", "WL_CONNECT_FAILED", "WL_CONNECTION_LOST",
"WL_DISCONNECTED"};
char wstatus[7][25] = {"WL_IDLE_STATUS", "WL_NO_SSID_AVAIL",
"WL_SCAN_COMPLETED", "WL_CONNECTED",
"WL_CONNECT_FAILED", "WL_CONNECTION_LOST",
"WL_DISCONNECTED"};
// WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library
// WL_IDLE_STATUS = 0,
// WL_NO_SSID_AVAIL = 1,
Expand Down Expand Up @@ -45,19 +50,43 @@ void wifiStart() {

//------------------------------------------------------------------------------

void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %d\n", event);
void handleWiFiEvent(const char *eventType, const IPAddress &ip) {
Serial.printf("[WiFi-event] event: %s\n", eventType);
if (strcmp(eventType, "GOT_IP") == 0) {
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(ip);
} else if (strcmp(eventType, "DISCONNECTED") == 0) {
Serial.println("WiFi lost connection");
}
}

//------------------------------------------------------------------------------

#ifdef ESP8266
void WiFiEvent(WiFiEvent_t event) {
switch (event) {
case WIFI_EVENT_STAMODE_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
handleWiFiEvent("GOT_IP", WiFi.localIP());
break;
case WIFI_EVENT_STAMODE_DISCONNECTED:
Serial.println("WiFi lost connection");
handleWiFiEvent("DISCONNECTED", IPAddress());
break;
default:
break;
}
}
#elif defined(ESP32)
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
switch (event) {
case SYSTEM_EVENT_STA_GOT_IP:
handleWiFiEvent("GOT_IP", WiFi.localIP());
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
handleWiFiEvent("DISCONNECTED", IPAddress());
break;
default:
break;
}
}
#endif
17 changes: 17 additions & 0 deletions include/clockWork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,13 @@ void ClockWork::initLedStrip(uint8_t num) {
strip_RGB = NULL;
}
if (strip_RGBW == NULL) {
#ifdef ESP8266
strip_RGBW = new NeoPixelBus<NeoGrbwFeature, Neo800KbpsMethod>(500);
#elif defined(ESP32)
strip_RGBW =
new NeoPixelBus<NeoGrbwFeature, NeoEsp32I2s1X8Sk6812Method>(500,
27);
#endif
strip_RGBW->Begin();
}
} else {
Expand All @@ -162,7 +168,13 @@ void ClockWork::initLedStrip(uint8_t num) {
strip_RGBW = NULL;
}
if (strip_RGB == NULL) {
#ifdef ESP8266
strip_RGB = new NeoPixelBus<NeoMultiFeature, Neo800KbpsMethod>(500);
#elif defined(ESP32)
strip_RGB =
new NeoPixelBus<NeoMultiFeature, NeoEsp32I2s1X8Ws2812xMethod>(
500, 27);
#endif
strip_RGB->Begin();
}
}
Expand Down Expand Up @@ -1030,8 +1042,13 @@ void ClockWork::loop(struct tm &tm) {

case COMMAND_RESET: {
delay(500);
#ifdef ESP8266
ESP.reset();
ESP.restart();
#elif defined(ESP32)
ESP.restart();
esp_restart();
#endif
while (true) {
}
break;
Expand Down
5 changes: 5 additions & 0 deletions partitions_singleapp_large.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Name, Type, SubType, Offset, Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs, data, nvs, , 0x6000,
phy_init, data, phy, , 0x1000,
factory, app, factory, , 1500K,
21 changes: 21 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,24 @@ lib_deps =
https://github.com/tzapu/WiFiManager#v2.0.17
claws/BH1750@^1.3.0
extra_scripts = pre:extra_scripts.py

[env:esp32dev]
platform = espressif32
board = esp32dev
board_build.partitions = partitions_singleapp_large.csv
framework = arduino
upload_speed = 921600
monitor_speed = 460800
build_flags =
-Os
-ffunction-sections
-fdata-sections
-Wl,--gc-sections
lib_deps =
makuna/NeoPixelBus@^2.7.6
bblanchon/ArduinoJson@^6.17.2
links2004/[email protected]
adafruit/RTClib@^1.11.2
knolleary/PubSubClient@^2.8.0
https://github.com/tzapu/WiFiManager#v2.0.17
extra_scripts = pre:extra_scripts.py
46 changes: 37 additions & 9 deletions src/Wortuhr.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
#include <Arduino.h>
#include <ArduinoJson.h>
#ifdef ESP8266
#include <ESP8266HTTPUpdateServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <coredecls.h>
#elif defined(ESP32)
#include <ESPmDNS.h>
#include <HTTPUpdateServer.h>
#include <Update.h>
#include <WebServer.h>
#include <WiFi.h>
#endif
#include <NeoPixelBus.h>
#include <RTClib.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>
#include <Wire.h>
#include <coredecls.h>
#include <sntp.h>

#include "Uhr.h"
Expand All @@ -22,14 +30,25 @@
iUhrType *usedUhrType = nullptr;

#include "NeoMultiFeature.hpp"

#ifdef ESP8266
NeoPixelBus<NeoMultiFeature, Neo800KbpsMethod> *strip_RGB = NULL;
NeoPixelBus<NeoGrbwFeature, Neo800KbpsMethod> *strip_RGBW = NULL;
#elif defined(ESP32)
NeoPixelBus<NeoGrbwFeature, NeoEsp32I2s1X8Sk6812Method> *strip_RGBW = NULL;
NeoPixelBus<NeoMultiFeature, NeoEsp32I2s1X8Ws2812xMethod> *strip_RGB = NULL;
#endif

WiFiClient client;

//--OTA--
#ifdef ESP8266
ESP8266WebServer httpServer(81);
ESP8266HTTPUpdateServer httpUpdater;
#elif defined(ESP32)
WebServer httpServer(81);
HTTPUpdateServer httpUpdater;
#endif
//--OTA--

// Timezone from
Expand Down Expand Up @@ -103,7 +122,12 @@ void time_is_set() {
if (sntp_getreachability(0)) {
origin = sntp_getservername(0);
if (origin.length() == 0) {
origin = IPAddress(sntp_getserver(0)).toString();
const ip_addr_t *ip_addr = sntp_getserver(0);
#ifdef ESP8266
origin = IPAddress(ip_addr->addr).toString();
#elif defined(ESP32)
origin = IPAddress(ip_addr->u_addr.ip4.addr).toString();
#endif
}
} else {
origin = "SNTP not reachable";
Expand Down Expand Up @@ -306,7 +330,9 @@ void setup() {
Serial.println("No external real-time clock found");
externalRTC = false;
}
#ifdef ESP8266
settimeofday_cb(time_is_set);
#endif

//-------------------------------------
// Start WiFi
Expand Down Expand Up @@ -366,23 +392,23 @@ void setup() {
Serial.println("--------------------------------------");
Serial.println("ESP Uhr");
Serial.printf("Version : %s\n", VERSION);

#ifdef ESP8266
Serial.printf("Chip ID : %08X\n", ESP.getChipId());
Serial.printf("Flash ID : %08X\n\n", ESP.getFlashChipId());
Serial.printf("CPU Speed : %u MHz \n\n", ESP.getCpuFreqMHz());
#elif defined(ESP32)
Serial.printf("Chip ID : %08X\n", (uint32_t)ESP.getEfuseMac());
Serial.printf("CPU Speed : %u MHz \n\n", getCpuFrequencyMhz());
#endif

Serial.printf("Flash real Size : %u KByte\n",
ESP.getFlashChipRealSize() / 1024);
Serial.printf("Flash ide Size : %u KByte\n",
ESP.getFlashChipSize() / 1024);
Serial.printf("Flash Size : %u KByte\n", ESP.getFlashChipSize() / 1024);
Serial.printf("Flash ide Speed : %u\n\n", ESP.getFlashChipSpeed());

Serial.printf("Free Heap Size : %u Byte\n", ESP.getFreeHeap());
Serial.printf("Sketch Size : %u Byte \n", ESP.getSketchSize());
Serial.printf("Free Sketch Size: %u Byte \n\n", ESP.getFreeSketchSpace());

Serial.printf("SDK Version : %s\n", ESP.getSdkVersion());
Serial.print("RESET Info : ");
Serial.println(ESP.getResetInfo());
Serial.print("COMPILED : ");
Serial.print(__DATE__);
Serial.print(" ");
Expand Down Expand Up @@ -428,7 +454,9 @@ void loop() {

network.loop();

#ifdef ESP8266
MDNS.update();
#endif

httpServer.handleClient();

Expand Down

0 comments on commit 8c7475b

Please sign in to comment.