Simple mqtt client using the MqttPacket lib to create a connect to the public broker and publish series of message. Done over Ethernet.
Dependencies: C12832_lcd EthernetInterface MQTTPacket mbed-rtos mbed
Diff: main.cpp
- Revision:
- 1:d7773c5860c2
- Parent:
- 0:96a5aefac643
diff -r 96a5aefac643 -r d7773c5860c2 main.cpp --- a/main.cpp Wed Jun 13 19:51:38 2018 +0000 +++ b/main.cpp Thu Jun 14 19:40:33 2018 +0000 @@ -1,115 +1,82 @@ -#include "MQTTPacket.h" - -#include <errno.h> -#include <stdlib.h> -#include <string.h> - +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + *******************************************************************************/ + +#include "mbed.h" #include "EthernetInterface.h" - - -TCPSocketConnection mysock; - -int getdata(char* buf, int count) +#include "C12832_lcd.h" + +#include "MQTTPacket.h" + +DigitalOut myled(LED2); +C12832_LCD lcd; + +int publish(char* payload) { - return mysock.receive(buf, (size_t)count); + int rc = 0; + int len = 0; + int payloadlen = strlen(payload); + MQTTString topicString = MQTTString_initializer; + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + TCPSocketConnection mysock; + unsigned char buf[1024]; + int buflen = sizeof(buf); + + data.clientID.cstring = "0x556677448899330000"; + data.keepAliveInterval = 30; + data.cleansession = 1; + data.MQTTVersion = 3; + + mysock.connect("m2m.eclipse.org", 1883); + + len = MQTTSerialize_connect((unsigned char*)buf, buflen, &data); + + topicString.cstring = "base"; + len += MQTTSerialize_publish((unsigned char*)buf + len, buflen - len, 0, 0, 0, 0, topicString, (unsigned char*)payload, payloadlen); + + len += MQTTSerialize_disconnect((unsigned char*)buf + len, buflen - len); + + rc = 0; + while (rc < len) { + int rc1 = mysock.send((char*)buf, len); + if (rc1 == -1) { + lcd.printf("Send failed\n"); + break; + } else + rc += rc1; + } + if (rc == len) + lcd.printf("Sent: %s \n", payload); + wait(0.2); + + return 0; } - -int toStop = 0; - - + int main() { - MQTTPacket_connectData data = MQTTPacket_connectData_initializer; - int rc = 0; - char buf[200]; - int buflen = sizeof(buf); - int msgid = 1; - MQTTString topicString = MQTTString_initializer; - int req_qos = 0; - char* payload = "mypayload"; - int payloadlen = strlen(payload); - int len = 0; EthernetInterface eth; - eth.init(); //Use DHCP eth.connect(); - - rc = mysock.connect("m2m.eclipse.org", 1883); - - data.clientID.cstring = "SendReceive mbed MQTT "; - data.keepAliveInterval = 20; - data.cleansession = 1; - - mysock.set_blocking(true, 1000); /* 1 second Timeout */ - - len = MQTTSerialize_connect(buf, buflen, &data); - rc = mysock.send(buf, len); - - /* wait for connack */ - if (MQTTPacket_read(buf, buflen, getdata) == CONNACK) - { - int connack_rc; - - if (MQTTDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0) - { - printf("Unable to connect, return code %d\n", connack_rc); - goto exit; - } - } - else - goto exit; - - /* subscribe */ - topicString.cstring = "substopic"; - len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos); - - rc = mysock.send(buf, len); - if (MQTTPacket_read(buf, buflen, getdata) == SUBACK) /* wait for suback */ - { - int submsgid; - int subcount; - int granted_qos; - - rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen); - if (granted_qos != 0) - { - printf("granted qos != 0, %d\n", granted_qos); - goto exit; - } - } - else - goto exit; - - topicString.cstring = "pubtopic"; - while (!toStop) - { - if (MQTTPacket_read(buf, buflen, getdata) == PUBLISH) - { - int dup; - int qos; - int retained; - int msgid; - int payloadlen_in; - char* payload_in; - int rc; - MQTTString receivedTopic; - - rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic, - &payload_in, &payloadlen_in, buf, buflen); - printf("message arrived %.*s\n", payloadlen_in, payload_in); - } - - printf("publishing reading\n"); - len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, payload, payloadlen); - rc = mysock.send(buf, len); - } - - printf("disconnecting\n"); - len = MQTTSerialize_disconnect(buf, buflen); - rc = mysock.send(buf, len); - -exit: + lcd.printf("IP Address is %s\n", eth.getIPAddress()); + + myled = 1; + publish("Message 1"); + wait(0.2); + myled = 0; + publish("Message 2"); + wait(0.2); + eth.disconnect(); - - return 0; } \ No newline at end of file