HelloMQTT porting to FRDM-K64F+AppShield
Dependencies: C12832 EthernetInterface MQTT mbed-rtos mbed
Fork of HelloMQTT by
Tested with RabbitMQ 3.4.4 on OSX.
modification are below
- skip QOS2 test since RabbitMQ is not supported QOS2 message type
main.cpp@1:a1d5c7a6acbc, 2014-02-05 (annotated)
- Committer:
- icraggs
- Date:
- Wed Feb 05 10:50:15 2014 +0000
- Revision:
- 1:a1d5c7a6acbc
- Parent:
- 0:0cae29831d01
- Child:
- 2:638c854c0695
Add copyright statement to main program
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:a1d5c7a6acbc | 16 | |
icraggs | 0:0cae29831d01 | 17 | #include "mbed.h" |
icraggs | 0:0cae29831d01 | 18 | #include "EthernetInterface.h" |
icraggs | 0:0cae29831d01 | 19 | #include "C12832_lcd.h" |
icraggs | 0:0cae29831d01 | 20 | |
icraggs | 0:0cae29831d01 | 21 | #include "MQTTPacket.h" |
icraggs | 0:0cae29831d01 | 22 | |
icraggs | 0:0cae29831d01 | 23 | DigitalOut myled(LED2); |
icraggs | 0:0cae29831d01 | 24 | C12832_LCD lcd; |
icraggs | 0:0cae29831d01 | 25 | |
icraggs | 0:0cae29831d01 | 26 | int publish() |
icraggs | 0:0cae29831d01 | 27 | { |
icraggs | 0:0cae29831d01 | 28 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
icraggs | 0:0cae29831d01 | 29 | int rc = 0; |
icraggs | 0:0cae29831d01 | 30 | char buf[200]; |
icraggs | 0:0cae29831d01 | 31 | int buflen = sizeof(buf); |
icraggs | 0:0cae29831d01 | 32 | TCPSocketConnection mysock; |
icraggs | 0:0cae29831d01 | 33 | MQTTString topicString = MQTTString_initializer; |
icraggs | 0:0cae29831d01 | 34 | char* payload = "I'm alive!"; |
icraggs | 0:0cae29831d01 | 35 | int payloadlen = strlen(payload); |
icraggs | 0:0cae29831d01 | 36 | int len = 0; |
icraggs | 0:0cae29831d01 | 37 | |
icraggs | 0:0cae29831d01 | 38 | mysock.connect("m2m.eclipse.org", 1883); |
icraggs | 0:0cae29831d01 | 39 | |
icraggs | 0:0cae29831d01 | 40 | data.clientID.cstring = "mbed test client - Ian Craggs"; |
icraggs | 0:0cae29831d01 | 41 | data.keepAliveInterval = 20; |
icraggs | 0:0cae29831d01 | 42 | data.cleansession = 1; |
icraggs | 0:0cae29831d01 | 43 | data.MQTTVersion = 3; |
icraggs | 0:0cae29831d01 | 44 | |
icraggs | 0:0cae29831d01 | 45 | len = MQTTSerialize_connect(buf, buflen, &data); |
icraggs | 0:0cae29831d01 | 46 | |
icraggs | 0:0cae29831d01 | 47 | topicString.cstring = "mbed NXP LPC1768"; |
icraggs | 0:0cae29831d01 | 48 | len += MQTTSerialize_publish(buf + len, buflen - len, 0, 0, 0, 0, topicString, payload, payloadlen); |
icraggs | 0:0cae29831d01 | 49 | |
icraggs | 0:0cae29831d01 | 50 | len += MQTTSerialize_disconnect(buf + len, buflen - len); |
icraggs | 0:0cae29831d01 | 51 | |
icraggs | 0:0cae29831d01 | 52 | rc = 0; |
icraggs | 0:0cae29831d01 | 53 | while (rc < len) |
icraggs | 0:0cae29831d01 | 54 | { |
icraggs | 0:0cae29831d01 | 55 | int rc1 = mysock.send(buf, len); |
icraggs | 0:0cae29831d01 | 56 | if (rc1 == -1) |
icraggs | 0:0cae29831d01 | 57 | { |
icraggs | 0:0cae29831d01 | 58 | lcd.printf("Send failed\n"); |
icraggs | 0:0cae29831d01 | 59 | break; |
icraggs | 0:0cae29831d01 | 60 | } |
icraggs | 0:0cae29831d01 | 61 | else |
icraggs | 0:0cae29831d01 | 62 | rc += rc1; |
icraggs | 0:0cae29831d01 | 63 | } |
icraggs | 0:0cae29831d01 | 64 | if (rc == len) |
icraggs | 0:0cae29831d01 | 65 | lcd.printf("Send succeeded\n"); |
icraggs | 0:0cae29831d01 | 66 | wait(0.2); |
icraggs | 0:0cae29831d01 | 67 | |
icraggs | 0:0cae29831d01 | 68 | return 0; |
icraggs | 0:0cae29831d01 | 69 | } |
icraggs | 0:0cae29831d01 | 70 | |
icraggs | 1:a1d5c7a6acbc | 71 | int main() |
icraggs | 1:a1d5c7a6acbc | 72 | { |
icraggs | 0:0cae29831d01 | 73 | EthernetInterface eth; |
icraggs | 0:0cae29831d01 | 74 | eth.init(); //Use DHCP |
icraggs | 0:0cae29831d01 | 75 | eth.connect(); |
icraggs | 0:0cae29831d01 | 76 | lcd.printf("IP Address is %s\n", eth.getIPAddress()); |
icraggs | 0:0cae29831d01 | 77 | |
icraggs | 1:a1d5c7a6acbc | 78 | while(1) |
icraggs | 1:a1d5c7a6acbc | 79 | { |
icraggs | 0:0cae29831d01 | 80 | myled = 1; |
icraggs | 0:0cae29831d01 | 81 | publish(); |
icraggs | 0:0cae29831d01 | 82 | wait(0.2); |
icraggs | 0:0cae29831d01 | 83 | myled = 0; |
icraggs | 0:0cae29831d01 | 84 | publish(); |
icraggs | 0:0cae29831d01 | 85 | wait(0.2); |
icraggs | 0:0cae29831d01 | 86 | } |
icraggs | 0:0cae29831d01 | 87 | |
icraggs | 0:0cae29831d01 | 88 | eth.disconnect(); |
icraggs | 0:0cae29831d01 | 89 | } |