Simple demo with GPIO MQTT protocol test on STM32 broker tests.mosquitto.org WIFI interface ESP8266 Issue of topic0 by pressing the button If reception of ', switching of the led If received from 'q' end of program

Dependencies:   MQTT

Revision:
24:cc01ff2c2603
Parent:
21:a68bd76740f9
Child:
25:bfdd064b9d99
--- a/main.cpp	Tue Jan 16 13:41:29 2018 +0000
+++ b/main.cpp	Thu Jun 11 14:09:40 2020 +0000
@@ -13,127 +13,162 @@
  * Contributors:
  *    Ian Craggs - initial API and implementation and/or initial documentation
  *    Ian Craggs - make sure QoS2 processing works, and add device headers
+ *
+ * Adaptation STM32 NUCLEO and mosquitto.org : C.Dupaty
+ * 06/2020
+ * 
+ This demo works on NUCLEO STM32.
+WIFI link with ESP8266 connected
+Configuration in mbed_app.json
+target_overrides for the UART connection of the ESP8266 and the WIFI connection (SSID / PASS)
+MQTT parameters are configured in #define below
+if receive payload with first q -> exit
+if receive payload with first l -> toggle LED1 on NUCLEO
+ * 
  *******************************************************************************/
 
- /**
-  This is a sample program to illustrate the use of the MQTT Client library
-  on the mbed platform.  The Client class requires two classes which mediate
-  access to system interfaces for networking and timing.  As long as these two
-  classes provide the required public programming interfaces, it does not matter
-  what facilities they use underneath. In this program, they use the mbed
-  system libraries.
-
- */
-
- // change this to 1 to output messages to LCD instead of serial
-#define USE_LCD 0
-
-#if USE_LCD
-#include "C12832.h"
-
-// the actual pins are defined in mbed_app.json and can be overridden per target
-C12832 lcd(LCD_MOSI, LCD_SCK, LCD_MISO, LCD_A0, LCD_NCS);
-
-#define logMessage lcd.cls();lcd.printf
-
-#else
-
-#define logMessage printf
-
-#endif
-
-#define MQTTCLIENT_QOS2 1
-
 #include "easy-connect.h"
 #include "MQTTNetwork.h"
 #include "MQTTmbed.h"
 #include "MQTTClient.h"
 
-int arrivedcount = 0;
+DigitalOut led(LED1);
+Serial pc(USBTX, USBRX);
 
+#define board "NUCLEO_F411RE"
+/*
+MQTT QOS,  There are 3 QoS levels in MQTT
+At most once (MQTT::QOS0)
+At least once (MQTT::QOS1)
+Exactly once (MQTT::QOS2).
+*/
+#define quality MQTT::QOS0
+/*
+retained flag : The broker stores the last retained message and the corresponding QoS for that topic. 
+Each client that subscribes to a topic pattern that matches the topic of the retained 
+message receives the retained message immediately after they subscribe. 
+The broker stores only one retained message per topic.
+*/
+#define ret false
+/*
+dup flag : The flag indicates that the message is a duplicate 
+and was resent because the intended recipient (client or broker) did not acknowledge the original message.
+*/
+#define dupli false
+
+//DigitalIn btn(USER_BUTTON); //PC13 sur F411
+DigitalIn btn(PA_8,PullUp);   // button connected with internal pullup
+
+const char* hostname = "test.mosquitto.org";
+const int port = 1883;
+char* ID ="mbedSTM32Fourcade";
+char* user =NULL;       // user & pass = NULL for broker with no credential.   
+char* pass =NULL;
+const char* topic = "topic0";
+int arrivedcount=0;
+bool quit=false;
+
+NetworkInterface* network = easy_connect(true);
+MQTTNetwork mqttNetwork(network);
+MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
+MQTT::Message message;
+MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
 
 void messageArrived(MQTT::MessageData& md)
 {
     MQTT::Message &message = md.message;
-    logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
-    logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
-    ++arrivedcount;
+    pc.printf("\x1B[33m");   // yellow
+    pc.printf("Message arrived -> qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
+    pc.printf("Payload -> %.*s\r\n", message.payloadlen, (char*)message.payload);
+    ++arrivedcount;    
+    if (*(char*)message.payload=='q') 
+                    {
+                    pc.printf("\x1B[31m");   // red
+                    pc.printf("Exit command received\n\r");    
+                    quit=true;
+                    }
+    if (*(char*)message.payload=='l') 
+                    {
+                    led=!led;
+                    pc.printf("\x1B[31m");   // red
+                    pc.printf("toggle LED\n\r");
+                    }
+    pc.printf("\x1B[0m"); // raz
 }
 
+void connection(void)
+{
+int rc;
+    pc.printf("\x1B[32m");   // green
+    pc.printf("Connecting to MQTT broker %s:%d\r\n", hostname, port);
+   
+    if ((rc = client.connect(data)) != 0)
+        pc.printf("rc from MQTT connect is %d\r\n", rc);
+    else pc.printf("connection MQTT OK ID:%s USER:%s PASS:%s\r\n",ID,user,pass);
+
+    if ((rc = client.subscribe(topic, quality, messageArrived)) != 0)
+        pc.printf("rc from MQTT subscribe is %d\r\n", rc);
+        else pc.printf("Subscribe MQTT OK topic:%s quality:%d\r\n",topic,quality);
+    pc.printf("\x1B[0m"); // raz
+}
+
+void deconnection(void)
+{
+ int rc;
+  pc.printf("\x1B[36m");   // cyan
+ if ((rc = client.unsubscribe(topic)) != 0) pc.printf("rc from unsubscribe was %d\r\n", rc);
+    else pc.printf("unsubscribe OK \r\n");
+    if ((rc = client.disconnect()) != 0) pc.printf("rc from disconnect was %d\r\n", rc);
+    else pc.printf("disconnect OK\r\n");
+    mqttNetwork.disconnect();
+    pc.printf("\x1B[0m"); // raz
+}
 
 int main(int argc, char* argv[])
 {
-    float version = 0.6;
-    char* topic = "mbed-sample";
-
-    logMessage("HelloMQTT: version is %.2f\r\n", version);
-
-    NetworkInterface* network = easy_connect(true);
-    if (!network) {
-        return -1;
+    int rc;
+    int cpt=0;
+    char buf[100];
+    
+    //pc.printf("\x1B[2J");  //efface ecran
+    //pc.printf("\x1B[0;0H");   // curseur en 0,0
+    pc.printf("\x1B[1m");   // brillant
+    pc.printf("\r\nMQTT test on %s\r\n",board);
+    pc.printf("--------------------------\r\n\r\n");
+    pc.printf("\x1B[33m");   // yellow
+    wait(1);
+  
+// TCP connection
+    mqttNetwork.connect(hostname, port);
+    if (rc != 0) pc.printf("rc from TCP connect is %d\r\n", rc);
+    else pc.printf("WIFI network connection OK\r\n");
+    
+// MQTT data
+    data.MQTTVersion = 3;
+    data.clientID.cstring = ID;
+    data.username.cstring = user;
+    data.password.cstring = pass;
+    message.qos = quality;
+    message.retained = ret;
+    message.dup = dupli;
+    message.payload = (void*)buf;
+    connection();
+    while(!quit)
+    {
+        pc.printf("---> Press button to send : %s<--- \n\r",topic);
+        while(btn);
+        while(!btn);
+        //connection();
+        sprintf(buf, "Message from %s number -> %d \r\n", board,++cpt);
+        pc.printf("Send message -> %s\n\r",buf);
+        message.payloadlen = strlen(buf)+1;
+        if ((rc = client.publish(topic, message)) !=0) pc.printf("rc publication error %d\r\n", rc);
+        client.yield(100);
+        //deconnection();
     }
 
-    MQTTNetwork mqttNetwork(network);
-
-    MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
-
-    const char* hostname = "m2m.eclipse.org";
-    int port = 1883;
-    logMessage("Connecting to %s:%d\r\n", hostname, port);
-    int rc = mqttNetwork.connect(hostname, port);
-    if (rc != 0)
-        logMessage("rc from TCP connect is %d\r\n", rc);
-
-    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
-    data.MQTTVersion = 3;
-    data.clientID.cstring = "mbed-sample";
-    data.username.cstring = "testuser";
-    data.password.cstring = "testpassword";
-    if ((rc = client.connect(data)) != 0)
-        logMessage("rc from MQTT connect is %d\r\n", rc);
-
-    if ((rc = client.subscribe(topic, MQTT::QOS2, messageArrived)) != 0)
-        logMessage("rc from MQTT subscribe is %d\r\n", rc);
-
-    MQTT::Message message;
-
-    // QoS 0
-    char buf[100];
-    sprintf(buf, "Hello World!  QoS 0 message from app version %f\r\n", version);
-    message.qos = MQTT::QOS0;
-    message.retained = false;
-    message.dup = false;
-    message.payload = (void*)buf;
-    message.payloadlen = strlen(buf)+1;
-    rc = client.publish(topic, message);
-    while (arrivedcount < 1)
-        client.yield(100);
-
-    // QoS 1
-    sprintf(buf, "Hello World!  QoS 1 message from app version %f\r\n", version);
-    message.qos = MQTT::QOS1;
-    message.payloadlen = strlen(buf)+1;
-    rc = client.publish(topic, message);
-    while (arrivedcount < 2)
-        client.yield(100);
-
-    // QoS 2
-    sprintf(buf, "Hello World!  QoS 2 message from app version %f\r\n", version);
-    message.qos = MQTT::QOS2;
-    message.payloadlen = strlen(buf)+1;
-    rc = client.publish(topic, message);
-    while (arrivedcount < 3)
-        client.yield(100);
-
-    if ((rc = client.unsubscribe(topic)) != 0)
-        logMessage("rc from unsubscribe was %d\r\n", rc);
-
-    if ((rc = client.disconnect()) != 0)
-        logMessage("rc from disconnect was %d\r\n", rc);
-
-    mqttNetwork.disconnect();
-
-    logMessage("Version %.2f: finish %d msgs\r\n", version, arrivedcount);
+    deconnection();
+    pc.printf("Disconnect from MQTT network. End of program, %d messages arrived\r\n",arrivedcount);
 
     return 0;
 }