Added MQTTPacket_Read() in MQTTPacket.h

Fork of MQTTPacket by MQTT

Committer:
icraggs
Date:
Thu Apr 10 15:11:16 2014 +0000
Revision:
2:bc3bc0e3b764
Add samples and test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 2:bc3bc0e3b764 1 /*******************************************************************************
icraggs 2:bc3bc0e3b764 2 * Copyright (c) 2014 IBM Corp.
icraggs 2:bc3bc0e3b764 3 *
icraggs 2:bc3bc0e3b764 4 * All rights reserved. This program and the accompanying materials
icraggs 2:bc3bc0e3b764 5 * are made available under the terms of the Eclipse Public License v1.0
icraggs 2:bc3bc0e3b764 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
icraggs 2:bc3bc0e3b764 7 *
icraggs 2:bc3bc0e3b764 8 * The Eclipse Public License is available at
icraggs 2:bc3bc0e3b764 9 * http://www.eclipse.org/legal/epl-v10.html
icraggs 2:bc3bc0e3b764 10 * and the Eclipse Distribution License is available at
icraggs 2:bc3bc0e3b764 11 * http://www.eclipse.org/org/documents/edl-v10.php.
icraggs 2:bc3bc0e3b764 12 *
icraggs 2:bc3bc0e3b764 13 * Contributors:
icraggs 2:bc3bc0e3b764 14 * Ian Craggs - initial API and implementation and/or initial documentation
icraggs 2:bc3bc0e3b764 15 *******************************************************************************/
icraggs 2:bc3bc0e3b764 16
icraggs 2:bc3bc0e3b764 17 #include "mbed.h"
icraggs 2:bc3bc0e3b764 18 #include "EthernetInterface.h"
icraggs 2:bc3bc0e3b764 19 #include "C12832_lcd.h"
icraggs 2:bc3bc0e3b764 20
icraggs 2:bc3bc0e3b764 21 #include "MQTTPacket.h"
icraggs 2:bc3bc0e3b764 22
icraggs 2:bc3bc0e3b764 23 DigitalOut myled(LED2);
icraggs 2:bc3bc0e3b764 24 C12832_LCD lcd;
icraggs 2:bc3bc0e3b764 25
icraggs 2:bc3bc0e3b764 26 int publish()
icraggs 2:bc3bc0e3b764 27 {
icraggs 2:bc3bc0e3b764 28 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 2:bc3bc0e3b764 29 int rc = 0;
icraggs 2:bc3bc0e3b764 30 char buf[200];
icraggs 2:bc3bc0e3b764 31 int buflen = sizeof(buf);
icraggs 2:bc3bc0e3b764 32 TCPSocketConnection mysock;
icraggs 2:bc3bc0e3b764 33 MQTTString topicString = MQTTString_initializer;
icraggs 2:bc3bc0e3b764 34 char* payload = "I'm alive!";
icraggs 2:bc3bc0e3b764 35 int payloadlen = strlen(payload);
icraggs 2:bc3bc0e3b764 36 int len = 0;
icraggs 2:bc3bc0e3b764 37
icraggs 2:bc3bc0e3b764 38 mysock.connect("m2m.eclipse.org", 1883);
icraggs 2:bc3bc0e3b764 39
icraggs 2:bc3bc0e3b764 40 data.clientID.cstring = "mbed test client - Ian Craggs";
icraggs 2:bc3bc0e3b764 41 data.keepAliveInterval = 20;
icraggs 2:bc3bc0e3b764 42 data.cleansession = 1;
icraggs 2:bc3bc0e3b764 43 data.MQTTVersion = 3;
icraggs 2:bc3bc0e3b764 44
icraggs 2:bc3bc0e3b764 45 len = MQTTSerialize_connect(buf, buflen, &data);
icraggs 2:bc3bc0e3b764 46
icraggs 2:bc3bc0e3b764 47 topicString.cstring = "mbed NXP LPC1768";
icraggs 2:bc3bc0e3b764 48 len += MQTTSerialize_publish(buf + len, buflen - len, 0, 0, 0, 0, topicString, payload, payloadlen);
icraggs 2:bc3bc0e3b764 49
icraggs 2:bc3bc0e3b764 50 len += MQTTSerialize_disconnect(buf + len, buflen - len);
icraggs 2:bc3bc0e3b764 51
icraggs 2:bc3bc0e3b764 52 rc = 0;
icraggs 2:bc3bc0e3b764 53 while (rc < len)
icraggs 2:bc3bc0e3b764 54 {
icraggs 2:bc3bc0e3b764 55 int rc1 = mysock.send(buf, len);
icraggs 2:bc3bc0e3b764 56 if (rc1 == -1)
icraggs 2:bc3bc0e3b764 57 {
icraggs 2:bc3bc0e3b764 58 lcd.printf("Send failed\n");
icraggs 2:bc3bc0e3b764 59 break;
icraggs 2:bc3bc0e3b764 60 }
icraggs 2:bc3bc0e3b764 61 else
icraggs 2:bc3bc0e3b764 62 rc += rc1;
icraggs 2:bc3bc0e3b764 63 }
icraggs 2:bc3bc0e3b764 64 if (rc == len)
icraggs 2:bc3bc0e3b764 65 lcd.printf("Send succeeded\n");
icraggs 2:bc3bc0e3b764 66 wait(0.2);
icraggs 2:bc3bc0e3b764 67
icraggs 2:bc3bc0e3b764 68 return 0;
icraggs 2:bc3bc0e3b764 69 }
icraggs 2:bc3bc0e3b764 70
icraggs 2:bc3bc0e3b764 71 int main()
icraggs 2:bc3bc0e3b764 72 {
icraggs 2:bc3bc0e3b764 73 EthernetInterface eth;
icraggs 2:bc3bc0e3b764 74 eth.init(); //Use DHCP
icraggs 2:bc3bc0e3b764 75 eth.connect();
icraggs 2:bc3bc0e3b764 76 lcd.printf("IP Address is %s\n", eth.getIPAddress());
icraggs 2:bc3bc0e3b764 77
icraggs 2:bc3bc0e3b764 78 while(1)
icraggs 2:bc3bc0e3b764 79 {
icraggs 2:bc3bc0e3b764 80 myled = 1;
icraggs 2:bc3bc0e3b764 81 publish();
icraggs 2:bc3bc0e3b764 82 wait(0.2);
icraggs 2:bc3bc0e3b764 83 myled = 0;
icraggs 2:bc3bc0e3b764 84 publish();
icraggs 2:bc3bc0e3b764 85 wait(0.2);
icraggs 2:bc3bc0e3b764 86 }
icraggs 2:bc3bc0e3b764 87
icraggs 2:bc3bc0e3b764 88 eth.disconnect();
icraggs 2:bc3bc0e3b764 89 }