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-11 04:32:41 +00:00
|
|
|
#include <WiFi.h>
|
2019-11-16 22:03:13 +00:00
|
|
|
#include <ESPmDNS.h>
|
2019-11-16 23:35:55 +00:00
|
|
|
#include <SPIFFS.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-11-14 19:42:02 +00:00
|
|
|
#include "playlist_manager.h"
|
2019-11-28 05:42:30 +00:00
|
|
|
#include "updater.h"
|
2019-08-02 21:48:36 +00:00
|
|
|
|
|
|
|
Controller* controller;
|
|
|
|
Player* player;
|
2019-11-14 19:42:02 +00:00
|
|
|
PlaylistManager* pm;
|
2019-11-16 22:03:13 +00:00
|
|
|
HTTPServer* http_server;
|
|
|
|
|
2019-11-17 13:25:22 +00:00
|
|
|
uint8_t SPIMaster::state = 0;
|
2019-08-02 21:48:36 +00:00
|
|
|
|
2019-11-27 05:51:20 +00:00
|
|
|
bool connect_to_wifi(String ssid, String pass) {
|
|
|
|
TRACE("Connecting to wifi \"%s\"...\n", ssid.c_str());
|
|
|
|
WiFi.mode(WIFI_AP_STA);
|
|
|
|
WiFi.begin(ssid.c_str(), pass.c_str());
|
|
|
|
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
|
|
|
DEBUG("Could not connect to wifi \"%s\".\n", ssid.c_str());
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
INFO("Connected to \"%s\". IP address: %s\n", ssid.c_str(), WiFi.localIP().toString().c_str());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-11 04:32:41 +00:00
|
|
|
Serial.println("Starting...");
|
2019-11-10 13:45:33 +00:00
|
|
|
Serial.println("Started.");
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("Starting.\n");
|
2019-08-13 17:39:03 +00:00
|
|
|
#ifdef VERSION
|
2019-11-28 05:42:30 +00:00
|
|
|
INFO("ESMP3 version %s (OTA_VERSION %d)\n", VERSION, OTA_VERSION);
|
2019-08-13 17:39:03 +00:00
|
|
|
#else
|
2019-11-28 05:42:30 +00:00
|
|
|
INFO("ESMP3, version unknown (OTA_VERSION %d)\n", OTA_VERSION);
|
2019-08-13 17:39:03 +00:00
|
|
|
#endif
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("Initializing...\n");
|
2019-08-13 04:16:08 +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);
|
2019-11-14 19:42:02 +00:00
|
|
|
SPIMaster::init();
|
2019-11-11 04:32:41 +00:00
|
|
|
SPIMaster* spi = new SPIMaster();
|
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();
|
2019-11-16 23:35:55 +00:00
|
|
|
if (SD.begin(42, SPI, 25000000)) {
|
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-11-16 23:35:55 +00:00
|
|
|
DEBUG("Starting SPIFFS...\n");
|
2019-11-28 05:42:30 +00:00
|
|
|
uint16_t spiffs_version = 0;
|
2019-11-16 23:35:55 +00:00
|
|
|
SPIFFS.begin(true);
|
2019-11-28 05:42:30 +00:00
|
|
|
if (SPIFFS.exists("/_version.txt")) {
|
|
|
|
File f = SPIFFS.open("/_version.txt", "r");
|
|
|
|
spiffs_version = f.readString().toInt();
|
|
|
|
f.close();
|
|
|
|
DEBUG("SPIFFS filesystem version is %d.\n", spiffs_version);
|
|
|
|
} else {
|
|
|
|
DEBUG("No SPIFFS filesystem version found - setting spiffs_version to 0.\n");
|
|
|
|
}
|
2019-11-16 23:35:55 +00:00
|
|
|
|
2019-11-14 19:42:02 +00:00
|
|
|
DEBUG("Initializing PlaylistManager...\n");
|
|
|
|
pm = new PlaylistManager();
|
|
|
|
|
2019-08-12 18:15:00 +00:00
|
|
|
DEBUG("Initializing Player and Controller...\n");
|
2019-11-11 04:32:41 +00:00
|
|
|
player = new Player(spi);
|
2019-11-14 19:42:02 +00:00
|
|
|
controller = new Controller(player, pm);
|
2019-08-12 18:15:00 +00:00
|
|
|
INFO("Player and controller initialized.\n");
|
2019-08-13 04:16:08 +00:00
|
|
|
|
2019-11-27 05:51:20 +00:00
|
|
|
bool connected = false;
|
|
|
|
INFO("Connecting to WiFi...\n");
|
|
|
|
SPIMaster::select_sd();
|
|
|
|
if (SD.exists("/_wifis.txt")) {
|
|
|
|
DEBUG("Reading /_wifis.txt\n");
|
|
|
|
File f = SD.open("/_wifis.txt", "r");
|
|
|
|
while (String line = f.readStringUntil('\n')) {
|
|
|
|
if (line.length()==0 || line.startsWith("#") || line.indexOf('=')==-1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
String ssid = line.substring(0, line.indexOf('='));
|
|
|
|
String pass = line.substring(line.indexOf('=')+1);
|
|
|
|
connected = connect_to_wifi(ssid, pass);
|
|
|
|
if (connected) break;
|
|
|
|
}
|
|
|
|
f.close();
|
|
|
|
} else {
|
|
|
|
File f = SD.open("/_wifis.txt", "w");
|
|
|
|
f.print("# WiFi definitions. Syntax: <SSID>=<PASS>. Lines starting with # are ignored. Example:\n# My WiFi=VerySecretPassword\n");
|
|
|
|
f.close();
|
|
|
|
}
|
|
|
|
SPIMaster::select_sd(false);
|
2019-11-28 05:42:30 +00:00
|
|
|
|
|
|
|
|
2019-11-27 05:51:20 +00:00
|
|
|
if (!connected) {
|
2019-11-28 05:42:30 +00:00
|
|
|
#if defined(WIFI_SSID) and defined(WIFI_PASS)
|
2019-11-27 05:51:20 +00:00
|
|
|
DEBUG("Trying hardcoded WiFi data...\n");
|
|
|
|
connected = connect_to_wifi(WIFI_SSID, WIFI_PASS);
|
2019-11-28 05:42:30 +00:00
|
|
|
#else
|
|
|
|
DEBUG("No hardcoded WiFi data set.\n");
|
|
|
|
#endif
|
2019-11-27 05:51:20 +00:00
|
|
|
}
|
|
|
|
if (!connected) {
|
|
|
|
INFO("No WiFi connection!\n");
|
2019-11-10 13:45:33 +00:00
|
|
|
}
|
|
|
|
|
2019-11-16 22:03:13 +00:00
|
|
|
MDNS.begin("esmp3");
|
2019-11-10 13:45:33 +00:00
|
|
|
|
2019-11-16 22:03:13 +00:00
|
|
|
DEBUG("Setting up HTTP server...\n");
|
|
|
|
http_server = new HTTPServer(player, controller);
|
|
|
|
controller->register_http_server(http_server);
|
2019-08-13 17:39:03 +00:00
|
|
|
|
2019-11-16 22:03:13 +00:00
|
|
|
DEBUG("Starting NTP client...\n");
|
|
|
|
// Taken from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h
|
|
|
|
configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "europe.pool.ntp.org");
|
|
|
|
struct tm time;
|
|
|
|
if (getLocalTime(&time, 10000)) {
|
|
|
|
char buffer[100];
|
|
|
|
strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", &time);
|
|
|
|
DEBUG("Got time: %s\n", buffer);
|
|
|
|
} else {
|
|
|
|
INFO("Could not fetch current time via NTP.\n");
|
|
|
|
}
|
2019-11-28 05:42:30 +00:00
|
|
|
|
|
|
|
#ifdef OTA_UPDATE_URL
|
|
|
|
if (spiffs_version < OTA_VERSION) {
|
|
|
|
Updater::run();
|
|
|
|
}
|
|
|
|
#endif
|
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-02 21:48:36 +00:00
|
|
|
}
|