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