|
| 1 | +#defineDISPLAY_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 | +#defineSCREEN_WIDTH128 |
| 11 | +#defineSCREEN_HEIGHT64 |
| 12 | +#defineOLED_RESET -1 |
| 13 | +#include<Adafruit_GFX.h> |
| 14 | +#include<Adafruit_SSD1306.h> |
| 15 | +Adafruit_SSD1306display(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 | +#endif |
| 20 | +#include"esp_partition.h" |
| 21 | +#include"esp_ota_ops.h" |
| 22 | +#include"esp_system.h" |
| 23 | + |
| 24 | +String ssid; |
| 25 | +uint8_t mac[6]; |
| 26 | + |
| 27 | +// Create an instance of the server |
| 28 | +WebServerserver(80); |
| 29 | +bool displayEnabled; |
| 30 | + |
| 31 | +constint BUTTON_PIN =0;// GPIO for the button |
| 32 | +volatileunsignedlong lastPressTime =0;// Time of last button press |
| 33 | +volatilebool doublePressDetected =false;// Flag for double press |
| 34 | +constunsignedlong doublePressInterval =500;// Max. time (in ms) between two presses for double press |
| 35 | +volatileint pressCount =0;// Counts the button presses |
| 36 | + |
| 37 | +constunsignedchar epd_bitmap_wifi[] PROGMEM = { |
| 38 | +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 39 | +0x00,0x00,0x1f,0x80,0x00,0x00,0x00,0x01,0xff,0xf0,0x00,0x00,0x00,0x07,0xff,0xfc,0x00,0x00,0x00,0x1f,0xe0,0xff,0x00,0x00, |
| 40 | +0x00,0x3f,0x00,0x0f,0x80,0x00,0x00,0x7c,0x00,0x03,0xe0,0x00,0x00,0xf0,0x00,0x01,0xf0,0x00,0x01,0xe0,0x00,0x00,0x78,0x00, |
| 41 | +0x03,0xc0,0x00,0x00,0x38,0x00,0x07,0x80,0x00,0x00,0x1c,0x00,0x0f,0x00,0x06,0x00,0x0e,0x00,0x0e,0x00,0x7f,0xe0,0x0e,0x00, |
| 42 | +0x0c,0x01,0xff,0xf0,0x06,0x00,0x00,0x07,0xff,0xfc,0x02,0x00,0x00,0x0f,0x80,0x3e,0x00,0x00,0x00,0x1f,0x00,0x0f,0x00,0x00, |
| 43 | +0x00,0x1c,0x00,0x07,0x80,0x00,0x00,0x38,0x00,0x03,0xc0,0x00,0x00,0x70,0x00,0x01,0xc0,0x00,0x00,0x70,0x00,0x00,0xc0,0x00, |
| 44 | +0x00,0x20,0x00,0x00,0x40,0x00,0x00,0x00,0x1f,0x80,0x00,0x00,0x00,0x00,0x7f,0xc0,0x00,0x00,0x00,0x00,0xff,0xe0,0x00,0x00, |
| 45 | +0x00,0x01,0xe0,0xf0,0x00,0x00,0x00,0x01,0xc0,0x78,0x00,0x00,0x00,0x03,0x80,0x38,0x00,0x00,0x00,0x01,0x00,0x10,0x00,0x00, |
| 46 | +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 47 | +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00, |
| 48 | +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 |
| 49 | +}; |
| 50 | + |
| 51 | +// 'checkmark', 44x44px |
| 52 | +constunsignedchar epd_bitmap_checkmark[] PROGMEM = { |
| 53 | +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 54 | +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 55 | +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 56 | +0x00,0x00,0x00,0x01,0x80,0x00,0x00,0x00,0x00,0x03,0x80,0x00,0x00,0x00,0x00,0x07,0x80,0x00,0x00,0x00,0x00,0x0f,0x00,0x00, |
| 57 | +0x00,0x00,0x00,0x1e,0x00,0x00,0x00,0x00,0x00,0x3c,0x00,0x00,0x00,0x00,0x00,0x78,0x00,0x00,0x00,0x0e,0x00,0xf0,0x00,0x00, |
| 58 | +0x00,0x0f,0x01,0xe0,0x00,0x00,0x00,0x0f,0x83,0xc0,0x00,0x00,0x00,0x07,0xc7,0x80,0x00,0x00,0x00,0x03,0xef,0x00,0x00,0x00, |
| 59 | +0x00,0x01,0xfe,0x00,0x00,0x00,0x00,0x00,0xfc,0x00,0x00,0x00,0x00,0x00,0x78,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00, |
| 60 | +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 61 | +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 62 | +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 63 | +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 |
| 64 | +}; |
| 65 | + |
| 66 | +void IRAM_ATTRhandleButtonPress() { |
| 67 | +unsignedlong currentTime =millis();// Get current time |
| 68 | + |
| 69 | +// Debounce: If the current press is too close to the last one, ignore it |
| 70 | +if (currentTime - lastPressTime >50) { |
| 71 | + pressCount++;// Count the button press |
| 72 | + |
| 73 | +// Check if this is the second press within the double-press interval |
| 74 | +if (pressCount ==2 && (currentTime - lastPressTime <= doublePressInterval)) { |
| 75 | + doublePressDetected =true;// Double press detected |
| 76 | + pressCount =0;// Reset counter |
| 77 | + } |
| 78 | + |
| 79 | + lastPressTime = currentTime;// Update the time of the last press |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// Function to switch the boot partition to OTA1 |
| 84 | +voidsetBootPartitionToOTA0() { |
| 85 | +constesp_partition_t *ota0_partition =esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_0,NULL); |
| 86 | + |
| 87 | +if (ota0_partition) { |
| 88 | +// Set OTA1 as new boot partition |
| 89 | +esp_ota_set_boot_partition(ota0_partition); |
| 90 | + Serial.println("Boot partition changed to OTA0. Restarting..."); |
| 91 | + |
| 92 | +// Restart to boot from the new partition |
| 93 | +esp_restart(); |
| 94 | + }else { |
| 95 | + Serial.println("OTA1 partition not found!"); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +voidsetupDisplay() { |
| 100 | + displayEnabled = display.begin(SSD1306_SWITCHCAPVCC,0x3D); |
| 101 | +if (displayEnabled) { |
| 102 | + display.display(); |
| 103 | +delay(100); |
| 104 | + display.clearDisplay(); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +voiddisplayStatusBar(int progress) { |
| 109 | + display.clearDisplay(); |
| 110 | + display.setCursor(24,8); |
| 111 | + display.println("Sketch wird"); |
| 112 | + display.setCursor(22,22); |
| 113 | + display.println("hochgeladen!"); |
| 114 | + |
| 115 | + display.fillRect(0, SCREEN_HEIGHT -24, SCREEN_WIDTH -4,8, BLACK);// Clear status bar area |
| 116 | + display.drawRect(0, SCREEN_HEIGHT -24, SCREEN_WIDTH -4,8, WHITE);// Draw border |
| 117 | +int filledWidth = (progress * SCREEN_WIDTH -4) /100;// Calculate progress width |
| 118 | + display.fillRect(1, SCREEN_HEIGHT -23, filledWidth -4,6, WHITE);// Fill progress bar |
| 119 | + |
| 120 | + display.setCursor((SCREEN_WIDTH /2) -12, SCREEN_HEIGHT -10); |
| 121 | + display.setTextSize(1); |
| 122 | + display.setTextColor(WHITE, BLACK); |
| 123 | + display.print(progress); |
| 124 | + display.println(" %"); |
| 125 | + display.display(); |
| 126 | +} |
| 127 | + |
| 128 | +voiddisplayWelcomeScreen() { |
| 129 | + display.clearDisplay(); |
| 130 | + |
| 131 | +// Draw WiFi symbol |
| 132 | + display.drawBitmap(0,12, epd_bitmap_wifi,44,44, WHITE); |
| 133 | + |
| 134 | +// Display SSID text |
| 135 | + display.setCursor(40,13); |
| 136 | + display.setTextSize(1); |
| 137 | + display.setTextColor(WHITE, BLACK); |
| 138 | + display.println("Verbinde dich");// "Connect" |
| 139 | + display.setCursor(60,27); |
| 140 | + display.println("mit:");// "with" |
| 141 | + |
| 142 | +// Display SSID |
| 143 | + display.setCursor(40,43); |
| 144 | + display.setTextSize(1);// Larger text for SSID |
| 145 | + display.print(ssid); |
| 146 | + |
| 147 | + display.display(); |
| 148 | +} |
| 149 | + |
| 150 | +voiddisplaySuccessScreen() { |
| 151 | + display.clearDisplay(); |
| 152 | + |
| 153 | +// Draw WiFi symbol |
| 154 | + display.drawBitmap(0,12, epd_bitmap_checkmark,44,44, WHITE); |
| 155 | + |
| 156 | +// Display SSID text |
| 157 | + display.setCursor(48,22); |
| 158 | + display.setTextSize(1); |
| 159 | + display.setTextColor(WHITE, BLACK); |
| 160 | + display.println("Erfolgreich");// "Successfully" |
| 161 | + display.setCursor(48,36); |
| 162 | + display.println("hochgeladen!");// "uploaded!" |
| 163 | + |
| 164 | + display.display(); |
| 165 | +} |
| 166 | + |
| 167 | +voidwipeDisplay() { |
| 168 | + display.clearDisplay(); |
| 169 | + display.println(""); |
| 170 | + display.display(); |
| 171 | +} |
| 172 | + |
| 173 | +voidsetupWiFi() { |
| 174 | + WiFi.macAddress(mac); |
| 175 | +char macLastFour[5]; |
| 176 | +snprintf(macLastFour,sizeof(macLastFour),"%02X%02X", mac[4], mac[5]); |
| 177 | + ssid ="senseBox:" +String(macLastFour); |
| 178 | + |
| 179 | +// Define the IP address, gateway, and subnet mask |
| 180 | + IPAddresslocal_IP(192,168,1,1);// The new IP address |
| 181 | + IPAddressgateway(192,168,1,1);// Gateway address (can be the same as the AP's IP) |
| 182 | + IPAddresssubnet(255,255,255,0);// Subnet mask |
| 183 | + |
| 184 | +// Set the IP address, gateway, and subnet mask of the access point |
| 185 | + WiFi.softAPConfig(local_IP, gateway, subnet); |
| 186 | + |
| 187 | +// Start the access point |
| 188 | + WiFi.softAP(ssid.c_str()); |
| 189 | +} |
| 190 | + |
| 191 | +voidsetupOTA() { |
| 192 | +// Handle updating process |
| 193 | + server.on( |
| 194 | +"/sketch", HTTP_POST, |
| 195 | + []() { |
| 196 | + server.sendHeader("Connection","close"); |
| 197 | + server.send(200,"text/plain", (Update.hasError()) ?"FAIL" :"OK"); |
| 198 | + ESP.restart(); |
| 199 | + }, |
| 200 | + []() { |
| 201 | + HTTPUpload &upload = server.upload(); |
| 202 | + |
| 203 | +if (upload.status == UPLOAD_FILE_START) { |
| 204 | + Serial.setDebugOutput(true); |
| 205 | +size_t fsize = UPDATE_SIZE_UNKNOWN; |
| 206 | +if (server.clientContentLength() >0) { |
| 207 | + fsize = server.clientContentLength(); |
| 208 | + } |
| 209 | + Serial.printf("Receiving Update: %s, Size: %d\n", upload.filename.c_str(), fsize); |
| 210 | + |
| 211 | + Serial.printf("Update: %s\n", upload.filename.c_str()); |
| 212 | +if (!Update.begin(fsize)) {//start with max available size |
| 213 | + Update.printError(Serial); |
| 214 | + } |
| 215 | + }elseif (upload.status == UPLOAD_FILE_WRITE) { |
| 216 | +/* flashing firmware to ESP*/ |
| 217 | +if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { |
| 218 | + Update.printError(Serial); |
| 219 | + }else { |
| 220 | +int progress = (Update.progress() *100) / Update.size(); |
| 221 | +displayStatusBar(progress);// Update progress on status bar |
| 222 | + } |
| 223 | + }elseif (upload.status == UPLOAD_FILE_END) { |
| 224 | +if (Update.end(true)) {//true to set the size to the current progress |
| 225 | +displaySuccessScreen(); |
| 226 | +delay(3000); |
| 227 | +wipeDisplay(); |
| 228 | + }else { |
| 229 | + Update.printError(Serial); |
| 230 | + } |
| 231 | + Serial.setDebugOutput(false); |
| 232 | + } |
| 233 | +yield(); |
| 234 | + } |
| 235 | + ); |
| 236 | +} |
| 237 | + |
| 238 | +voidsetup() { |
| 239 | +// Start Serial communication |
| 240 | + Serial.begin(115200); |
| 241 | + rgb_led_1.begin(); |
| 242 | + rgb_led_1.setBrightness(30); |
| 243 | + rgb_led_1.setPixelColor(0, rgb_led_1.Color(51,51,255)); |
| 244 | + rgb_led_1.show(); |
| 245 | + |
| 246 | +// Configure button pin as input |
| 247 | +pinMode(BUTTON_PIN, INPUT_PULLUP); |
| 248 | + |
| 249 | +// Interrupt for the button |
| 250 | +attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), handleButtonPress, FALLING); |
| 251 | + |
| 252 | +#ifdef DISPLAY_ENABLED |
| 253 | +setupDisplay(); |
| 254 | +#endif |
| 255 | +setupWiFi(); |
| 256 | +// Set the ESP32 as an access point |
| 257 | +setupOTA(); |
| 258 | + server.begin(); |
| 259 | +} |
| 260 | + |
| 261 | +voidloop() { |
| 262 | +// Handle client requests |
| 263 | + server.handleClient(); |
| 264 | + |
| 265 | +#ifdef DISPLAY_ENABLED |
| 266 | +displayWelcomeScreen(); |
| 267 | +#endif |
| 268 | + |
| 269 | +if (doublePressDetected) { |
| 270 | + Serial.println("Doppeldruck erkannt!");// "Double press detected!" |
| 271 | +setBootPartitionToOTA0(); |
| 272 | +#ifdef DISPLAY_ENABLED |
| 273 | + display.setCursor(0,0); |
| 274 | + display.setTextSize(1); |
| 275 | + display.setTextColor(WHITE, BLACK); |
| 276 | + display.println(""); |
| 277 | + display.display(); |
| 278 | +delay(50); |
| 279 | +#endif |
| 280 | +// Restart to boot from the new partition |
| 281 | +esp_restart(); |
| 282 | + } |
| 283 | +} |