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