2019-08-02 21:48:36 +00:00
|
|
|
#include <Arduino.h>
|
2019-08-04 11:42:07 +00:00
|
|
|
#include <SPI.h>
|
2019-08-06 18:50:11 +00:00
|
|
|
#include <SD.h>
|
2019-11-10 13:45:33 +00:00
|
|
|
#include <Wire.h>
|
|
|
|
#include <Adafruit_MCP23017.h>
|
2019-08-02 21:48:36 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "controller.h"
|
|
|
|
#include "player.h"
|
2019-08-04 11:42:07 +00:00
|
|
|
#include "spi_master.h"
|
2019-08-12 18:15:00 +00:00
|
|
|
#include "http_server.h"
|
2019-08-14 19:01:01 +00:00
|
|
|
#include "mqtt_client.h"
|
2019-08-13 17:39:03 +00:00
|
|
|
#include <ESP8266FtpServer.h>
|
2019-08-02 21:48:36 +00:00
|
|
|
|
|
|
|
Controller* controller;
|
|
|
|
Player* player;
|
2019-11-10 13:45:33 +00:00
|
|
|
Adafruit_MCP23017* mcp;
|
2019-08-12 18:15:00 +00:00
|
|
|
HTTPServer* http_server;
|
2019-08-13 17:39:03 +00:00
|
|
|
FtpServer* ftp_server;
|
2019-08-14 19:01:01 +00:00
|
|
|
MQTTClient* mqtt_client;
|
|
|
|
unsigned long last_mqtt_report = 0;
|
2019-08-02 21:48:36 +00:00
|
|
|
|
|
|
|
void setup() {
|
2019-08-06 18:50:11 +00:00
|
|
|
delay(500);
|
2019-08-04 11:42:07 +00:00
|
|
|
Serial.begin(74880);
|
2019-11-10 13:45:33 +00:00
|
|
|
/*Serial.println("Starting...");
|
|
|
|
Serial.println("Started.");
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("Starting.\n");
|
2019-08-13 17:39:03 +00:00
|
|
|
#ifdef VERSION
|
|
|
|
INFO("ESMP3 version %s\n", VERSION);
|
|
|
|
#else
|
|
|
|
INFO("ESMP3, version unknown\n");
|
|
|
|
#endif
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("Initializing...\n");
|
2019-08-13 04:16:08 +00:00
|
|
|
|
2019-11-10 13:45:33 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-08-13 04:16:08 +00:00
|
|
|
|
2019-11-10 13:45:33 +00:00
|
|
|
mcp = new Adafruit_MCP23017();
|
|
|
|
mcp->begin(MCP_I2C_ADDR);
|
|
|
|
INFO("MCP initialized.\n");
|
2019-08-04 11:42:07 +00:00
|
|
|
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("Setting up SPI...\n");
|
2019-08-04 11:42:07 +00:00
|
|
|
SPI.begin();
|
2019-11-10 13:45:33 +00:00
|
|
|
SPI.setHwCs(false);
|
|
|
|
SPIMaster* spi = new SPIMaster(mcp);
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("SPI initialized.\n");
|
2019-08-04 11:42:07 +00:00
|
|
|
|
2019-08-09 04:27:33 +00:00
|
|
|
DEBUG("Setting up SD card...\n");
|
2019-11-10 13:45:33 +00:00
|
|
|
spi->select_sd();
|
|
|
|
if (SD.begin(42)) {
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("SD card initialized.\n");
|
2019-08-06 18:50:11 +00:00
|
|
|
} else {
|
2019-08-13 04:16:08 +00:00
|
|
|
ERROR("Could not initialize SD card.\n");
|
2019-08-06 18:50:11 +00:00
|
|
|
}
|
2019-11-10 13:45:33 +00:00
|
|
|
spi->select_sd(false);
|
2019-08-13 04:16:08 +00:00
|
|
|
|
2019-08-12 18:15:00 +00:00
|
|
|
DEBUG("Initializing Player and Controller...\n");
|
2019-11-10 13:45:33 +00:00
|
|
|
player = new Player(mcp, spi);
|
|
|
|
controller = new Controller(player, mcp, spi);
|
2019-08-12 18:15:00 +00:00
|
|
|
INFO("Player and controller initialized.\n");
|
2019-08-13 04:16:08 +00:00
|
|
|
|
2019-11-10 13:45:33 +00:00
|
|
|
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);
|
|
|
|
|
2019-08-12 18:15:00 +00:00
|
|
|
DEBUG("Setting up WiFi and web server...\n");
|
|
|
|
http_server = new HTTPServer(player, controller);
|
2019-08-13 17:39:03 +00:00
|
|
|
|
|
|
|
ftp_server = new FtpServer();
|
|
|
|
ftp_server->begin("user", "pass");
|
2019-08-11 15:15:22 +00:00
|
|
|
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("Initialization completed.\n");
|
2019-08-02 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2019-08-06 18:50:11 +00:00
|
|
|
bool more_data_needed = player->loop();
|
|
|
|
if (more_data_needed) return;
|
2019-08-02 21:48:36 +00:00
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
controller->loop();
|
2019-08-12 18:15:00 +00:00
|
|
|
http_server->loop();
|
2019-08-13 17:39:03 +00:00
|
|
|
ftp_server->handleFTP();
|
2019-08-14 19:01:01 +00:00
|
|
|
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());
|
|
|
|
}
|
2019-08-02 21:48:36 +00:00
|
|
|
}
|