the code of obtain senor data and connect to MQTT
Dependencies: C12832 EthernetInterface MMA7660 MQTT USBDevice mbed-rtos mbed
Fork of HelloMQTT by
main.cpp@19:7f7aba7a4a8b, 2015-07-30 (annotated)
- Committer:
- icraggs
- Date:
- Thu Jul 30 13:49:18 2015 +0000
- Revision:
- 19:7f7aba7a4a8b
- Parent:
- 18:07a79d8f01c3
- Child:
- 20:63e59fe8d75d
Subscribe at QoS 2, remove lcd.cls() calls
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icraggs | 1:a1d5c7a6acbc | 1 | /******************************************************************************* |
icraggs | 17:0811bdbdd78a | 2 | * Copyright (c) 2014, 2015 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 | 17:0811bdbdd78a | 15 | * Ian Craggs - make sure QoS2 processing works, and add device headers |
icraggs | 1:a1d5c7a6acbc | 16 | *******************************************************************************/ |
icraggs | 2:638c854c0695 | 17 | |
icraggs | 2:638c854c0695 | 18 | /** |
icraggs | 2:638c854c0695 | 19 | This is a sample program to illustrate the use of the MQTT Client library |
icraggs | 2:638c854c0695 | 20 | on the mbed platform. The Client class requires two classes which mediate |
icraggs | 2:638c854c0695 | 21 | access to system interfaces for networking and timing. As long as these two |
icraggs | 2:638c854c0695 | 22 | classes provide the required public programming interfaces, it does not matter |
icraggs | 2:638c854c0695 | 23 | what facilities they use underneath. In this program, they use the mbed |
icraggs | 2:638c854c0695 | 24 | system libraries. |
icraggs | 2:638c854c0695 | 25 | |
icraggs | 2:638c854c0695 | 26 | */ |
icraggs | 18:07a79d8f01c3 | 27 | |
icraggs | 18:07a79d8f01c3 | 28 | // change this to 0 to output messages to serial instead of LCD |
icraggs | 18:07a79d8f01c3 | 29 | #define USE_LCD 1 |
icraggs | 1:a1d5c7a6acbc | 30 | |
icraggs | 18:07a79d8f01c3 | 31 | #if USE_LCD |
icraggs | 17:0811bdbdd78a | 32 | #include "C12832.h" |
icraggs | 2:638c854c0695 | 33 | |
icraggs | 17:0811bdbdd78a | 34 | #if defined(TARGET_UBLOX_C027) |
icraggs | 17:0811bdbdd78a | 35 | #warning "Compiling for mbed C027" |
icraggs | 17:0811bdbdd78a | 36 | #include "C027.h" |
icraggs | 17:0811bdbdd78a | 37 | #elif defined(TARGET_LPC1768) |
icraggs | 17:0811bdbdd78a | 38 | #warning "Compiling for mbed LPC1768" |
icraggs | 17:0811bdbdd78a | 39 | #include "LPC1768.h" |
icraggs | 17:0811bdbdd78a | 40 | #elif defined(TARGET_K64F) |
icraggs | 17:0811bdbdd78a | 41 | #warning "Compiling for mbed K64F" |
icraggs | 17:0811bdbdd78a | 42 | #include "K64F.h" |
icraggs | 17:0811bdbdd78a | 43 | #endif |
icraggs | 17:0811bdbdd78a | 44 | |
icraggs | 18:07a79d8f01c3 | 45 | #define printf lcd.cls();lcd.printf |
icraggs | 18:07a79d8f01c3 | 46 | |
icraggs | 18:07a79d8f01c3 | 47 | #endif |
icraggs | 18:07a79d8f01c3 | 48 | |
icraggs | 17:0811bdbdd78a | 49 | #define MQTTCLIENT_QOS2 1 |
icraggs | 0:0cae29831d01 | 50 | |
icraggs | 8:a3e3113054a1 | 51 | #include "MQTTEthernet.h" |
icraggs | 2:638c854c0695 | 52 | #include "MQTTClient.h" |
icraggs | 2:638c854c0695 | 53 | |
icraggs | 2:638c854c0695 | 54 | int arrivedcount = 0; |
icraggs | 2:638c854c0695 | 55 | |
icraggs | 8:a3e3113054a1 | 56 | |
icraggs | 9:5beb8609e9f7 | 57 | void messageArrived(MQTT::MessageData& md) |
icraggs | 2:638c854c0695 | 58 | { |
icraggs | 9:5beb8609e9f7 | 59 | MQTT::Message &message = md.message; |
icraggs | 9:5beb8609e9f7 | 60 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); |
icraggs | 9:5beb8609e9f7 | 61 | printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); |
icraggs | 2:638c854c0695 | 62 | ++arrivedcount; |
icraggs | 2:638c854c0695 | 63 | } |
icraggs | 0:0cae29831d01 | 64 | |
icraggs | 2:638c854c0695 | 65 | |
icraggs | 2:638c854c0695 | 66 | int main(int argc, char* argv[]) |
icraggs | 2:638c854c0695 | 67 | { |
icraggs | 8:a3e3113054a1 | 68 | MQTTEthernet ipstack = MQTTEthernet(); |
icraggs | 17:0811bdbdd78a | 69 | float version = 0.5; |
icraggs | 2:638c854c0695 | 70 | char* topic = "mbed-sample"; |
icraggs | 2:638c854c0695 | 71 | |
icraggs | 18:07a79d8f01c3 | 72 | printf("HelloMQTT: version is %f\n", version); |
icraggs | 2:638c854c0695 | 73 | |
icraggs | 8:a3e3113054a1 | 74 | MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); |
icraggs | 3:7a6a899de7cc | 75 | |
icraggs | 6:e4c690c45021 | 76 | char* hostname = "m2m.eclipse.org"; |
icraggs | 6:e4c690c45021 | 77 | int port = 1883; |
icraggs | 18:07a79d8f01c3 | 78 | printf("Connecting to %s:%d\n", hostname, port); |
icraggs | 6:e4c690c45021 | 79 | int rc = ipstack.connect(hostname, port); |
icraggs | 6:e4c690c45021 | 80 | if (rc != 0) |
icraggs | 18:07a79d8f01c3 | 81 | printf("rc from TCP connect is %d\n", rc); |
icraggs | 6:e4c690c45021 | 82 | |
icraggs | 6:e4c690c45021 | 83 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
icraggs | 6:e4c690c45021 | 84 | data.MQTTVersion = 3; |
icraggs | 8:a3e3113054a1 | 85 | data.clientID.cstring = "mbed-sample"; |
icraggs | 12:086a9314e8a5 | 86 | data.username.cstring = "testuser"; |
icraggs | 12:086a9314e8a5 | 87 | data.password.cstring = "testpassword"; |
icraggs | 16:28d062c5522b | 88 | if ((rc = client.connect(data)) != 0) |
icraggs | 18:07a79d8f01c3 | 89 | printf("rc from MQTT connect is %d\n", rc); |
icraggs | 2:638c854c0695 | 90 | |
icraggs | 19:7f7aba7a4a8b | 91 | if ((rc = client.subscribe(topic, MQTT::QOS2, messageArrived)) != 0) |
icraggs | 18:07a79d8f01c3 | 92 | printf("rc from MQTT subscribe is %d\n", rc); |
icraggs | 2:638c854c0695 | 93 | |
icraggs | 2:638c854c0695 | 94 | MQTT::Message message; |
icraggs | 0:0cae29831d01 | 95 | |
icraggs | 2:638c854c0695 | 96 | // QoS 0 |
icraggs | 2:638c854c0695 | 97 | char buf[100]; |
icraggs | 2:638c854c0695 | 98 | sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version); |
icraggs | 2:638c854c0695 | 99 | message.qos = MQTT::QOS0; |
icraggs | 2:638c854c0695 | 100 | message.retained = false; |
icraggs | 2:638c854c0695 | 101 | message.dup = false; |
icraggs | 2:638c854c0695 | 102 | message.payload = (void*)buf; |
icraggs | 2:638c854c0695 | 103 | message.payloadlen = strlen(buf)+1; |
icraggs | 16:28d062c5522b | 104 | rc = client.publish(topic, message); |
icraggs | 12:086a9314e8a5 | 105 | while (arrivedcount < 1) |
icraggs | 2:638c854c0695 | 106 | client.yield(100); |
icraggs | 2:638c854c0695 | 107 | |
icraggs | 2:638c854c0695 | 108 | // QoS 1 |
icraggs | 2:638c854c0695 | 109 | sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version); |
icraggs | 2:638c854c0695 | 110 | message.qos = MQTT::QOS1; |
icraggs | 2:638c854c0695 | 111 | message.payloadlen = strlen(buf)+1; |
icraggs | 16:28d062c5522b | 112 | rc = client.publish(topic, message); |
icraggs | 12:086a9314e8a5 | 113 | while (arrivedcount < 2) |
icraggs | 2:638c854c0695 | 114 | client.yield(100); |
icraggs | 2:638c854c0695 | 115 | |
icraggs | 2:638c854c0695 | 116 | // QoS 2 |
icraggs | 2:638c854c0695 | 117 | sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version); |
icraggs | 2:638c854c0695 | 118 | message.qos = MQTT::QOS2; |
icraggs | 2:638c854c0695 | 119 | message.payloadlen = strlen(buf)+1; |
icraggs | 16:28d062c5522b | 120 | rc = client.publish(topic, message); |
icraggs | 12:086a9314e8a5 | 121 | while (arrivedcount < 3) |
icraggs | 2:638c854c0695 | 122 | client.yield(100); |
icraggs | 17:0811bdbdd78a | 123 | |
icraggs | 17:0811bdbdd78a | 124 | if ((rc = client.unsubscribe(topic)) != 0) |
icraggs | 18:07a79d8f01c3 | 125 | printf("rc from unsubscribe was %d\n", rc); |
icraggs | 2:638c854c0695 | 126 | |
icraggs | 8:a3e3113054a1 | 127 | if ((rc = client.disconnect()) != 0) |
icraggs | 18:07a79d8f01c3 | 128 | printf("rc from disconnect was %d\n", rc); |
icraggs | 2:638c854c0695 | 129 | |
icraggs | 2:638c854c0695 | 130 | ipstack.disconnect(); |
icraggs | 2:638c854c0695 | 131 | |
icraggs | 18:07a79d8f01c3 | 132 | printf("Version %.2f: finish %d msgs\n", version, arrivedcount); |
icraggs | 2:638c854c0695 | 133 | |
icraggs | 0:0cae29831d01 | 134 | return 0; |
icraggs | 0:0cae29831d01 | 135 | } |