Skip to content

Commit 30e8515

Browse files
author
Eric Thieme-Garmann
committed
add .ino sketch
1 parent ff819d6 commit 30e8515

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#define DISPLAY_ENABLED
2+
3+
#include <WiFi.h>
4+
#include <WebServer.h>
5+
#include <ESPmDNS.h>
6+
#include <WiFiAP.h>
7+
#include <Update.h>
8+
#include <Wire.h>
9+
#ifdef DISPLAY_ENABLED
10+
#define SCREEN_WIDTH 128
11+
#define SCREEN_HEIGHT 64
12+
#define OLED_RESET -1
13+
#include <Adafruit_GFX.h>
14+
#include <Adafruit_SSD1306.h>
15+
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
16+
#include <Adafruit_NeoPixel.h>
17+
Adafruit_NeoPixel rgb_led_1= Adafruit_NeoPixel(1, 1,NEO_GRB + NEO_KHZ800);
18+
19+
20+
#endif
21+
#include "esp_partition.h"
22+
#include "esp_ota_ops.h"
23+
#include "esp_system.h"
24+
25+
String ssid;
26+
uint8_t mac[6];
27+
28+
29+
// Create an instance of the server
30+
WebServer server(80);
31+
bool displayEnabled;
32+
33+
const int BUTTON_PIN = 0; // GPIO für den Button
34+
volatile unsigned long lastPressTime = 0; // Zeitpunkt des letzten Drucks
35+
volatile bool doublePressDetected = false; // Flag für Doppeldruck
36+
const unsigned long doublePressInterval = 500; // Max. Zeit (in ms) zwischen zwei Drücken für Doppeldruck
37+
volatile int pressCount = 0; // Zählt die Button-Drucke
38+
39+
void IRAM_ATTR handleButtonPress() {
40+
unsigned long currentTime = millis(); // Hole aktuelle Zeit
41+
42+
// Vermeidung von Prellen: Wenn der aktuelle Druck zu nahe am letzten liegt, ignoriere ihn
43+
if (currentTime - lastPressTime > 50) {
44+
pressCount++; // Zähle den Button-Druck
45+
lastPressTime = currentTime; // Aktualisiere die Zeit des letzten Drückens
46+
47+
// Überprüfe, ob dies der zweite Druck innerhalb des Double-Press-Intervalls ist
48+
if (pressCount == 2 && (currentTime - lastPressTime <= doublePressInterval)) {
49+
doublePressDetected = true; // Doppeldruck erkannt
50+
pressCount = 0; // Zähler zurücksetzen
51+
}
52+
}
53+
}
54+
55+
// Funktion zum Wechseln der Boot-Partition auf OTA1
56+
void setBootPartitionToOTA0() {
57+
const esp_partition_t* ota0_partition = esp_partition_find_first(
58+
ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_0, NULL);
59+
60+
if (ota0_partition) {
61+
// Setze OTA1 als neue Boot-Partition
62+
esp_ota_set_boot_partition(ota0_partition);
63+
Serial.println("Boot partition changed to OTA0. Restarting...");
64+
65+
// Neustart, um von der neuen Partition zu booten
66+
esp_restart();
67+
} else {
68+
Serial.println("OTA1 partition not found!");
69+
}
70+
}
71+
72+
void setupDisplay() {
73+
displayEnabled = display.begin(SSD1306_SWITCHCAPVCC, 0x3D);
74+
if(displayEnabled) {
75+
display.display();
76+
delay(100);
77+
display.clearDisplay();
78+
}
79+
}
80+
81+
void displaySSID() {
82+
display.setCursor(0, 0);
83+
display.setTextSize(1);
84+
display.setTextColor(WHITE, BLACK);
85+
display.println("Verbinde dich mit dem Access Point:");
86+
display.setTextSize(1);
87+
display.println();
88+
display.setCursor(10, 32);
89+
display.println(ssid);
90+
display.display();
91+
display.clearDisplay();
92+
}
93+
94+
void setupWiFi() {
95+
WiFi.macAddress(mac);
96+
char macLastFour[5];
97+
snprintf(macLastFour, sizeof(macLastFour), "%02X%02X", mac[4], mac[5]);
98+
ssid = "senseBox:" + String(macLastFour);
99+
100+
// Definiere die IP-Adresse, Gateway und Subnetzmaske
101+
IPAddress local_IP(192.168.1.1); // Die neue IP-Adresse
102+
IPAddress gateway(192.168.1.1); // Gateway-Adresse (kann gleich der IP des APs sein)
103+
IPAddress subnet(255.255.255.0); // Subnetzmaske
104+
105+
// Setze die IP-Adresse, Gateway und Subnetzmaske des Access Points
106+
WiFi.softAPConfig(local_IP, gateway, subnet);
107+
108+
// Starte den Access Point
109+
WiFi.softAP(ssid.c_str());
110+
}
111+
112+
void setupOTA() {
113+
// Handle updating process
114+
server.on("/sketch", HTTP_POST, []() {
115+
server.sendHeader("Connection", "close");
116+
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
117+
ESP.restart();
118+
}, []() {
119+
HTTPUpload& upload = server.upload();
120+
121+
if (upload.status == UPLOAD_FILE_START) {
122+
Serial.setDebugOutput(true);
123+
Serial.printf("Update: %s\n", upload.filename.c_str());
124+
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
125+
Update.printError(Serial);
126+
}
127+
} else if (upload.status == UPLOAD_FILE_WRITE) {
128+
/* flashing firmware to ESP*/
129+
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
130+
Update.printError(Serial);
131+
}
132+
} else if (upload.status == UPLOAD_FILE_END) {
133+
if (Update.end(true)) { //true to set the size to the current progress
134+
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
135+
} else {
136+
Update.printError(Serial);
137+
}
138+
Serial.setDebugOutput(false);
139+
}
140+
yield();
141+
});
142+
}
143+
144+
void setup() {
145+
// Start Serial communication
146+
Serial.begin(115200);
147+
rgb_led_1.begin();
148+
rgb_led_1.setBrightness(30);
149+
rgb_led_1.setPixelColor(0,rgb_led_1.Color(51, 51, 255));
150+
rgb_led_1.show();
151+
152+
// Button-Pin als Input konfigurieren
153+
pinMode(BUTTON_PIN, INPUT_PULLUP);
154+
155+
// Interrupt für den Button
156+
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), handleButtonPress, FALLING);
157+
158+
#ifdef DISPLAY_ENABLED
159+
setupDisplay();
160+
#endif
161+
setupWiFi();
162+
// Set the ESP32 as an access point
163+
setupOTA();
164+
server.begin();
165+
}
166+
167+
void loop() {
168+
// Handle client requests
169+
server.handleClient();
170+
171+
#ifdef DISPLAY_ENABLED
172+
displaySSID();
173+
#endif
174+
175+
if (doublePressDetected) {
176+
Serial.println("Doppeldruck erkannt!");
177+
setBootPartitionToOTA0();
178+
// Neustart, um von der neuen Partition zu booten
179+
esp_restart();
180+
181+
}
182+
}

0 commit comments

Comments
 (0)