v0.4.320
This commit is contained in:
112
yoRadio/mqtt.cpp
Normal file
112
yoRadio/mqtt.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "mqtt.h"
|
||||
|
||||
#ifdef MQTT_HOST
|
||||
#include "WiFi.h"
|
||||
|
||||
#include "telnet.h"
|
||||
#include "player.h"
|
||||
#include "config.h"
|
||||
|
||||
AsyncMqttClient mqttClient;
|
||||
TimerHandle_t mqttReconnectTimer;
|
||||
|
||||
void connectToMqtt() {
|
||||
mqttClient.connect();
|
||||
}
|
||||
|
||||
void mqttInit() {
|
||||
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
|
||||
mqttClient.onConnect(onMqttConnect);
|
||||
mqttClient.onDisconnect(onMqttDisconnect);
|
||||
mqttClient.onMessage(onMqttMessage);
|
||||
if(MQTT_USER!="") mqttClient.setCredentials(MQTT_USER, MQTT_PASS);
|
||||
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
|
||||
connectToMqtt();
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
char buf[140];
|
||||
sprintf(buf, "%s%s", MQTT_ROOT_TOPIC, "command");
|
||||
mqttClient.subscribe(buf, 2);
|
||||
mqttPublishStatus();
|
||||
mqttPublishVolume();
|
||||
mqttPublishPlaylist();
|
||||
}
|
||||
|
||||
void mqttPublishStatus() {
|
||||
if(mqttClient.connected()){
|
||||
char topic[140], status[255];
|
||||
sprintf(topic, "%s%s", MQTT_ROOT_TOPIC, "status");
|
||||
sprintf(status, "{\"status\": %d, \"station\": %d, \"name\": \"%s\", \"title\": \"%s\"}", player.mode==PLAYING?1:0, config.store.lastStation, config.station.name, config.station.title);
|
||||
mqttClient.publish(topic, 0, true, status);
|
||||
}
|
||||
}
|
||||
|
||||
void mqttPublishPlaylist() {
|
||||
if(mqttClient.connected()){
|
||||
char topic[140], playlist[140];
|
||||
sprintf(topic, "%s%s", MQTT_ROOT_TOPIC, "playlist");
|
||||
sprintf(playlist, "http://%s%s", WiFi.localIP().toString().c_str(), PLAYLIST_PATH);
|
||||
mqttClient.publish(topic, 0, true, playlist);
|
||||
}
|
||||
}
|
||||
|
||||
void mqttPublishVolume(){
|
||||
if(mqttClient.connected()){
|
||||
char topic[140], vol[5];
|
||||
sprintf(topic, "%s%s", MQTT_ROOT_TOPIC, "volume");
|
||||
sprintf(vol, "%d", config.store.volume);
|
||||
mqttClient.publish(topic, 0, true, vol);
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
if (WiFi.isConnected()) {
|
||||
xTimerStart(mqttReconnectTimer, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
if (strlen(payload) == 0) return;
|
||||
if (strcmp(payload, "prev") == 0) {
|
||||
player.prev();
|
||||
return;
|
||||
}
|
||||
if (strcmp(payload, "next") == 0) {
|
||||
player.next();
|
||||
return;
|
||||
}
|
||||
if (strcmp(payload, "toggle") == 0) {
|
||||
player.toggle();
|
||||
return;
|
||||
}
|
||||
if (strcmp(payload, "stop") == 0) {
|
||||
player.mode = STOPPED;
|
||||
telnet.info();
|
||||
return;
|
||||
}
|
||||
if (strcmp(payload, "start") == 0 || strcmp(payload, "play") == 0) {
|
||||
player.play(config.store.lastStation);
|
||||
return;
|
||||
}
|
||||
if (strcmp(payload, "boot") == 0 || strcmp(payload, "reboot") == 0) {
|
||||
ESP.restart();
|
||||
return;
|
||||
}
|
||||
int volume;
|
||||
if ( sscanf(payload, "vol %d", &volume) == 1) {
|
||||
if (volume < 0) volume = 0;
|
||||
if (volume > 254) volume = 254;
|
||||
player.setVol(volume, false);
|
||||
return;
|
||||
}
|
||||
uint16_t sb;
|
||||
if (sscanf(payload, "play %d", &sb) == 1 ) {
|
||||
if (sb < 1) sb = 1;
|
||||
if (sb >= config.store.countStation) sb = config.store.countStation;
|
||||
player.play(sb);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // ifdef MQTT_HOST
|
||||
21
yoRadio/mqtt.h
Normal file
21
yoRadio/mqtt.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef mqtt_h
|
||||
#define mqtt_h
|
||||
|
||||
#if __has_include("mqttoptions.h")
|
||||
#include "mqttoptions.h"
|
||||
#include <AsyncMqttClient.h>
|
||||
|
||||
|
||||
void mqttInit();
|
||||
void connectToMqtt();
|
||||
void onMqttConnect(bool sessionPresent);
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
|
||||
void mqttPublishStatus();
|
||||
void mqttPublishPlaylist();
|
||||
void mqttPublishVolume();
|
||||
|
||||
#endif // if __has_include("mqttoptions.h")
|
||||
|
||||
|
||||
#endif
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "display.h"
|
||||
#include "options.h"
|
||||
#include "network.h"
|
||||
#include "mqtt.h"
|
||||
|
||||
NetServer netserver;
|
||||
|
||||
@@ -183,6 +184,9 @@ void NetServer::requestOnChange(requestType_e request, uint8_t clientId) {
|
||||
config.indexPlaylist();
|
||||
config.initPlaylist();
|
||||
getPlaylist(clientId);
|
||||
#ifdef MQTT_HOST
|
||||
mqttPublishPlaylist();
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
case STATION: {
|
||||
@@ -200,6 +204,9 @@ void NetServer::requestOnChange(requestType_e request, uint8_t clientId) {
|
||||
}
|
||||
case VOLUME: {
|
||||
sprintf (buf, "{\"vol\": %d}", config.store.volume);
|
||||
#ifdef MQTT_HOST
|
||||
if (clientId == 0) mqttPublishVolume();
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
case NRSSI: {
|
||||
@@ -226,6 +233,9 @@ void NetServer::requestOnChange(requestType_e request, uint8_t clientId) {
|
||||
if (strlen(buf) > 0) {
|
||||
if (clientId == 0) {
|
||||
websocket.textAll(buf);
|
||||
#ifdef MQTT_HOST
|
||||
if(request==STATION || request==ITEM || request==TITLE || request==MODE) mqttPublishStatus();
|
||||
#endif
|
||||
} else {
|
||||
websocket.text(clientId, buf);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef options_h
|
||||
#define options_h
|
||||
|
||||
#define VERSION "0.4.315"
|
||||
#define VERSION "0.4.320"
|
||||
|
||||
#if __has_include("myoptions.h")
|
||||
#include "myoptions.h" // <- write your variable values here
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "network.h"
|
||||
#include "netserver.h"
|
||||
#include "controls.h"
|
||||
#include "mqtt.h"
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
@@ -30,6 +31,9 @@ void setup() {
|
||||
player.setVol(config.store.volume, true);
|
||||
display.start();
|
||||
if(config.store.smartstart==1) player.play(config.store.lastStation);
|
||||
#ifdef MQTT_HOST
|
||||
mqttInit();
|
||||
#endif
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
Reference in New Issue
Block a user