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

Dependencies:   ENC28J60-EMAC

Files at this revision

API Documentation at this revision

Comitter:
hudakz
Date:
Mon Mar 29 09:32:44 2021 +0000
Parent:
4:f8abf1fc6615
Commit message:
MQTT client to test the ENC28J60-EMAC on NUCLEO-F446RE.

Changed in this revision

ENC28J60-EMAC.lib Show annotated file Show diff for this revision Revisions of this file
MQTTNetwork.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r f8abf1fc6615 -r d9570dbf2f82 ENC28J60-EMAC.lib
--- a/ENC28J60-EMAC.lib	Sat Mar 27 22:56:46 2021 +0000
+++ b/ENC28J60-EMAC.lib	Mon Mar 29 09:32:44 2021 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/hudakz/code/ENC28J60-EMAC/#19d290369c66
+https://os.mbed.com/users/hudakz/code/ENC28J60-EMAC/#aa88808326b9
diff -r f8abf1fc6615 -r d9570dbf2f82 MQTTNetwork.h
--- a/MQTTNetwork.h	Sat Mar 27 22:56:46 2021 +0000
+++ b/MQTTNetwork.h	Mon Mar 29 09:32:44 2021 +0000
@@ -8,10 +8,9 @@
 {
 public:
     MQTTNetwork(EthernetInterface* aNetwork) :
-    network(aNetwork)
-    {
-        socket = new TCPSocket();
-    }
+    network(aNetwork),
+    socket(new TCPSocket)
+    {}
 
     ~   MQTTNetwork()                                       { delete socket; }
     int read(unsigned char* buffer, int len, int timeout)   { return socket->recv(buffer, len); }
@@ -25,7 +24,6 @@
         addr.set_port(port);
         return socket->connect(addr);
     }
-
     int disconnect()    { return socket->close(); }
 private:
     EthernetInterface*  network;
diff -r f8abf1fc6615 -r d9570dbf2f82 main.cpp
--- 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