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

This is a MQTT protocol test on STM32 NUCLEO The broker is tests.mosquitto.org The WIFI interface is ESP8266

For configuration please check mbed_app.json and #define in main.cpp

Issue of topic0 by pressing the button If received 'l' from broker then switching of the led If received 'q' from broker then end the program

Information and debug with a terminal using UART over USB https://os.mbed.com/media/uploads/cdupaty/mqtt2.jpg

This test has been checked with MQTT.fx https://os.mbed.com/media/uploads/cdupaty/mqttfx.jpg

Committer:
cdupaty
Date:
Thu Jun 11 14:09:40 2020 +0000
Revision:
24:cc01ff2c2603
Parent:
21:a68bd76740f9
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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 1:a1d5c7a6acbc 1 /*******************************************************************************
icraggs 17:0811bdbdd78a 2 * Copyright (c) 2014, 2015 IBM Corp.
icraggs 1:a1d5c7a6acbc 3 *
icraggs 1:a1d5c7a6acbc 4 * All rights reserved. This program and the accompanying materials
icraggs 1:a1d5c7a6acbc 5 * are made available under the terms of the Eclipse Public License v1.0
icraggs 1:a1d5c7a6acbc 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
icraggs 1:a1d5c7a6acbc 7 *
icraggs 1:a1d5c7a6acbc 8 * The Eclipse Public License is available at
icraggs 1:a1d5c7a6acbc 9 * http://www.eclipse.org/legal/epl-v10.html
icraggs 1:a1d5c7a6acbc 10 * and the Eclipse Distribution License is available at
icraggs 1:a1d5c7a6acbc 11 * http://www.eclipse.org/org/documents/edl-v10.php.
icraggs 1:a1d5c7a6acbc 12 *
icraggs 1:a1d5c7a6acbc 13 * Contributors:
icraggs 1:a1d5c7a6acbc 14 * Ian Craggs - initial API and implementation and/or initial documentation
icraggs 17:0811bdbdd78a 15 * Ian Craggs - make sure QoS2 processing works, and add device headers
cdupaty 24:cc01ff2c2603 16 *
cdupaty 24:cc01ff2c2603 17 * Adaptation STM32 NUCLEO and mosquitto.org : C.Dupaty
cdupaty 24:cc01ff2c2603 18 * 06/2020
cdupaty 24:cc01ff2c2603 19 *
cdupaty 24:cc01ff2c2603 20 This demo works on NUCLEO STM32.
cdupaty 24:cc01ff2c2603 21 WIFI link with ESP8266 connected
cdupaty 24:cc01ff2c2603 22 Configuration in mbed_app.json
cdupaty 24:cc01ff2c2603 23 target_overrides for the UART connection of the ESP8266 and the WIFI connection (SSID / PASS)
cdupaty 24:cc01ff2c2603 24 MQTT parameters are configured in #define below
cdupaty 24:cc01ff2c2603 25 if receive payload with first q -> exit
cdupaty 24:cc01ff2c2603 26 if receive payload with first l -> toggle LED1 on NUCLEO
cdupaty 24:cc01ff2c2603 27 *
icraggs 1:a1d5c7a6acbc 28 *******************************************************************************/
Jan Jongboom 20:49c9daf2b0ff 29
Jan Jongboom 20:49c9daf2b0ff 30 #include "easy-connect.h"
Jan Jongboom 20:49c9daf2b0ff 31 #include "MQTTNetwork.h"
Jan Jongboom 20:49c9daf2b0ff 32 #include "MQTTmbed.h"
icraggs 2:638c854c0695 33 #include "MQTTClient.h"
icraggs 2:638c854c0695 34
cdupaty 24:cc01ff2c2603 35 DigitalOut led(LED1);
cdupaty 24:cc01ff2c2603 36 Serial pc(USBTX, USBRX);
icraggs 2:638c854c0695 37
cdupaty 24:cc01ff2c2603 38 #define board "NUCLEO_F411RE"
cdupaty 24:cc01ff2c2603 39 /*
cdupaty 24:cc01ff2c2603 40 MQTT QOS, There are 3 QoS levels in MQTT
cdupaty 24:cc01ff2c2603 41 At most once (MQTT::QOS0)
cdupaty 24:cc01ff2c2603 42 At least once (MQTT::QOS1)
cdupaty 24:cc01ff2c2603 43 Exactly once (MQTT::QOS2).
cdupaty 24:cc01ff2c2603 44 */
cdupaty 24:cc01ff2c2603 45 #define quality MQTT::QOS0
cdupaty 24:cc01ff2c2603 46 /*
cdupaty 24:cc01ff2c2603 47 retained flag : The broker stores the last retained message and the corresponding QoS for that topic.
cdupaty 24:cc01ff2c2603 48 Each client that subscribes to a topic pattern that matches the topic of the retained
cdupaty 24:cc01ff2c2603 49 message receives the retained message immediately after they subscribe.
cdupaty 24:cc01ff2c2603 50 The broker stores only one retained message per topic.
cdupaty 24:cc01ff2c2603 51 */
cdupaty 24:cc01ff2c2603 52 #define ret false
cdupaty 24:cc01ff2c2603 53 /*
cdupaty 24:cc01ff2c2603 54 dup flag : The flag indicates that the message is a duplicate
cdupaty 24:cc01ff2c2603 55 and was resent because the intended recipient (client or broker) did not acknowledge the original message.
cdupaty 24:cc01ff2c2603 56 */
cdupaty 24:cc01ff2c2603 57 #define dupli false
cdupaty 24:cc01ff2c2603 58
cdupaty 24:cc01ff2c2603 59 //DigitalIn btn(USER_BUTTON); //PC13 sur F411
cdupaty 24:cc01ff2c2603 60 DigitalIn btn(PA_8,PullUp); // button connected with internal pullup
cdupaty 24:cc01ff2c2603 61
cdupaty 24:cc01ff2c2603 62 const char* hostname = "test.mosquitto.org";
cdupaty 24:cc01ff2c2603 63 const int port = 1883;
cdupaty 24:cc01ff2c2603 64 char* ID ="mbedSTM32Fourcade";
cdupaty 24:cc01ff2c2603 65 char* user =NULL; // user & pass = NULL for broker with no credential.
cdupaty 24:cc01ff2c2603 66 char* pass =NULL;
cdupaty 24:cc01ff2c2603 67 const char* topic = "topic0";
cdupaty 24:cc01ff2c2603 68 int arrivedcount=0;
cdupaty 24:cc01ff2c2603 69 bool quit=false;
cdupaty 24:cc01ff2c2603 70
cdupaty 24:cc01ff2c2603 71 NetworkInterface* network = easy_connect(true);
cdupaty 24:cc01ff2c2603 72 MQTTNetwork mqttNetwork(network);
cdupaty 24:cc01ff2c2603 73 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
cdupaty 24:cc01ff2c2603 74 MQTT::Message message;
cdupaty 24:cc01ff2c2603 75 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 8:a3e3113054a1 76
icraggs 9:5beb8609e9f7 77 void messageArrived(MQTT::MessageData& md)
icraggs 2:638c854c0695 78 {
icraggs 9:5beb8609e9f7 79 MQTT::Message &message = md.message;
cdupaty 24:cc01ff2c2603 80 pc.printf("\x1B[33m"); // yellow
cdupaty 24:cc01ff2c2603 81 pc.printf("Message arrived -> qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
cdupaty 24:cc01ff2c2603 82 pc.printf("Payload -> %.*s\r\n", message.payloadlen, (char*)message.payload);
cdupaty 24:cc01ff2c2603 83 ++arrivedcount;
cdupaty 24:cc01ff2c2603 84 if (*(char*)message.payload=='q')
cdupaty 24:cc01ff2c2603 85 {
cdupaty 24:cc01ff2c2603 86 pc.printf("\x1B[31m"); // red
cdupaty 24:cc01ff2c2603 87 pc.printf("Exit command received\n\r");
cdupaty 24:cc01ff2c2603 88 quit=true;
cdupaty 24:cc01ff2c2603 89 }
cdupaty 24:cc01ff2c2603 90 if (*(char*)message.payload=='l')
cdupaty 24:cc01ff2c2603 91 {
cdupaty 24:cc01ff2c2603 92 led=!led;
cdupaty 24:cc01ff2c2603 93 pc.printf("\x1B[31m"); // red
cdupaty 24:cc01ff2c2603 94 pc.printf("toggle LED\n\r");
cdupaty 24:cc01ff2c2603 95 }
cdupaty 24:cc01ff2c2603 96 pc.printf("\x1B[0m"); // raz
icraggs 2:638c854c0695 97 }
icraggs 0:0cae29831d01 98
cdupaty 24:cc01ff2c2603 99 void connection(void)
cdupaty 24:cc01ff2c2603 100 {
cdupaty 24:cc01ff2c2603 101 int rc;
cdupaty 24:cc01ff2c2603 102 pc.printf("\x1B[32m"); // green
cdupaty 24:cc01ff2c2603 103 pc.printf("Connecting to MQTT broker %s:%d\r\n", hostname, port);
cdupaty 24:cc01ff2c2603 104
cdupaty 24:cc01ff2c2603 105 if ((rc = client.connect(data)) != 0)
cdupaty 24:cc01ff2c2603 106 pc.printf("rc from MQTT connect is %d\r\n", rc);
cdupaty 24:cc01ff2c2603 107 else pc.printf("connection MQTT OK ID:%s USER:%s PASS:%s\r\n",ID,user,pass);
cdupaty 24:cc01ff2c2603 108
cdupaty 24:cc01ff2c2603 109 if ((rc = client.subscribe(topic, quality, messageArrived)) != 0)
cdupaty 24:cc01ff2c2603 110 pc.printf("rc from MQTT subscribe is %d\r\n", rc);
cdupaty 24:cc01ff2c2603 111 else pc.printf("Subscribe MQTT OK topic:%s quality:%d\r\n",topic,quality);
cdupaty 24:cc01ff2c2603 112 pc.printf("\x1B[0m"); // raz
cdupaty 24:cc01ff2c2603 113 }
cdupaty 24:cc01ff2c2603 114
cdupaty 24:cc01ff2c2603 115 void deconnection(void)
cdupaty 24:cc01ff2c2603 116 {
cdupaty 24:cc01ff2c2603 117 int rc;
cdupaty 24:cc01ff2c2603 118 pc.printf("\x1B[36m"); // cyan
cdupaty 24:cc01ff2c2603 119 if ((rc = client.unsubscribe(topic)) != 0) pc.printf("rc from unsubscribe was %d\r\n", rc);
cdupaty 24:cc01ff2c2603 120 else pc.printf("unsubscribe OK \r\n");
cdupaty 24:cc01ff2c2603 121 if ((rc = client.disconnect()) != 0) pc.printf("rc from disconnect was %d\r\n", rc);
cdupaty 24:cc01ff2c2603 122 else pc.printf("disconnect OK\r\n");
cdupaty 24:cc01ff2c2603 123 mqttNetwork.disconnect();
cdupaty 24:cc01ff2c2603 124 pc.printf("\x1B[0m"); // raz
cdupaty 24:cc01ff2c2603 125 }
icraggs 2:638c854c0695 126
icraggs 2:638c854c0695 127 int main(int argc, char* argv[])
Jan Jongboom 20:49c9daf2b0ff 128 {
cdupaty 24:cc01ff2c2603 129 int rc;
cdupaty 24:cc01ff2c2603 130 int cpt=0;
cdupaty 24:cc01ff2c2603 131 char buf[100];
cdupaty 24:cc01ff2c2603 132
cdupaty 24:cc01ff2c2603 133 //pc.printf("\x1B[2J"); //efface ecran
cdupaty 24:cc01ff2c2603 134 //pc.printf("\x1B[0;0H"); // curseur en 0,0
cdupaty 24:cc01ff2c2603 135 pc.printf("\x1B[1m"); // brillant
cdupaty 24:cc01ff2c2603 136 pc.printf("\r\nMQTT test on %s\r\n",board);
cdupaty 24:cc01ff2c2603 137 pc.printf("--------------------------\r\n\r\n");
cdupaty 24:cc01ff2c2603 138 pc.printf("\x1B[33m"); // yellow
cdupaty 24:cc01ff2c2603 139 wait(1);
cdupaty 24:cc01ff2c2603 140
cdupaty 24:cc01ff2c2603 141 // TCP connection
cdupaty 24:cc01ff2c2603 142 mqttNetwork.connect(hostname, port);
cdupaty 24:cc01ff2c2603 143 if (rc != 0) pc.printf("rc from TCP connect is %d\r\n", rc);
cdupaty 24:cc01ff2c2603 144 else pc.printf("WIFI network connection OK\r\n");
cdupaty 24:cc01ff2c2603 145
cdupaty 24:cc01ff2c2603 146 // MQTT data
cdupaty 24:cc01ff2c2603 147 data.MQTTVersion = 3;
cdupaty 24:cc01ff2c2603 148 data.clientID.cstring = ID;
cdupaty 24:cc01ff2c2603 149 data.username.cstring = user;
cdupaty 24:cc01ff2c2603 150 data.password.cstring = pass;
cdupaty 24:cc01ff2c2603 151 message.qos = quality;
cdupaty 24:cc01ff2c2603 152 message.retained = ret;
cdupaty 24:cc01ff2c2603 153 message.dup = dupli;
cdupaty 24:cc01ff2c2603 154 message.payload = (void*)buf;
cdupaty 24:cc01ff2c2603 155 connection();
cdupaty 24:cc01ff2c2603 156 while(!quit)
cdupaty 24:cc01ff2c2603 157 {
cdupaty 24:cc01ff2c2603 158 pc.printf("---> Press button to send : %s<--- \n\r",topic);
cdupaty 24:cc01ff2c2603 159 while(btn);
cdupaty 24:cc01ff2c2603 160 while(!btn);
cdupaty 24:cc01ff2c2603 161 //connection();
cdupaty 24:cc01ff2c2603 162 sprintf(buf, "Message from %s number -> %d \r\n", board,++cpt);
cdupaty 24:cc01ff2c2603 163 pc.printf("Send message -> %s\n\r",buf);
cdupaty 24:cc01ff2c2603 164 message.payloadlen = strlen(buf)+1;
cdupaty 24:cc01ff2c2603 165 if ((rc = client.publish(topic, message)) !=0) pc.printf("rc publication error %d\r\n", rc);
cdupaty 24:cc01ff2c2603 166 client.yield(100);
cdupaty 24:cc01ff2c2603 167 //deconnection();
Jan Jongboom 20:49c9daf2b0ff 168 }
Jan Jongboom 20:49c9daf2b0ff 169
cdupaty 24:cc01ff2c2603 170 deconnection();
cdupaty 24:cc01ff2c2603 171 pc.printf("Disconnect from MQTT network. End of program, %d messages arrived\r\n",arrivedcount);
Jan Jongboom 20:49c9daf2b0ff 172
icraggs 0:0cae29831d01 173 return 0;
icraggs 0:0cae29831d01 174 }