#include #include #include #include #include #include "config.h" #include "controller.h" #include "player.h" #include "spi_master.h" #include "http_server.h" #include "mqtt_client.h" #include Controller* controller; Player* player; Adafruit_MCP23017* mcp; HTTPServer* http_server; FtpServer* ftp_server; MQTTClient* mqtt_client; unsigned long last_mqtt_report = 0; void setup() { delay(500); Serial.begin(74880); /*Serial.println("Starting..."); Serial.println("Started."); INFO("Starting.\n"); #ifdef VERSION INFO("ESMP3 version %s\n", VERSION); #else INFO("ESMP3, version unknown\n"); #endif INFO("Initializing...\n"); DEBUG("Setting up MCP...\n");*/ Wire.begin(); Wire.setClock(1700000); uint8_t addr = MCP23017_ADDRESS + MCP_I2C_ADDR; while(true) { Wire.beginTransmission(addr); byte status = Wire.endTransmission(); if (status==0) { DEBUG("I2C device found."); break; } DEBUG("No I2C device found."); delay(100); } mcp = new Adafruit_MCP23017(); mcp->begin(MCP_I2C_ADDR); INFO("MCP initialized.\n"); DEBUG("Setting up SPI...\n"); SPI.begin(); SPI.setHwCs(false); SPIMaster* spi = new SPIMaster(mcp); INFO("SPI initialized.\n"); DEBUG("Setting up SD card...\n"); spi->select_sd(); if (SD.begin(42)) { INFO("SD card initialized.\n"); } else { ERROR("Could not initialize SD card.\n"); } spi->select_sd(false); DEBUG("Initializing Player and Controller...\n"); player = new Player(mcp, spi); controller = new Controller(player, mcp, spi); INFO("Player and controller initialized.\n"); DEBUG("Connecting to wifi \"%s\"...\n", WIFI_SSID); WiFi.mode(WIFI_AP_STA); WiFi.begin(WIFI_SSID, WIFI_PASS); if (WiFi.waitForConnectResult() != WL_CONNECTED) { ERROR("Could not connect to Wifi. Rebooting."); delay(1000); ESP.restart(); } INFO("WiFi connected.\n"); mqtt_client = new MQTTClient(); MDNS.begin("esmp3"); controller->set_mqtt_client(mqtt_client); DEBUG("Setting up WiFi and web server...\n"); http_server = new HTTPServer(player, controller); ftp_server = new FtpServer(); ftp_server->begin("user", "pass"); INFO("Initialization completed.\n"); } void loop() { bool more_data_needed = player->loop(); if (more_data_needed) return; controller->loop(); http_server->loop(); ftp_server->handleFTP(); mqtt_client->loop(); if ((last_mqtt_report + 10000 < millis()) || last_mqtt_report > millis()) { last_mqtt_report = millis(); mqtt_client->publish_status(controller->get_status_json()); } }