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