MQTT client to test the ENC28J60-EMAC on NUCLEO-F446RE.

Dependencies:   ENC28J60-EMAC

Revision:
5:d9570dbf2f82
Parent:
4:f8abf1fc6615
--- a/main.cpp	Sat Mar 27 22:56:46 2021 +0000
+++ b/main.cpp	Mon Mar 29 09:32:44 2021 +0000
@@ -12,15 +12,18 @@
 #define MQTT_PORT   1883            // MQTT port
 
 const uint8_t                           MAC[6] = { 0, 1, 2, 3, 4, 5 };
+const char                              payload[] = "Hello, World!";
 
 // Global variables
 char                                    topic[256];
 EthernetInterface                       net;
 MQTTNetwork                             mqttNetwork(&net);
 MQTT::Client<MQTTNetwork, Countdown>    mqttClient(mqttNetwork);
+Timer                                   timer;
 
 // Function prototypes
 void                                    onMqttMsgReceived(MQTT::MessageData& md);
+void                                    publishMsg();
 
 /**
  * @brief
@@ -30,7 +33,7 @@
  */
 FileHandle* mbed::mbed_override_console(int)
 {
-    static BufferedSerial   myConsole(USBTX, USBRX, 460800);
+    static BufferedSerial   myConsole(USBTX, USBRX, 115200);
     return &myConsole;
 }
 
@@ -85,11 +88,18 @@
     printf("MQTT Broker connected.\r\n");
 
     // Subscribe to topics
-    mqttClient.subscribe("workroomThermostat/#", MQTT::QOS0, onMqttMsgReceived);
+    mqttClient.subscribe("outdoorTemperature", MQTT::QOS0, onMqttMsgReceived);
 
+    // Start timer
+    timer.start();
+    
     // Main thread loop
     while (1) {
         mqttClient.yield(10);
+        if (timer.read_ms() > 1000) {
+            timer.reset();
+            publishMsg();   // once a second publish the MQTT message
+        }
     }
 }
 
@@ -105,3 +115,21 @@
     memcpy(topic, md.topicName.lenstring.data, md.topicName.lenstring.len);
     printf("topic: %s\r\n", topic);
 }
+
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
+void publishMsg()
+{
+    MQTT::Message mqttMsg;
+
+    mqttMsg.qos = MQTT::QOS0;
+    mqttMsg.retained = false;
+    mqttMsg.dup = false;
+    mqttMsg.payload = (void*)payload;
+    mqttMsg.payloadlen = strlen(payload);    
+    mqttClient.publish("topic_hello_world", mqttMsg);
+}
\ No newline at end of file