Program to test extending the C027_Support libraries to support TLS/SSL
Dependencies: C027_Support MQTT mbed-dev
main.cpp@0:29cc8656f38b, 2017-01-24 (annotated)
- Committer:
- michaelVisimid
- Date:
- Tue Jan 24 22:34:04 2017 +0000
- Revision:
- 0:29cc8656f38b
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaelVisimid | 0:29cc8656f38b | 1 | /******************************************************************************* |
michaelVisimid | 0:29cc8656f38b | 2 | * Copyright (c) 2014 IBM Corp. |
michaelVisimid | 0:29cc8656f38b | 3 | * |
michaelVisimid | 0:29cc8656f38b | 4 | * All rights reserved. This program and the accompanying materials |
michaelVisimid | 0:29cc8656f38b | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
michaelVisimid | 0:29cc8656f38b | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
michaelVisimid | 0:29cc8656f38b | 7 | * |
michaelVisimid | 0:29cc8656f38b | 8 | * The Eclipse Public License is available at |
michaelVisimid | 0:29cc8656f38b | 9 | * http://www.eclipse.org/legal/epl-v10.html |
michaelVisimid | 0:29cc8656f38b | 10 | * and the Eclipse Distribution License is available at |
michaelVisimid | 0:29cc8656f38b | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
michaelVisimid | 0:29cc8656f38b | 12 | * |
michaelVisimid | 0:29cc8656f38b | 13 | * Contributors: |
michaelVisimid | 0:29cc8656f38b | 14 | * Ian Craggs - initial API and implementation and/or initial documentation |
michaelVisimid | 0:29cc8656f38b | 15 | *******************************************************************************/ |
michaelVisimid | 0:29cc8656f38b | 16 | |
michaelVisimid | 0:29cc8656f38b | 17 | /** |
michaelVisimid | 0:29cc8656f38b | 18 | This is a sample program to illustrate the use of the MQTT Client library |
michaelVisimid | 0:29cc8656f38b | 19 | on the mbed platform. The Client class requires two classes which mediate |
michaelVisimid | 0:29cc8656f38b | 20 | access to system interfaces for networking and timing. As long as these two |
michaelVisimid | 0:29cc8656f38b | 21 | classes provide the required public programming interfaces, it does not matter |
michaelVisimid | 0:29cc8656f38b | 22 | what facilities they use underneath. In this program, they use the mbed |
michaelVisimid | 0:29cc8656f38b | 23 | system libraries. |
michaelVisimid | 0:29cc8656f38b | 24 | |
michaelVisimid | 0:29cc8656f38b | 25 | */ |
michaelVisimid | 0:29cc8656f38b | 26 | #include "mbed.h" |
michaelVisimid | 0:29cc8656f38b | 27 | |
michaelVisimid | 0:29cc8656f38b | 28 | //------------------------------------------------------------------------------------ |
michaelVisimid | 0:29cc8656f38b | 29 | // You need to configure these cellular modem / SIM parameters. |
michaelVisimid | 0:29cc8656f38b | 30 | // These parameters are ignored for LISA-C200 variants and can be left NULL. |
michaelVisimid | 0:29cc8656f38b | 31 | //------------------------------------------------------------------------------------ |
michaelVisimid | 0:29cc8656f38b | 32 | #include "MDM.h" |
michaelVisimid | 0:29cc8656f38b | 33 | //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual. |
michaelVisimid | 0:29cc8656f38b | 34 | #define SIMPIN NULL |
michaelVisimid | 0:29cc8656f38b | 35 | /*! The APN of your network operator SIM, sometimes it is "internet" check your |
michaelVisimid | 0:29cc8656f38b | 36 | contract with the network operator. You can also try to look-up your settings in |
michaelVisimid | 0:29cc8656f38b | 37 | google: https://www.google.de/search?q=APN+list */ |
michaelVisimid | 0:29cc8656f38b | 38 | #define APN "epc.tmobile.com" |
michaelVisimid | 0:29cc8656f38b | 39 | //! Set the user name for your APN, or NULL if not needed |
michaelVisimid | 0:29cc8656f38b | 40 | #define USERNAME NULL |
michaelVisimid | 0:29cc8656f38b | 41 | //! Set the password for your APN, or NULL if not needed |
michaelVisimid | 0:29cc8656f38b | 42 | #define PASSWORD NULL |
michaelVisimid | 0:29cc8656f38b | 43 | //------------------------------------------------------------------------------------ |
michaelVisimid | 0:29cc8656f38b | 44 | |
michaelVisimid | 0:29cc8656f38b | 45 | #include "MQTTSocket.h" |
michaelVisimid | 0:29cc8656f38b | 46 | #include "MQTTClient.h" |
michaelVisimid | 0:29cc8656f38b | 47 | DigitalOut myled(LED1); |
michaelVisimid | 0:29cc8656f38b | 48 | |
michaelVisimid | 0:29cc8656f38b | 49 | int arrivedcount = 0; |
michaelVisimid | 0:29cc8656f38b | 50 | |
michaelVisimid | 0:29cc8656f38b | 51 | void messageArrived(MQTT::MessageData& md) |
michaelVisimid | 0:29cc8656f38b | 52 | { |
michaelVisimid | 0:29cc8656f38b | 53 | MQTT::Message &message = md.message; |
michaelVisimid | 0:29cc8656f38b | 54 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); |
michaelVisimid | 0:29cc8656f38b | 55 | printf("Payload %.*s\r", message.payloadlen, (char*)message.payload); |
michaelVisimid | 0:29cc8656f38b | 56 | ++arrivedcount; |
michaelVisimid | 0:29cc8656f38b | 57 | } |
michaelVisimid | 0:29cc8656f38b | 58 | |
michaelVisimid | 0:29cc8656f38b | 59 | |
michaelVisimid | 0:29cc8656f38b | 60 | int main(int argc, char* argv[]) |
michaelVisimid | 0:29cc8656f38b | 61 | { |
michaelVisimid | 0:29cc8656f38b | 62 | |
michaelVisimid | 0:29cc8656f38b | 63 | printf("start\r\n"); |
michaelVisimid | 0:29cc8656f38b | 64 | MDMSerial mdm(PC_10,PC_11); |
michaelVisimid | 0:29cc8656f38b | 65 | //mdm.setDebug(4); // enable this for debugging issues |
michaelVisimid | 0:29cc8656f38b | 66 | printf("attempting to connect to network\r\n"); |
michaelVisimid | 0:29cc8656f38b | 67 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)){ |
michaelVisimid | 0:29cc8656f38b | 68 | printf("no connect\r\n"); |
michaelVisimid | 0:29cc8656f38b | 69 | while(1) { |
michaelVisimid | 0:29cc8656f38b | 70 | myled = 1; |
michaelVisimid | 0:29cc8656f38b | 71 | wait(0.25); |
michaelVisimid | 0:29cc8656f38b | 72 | myled = 0; |
michaelVisimid | 0:29cc8656f38b | 73 | wait(0.25); |
michaelVisimid | 0:29cc8656f38b | 74 | } |
michaelVisimid | 0:29cc8656f38b | 75 | } |
michaelVisimid | 0:29cc8656f38b | 76 | |
michaelVisimid | 0:29cc8656f38b | 77 | MQTTSocket ipstack = MQTTSocket(); |
michaelVisimid | 0:29cc8656f38b | 78 | float version = 0.47; |
michaelVisimid | 0:29cc8656f38b | 79 | char* topic = "HelloWorld"; |
michaelVisimid | 0:29cc8656f38b | 80 | |
michaelVisimid | 0:29cc8656f38b | 81 | printf("Version is %f\r\n", version); |
michaelVisimid | 0:29cc8656f38b | 82 | |
michaelVisimid | 0:29cc8656f38b | 83 | MQTT::Client<MQTTSocket, Countdown> client = MQTT::Client<MQTTSocket, Countdown>(ipstack); |
michaelVisimid | 0:29cc8656f38b | 84 | |
michaelVisimid | 0:29cc8656f38b | 85 | char* hostname = "m2m.eclipse.org"; |
michaelVisimid | 0:29cc8656f38b | 86 | int port = 1883; |
michaelVisimid | 0:29cc8656f38b | 87 | printf("Connecting to %s:%d\r\n", hostname, port); |
michaelVisimid | 0:29cc8656f38b | 88 | int rc = ipstack.connect(hostname, port); |
michaelVisimid | 0:29cc8656f38b | 89 | if (rc != 0) |
michaelVisimid | 0:29cc8656f38b | 90 | printf("rc from TCP connect is %d\r\n", rc); |
michaelVisimid | 0:29cc8656f38b | 91 | |
michaelVisimid | 0:29cc8656f38b | 92 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
michaelVisimid | 0:29cc8656f38b | 93 | data.MQTTVersion = 4; |
michaelVisimid | 0:29cc8656f38b | 94 | data.clientID.cstring = "MQTT_Test"; |
michaelVisimid | 0:29cc8656f38b | 95 | if ((rc = client.connect(data)) != 0) |
michaelVisimid | 0:29cc8656f38b | 96 | printf("rc from MQTT connect is %d\n", rc); |
michaelVisimid | 0:29cc8656f38b | 97 | |
michaelVisimid | 0:29cc8656f38b | 98 | if ((rc = client.subscribe(topic, MQTT::QOS0, messageArrived)) != 0) |
michaelVisimid | 0:29cc8656f38b | 99 | printf("rc from MQTT subscribe is %d\r\n", rc); |
michaelVisimid | 0:29cc8656f38b | 100 | |
michaelVisimid | 0:29cc8656f38b | 101 | MQTT::Message message; |
michaelVisimid | 0:29cc8656f38b | 102 | |
michaelVisimid | 0:29cc8656f38b | 103 | // QoS 0 |
michaelVisimid | 0:29cc8656f38b | 104 | char buf[100]; |
michaelVisimid | 0:29cc8656f38b | 105 | sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version); |
michaelVisimid | 0:29cc8656f38b | 106 | message.qos = MQTT::QOS0; |
michaelVisimid | 0:29cc8656f38b | 107 | message.retained = false; |
michaelVisimid | 0:29cc8656f38b | 108 | message.dup = false; |
michaelVisimid | 0:29cc8656f38b | 109 | message.payload = (void*)buf; |
michaelVisimid | 0:29cc8656f38b | 110 | message.payloadlen = strlen(buf)+1; |
michaelVisimid | 0:29cc8656f38b | 111 | rc = client.publish(topic, message); |
michaelVisimid | 0:29cc8656f38b | 112 | while (arrivedcount == 0) |
michaelVisimid | 0:29cc8656f38b | 113 | client.yield(100); |
michaelVisimid | 0:29cc8656f38b | 114 | |
michaelVisimid | 0:29cc8656f38b | 115 | // QoS 1 |
michaelVisimid | 0:29cc8656f38b | 116 | sprintf(buf, "Hello World! QoS 1 message from app version %f\r\n", version); |
michaelVisimid | 0:29cc8656f38b | 117 | message.qos = MQTT::QOS1; |
michaelVisimid | 0:29cc8656f38b | 118 | message.payloadlen = strlen(buf)+1; |
michaelVisimid | 0:29cc8656f38b | 119 | rc = client.publish(topic, message); |
michaelVisimid | 0:29cc8656f38b | 120 | while (arrivedcount == 1) |
michaelVisimid | 0:29cc8656f38b | 121 | client.yield(100); |
michaelVisimid | 0:29cc8656f38b | 122 | /* |
michaelVisimid | 0:29cc8656f38b | 123 | // QoS 2 |
michaelVisimid | 0:29cc8656f38b | 124 | //todo configure server for QoS2 |
michaelVisimid | 0:29cc8656f38b | 125 | sprintf(buf, "Hello World! QoS 2 message from app version %f\r\n", version); |
michaelVisimid | 0:29cc8656f38b | 126 | message.qos = MQTT::QOS2; |
michaelVisimid | 0:29cc8656f38b | 127 | message.payloadlen = strlen(buf)+1; |
michaelVisimid | 0:29cc8656f38b | 128 | rc = client.publish(topic, message); |
michaelVisimid | 0:29cc8656f38b | 129 | while (arrivedcount == 2) |
michaelVisimid | 0:29cc8656f38b | 130 | client.yield(100); |
michaelVisimid | 0:29cc8656f38b | 131 | */ |
michaelVisimid | 0:29cc8656f38b | 132 | if ((rc = client.unsubscribe(topic)) != 0) |
michaelVisimid | 0:29cc8656f38b | 133 | printf("rc from unsubscribe was %d\r\n", rc); |
michaelVisimid | 0:29cc8656f38b | 134 | |
michaelVisimid | 0:29cc8656f38b | 135 | if ((rc = client.disconnect()) != 0) |
michaelVisimid | 0:29cc8656f38b | 136 | printf("rc from disconnect was %d\r\n", rc); |
michaelVisimid | 0:29cc8656f38b | 137 | |
michaelVisimid | 0:29cc8656f38b | 138 | ipstack.disconnect(); |
michaelVisimid | 0:29cc8656f38b | 139 | mdm.disconnect(); |
michaelVisimid | 0:29cc8656f38b | 140 | mdm.powerOff(); |
michaelVisimid | 0:29cc8656f38b | 141 | |
michaelVisimid | 0:29cc8656f38b | 142 | printf("Version %.2f: finish %d msgs\r\n", version, arrivedcount); |
michaelVisimid | 0:29cc8656f38b | 143 | printf("Finishing with %d messages received\r\n", arrivedcount); |
michaelVisimid | 0:29cc8656f38b | 144 | |
michaelVisimid | 0:29cc8656f38b | 145 | return 0; |
michaelVisimid | 0:29cc8656f38b | 146 | } |
michaelVisimid | 0:29cc8656f38b | 147 |