diff --git a/ESP32_Utils_MQTT_Async.hpp b/ESP32_Utils_MQTT_Async.hpp index 016392a..8609956 100644 --- a/ESP32_Utils_MQTT_Async.hpp +++ b/ESP32_Utils_MQTT_Async.hpp @@ -85,5 +85,5 @@ void InitMqtt() mqttClient.onPublish(OnMqttPublish); mqttClient.setServer(MQTT_HOST, MQTT_PORT); - mqttClient.setCredentials(MQTT_USER, MQTT_PASS); + mqttClient.setCredentials(MQTT_USER, MQTT_PASS); } \ No newline at end of file diff --git a/JSON_functions.hpp b/JSON_functions.hpp deleted file mode 100644 index 1646873..0000000 --- a/JSON_functions.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -void reconstr (String payload) { - JsonDocument doc; - - DeserializationError error = deserializeJson(doc, payload); - - if (error) { - Serial.print("deserializeJson() failed: "); - Serial.println(error.c_str()); - return; - } - - // EXAMPLE PAYLOAD. CHANGE THIS TO THE APPROPRIATE ONE. - int a = doc["a"]; - bool b = doc["b"]; - float c = doc["c"]; - Serial.println("The payload received contains: "); - Serial.print("a: "); - Serial.print(a); - Serial.print(" b: "); - Serial.print(b); - Serial.print(" c: "); - Serial.println(c); - -} \ No newline at end of file diff --git a/MQTT.hpp b/MQTT.hpp index 839a7e9..b35de49 100644 --- a/MQTT.hpp +++ b/MQTT.hpp @@ -1,12 +1,11 @@ #pragma once -const IPAddress MQTT_HOST(10, 98, 20, 10); +const IPAddress MQTT_HOST(10,98,20,10); const int MQTT_PORT = 1883; AsyncMqttClient mqttClient; -String GetPayloadContent(char* data, size_t len) -{ +String GetPayloadContent(char* data, size_t len) { String content = ""; for(size_t i = 0; i < len; i++) { @@ -15,28 +14,51 @@ String GetPayloadContent(char* data, size_t len) return content; } -void SuscribeMqtt() -{ - uint16_t packetIdSub = mqttClient.subscribe("user123456/channel", 0); - Serial.print("Subscribing at QoS 2, packetId: "); - Serial.println(packetIdSub); +void SuscribeMqtt() { + + String topicPlayback = String("vesper/") + DEV_ID + "/control/playback"; + String topicAddMelody = String("vesper/") + DEV_ID + "/control/add_melody"; + + uint16_t control_id = mqttClient.subscribe(topicPlayback.c_str(), 2); + Serial.print("Subscribing to playback topic, QoS 2, packetId: "); + Serial.println(control_id); + + // doesn't work yet: + uint16_t add_melody_id = mqttClient.subscribe(topicAddMelody.c_str(), 2); + Serial.print("Subscribing to add_melody topic, QoS 2, packetId: "); + Serial.println(add_melody_id); + + } -String payload; -void PublishMqtt(unsigned long data) -{ +void PublishMqtt(unsigned long data) { + + String topicData = String("vesper/") + DEV_ID + "/data"; + String payload = String(data); - mqttClient.publish("hello/world", 0, true, (char*)payload.c_str()); + mqttClient.publish(topicData.c_str(), 0, true, (char*)payload.c_str()); + } void OnMqttReceived(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { - //Serial.print("Received on "); - //Serial.print(topic); - //Serial.print(": "); - - String content = GetPayloadContent(payload, len); - reconstr(content); - //Serial.print(content); - //Serial.println(); + + String topicPlayback = String("vesper/") + DEV_ID + "/control/playback"; + String topicAddMelody = String("vesper/") + DEV_ID + "/control/add_melody"; // doesn't work yet. + + String payloadContent = String(payload).substring(0, len); + + if (String(topic) == topicPlayback) { + // Handle the playback command + handleJSON(payload); + } + else if (String(topic) == topicAddMelody) { + // Handle adding melody + Serial.println("Adding melody..."); + // You can call a function here to handle adding the melody + } + else { + // Handle unknown topics + Serial.println("Unknown topic received."); + } } diff --git a/data/data.bin b/data/data.bin new file mode 100644 index 0000000..139e511 Binary files /dev/null and b/data/data.bin differ diff --git a/data/melody1.bin b/data/melody1.bin new file mode 100644 index 0000000..45403bd Binary files /dev/null and b/data/melody1.bin differ diff --git a/data/melody1.txt b/data/melody1.txt new file mode 100644 index 0000000..5162ced --- /dev/null +++ b/data/melody1.txt @@ -0,0 +1 @@ +[0000000000000001,0000000000000000,0000000000000010,0000000000000000,0000000000000001,0100000000010010,0000000000000001,0000000000001010,0000000000000000,0000000000000001] diff --git a/functions.hpp b/functions.hpp new file mode 100644 index 0000000..545d516 --- /dev/null +++ b/functions.hpp @@ -0,0 +1,71 @@ +#pragma once + +extern volatile bool playing; + + +void playback(JsonDocument doc){ + if (doc["playback"].as()){ + playing = true; + Serial.println("START Playback!"); + } +else if (doc["playback"].as()){ + playing = false; + Serial.println("STOP Playback!"); + } +} + +void selectMelody(JsonDocument doc){ + + melody.name = doc["name"].as(); // Convert to std::string + melody.id = doc["id"].as(); + melody.duration = doc["duration"].as(); + melody.infinite_play = doc["infinite"].as(); + melody.interval_duration = doc["inter_dur"].as(); + melody.speed = doc["speed"].as(); + + Serial.printf("Name: %s, ID: %d, Duration: %lu, Inf: %s, Inter: %d, Speed: %d\n", + melody.name.c_str(), + melody.id, + melody.duration, + melody.infinite_play ? "true" : "false", + melody.interval_duration, + melody.speed); + +} + +void handleJSON(String payload) { + + JsonDocument doc; + DeserializationError error = deserializeJson(doc, payload); + + if (error) { + Serial.print("deserializeJson() failed: "); + Serial.println(error.c_str()); + return; + } + + selectMelody(doc); + playback(doc); + +} + + + + + + + + +/* +possible topics: + +vesper/client-id/control + /play + /stop + +vesper/client-id/select_melody + +vesper/client-id/update_melody + +vesper/client-id/add_melody +*/ diff --git a/melody_handling.hpp b/melody_handling.hpp new file mode 100644 index 0000000..85d5511 --- /dev/null +++ b/melody_handling.hpp @@ -0,0 +1 @@ +// MELODY PLAYBACK WILL BE HANDLED HERE \ No newline at end of file diff --git a/vesper.ino b/vesper.ino index cfe55e3..b65a14c 100644 --- a/vesper.ino +++ b/vesper.ino @@ -1,31 +1,55 @@ -# - #include #include #include +#include +#include +#include +#define DEV_ID "id-96638646" + +struct melody_attributes { + std::string name; // Contains the name of each Melody saved + uint16_t id; // The (internal) ID of the selected melody + uint32_t duration; // Indicates the total Duration in Minutes + bool infinite_play; // Infinite Loop Indicator (If True the melody will loop forever or until stoped, with pauses of "interval duration in between loops") + uint16_t interval_duration; // Indicates the Duration of the Interval between finished loops, IF "inf" is true + uint8_t speed; // Indicates the Speed in 9 Steps. 1-9 (Steps can be adjusted in the bellEngine function) +}; + +melody_attributes melody; + +volatile bool playing = false; + +#include "functions.hpp" #include "config.h" // Sustituir con datos de vuestra red -#include "JSON_functions.hpp" #include "MQTT.hpp" #include "ESP32_Utils.hpp" #include "ESP32_Utils_MQTT_Async.hpp" +#include "melody_handling.hpp" void setup() { + // Initialize Serial Communication (for debuggin) Serial.begin(115200); + delay(50); - delay(500); - + // Initialize SPIFFS + if (!SPIFFS.begin(true)) { // 'true' means format SPIFFS if initialization fails + Serial.println("Failed to mount SPIFFS"); + return; + } + Serial.println("SPIFFS mounted successfully"); + delay(50); + + // Initialize WiFi and MQTT WiFi.onEvent(WiFiEvent); InitMqtt(); - ConnectWiFi_STA(); + delay(1000); + } void loop() { - // EXAMPLE PUBLISH. CHANGE THIS, IF NEEDED. - //delay(1000); - //PublishMqtt(millis()); } \ No newline at end of file