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