MQTT Client for mbed LPC1768 and Application Board over Ethernet; publish only.

Dependencies:   C12832 MMA7660 MQTT

Fork of HelloMQTT by MQTT

Machine-to-Machine-Communication

Das Message Queue Telemetry Transport (MQTT) Protokoll ist ein effizientes Internet of Things (IoT) Protokoll mit wenig Protokolloverhead. Es wurde als lightweight publish/subscribe messaging transport entworfen. Es ist sinnvoll für Verbindungen mit Remote-Standorten, wo nur wenig Resourcen benötigt werden bzw. geringe Bandbreite zur Verfügung steht. Neben MQTT gibt es weitere IoT-Protokoll, wie HTTP, CoAP, XMPP, ... (Überischt über IoT Protokolle). Eine Beschreibung des MQTT ist z.B. hier zu finden.

MQTT implementiert das Publish/Subscribe-Pattern, daher wird ein Brocker für die Vermittlung der Nachrichten (topics) benötigt. Neben vielen quelloffenen Implementierungen (z. B. mosquitto) gibt es auch kommerzielle bzw. freie Server (Broker), die unterschiedliche Features bereit stellen (z. B. den in Deutschland entwickelten HiveMQ).

Für die Programmierung der Clients bzw. Ebedded Systems stehen ebenso sehr viele Libaries in verschiedenen Programmiersprachen zur Verfügung.

Desktop MQTT-Clients

M3-Ethernet MQTT Client

M3-Ethernet ThingSpeak

C# Wpf Client

Weitere Kommunikationsmöglichkeiten

Committer:
fpucher
Date:
Thu May 04 10:07:14 2017 +0000
Revision:
21:4b8d80bf664f
Parent:
20:49c9daf2b0ff
MQTT Client for mbed LPC1768 and Application Board over Ethernet.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 1:a1d5c7a6acbc 1 /*******************************************************************************
fpucher 21:4b8d80bf664f 2 * Changed: PE, v0.71 works with Ethernet and random Client-ID
icraggs 17:0811bdbdd78a 3 * Copyright (c) 2014, 2015 IBM Corp.
icraggs 1:a1d5c7a6acbc 4 *
icraggs 1:a1d5c7a6acbc 5 * All rights reserved. This program and the accompanying materials
icraggs 1:a1d5c7a6acbc 6 * are made available under the terms of the Eclipse Public License v1.0
icraggs 1:a1d5c7a6acbc 7 * and Eclipse Distribution License v1.0 which accompany this distribution.
icraggs 1:a1d5c7a6acbc 8 *
icraggs 1:a1d5c7a6acbc 9 * The Eclipse Public License is available at
icraggs 1:a1d5c7a6acbc 10 * http://www.eclipse.org/legal/epl-v10.html
icraggs 1:a1d5c7a6acbc 11 * and the Eclipse Distribution License is available at
icraggs 1:a1d5c7a6acbc 12 * http://www.eclipse.org/org/documents/edl-v10.php.
icraggs 1:a1d5c7a6acbc 13 *
icraggs 1:a1d5c7a6acbc 14 * Contributors:
icraggs 1:a1d5c7a6acbc 15 * Ian Craggs - initial API and implementation and/or initial documentation
icraggs 17:0811bdbdd78a 16 * Ian Craggs - make sure QoS2 processing works, and add device headers
icraggs 1:a1d5c7a6acbc 17 *******************************************************************************/
Jan Jongboom 20:49c9daf2b0ff 18
fpucher 21:4b8d80bf664f 19 /**
fpucher 21:4b8d80bf664f 20 This is a sample program to illustrate the use of the MQTT Client library
fpucher 21:4b8d80bf664f 21 on the mbed platform. The Client class requires two classes which mediate
fpucher 21:4b8d80bf664f 22 access to system interfaces for networking and timing. As long as these two
fpucher 21:4b8d80bf664f 23 classes provide the required public programming interfaces, it does not matter
fpucher 21:4b8d80bf664f 24 what facilities they use underneath. In this program, they use the mbed
fpucher 21:4b8d80bf664f 25 system libraries.
Jan Jongboom 20:49c9daf2b0ff 26
fpucher 21:4b8d80bf664f 27 */
Jan Jongboom 20:49c9daf2b0ff 28
fpucher 21:4b8d80bf664f 29 // change this to 0 to output messages to serial instead of LCD
fpucher 21:4b8d80bf664f 30 #define USE_LCD 0
icraggs 1:a1d5c7a6acbc 31
icraggs 18:07a79d8f01c3 32 #if USE_LCD
icraggs 17:0811bdbdd78a 33 #include "C12832.h"
icraggs 2:638c854c0695 34
Jan Jongboom 20:49c9daf2b0ff 35 // the actual pins are defined in mbed_app.json and can be overridden per target
Jan Jongboom 20:49c9daf2b0ff 36 C12832 lcd(LCD_MOSI, LCD_SCK, LCD_MISO, LCD_A0, LCD_NCS);
icraggs 17:0811bdbdd78a 37
Jan Jongboom 20:49c9daf2b0ff 38 #define logMessage lcd.cls();lcd.printf
Jan Jongboom 20:49c9daf2b0ff 39
Jan Jongboom 20:49c9daf2b0ff 40 #else
Jan Jongboom 20:49c9daf2b0ff 41
Jan Jongboom 20:49c9daf2b0ff 42 #define logMessage printf
icraggs 18:07a79d8f01c3 43
icraggs 18:07a79d8f01c3 44 #endif
icraggs 18:07a79d8f01c3 45
icraggs 17:0811bdbdd78a 46 #define MQTTCLIENT_QOS2 1
icraggs 0:0cae29831d01 47
Jan Jongboom 20:49c9daf2b0ff 48 #include "easy-connect.h"
Jan Jongboom 20:49c9daf2b0ff 49 #include "MQTTNetwork.h"
Jan Jongboom 20:49c9daf2b0ff 50 #include "MQTTmbed.h"
icraggs 2:638c854c0695 51 #include "MQTTClient.h"
icraggs 2:638c854c0695 52
fpucher 21:4b8d80bf664f 53 Serial pc(USBTX, USBRX);
fpucher 21:4b8d80bf664f 54
fpucher 21:4b8d80bf664f 55 static const char alphanum[] =
fpucher 21:4b8d80bf664f 56 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
icraggs 2:638c854c0695 57
fpucher 21:4b8d80bf664f 58 int stringLength = sizeof(alphanum) - 1;
fpucher 21:4b8d80bf664f 59 // use for random
fpucher 21:4b8d80bf664f 60 #include "MMA7660.h"
fpucher 21:4b8d80bf664f 61
fpucher 21:4b8d80bf664f 62 MMA7660 MMA(p28, p27);
icraggs 8:a3e3113054a1 63
fpucher 21:4b8d80bf664f 64 char genRandom()
icraggs 2:638c854c0695 65 {
fpucher 21:4b8d80bf664f 66 if (MMA.testConnection())
fpucher 21:4b8d80bf664f 67 logMessage("get seed: Please shake the board for 1s \r\n");
fpucher 21:4b8d80bf664f 68 int x;
fpucher 21:4b8d80bf664f 69 for (int i=0; i < 5; i++) {
fpucher 21:4b8d80bf664f 70 x=(int) (MMA.z() * MMA.z() * MMA.z()*100)% 65;
fpucher 21:4b8d80bf664f 71 //logMessage("seed: %d\r\n", x);
fpucher 21:4b8d80bf664f 72 wait_ms(200);
fpucher 21:4b8d80bf664f 73 }
fpucher 21:4b8d80bf664f 74 srand (x);
fpucher 21:4b8d80bf664f 75 return alphanum[rand() % stringLength];
icraggs 2:638c854c0695 76 }
icraggs 0:0cae29831d01 77
icraggs 2:638c854c0695 78 int main(int argc, char* argv[])
Jan Jongboom 20:49c9daf2b0ff 79 {
fpucher 21:4b8d80bf664f 80 float version = 0.71;
fpucher 21:4b8d80bf664f 81 char* topic = "mbed/sample/Eth1";
Jan Jongboom 20:49c9daf2b0ff 82
fpucher 21:4b8d80bf664f 83 logMessage("MQTT Publish PE: version is %.2f\r\n", version);
Jan Jongboom 20:49c9daf2b0ff 84
fpucher 21:4b8d80bf664f 85 // connect to Ethernet-LAN with DHCP
Jan Jongboom 20:49c9daf2b0ff 86 NetworkInterface* network = easy_connect(true);
Jan Jongboom 20:49c9daf2b0ff 87 if (!network) {
Jan Jongboom 20:49c9daf2b0ff 88 return -1;
Jan Jongboom 20:49c9daf2b0ff 89 }
fpucher 21:4b8d80bf664f 90
fpucher 21:4b8d80bf664f 91 // connect to broker over Ethernet-LAN
Jan Jongboom 20:49c9daf2b0ff 92 MQTTNetwork mqttNetwork(network);
Jan Jongboom 20:49c9daf2b0ff 93 MQTT::Client<MQTTNetwork, Countdown> client = MQTT::Client<MQTTNetwork, Countdown>(mqttNetwork);
Jan Jongboom 20:49c9daf2b0ff 94
fpucher 21:4b8d80bf664f 95 const char* hostname = "broker.hivemq.com"; // local RPi: 172.16.63.188; broker.hivemq.com: 35.157.21.220
icraggs 6:e4c690c45021 96 int port = 1883;
Jan Jongboom 20:49c9daf2b0ff 97 logMessage("Connecting to %s:%d\r\n", hostname, port);
fpucher 21:4b8d80bf664f 98
Jan Jongboom 20:49c9daf2b0ff 99 int rc = mqttNetwork.connect(hostname, port);
icraggs 6:e4c690c45021 100 if (rc != 0)
Jan Jongboom 20:49c9daf2b0ff 101 logMessage("rc from TCP connect is %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 102
Jan Jongboom 20:49c9daf2b0ff 103 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 6:e4c690c45021 104 data.MQTTVersion = 3;
fpucher 21:4b8d80bf664f 105
fpucher 21:4b8d80bf664f 106 // generate a unique random Client-ID
fpucher 21:4b8d80bf664f 107 char str[20] ="mbed-clientID-";
fpucher 21:4b8d80bf664f 108 srand(time(NULL));
fpucher 21:4b8d80bf664f 109 int i;
fpucher 21:4b8d80bf664f 110 for(i = 14; i < 18; ++i) {
fpucher 21:4b8d80bf664f 111 str[i] = genRandom();
fpucher 21:4b8d80bf664f 112 }
fpucher 21:4b8d80bf664f 113 str[i] = 0;
fpucher 21:4b8d80bf664f 114 data.clientID.cstring = str;
fpucher 21:4b8d80bf664f 115 logMessage("ClientID: %s\r\n", data.clientID.cstring);
fpucher 21:4b8d80bf664f 116 // only if authentication is used (not now):
fpucher 21:4b8d80bf664f 117 // data.username.cstring = "testuser";
fpucher 21:4b8d80bf664f 118 // data.password.cstring = "testpassword";
icraggs 16:28d062c5522b 119 if ((rc = client.connect(data)) != 0)
Jan Jongboom 20:49c9daf2b0ff 120 logMessage("rc from MQTT connect is %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 121
icraggs 2:638c854c0695 122 MQTT::Message message;
icraggs 0:0cae29831d01 123
fpucher 21:4b8d80bf664f 124 // publish 5 messages
fpucher 21:4b8d80bf664f 125 for(int t=0; t<5; t++) {
fpucher 21:4b8d80bf664f 126 char buf[100];
fpucher 21:4b8d80bf664f 127 sprintf(buf, "28.4.2017/16:30/21"); // published message
fpucher 21:4b8d80bf664f 128 message.qos = MQTT::QOS0;
fpucher 21:4b8d80bf664f 129 message.retained = false;
fpucher 21:4b8d80bf664f 130 message.dup = false;
fpucher 21:4b8d80bf664f 131 message.payload = (void*)buf;
fpucher 21:4b8d80bf664f 132 message.payloadlen = strlen(buf)+1;
fpucher 21:4b8d80bf664f 133 rc = client.publish(topic, message);
fpucher 21:4b8d80bf664f 134 wait(2);
fpucher 21:4b8d80bf664f 135 }
Jan Jongboom 20:49c9daf2b0ff 136
fpucher 21:4b8d80bf664f 137 // unsubscribe and disconnect from broker
icraggs 17:0811bdbdd78a 138 if ((rc = client.unsubscribe(topic)) != 0)
Jan Jongboom 20:49c9daf2b0ff 139 logMessage("rc from unsubscribe was %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 140
icraggs 8:a3e3113054a1 141 if ((rc = client.disconnect()) != 0)
Jan Jongboom 20:49c9daf2b0ff 142 logMessage("rc from disconnect was %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 143
Jan Jongboom 20:49c9daf2b0ff 144 mqttNetwork.disconnect();
Jan Jongboom 20:49c9daf2b0ff 145
fpucher 21:4b8d80bf664f 146 logMessage("Version %.2f: finished\r\n", version);
Jan Jongboom 20:49c9daf2b0ff 147
icraggs 0:0cae29831d01 148 return 0;
icraggs 0:0cae29831d01 149 }