ModelessWithInterrupts.ino 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #if defined(ESP8266)
  2. #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
  3. #else
  4. #include <WiFi.h>
  5. #endif
  6. //needed for library
  7. #include <DNSServer.h>
  8. #include <ESPAsyncWebServer.h>
  9. #include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager
  10. AsyncWebServer server(80);
  11. DNSServer dns;
  12. AsyncWiFiManager wifiManager(&server,&dns);
  13. volatile boolean guard = true;
  14. const uint32_t callCycleCount = ESP.getCpuFreqMHz() * 1024 / 8;
  15. /*
  16. * If some other code was in the process of calling a method in
  17. * WiFi, ESP or EEPROM, and we do too, there is a very good chance
  18. * a reset will happen.
  19. */
  20. void ICACHE_RAM_ATTR interruptFunction() {
  21. /*
  22. * This is equivalent to:
  23. * timer0_write(ESP.getCycleCount() + ESP.getCpuFreqMHz() * 1024 / 8)
  24. * But that could clash with the main thread code.
  25. */
  26. uint32_t ccount;
  27. __asm__ __volatile__("esync; rsr %0,ccount":"=a" (ccount));
  28. timer0_write(ccount + callCycleCount);
  29. if (!guard) {
  30. // An example call that would cause a reset if it happens
  31. // to be called while AsyncWiFiManager is also calling
  32. // methods that modify flash.
  33. ESP.getChipId();
  34. }
  35. }
  36. void setup() {
  37. // put your setup code here, to run once:
  38. Serial.begin(115200);
  39. //WiFiManager
  40. //Local intialization. Once its business is done, there is no need to keep it around
  41. //reset saved settings
  42. //wifiManager.resetSettings();
  43. //set custom ip for portal
  44. //wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
  45. //fetches ssid and pass from eeprom and tries to connect
  46. //if it does not connect it starts an access point with the specified name
  47. //here "AutoConnectAP"
  48. //and goes into a blocking loop awaiting configuration
  49. //wifiManager.autoConnect("AutoConnectAP");
  50. //or use this for auto generated name ESP + ChipID
  51. //wifiManager.autoConnect();
  52. wifiManager.startConfigPortalModeless("ModelessAP", "Password");
  53. server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  54. request->send(200, "text/plain", "Hello World");
  55. });
  56. // Set up some code that could cause a reset.
  57. guard = true;
  58. timer0_isr_init();
  59. timer0_attachInterrupt(interruptFunction);
  60. timer0_write(ESP.getCycleCount() + ESP.getCpuFreqMHz() * 1024); // Wait 2 seconds before displaying
  61. updateDisplay = false;
  62. }
  63. void loop() {
  64. // put your main code here, to run repeatedly:
  65. // This can be called with guarding interrupt routines - so
  66. // interrupt routines can interrupt it!
  67. wifiManager.safeLoop();
  68. guard = true;
  69. // This call can not be interrupted without possibly causing a reset.
  70. wifiManager.criticalLoop();
  71. guard = false;
  72. }