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:
sam_grove
Date:
Fri May 09 23:06:43 2014 +0000
Revision:
5:4a257f6ac09a
Parent:
3:7a6a899de7cc
Child:
7:3de634f2d40c
Updates to the example to use the virtual serial port and the LCD for feedback

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 1:a1d5c7a6acbc 1 /*******************************************************************************
icraggs 1:a1d5c7a6acbc 2 * Copyright (c) 2014 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 1:a1d5c7a6acbc 15 *******************************************************************************/
icraggs 2:638c854c0695 16
icraggs 2:638c854c0695 17 /**
icraggs 2:638c854c0695 18 This is a sample program to illustrate the use of the MQTT Client library
icraggs 2:638c854c0695 19 on the mbed platform. The Client class requires two classes which mediate
icraggs 2:638c854c0695 20 access to system interfaces for networking and timing. As long as these two
icraggs 2:638c854c0695 21 classes provide the required public programming interfaces, it does not matter
icraggs 2:638c854c0695 22 what facilities they use underneath. In this program, they use the mbed
icraggs 2:638c854c0695 23 system libraries.
icraggs 2:638c854c0695 24
icraggs 2:638c854c0695 25 */
icraggs 1:a1d5c7a6acbc 26
icraggs 0:0cae29831d01 27 #include "mbed.h"
sam_grove 5:4a257f6ac09a 28 #include "EthernetInterfaceIPStack.h"
icraggs 2:638c854c0695 29
sam_grove 5:4a257f6ac09a 30 #include "C12832.h"
sam_grove 5:4a257f6ac09a 31 C12832 lcd(p5, p7, p6, p8, p11);
icraggs 0:0cae29831d01 32
icraggs 2:638c854c0695 33 #include "FP.cpp"
icraggs 2:638c854c0695 34 #include "MQTTClient.h"
icraggs 2:638c854c0695 35
icraggs 2:638c854c0695 36 int arrivedcount = 0;
icraggs 2:638c854c0695 37
icraggs 2:638c854c0695 38 void messageArrived(MQTT::Message* message)
icraggs 2:638c854c0695 39 {
sam_grove 5:4a257f6ac09a 40 lcd.cls();
sam_grove 5:4a257f6ac09a 41 lcd.locate(0,3);
sam_grove 5:4a257f6ac09a 42 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message->qos, message->retained, message->dup, message->id);
sam_grove 5:4a257f6ac09a 43 printf("Payload %.*s\n", message->payloadlen, (char*)message->payload);
icraggs 2:638c854c0695 44 ++arrivedcount;
sam_grove 5:4a257f6ac09a 45 lcd.puts((char*)message->payload);
icraggs 2:638c854c0695 46 }
icraggs 0:0cae29831d01 47
icraggs 2:638c854c0695 48 int connect(MQTT::Client<IPStack, Countdown>::connectionLostInfo* info)
icraggs 2:638c854c0695 49 {
icraggs 2:638c854c0695 50 char* hostname = "m2m.eclipse.org";
icraggs 2:638c854c0695 51 int port = 1883;
sam_grove 5:4a257f6ac09a 52
sam_grove 5:4a257f6ac09a 53 lcd.cls();
sam_grove 5:4a257f6ac09a 54 lcd.locate(0,3);
sam_grove 5:4a257f6ac09a 55 lcd.printf("%s:%d\n", hostname, port);
sam_grove 5:4a257f6ac09a 56 printf("Connecting to %s:%d\n", hostname, port);
icraggs 2:638c854c0695 57 int rc = info->network->connect(hostname, port);
sam_grove 5:4a257f6ac09a 58 lcd.printf("TCP connect = %d\n", rc);
sam_grove 5:4a257f6ac09a 59 printf("rc from TCP connect is %d\n", rc);
icraggs 2:638c854c0695 60
icraggs 2:638c854c0695 61 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 2:638c854c0695 62 data.MQTTVersion = 3;
icraggs 2:638c854c0695 63 data.clientID.cstring = "mbed-icraggs";
icraggs 2:638c854c0695 64 rc = info->client->connect(&data);
sam_grove 5:4a257f6ac09a 65 lcd.printf("MQTT connect = %d\n", rc);
sam_grove 5:4a257f6ac09a 66 printf("rc from MQTT connect is %d\n", rc);
icraggs 2:638c854c0695 67
icraggs 2:638c854c0695 68 return rc;
icraggs 2:638c854c0695 69 }
icraggs 2:638c854c0695 70
icraggs 2:638c854c0695 71
icraggs 2:638c854c0695 72 int main(int argc, char* argv[])
icraggs 2:638c854c0695 73 {
icraggs 2:638c854c0695 74 IPStack ipstack = IPStack();
icraggs 2:638c854c0695 75 float version = 0.3;
icraggs 2:638c854c0695 76 char* topic = "mbed-sample";
icraggs 2:638c854c0695 77
icraggs 2:638c854c0695 78 lcd.printf("Version is %f\n", version);
sam_grove 5:4a257f6ac09a 79 printf("Version is %f\n", version);
icraggs 2:638c854c0695 80
icraggs 3:7a6a899de7cc 81 MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack);
icraggs 3:7a6a899de7cc 82
icraggs 3:7a6a899de7cc 83 client.setConnectionLostHandler(connect);
icraggs 0:0cae29831d01 84
icraggs 2:638c854c0695 85 MQTT::Client<IPStack, Countdown>::connectionLostInfo info = {&client, &ipstack};
icraggs 2:638c854c0695 86 int rc = connect(&info);
icraggs 2:638c854c0695 87
icraggs 2:638c854c0695 88 rc = client.subscribe(topic, MQTT::QOS1, messageArrived);
sam_grove 5:4a257f6ac09a 89 if (rc != 0) {
sam_grove 5:4a257f6ac09a 90 printf("rc from MQTT subscribe is %d\n", rc);
sam_grove 5:4a257f6ac09a 91 }
icraggs 2:638c854c0695 92
icraggs 2:638c854c0695 93 MQTT::Message message;
icraggs 0:0cae29831d01 94
icraggs 2:638c854c0695 95 // QoS 0
icraggs 2:638c854c0695 96 char buf[100];
icraggs 2:638c854c0695 97 sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version);
icraggs 2:638c854c0695 98 message.qos = MQTT::QOS0;
icraggs 2:638c854c0695 99 message.retained = false;
icraggs 2:638c854c0695 100 message.dup = false;
icraggs 2:638c854c0695 101 message.payload = (void*)buf;
icraggs 2:638c854c0695 102 message.payloadlen = strlen(buf)+1;
icraggs 2:638c854c0695 103 rc = client.publish(topic, &message);
icraggs 2:638c854c0695 104 while (arrivedcount == 0)
icraggs 2:638c854c0695 105 client.yield(100);
icraggs 2:638c854c0695 106
icraggs 2:638c854c0695 107 // QoS 1
icraggs 2:638c854c0695 108 sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version);
icraggs 2:638c854c0695 109 message.qos = MQTT::QOS1;
icraggs 2:638c854c0695 110 message.payloadlen = strlen(buf)+1;
icraggs 2:638c854c0695 111 rc = client.publish(topic, &message);
icraggs 2:638c854c0695 112 while (arrivedcount == 1)
icraggs 2:638c854c0695 113 client.yield(100);
icraggs 2:638c854c0695 114
icraggs 2:638c854c0695 115 // QoS 2
icraggs 2:638c854c0695 116 sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version);
icraggs 2:638c854c0695 117 message.qos = MQTT::QOS2;
icraggs 2:638c854c0695 118 message.payloadlen = strlen(buf)+1;
icraggs 2:638c854c0695 119 rc = client.publish(topic, &message);
icraggs 2:638c854c0695 120 while (arrivedcount == 2)
icraggs 2:638c854c0695 121 client.yield(100);
icraggs 2:638c854c0695 122
icraggs 2:638c854c0695 123 rc = client.unsubscribe(topic);
sam_grove 5:4a257f6ac09a 124 if (rc != 0) {
sam_grove 5:4a257f6ac09a 125 printf("rc from unsubscribe was %d\n", rc);
sam_grove 5:4a257f6ac09a 126 }
icraggs 2:638c854c0695 127
icraggs 2:638c854c0695 128 rc = client.disconnect();
sam_grove 5:4a257f6ac09a 129 if (rc != 0) {
sam_grove 5:4a257f6ac09a 130 printf("rc from disconnect was %d\n", rc);
sam_grove 5:4a257f6ac09a 131 }
icraggs 2:638c854c0695 132
icraggs 2:638c854c0695 133 ipstack.disconnect();
icraggs 2:638c854c0695 134
sam_grove 5:4a257f6ac09a 135 lcd.cls();
sam_grove 5:4a257f6ac09a 136 lcd.locate(0,3);
sam_grove 5:4a257f6ac09a 137 lcd.printf("Finish: %d msgs\n", arrivedcount);
sam_grove 5:4a257f6ac09a 138 printf("Finishing with %d messages received\n", arrivedcount);
icraggs 2:638c854c0695 139
icraggs 0:0cae29831d01 140 return 0;
icraggs 0:0cae29831d01 141 }