Skip to content

Commit 8c7475b

Browse files
authored
Merge pull request #463 from dbambus/ESP32
Added ESP32 as an optional build option
2 parents 56de7fa + fdca644 commit 8c7475b

File tree

5 files changed

+120
-20
lines changed

5 files changed

+120
-20
lines changed

include/Wifi.hpp

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#pragma once
22

3+
#ifdef ESP8266
4+
#include <ESP8266WiFi.h>
5+
#elif defined(ESP32)
6+
#include <WiFi.h>
7+
#endif
8+
39
//---------------------------------------------------------
410
// WLAN-Status
511
//---------------------------------------------------------
6-
char wstatus[7][25] = {
7-
8-
"WL_IDLE_STATUS", "WL_NO_SSID_AVAIL", "WL_SCAN_COMPLETED",
9-
"WL_CONNECTED", "WL_CONNECT_FAILED", "WL_CONNECTION_LOST",
10-
"WL_DISCONNECTED"};
12+
char wstatus[7][25] = {"WL_IDLE_STATUS", "WL_NO_SSID_AVAIL",
13+
"WL_SCAN_COMPLETED", "WL_CONNECTED",
14+
"WL_CONNECT_FAILED", "WL_CONNECTION_LOST",
15+
"WL_DISCONNECTED"};
1116
// WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library
1217
// WL_IDLE_STATUS = 0,
1318
// WL_NO_SSID_AVAIL = 1,
@@ -45,19 +50,43 @@ void wifiStart() {
4550

4651
//------------------------------------------------------------------------------
4752

48-
void WiFiEvent(WiFiEvent_t event) {
49-
Serial.printf("[WiFi-event] event: %d\n", event);
53+
void handleWiFiEvent(const char *eventType, const IPAddress &ip) {
54+
Serial.printf("[WiFi-event] event: %s\n", eventType);
55+
if (strcmp(eventType, "GOT_IP") == 0) {
56+
Serial.println("WiFi connected");
57+
Serial.println("IP address: ");
58+
Serial.println(ip);
59+
} else if (strcmp(eventType, "DISCONNECTED") == 0) {
60+
Serial.println("WiFi lost connection");
61+
}
62+
}
5063

64+
//------------------------------------------------------------------------------
65+
66+
#ifdef ESP8266
67+
void WiFiEvent(WiFiEvent_t event) {
5168
switch (event) {
5269
case WIFI_EVENT_STAMODE_GOT_IP:
53-
Serial.println("WiFi connected");
54-
Serial.println("IP address: ");
55-
Serial.println(WiFi.localIP());
70+
handleWiFiEvent("GOT_IP", WiFi.localIP());
5671
break;
5772
case WIFI_EVENT_STAMODE_DISCONNECTED:
58-
Serial.println("WiFi lost connection");
73+
handleWiFiEvent("DISCONNECTED", IPAddress());
74+
break;
75+
default:
76+
break;
77+
}
78+
}
79+
#elif defined(ESP32)
80+
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
81+
switch (event) {
82+
case SYSTEM_EVENT_STA_GOT_IP:
83+
handleWiFiEvent("GOT_IP", WiFi.localIP());
84+
break;
85+
case SYSTEM_EVENT_STA_DISCONNECTED:
86+
handleWiFiEvent("DISCONNECTED", IPAddress());
5987
break;
6088
default:
6189
break;
6290
}
6391
}
92+
#endif

include/clockWork.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,13 @@ void ClockWork::initLedStrip(uint8_t num) {
152152
strip_RGB = NULL;
153153
}
154154
if (strip_RGBW == NULL) {
155+
#ifdef ESP8266
155156
strip_RGBW = new NeoPixelBus<NeoGrbwFeature, Neo800KbpsMethod>(500);
157+
#elif defined(ESP32)
158+
strip_RGBW =
159+
new NeoPixelBus<NeoGrbwFeature, NeoEsp32I2s1X8Sk6812Method>(500,
160+
27);
161+
#endif
156162
strip_RGBW->Begin();
157163
}
158164
} else {
@@ -162,7 +168,13 @@ void ClockWork::initLedStrip(uint8_t num) {
162168
strip_RGBW = NULL;
163169
}
164170
if (strip_RGB == NULL) {
171+
#ifdef ESP8266
165172
strip_RGB = new NeoPixelBus<NeoMultiFeature, Neo800KbpsMethod>(500);
173+
#elif defined(ESP32)
174+
strip_RGB =
175+
new NeoPixelBus<NeoMultiFeature, NeoEsp32I2s1X8Ws2812xMethod>(
176+
500, 27);
177+
#endif
166178
strip_RGB->Begin();
167179
}
168180
}
@@ -1030,8 +1042,13 @@ void ClockWork::loop(struct tm &tm) {
10301042

10311043
case COMMAND_RESET: {
10321044
delay(500);
1045+
#ifdef ESP8266
10331046
ESP.reset();
10341047
ESP.restart();
1048+
#elif defined(ESP32)
1049+
ESP.restart();
1050+
esp_restart();
1051+
#endif
10351052
while (true) {
10361053
}
10371054
break;

partitions_singleapp_large.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Name, Type, SubType, Offset, Size, Flags
2+
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
3+
nvs, data, nvs, , 0x6000,
4+
phy_init, data, phy, , 0x1000,
5+
factory, app, factory, , 1500K,

platformio.ini

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,24 @@ lib_deps =
2828
https://github.com/tzapu/WiFiManager#v2.0.17
2929
claws/BH1750@^1.3.0
3030
extra_scripts = pre:extra_scripts.py
31+
32+
[env:esp32dev]
33+
platform = espressif32
34+
board = esp32dev
35+
board_build.partitions = partitions_singleapp_large.csv
36+
framework = arduino
37+
upload_speed = 921600
38+
monitor_speed = 460800
39+
build_flags =
40+
-Os
41+
-ffunction-sections
42+
-fdata-sections
43+
-Wl,--gc-sections
44+
lib_deps =
45+
makuna/NeoPixelBus@^2.7.6
46+
bblanchon/ArduinoJson@^6.17.2
47+
48+
adafruit/RTClib@^1.11.2
49+
knolleary/PubSubClient@^2.8.0
50+
https://github.com/tzapu/WiFiManager#v2.0.17
51+
extra_scripts = pre:extra_scripts.py

src/Wortuhr.cpp

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
#include <Arduino.h>
22
#include <ArduinoJson.h>
3+
#ifdef ESP8266
34
#include <ESP8266HTTPUpdateServer.h>
45
#include <ESP8266WebServer.h>
56
#include <ESP8266WiFi.h>
67
#include <ESP8266mDNS.h>
8+
#include <coredecls.h>
9+
#elif defined(ESP32)
10+
#include <ESPmDNS.h>
11+
#include <HTTPUpdateServer.h>
12+
#include <Update.h>
13+
#include <WebServer.h>
14+
#include <WiFi.h>
15+
#endif
716
#include <NeoPixelBus.h>
817
#include <RTClib.h>
918
#include <WiFiClient.h>
1019
#include <WiFiUdp.h>
1120
#include <Wire.h>
12-
#include <coredecls.h>
1321
#include <sntp.h>
1422

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

2432
#include "NeoMultiFeature.hpp"
33+
34+
#ifdef ESP8266
2535
NeoPixelBus<NeoMultiFeature, Neo800KbpsMethod> *strip_RGB = NULL;
2636
NeoPixelBus<NeoGrbwFeature, Neo800KbpsMethod> *strip_RGBW = NULL;
37+
#elif defined(ESP32)
38+
NeoPixelBus<NeoGrbwFeature, NeoEsp32I2s1X8Sk6812Method> *strip_RGBW = NULL;
39+
NeoPixelBus<NeoMultiFeature, NeoEsp32I2s1X8Ws2812xMethod> *strip_RGB = NULL;
40+
#endif
2741

2842
WiFiClient client;
2943

3044
//--OTA--
45+
#ifdef ESP8266
3146
ESP8266WebServer httpServer(81);
3247
ESP8266HTTPUpdateServer httpUpdater;
48+
#elif defined(ESP32)
49+
WebServer httpServer(81);
50+
HTTPUpdateServer httpUpdater;
51+
#endif
3352
//--OTA--
3453

3554
// Timezone from
@@ -103,7 +122,12 @@ void time_is_set() {
103122
if (sntp_getreachability(0)) {
104123
origin = sntp_getservername(0);
105124
if (origin.length() == 0) {
106-
origin = IPAddress(sntp_getserver(0)).toString();
125+
const ip_addr_t *ip_addr = sntp_getserver(0);
126+
#ifdef ESP8266
127+
origin = IPAddress(ip_addr->addr).toString();
128+
#elif defined(ESP32)
129+
origin = IPAddress(ip_addr->u_addr.ip4.addr).toString();
130+
#endif
107131
}
108132
} else {
109133
origin = "SNTP not reachable";
@@ -306,7 +330,9 @@ void setup() {
306330
Serial.println("No external real-time clock found");
307331
externalRTC = false;
308332
}
333+
#ifdef ESP8266
309334
settimeofday_cb(time_is_set);
335+
#endif
310336

311337
//-------------------------------------
312338
// Start WiFi
@@ -366,23 +392,23 @@ void setup() {
366392
Serial.println("--------------------------------------");
367393
Serial.println("ESP Uhr");
368394
Serial.printf("Version : %s\n", VERSION);
395+
396+
#ifdef ESP8266
369397
Serial.printf("Chip ID : %08X\n", ESP.getChipId());
370-
Serial.printf("Flash ID : %08X\n\n", ESP.getFlashChipId());
371398
Serial.printf("CPU Speed : %u MHz \n\n", ESP.getCpuFreqMHz());
399+
#elif defined(ESP32)
400+
Serial.printf("Chip ID : %08X\n", (uint32_t)ESP.getEfuseMac());
401+
Serial.printf("CPU Speed : %u MHz \n\n", getCpuFrequencyMhz());
402+
#endif
372403

373-
Serial.printf("Flash real Size : %u KByte\n",
374-
ESP.getFlashChipRealSize() / 1024);
375-
Serial.printf("Flash ide Size : %u KByte\n",
376-
ESP.getFlashChipSize() / 1024);
404+
Serial.printf("Flash Size : %u KByte\n", ESP.getFlashChipSize() / 1024);
377405
Serial.printf("Flash ide Speed : %u\n\n", ESP.getFlashChipSpeed());
378406

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

383411
Serial.printf("SDK Version : %s\n", ESP.getSdkVersion());
384-
Serial.print("RESET Info : ");
385-
Serial.println(ESP.getResetInfo());
386412
Serial.print("COMPILED : ");
387413
Serial.print(__DATE__);
388414
Serial.print(" ");
@@ -428,7 +454,9 @@ void loop() {
428454

429455
network.loop();
430456

457+
#ifdef ESP8266
431458
MDNS.update();
459+
#endif
432460

433461
httpServer.handleClient();
434462

0 commit comments

Comments
 (0)