ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.4KB

  1. static const char *TAG = "blinky";
  2. #include <esp_log.h>
  3. #include <freertos/FreeRTOS.h>
  4. #include <freertos/task.h>
  5. #include <esp_spiffs.h>
  6. #include <nvs_flash.h>
  7. #include "wifi.hpp"
  8. #include "http_serv.hpp"
  9. #include "ota.hpp"
  10. #include "mqtt.hpp"
  11. // mDNS / NetBIOS
  12. #include <mdns.h>
  13. #include <lwip/apps/netbiosns.h>
  14. static void start_mdns_service(const char *hostname) {
  15. // Initialize mDNS service
  16. ESP_ERROR_CHECK(mdns_init());
  17. // Set hostname
  18. mdns_hostname_set(hostname);
  19. // Set default instance
  20. mdns_instance_name_set("Blinky Lights");
  21. // NetBIOS too
  22. netbiosns_init();
  23. netbiosns_set_name(hostname);
  24. }
  25. // Dummy FS
  26. static void start_filesystem() {
  27. ESP_LOGI(TAG, "Initializing filesystem");
  28. esp_vfs_spiffs_conf_t conf = {
  29. .base_path = "/spiffs",
  30. .partition_label = NULL,
  31. .max_files = 5,
  32. .format_if_mount_failed = true
  33. };
  34. // Use settings defined above to initialize and mount SPIFFS filesystem.
  35. // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
  36. ESP_ERROR_CHECK(esp_vfs_spiffs_register(&conf));
  37. }
  38. #include <string>
  39. #include <dirent.h>
  40. #include <sys/stat.h>
  41. #include <errno.h>
  42. static void log_dir(const std::string &path) {
  43. struct stat st;
  44. int err = stat(path.c_str(), &st);
  45. if (err < 0) {
  46. ESP_LOGE(TAG, "%s %d", path.c_str(), errno);
  47. } else {
  48. ESP_LOGI(TAG, "%s %d", path.c_str(), st.st_size);
  49. }
  50. DIR *dir;
  51. if ((dir = opendir(path.c_str())) == NULL) {
  52. //ESP_LOGE(TAG, "Failed to open %s", path.c_str());
  53. return;
  54. }
  55. struct dirent *de;
  56. while ((de = readdir(dir)) != NULL) {
  57. log_dir(path + "/" + de->d_name);
  58. }
  59. closedir(dir);
  60. }
  61. // Entry Point
  62. extern "C" void app_main(void) {
  63. // Initialize NVS for WiFi Data
  64. esp_err_t ret = nvs_flash_init();
  65. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  66. ESP_ERROR_CHECK(nvs_flash_erase());
  67. ret = nvs_flash_init();
  68. }
  69. ESP_ERROR_CHECK(ret);
  70. // WiFi
  71. config_wifi();
  72. // mDNS
  73. start_mdns_service("blinky-jr");
  74. // HTTP Server
  75. httpd_handle_t server = start_webserver();
  76. // OTA Server
  77. start_ota_serv(server);
  78. // Dummy Filesystem
  79. start_filesystem();
  80. log_dir("/spiffs");
  81. // TODO: Something useful
  82. esp_mqtt_client_handle_t client = start_mqtt_client(NULL, NULL, NULL);
  83. esp_mqtt_client_subscribe(client, "#", 0);
  84. // Spin
  85. }