ModelessWithInterrupts.ino 2.6 KB

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