Added MQTT client, better speaker handling, fixed bug in FTP server, ...

This commit is contained in:
2019-08-14 21:01:01 +02:00
parent dcbb42f5ef
commit 5f682c303f
54 changed files with 4945 additions and 52 deletions

View File

@ -1,9 +1,10 @@
#include "controller.h"
#include "spi_master.h"
Controller::Controller(Player* p, MCP* m) {
Controller::Controller(Player* p, MCP* m, MQTTClient* mqtt) {
_player = p;
_mcp = m;
_mqtt_client = mqtt;
_rfid = new MFRC522(PIN_RC522_CS, MFRC522::UNUSED_PIN);
SPIMaster::enable(PIN_MCP);
@ -32,6 +33,11 @@ void Controller::loop() {
}
_check_serial();
_check_buttons();
if ((_last_mqtt_report_at < now - MQTT_REPORT_INTERVAL) || (now < _last_mqtt_report_at)) {
_send_mqtt_report();
_last_mqtt_report_at = now;
}
}
uint32_t Controller::_get_rfid_card_uid() {
@ -53,6 +59,7 @@ void Controller::_check_rfid() {
uint32_t uid = _get_rfid_card_uid();
if (uid != _last_rfid_card_uid) {
if (uid > 0) {
_mqtt_client->publish_rfid_uid(uid);
_no_rfid_card_count = 0;
INFO("New RFID card uid: %08x\n", uid);
String s_uid = String(uid, HEX);
@ -157,7 +164,7 @@ void Controller::_check_buttons() {
bool Controller::_check_button(uint8_t index) {
if (index >= NUM_BUTTONS) return false;
bool ret = false;
uint8_t sum = 0;
/*uint8_t sum = 0;
while (1) {
sum = 0;
for (int i=0; i<8; i++) {
@ -165,7 +172,8 @@ bool Controller::_check_button(uint8_t index) {
}
if (sum==0 || sum==8) break;
}
if (sum == 0) {
if (sum == 0) {*/
if (_mcp->digitalRead(index + 1) == LOW) {
if (_button_last_pressed_at[index] + DEBOUNCE_MILLIS < millis()) {
DEBUG("Button %d pressed.\n", index);
ret = true;
@ -174,3 +182,33 @@ bool Controller::_check_button(uint8_t index) {
}
return ret;
}
String Controller::get_status_json() {
String response = String("{");
response.concat("\"state\": \"");
response.concat(_player->is_playing() ? "playing" : "idle");
response.concat("\", ");
if (_player->is_playing()) {
response.concat("\"album\": \"");
response.concat(_player->album());
response.concat("\", \"track\": ");
response.concat(_player->track());
response.concat(", \"position\": ");
response.concat(_player->position());
response.concat(", ");
}
response.concat("\"volume\": ");
response.concat(_player->volume());
response.concat(", \"volume_max\": ");
response.concat(VOLUME_MAX);
response.concat(", \"volume_min\": ");
response.concat(VOLUME_MIN);
response.concat(", \"rfid_uid\": ");
response.concat(String(_last_rfid_card_uid, HEX));
response.concat("}\n");
return response;
}
void Controller::_send_mqtt_report() {
}

View File

@ -4,16 +4,7 @@ HTTPServer::HTTPServer(Player* p, Controller* c) {
_player = p;
_controller = c;
_http_server = new ESP8266WebServer(80);
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");
MDNS.begin("esmp3");
//_http_server->onFileUpload([&]() { _handle_upload(); yield();});
_http_server->on("/upload", HTTP_POST, [&]() {
_http_server->sendHeader("Connection", "close");
@ -123,29 +114,7 @@ void HTTPServer::_handle_index() {
}
void HTTPServer::_handle_status() {
String response = String("{");
response.concat("\"state\": \"");
response.concat(_player->is_playing() ? "playing" : "idle");
response.concat("\", ");
if (_player->is_playing()) {
response.concat("\"album\": \"");
response.concat(_player->album());
response.concat("\", \"track\": ");
response.concat(_player->track());
response.concat(", \"position\": ");
response.concat(_player->position());
response.concat(", ");
}
response.concat("\"volume\": ");
response.concat(_player->volume());
response.concat(", \"volume_max\": ");
response.concat(VOLUME_MAX);
response.concat(", \"volume_min\": ");
response.concat(VOLUME_MIN);
response.concat(", \"rfid_uid\": ");
response.concat(_controller->rfid_uid());
response.concat("}");
_http_server->send(200, "application/json", response);
_http_server->send(200, "application/json", _controller->get_status_json());
}
void HTTPServer::loop() {

View File

@ -7,6 +7,7 @@
#include "player.h"
#include "spi_master.h"
#include "http_server.h"
#include "mqtt_client.h"
#include <ESP8266FtpServer.h>
Controller* controller;
@ -14,6 +15,8 @@ Player* player;
MCP* mcp;
HTTPServer* http_server;
FtpServer* ftp_server;
MQTTClient* mqtt_client;
unsigned long last_mqtt_report = 0;
void setup() {
delay(500);
@ -25,6 +28,19 @@ void setup() {
INFO("ESMP3, version unknown\n");
#endif
INFO("Initializing...\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");
@ -48,7 +64,7 @@ void setup() {
DEBUG("Initializing Player and Controller...\n");
player = new Player(mcp);
controller = new Controller(player, mcp);
controller = new Controller(player, mcp, mqtt_client);
INFO("Player and controller initialized.\n");
DEBUG("Setting up WiFi and web server...\n");
@ -67,4 +83,9 @@ void loop() {
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());
}
}

36
src/mqtt_client.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "mqtt_client.h"
MQTTClient::MQTTClient() {
_wifi_client = new WiFiClient();
_mqtt = new PubSubClient(*_wifi_client);
_mqtt->setServer("10.10.2.1", 1883);
_last_reconnect_attempt = 0;
}
void MQTTClient::loop() {
if (!_mqtt->connected()) {
unsigned long now = millis();
if (now - _last_reconnect_attempt > 5000) {
DEBUG("Connecting to MQTT server...\n");
_last_reconnect_attempt = now;
_reconnect();
}
} else {
_mqtt->loop();
}
}
void MQTTClient::_reconnect() {
_mqtt->connect("esmp3", "esmp3", "pairugp598hpknfblaaagvaie58oh", "esmp3/state", 0, true, "OFFLINE");
_mqtt->publish("esmp3/state", "ONLINE", true);
}
void MQTTClient::publish_status(String s) {
_mqtt->publish("esmp3/status", s.c_str(), true);
}
void MQTTClient::publish_rfid_uid(uint32_t uid) {
String s = String(uid, HEX);
_mqtt->publish("esmp3/rfid_uid", s.c_str(), true);
}

View File

@ -113,7 +113,7 @@ void Player::_wakeup() {
_write_control_register(SCI_AUDATA, 0x0000);
_write_control_register(SCI_CLOCKF, 0x6000);
delay(10);
_speaker_on();
//_speaker_on();
_spi_settings = &_spi_settings_fast;
_state = idle;
}
@ -224,12 +224,14 @@ void Player::vol_down() {
void Player::_mute() {
INFO("Muting.\n");
_speaker_off();
set_volume(1, false);
}
void Player::_unmute() {
INFO("Unmuting.\n");
set_volume(_volume, false);
_speaker_on();
}
void Player::track_next() {
@ -407,6 +409,7 @@ void Player::_play_file(String file, uint32_t file_offset) {
_current_play_position = _file.position();
_skip_to = file_offset;
if (_skip_to>0) _mute();
else _speaker_on();
INFO("Now playing.\n");
}
@ -459,7 +462,7 @@ void Player::_finish_playing() {
_init();
}
void Player::stop() {
void Player::stop(bool turn_speaker_off) {
if (_state != playing /* && _state != system_sound_while_playing && _state != system_sound_while_stopped*/) return;
INFO("Stopping...\n");
if (_state == playing) {
@ -474,7 +477,7 @@ void Player::stop() {
uint16_t mode = _read_control_register(SCI_MODE);
if ((mode & SM_CANCEL) == 0) {
_flush(2052, endbyte);
_finish_stopping();
_finish_stopping(turn_speaker_off);
break;
} else if (_stop_delay > 2048) {
init();
@ -484,7 +487,8 @@ void Player::stop() {
}
}
void Player::_finish_stopping() {
void Player::_finish_stopping(bool turn_speaker_off) {
if (turn_speaker_off) _speaker_off();
_state = idle;
_stopped_at = millis();
if (_file) {
@ -501,17 +505,17 @@ void Player::_refill() {
if (result == 0) {
// File is over.
DEBUG("EOF reached.\n");
_skip_to = 0;
_finish_playing();
if (_state == system_sound_while_playing) {
_finish_stopping();
_finish_stopping(false);
play_album(_playing_album);
return;
} else if (_state == system_sound_while_stopped) {
_finish_stopping();
_finish_stopping(true);
return;
}
_finish_stopping();
_finish_stopping(false);
bool result = play_song(_playing_album, _playing_index + 1);
if (!result) {
_set_last_track(_playing_album.c_str(), 0, 0);