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