added a custom MQTT topic option in the setup portal

master
MrDIY 5 years ago
parent 9477691e91
commit 73c96eac88

@ -22,26 +22,26 @@
Commands, to: Commands, to:
- Play MP3 MQTT topic: "/mrdiynotifier/play" - Play MP3 MQTT topic: "/mqttFullTopic()/play"
MQTT load: http://url-to-the-mp3-file/file.mp3 MQTT load: http://url-to-the-mp3-file/file.mp3
- Play Icecast Stream MQTT topic: "/mrdiynotifier/stream" - Play Icecast Stream MQTT topic: "/mqttFullTopic()/stream"
MQTT load: http://url-to-the-icecast-stream/file.mp3 MQTT load: http://url-to-the-icecast-stream/file.mp3, example: http://22203.live.streamtheworld.com/WHTAFM.mp3
- Play Ringtone MQTT topic: "/mrdiynotifier/tone" - Play Ringtone MQTT topic: "/mqttFullTopic()/tone"
MQTT load: RTTTL formated text, example: Soap:d=8,o=5,b=125:g,a,c6,p,a,4c6,4p,a,g,e,c,4p,4g,a MQTT load: RTTTL formated text, example: Soap:d=8,o=5,b=125:g,a,c6,p,a,4c6,4p,a,g,e,c,4p,4g,a
- Say Text MQTT topic: "/mrdiynotifier/say" - Say Text MQTT topic: "/mqttFullTopic()/say"
MQTT load: Text to be read, example: Hello There. How. Are. You? MQTT load: Text to be read, example: Hello There. How. Are. You?
- Change Volume MQTT topic: "/mrdiynotifier/volume" - Change Volume MQTT topic: "/mqttFullTopic()/volume"
MQTT load: a double between 0.00 and 1.00, example: 0.7 MQTT load: a double between 0.00 and 1.00, example: 0.7
- Stop Playing MQTT topic: "/mrdiynotifier/stop" - Stop Playing MQTT topic: "/mqttFullTopic()/stop"
To get status: To get status:
- The notifier sends status update on this MQTT topic: "/mrdiynotifier/status" - The notifier sends status update on this MQTT topic: "/mqttFullTopic()/status"
"playing" either paying an mp3, streaming, playing a ringtone or saying a text "playing" either paying an mp3, streaming, playing a ringtone or saying a text
"idle" waiting for a command "idle" waiting for a command
@ -118,12 +118,15 @@ byte i;
char mqttServer[16]; char mqttServer[16];
char mqttUserName[32]; char mqttUserName[32];
char mqttUserPassword[32]; char mqttUserPassword[32];
char mqttTopicPrefix[32];
char mqttTopic[MQTT_MSG_SIZE];
DNSServer dnsServer; DNSServer dnsServer;
WebServer server(80); WebServer server(80);
IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword); IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, "mrd2");
IotWebConfParameter mqttServerParam = IotWebConfParameter("MQTT server", "mqttServer", mqttServer, sizeof(mqttServer) ); IotWebConfParameter mqttServerParam = IotWebConfParameter("MQTT server", "mqttServer", mqttServer, sizeof(mqttServer) );
IotWebConfParameter mqttUserNameParam = IotWebConfParameter("MQTT username", "mqttUser", mqttUserName, sizeof(mqttUserName)); IotWebConfParameter mqttUserNameParam = IotWebConfParameter("MQTT username", "mqttUser", mqttUserName, sizeof(mqttUserName));
IotWebConfParameter mqttUserPasswordParam = IotWebConfParameter("MQTT password", "mqttPass", mqttUserPassword, sizeof(mqttUserPassword), "password"); IotWebConfParameter mqttUserPasswordParam = IotWebConfParameter("MQTT password", "mqttPass", mqttUserPassword, sizeof(mqttUserPassword), "password");
IotWebConfParameter mqttTopicParam = IotWebConfParameter("MQTT Topic", "mqttTopic", mqttTopicPrefix, sizeof(mqttTopicPrefix));
/* ################################## Setup ############################################# */ /* ################################## Setup ############################################# */
@ -136,6 +139,7 @@ void setup() {
iotWebConf.addParameter(&mqttServerParam); iotWebConf.addParameter(&mqttServerParam);
iotWebConf.addParameter(&mqttUserNameParam); iotWebConf.addParameter(&mqttUserNameParam);
iotWebConf.addParameter(&mqttUserPasswordParam); iotWebConf.addParameter(&mqttUserPasswordParam);
iotWebConf.addParameter(&mqttTopicParam);
iotWebConf.setWifiConnectionCallback(&wifiConnected); iotWebConf.setWifiConnectionCallback(&wifiConnected);
iotWebConf.setFormValidator(&formValidator); iotWebConf.setFormValidator(&formValidator);
iotWebConf.setStatusPin(LED_BUILTIN); iotWebConf.setStatusPin(LED_BUILTIN);
@ -163,7 +167,7 @@ void loop() {
mqttReconnect(); mqttReconnect();
mqttClient.loop(); mqttClient.loop();
// give processor priority to audio // give processor priority to audio
if(!mp3) iotWebConf.doLoop(); if (!mp3) iotWebConf.doLoop();
if (mp3 && !mp3->loop()) stopPlaying(); if (mp3 && !mp3->loop()) stopPlaying();
if (wav && !wav->loop()) stopPlaying(); if (wav && !wav->loop()) stopPlaying();
if (rtttl && !rtttl->loop()) stopPlaying(); if (rtttl && !rtttl->loop()) stopPlaying();
@ -259,7 +263,7 @@ void onMqttMessage(char* topic, byte* payload, unsigned int length) {
Serial.println(newMsg); Serial.println(newMsg);
#endif #endif
// got a new URL to play ------------------------------------------------ // got a new URL to play ------------------------------------------------
if ( !strcmp(topic, "/mrdiynotifier/play")) { if ( !strcmp(topic, mqttFullTopic("play") ) ) {
stopPlaying(); stopPlaying();
file_http = new AudioFileSourceHTTPStream(); file_http = new AudioFileSourceHTTPStream();
if ( file_http->open(newMsg)) { if ( file_http->open(newMsg)) {
@ -275,7 +279,7 @@ void onMqttMessage(char* topic, byte* payload, unsigned int length) {
} }
// got a new URL to play ------------------------------------------------ // got a new URL to play ------------------------------------------------
if ( !strcmp(topic, "/mrdiynotifier/stream")) { if ( !strcmp(topic, mqttFullTopic("stream"))) {
stopPlaying(); stopPlaying();
file_icy = new AudioFileSourceICYStream(); file_icy = new AudioFileSourceICYStream();
if ( file_icy->open(newMsg)) { if ( file_icy->open(newMsg)) {
@ -291,7 +295,7 @@ void onMqttMessage(char* topic, byte* payload, unsigned int length) {
} }
// got a tone request -------------------------------------------------- // got a tone request --------------------------------------------------
if ( !strcmp(topic, "/mrdiynotifier/tone")) { if ( !strcmp(topic, mqttFullTopic("tone") ) ) {
stopPlaying(); stopPlaying();
broadcastStatus("playing"); broadcastStatus("playing");
file_progmem = new AudioFileSourcePROGMEM( newMsg, sizeof(newMsg) ); file_progmem = new AudioFileSourcePROGMEM( newMsg, sizeof(newMsg) );
@ -301,7 +305,7 @@ void onMqttMessage(char* topic, byte* payload, unsigned int length) {
} }
//got a TTS request ---------------------------------------------------- //got a TTS request ----------------------------------------------------
if ( !strcmp(topic, "/mrdiynotifier/say")) { if ( !strcmp(topic, mqttFullTopic("say"))) {
stopPlaying(); stopPlaying();
broadcastStatus("playing"); broadcastStatus("playing");
ESP8266SAM *sam = new ESP8266SAM; ESP8266SAM *sam = new ESP8266SAM;
@ -312,7 +316,7 @@ void onMqttMessage(char* topic, byte* payload, unsigned int length) {
} }
// got a volume request, expecting double [0.0,1.0] --------------------- // got a volume request, expecting double [0.0,1.0] ---------------------
if ( !strcmp(topic, "/mrdiynotifier/volume")) { if ( !strcmp(topic, mqttFullTopic("volume"))) {
volume_level = atof(newMsg); volume_level = atof(newMsg);
if ( volume_level < 0.0 ) volume_level = 0; if ( volume_level < 0.0 ) volume_level = 0;
if ( volume_level > 1.0 ) volume_level = 0.7; if ( volume_level > 1.0 ) volume_level = 0.7;
@ -320,7 +324,7 @@ void onMqttMessage(char* topic, byte* payload, unsigned int length) {
} }
// got a stop request -------------------------------------------------- // got a stop request --------------------------------------------------
if ( !strcmp(topic, "/mrdiynotifier/stop")) { if ( !strcmp(topic, mqttFullTopic("stop"))) {
stopPlaying(); stopPlaying();
} }
} }
@ -331,7 +335,7 @@ void broadcastStatus(String msg) {
if ( playing_status != msg) { if ( playing_status != msg) {
char charBuf[msg.length() + 1]; char charBuf[msg.length() + 1];
msg.toCharArray(charBuf, msg.length() + 1); msg.toCharArray(charBuf, msg.length() + 1);
mqttClient.publish("/mrdiynotifier/status", charBuf); mqttClient.publish(mqttFullTopic("status"), charBuf);
playing_status = msg; playing_status = msg;
#ifdef DEBUG_FLAG #ifdef DEBUG_FLAG
Serial.println(); Serial.println();
@ -346,13 +350,12 @@ void mqttReconnect() {
if (!mqttClient.connected()) { if (!mqttClient.connected()) {
if (mqttClient.connect("MrDIY Notifier", mqttUserName, mqttUserPassword)) { if (mqttClient.connect("MrDIY Notifier", mqttUserName, mqttUserPassword)) {
broadcastStatus("connected"); broadcastStatus("connected");
mqttClient.subscribe("/mrdiynotifier/play"); mqttClient.subscribe(mqttFullTopic("play"));
mqttClient.subscribe("/mrdiynotifier/stream"); mqttClient.subscribe(mqttFullTopic("stream"));
mqttClient.subscribe("/mrdiynotifier/tone"); mqttClient.subscribe(mqttFullTopic("tone"));
mqttClient.subscribe("/mrdiynotifier/beep"); mqttClient.subscribe(mqttFullTopic("say"));
mqttClient.subscribe("/mrdiynotifier/say"); mqttClient.subscribe(mqttFullTopic("stop"));
mqttClient.subscribe("/mrdiynotifier/stop"); mqttClient.subscribe(mqttFullTopic("volume"));
mqttClient.subscribe("/mrdiynotifier/volume");
#ifdef DEBUG_FLAG #ifdef DEBUG_FLAG
Serial.println(); Serial.println();
Serial.print(F("Connected to MQTT: ")); Serial.print(F("Connected to MQTT: "));
@ -393,3 +396,12 @@ boolean formValidator() {
} }
return valid; return valid;
} }
char* mqttFullTopic(char action[]) {
strcpy (mqttTopic, "/");
strcat (mqttTopic, mqttTopicPrefix);
strcat (mqttTopic, "/");
strcat (mqttTopic, action);
return mqttTopic;
}

Loading…
Cancel
Save