Demo program for UBLOX cell modem using Nucleo-L073RZ
Dependencies: C027_Support MQTT mbed-dev
Fork of Cellular_HelloMQTT by
main.cpp@10:6f1c3b718b9c, 2014-05-13 (annotated)
- Committer:
- mazgch
- Date:
- Tue May 13 06:15:02 2014 +0000
- Revision:
- 10:6f1c3b718b9c
- Parent:
- 8:b32c94be6522
- Child:
- 11:c074866fcad7
use latest library
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 | 1:a1d5c7a6acbc | 26 | |
icraggs | 0:0cae29831d01 | 27 | #include "mbed.h" |
sam_grove | 5:4a257f6ac09a | 28 | #include "EthernetInterfaceIPStack.h" |
mazgch | 8:b32c94be6522 | 29 | #include "C027.h" |
mazgch | 8:b32c94be6522 | 30 | #include "MDM.h" |
mazgch | 8:b32c94be6522 | 31 | |
mazgch | 8:b32c94be6522 | 32 | //---------------------------------------------------------------------- |
mazgch | 8:b32c94be6522 | 33 | // You may need to configure these parameters |
mazgch | 8:b32c94be6522 | 34 | |
mazgch | 8:b32c94be6522 | 35 | /** Set your secret SIM pin here "1234" |
mazgch | 8:b32c94be6522 | 36 | */ |
mazgch | 8:b32c94be6522 | 37 | #define SIMPIN NULL |
mazgch | 8:b32c94be6522 | 38 | |
mazgch | 8:b32c94be6522 | 39 | /** The APN of your network operator, sometimes it is "internet" |
mazgch | 8:b32c94be6522 | 40 | check your contract with the network operator |
mazgch | 8:b32c94be6522 | 41 | */ |
mazgch | 8:b32c94be6522 | 42 | #define APN "gprs.swisscom.ch" |
mazgch | 8:b32c94be6522 | 43 | |
mazgch | 8:b32c94be6522 | 44 | /** Set the user name for your APN, or NULL if not needed |
mazgch | 8:b32c94be6522 | 45 | */ |
mazgch | 8:b32c94be6522 | 46 | #define USERNAME NULL |
mazgch | 8:b32c94be6522 | 47 | |
mazgch | 8:b32c94be6522 | 48 | /** Set the password for your APN, or NULL if not needed |
mazgch | 8:b32c94be6522 | 49 | */ |
mazgch | 8:b32c94be6522 | 50 | #define PASSWORD NULL |
mazgch | 8:b32c94be6522 | 51 | |
mazgch | 8:b32c94be6522 | 52 | C027 c027; |
icraggs | 2:638c854c0695 | 53 | |
sam_grove | 5:4a257f6ac09a | 54 | #include "C12832.h" |
mazgch | 8:b32c94be6522 | 55 | C12832 lcd(D11, D13, D12, D7, D10); |
icraggs | 0:0cae29831d01 | 56 | |
icraggs | 2:638c854c0695 | 57 | #include "FP.cpp" |
icraggs | 2:638c854c0695 | 58 | #include "MQTTClient.h" |
icraggs | 2:638c854c0695 | 59 | |
icraggs | 2:638c854c0695 | 60 | int arrivedcount = 0; |
icraggs | 2:638c854c0695 | 61 | |
icraggs | 2:638c854c0695 | 62 | void messageArrived(MQTT::Message* message) |
icraggs | 2:638c854c0695 | 63 | { |
sam_grove | 5:4a257f6ac09a | 64 | lcd.cls(); |
sam_grove | 5:4a257f6ac09a | 65 | lcd.locate(0,3); |
sam_grove | 5:4a257f6ac09a | 66 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message->qos, message->retained, message->dup, message->id); |
sam_grove | 5:4a257f6ac09a | 67 | printf("Payload %.*s\n", message->payloadlen, (char*)message->payload); |
icraggs | 2:638c854c0695 | 68 | ++arrivedcount; |
sam_grove | 5:4a257f6ac09a | 69 | lcd.puts((char*)message->payload); |
icraggs | 2:638c854c0695 | 70 | } |
icraggs | 0:0cae29831d01 | 71 | |
icraggs | 2:638c854c0695 | 72 | int main(int argc, char* argv[]) |
icraggs | 2:638c854c0695 | 73 | { |
mazgch | 10:6f1c3b718b9c | 74 | Serial pc(USBTX,USBRX); |
mazgch | 10:6f1c3b718b9c | 75 | pc.baud(115200); |
mazgch | 10:6f1c3b718b9c | 76 | |
mazgch | 8:b32c94be6522 | 77 | // turn on the supplies of the Modem and the GPS |
mazgch | 8:b32c94be6522 | 78 | c027.mdmPower(true); |
mazgch | 10:6f1c3b718b9c | 79 | printf("Modem Initialize\r\n"); |
mazgch | 8:b32c94be6522 | 80 | MDMSerial mdm; |
mazgch | 10:6f1c3b718b9c | 81 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD, true)) |
mazgch | 10:6f1c3b718b9c | 82 | return -1; |
mazgch | 8:b32c94be6522 | 83 | |
mazgch | 10:6f1c3b718b9c | 84 | IPStack ipstack = IPStack(); |
mazgch | 10:6f1c3b718b9c | 85 | float version = 0.43; |
mazgch | 10:6f1c3b718b9c | 86 | char* topic = "mbed-sample"; |
mazgch | 10:6f1c3b718b9c | 87 | |
mazgch | 10:6f1c3b718b9c | 88 | lcd.printf("Version is %f\n", version); |
mazgch | 10:6f1c3b718b9c | 89 | printf("Version is %f\n", version); |
mazgch | 10:6f1c3b718b9c | 90 | |
mazgch | 10:6f1c3b718b9c | 91 | MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack); |
mazgch | 10:6f1c3b718b9c | 92 | |
mazgch | 10:6f1c3b718b9c | 93 | char* hostname = "m2m.eclipse.org"; |
mazgch | 10:6f1c3b718b9c | 94 | int port = 1883; |
mazgch | 10:6f1c3b718b9c | 95 | lcd.printf("Connecting to %s:%d\n", hostname, port); |
mazgch | 10:6f1c3b718b9c | 96 | int rc = ipstack.connect(hostname, port); |
mazgch | 10:6f1c3b718b9c | 97 | if (rc != 0) |
mazgch | 10:6f1c3b718b9c | 98 | lcd.printf("rc from TCP connect is %d\n", rc); |
mazgch | 10:6f1c3b718b9c | 99 | |
mazgch | 10:6f1c3b718b9c | 100 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
mazgch | 10:6f1c3b718b9c | 101 | data.MQTTVersion = 3; |
mazgch | 10:6f1c3b718b9c | 102 | data.clientID.cstring = "mbed-icraggs"; |
mazgch | 10:6f1c3b718b9c | 103 | rc = client.connect(&data); |
mazgch | 10:6f1c3b718b9c | 104 | if (rc != 0) |
mazgch | 10:6f1c3b718b9c | 105 | lcd.printf("rc from MQTT connect is %d\n", rc); |
mazgch | 10:6f1c3b718b9c | 106 | |
mazgch | 10:6f1c3b718b9c | 107 | rc = client.subscribe(topic, MQTT::QOS1, messageArrived); |
mazgch | 10:6f1c3b718b9c | 108 | if (rc != 0) { |
mazgch | 10:6f1c3b718b9c | 109 | printf("rc from MQTT subscribe is %d\n", rc); |
mazgch | 10:6f1c3b718b9c | 110 | } |
mazgch | 10:6f1c3b718b9c | 111 | |
mazgch | 10:6f1c3b718b9c | 112 | MQTT::Message message; |
mazgch | 10:6f1c3b718b9c | 113 | |
mazgch | 10:6f1c3b718b9c | 114 | // QoS 0 |
mazgch | 10:6f1c3b718b9c | 115 | char buf[100]; |
mazgch | 10:6f1c3b718b9c | 116 | sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version); |
mazgch | 10:6f1c3b718b9c | 117 | message.qos = MQTT::QOS0; |
mazgch | 10:6f1c3b718b9c | 118 | message.retained = false; |
mazgch | 10:6f1c3b718b9c | 119 | message.dup = false; |
mazgch | 10:6f1c3b718b9c | 120 | message.payload = (void*)buf; |
mazgch | 10:6f1c3b718b9c | 121 | message.payloadlen = strlen(buf)+1; |
mazgch | 10:6f1c3b718b9c | 122 | rc = client.publish(topic, &message); |
mazgch | 10:6f1c3b718b9c | 123 | while (arrivedcount == 0) |
mazgch | 10:6f1c3b718b9c | 124 | client.yield(100); |
mazgch | 8:b32c94be6522 | 125 | |
mazgch | 10:6f1c3b718b9c | 126 | // QoS 1 |
mazgch | 10:6f1c3b718b9c | 127 | sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version); |
mazgch | 10:6f1c3b718b9c | 128 | message.qos = MQTT::QOS1; |
mazgch | 10:6f1c3b718b9c | 129 | message.payloadlen = strlen(buf)+1; |
mazgch | 10:6f1c3b718b9c | 130 | rc = client.publish(topic, &message); |
mazgch | 10:6f1c3b718b9c | 131 | while (arrivedcount == 1) |
mazgch | 10:6f1c3b718b9c | 132 | client.yield(100); |
mazgch | 10:6f1c3b718b9c | 133 | |
mazgch | 10:6f1c3b718b9c | 134 | // QoS 2 |
mazgch | 10:6f1c3b718b9c | 135 | sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version); |
mazgch | 10:6f1c3b718b9c | 136 | message.qos = MQTT::QOS2; |
mazgch | 10:6f1c3b718b9c | 137 | message.payloadlen = strlen(buf)+1; |
mazgch | 10:6f1c3b718b9c | 138 | rc = client.publish(topic, &message); |
mazgch | 10:6f1c3b718b9c | 139 | while (arrivedcount == 2) |
mazgch | 10:6f1c3b718b9c | 140 | client.yield(100); |
mazgch | 10:6f1c3b718b9c | 141 | |
mazgch | 10:6f1c3b718b9c | 142 | rc = client.unsubscribe(topic); |
mazgch | 10:6f1c3b718b9c | 143 | if (rc != 0) { |
mazgch | 10:6f1c3b718b9c | 144 | printf("rc from unsubscribe was %d\n", rc); |
mazgch | 10:6f1c3b718b9c | 145 | } |
mazgch | 10:6f1c3b718b9c | 146 | |
mazgch | 10:6f1c3b718b9c | 147 | rc = client.disconnect(); |
mazgch | 10:6f1c3b718b9c | 148 | if (rc != 0) { |
mazgch | 10:6f1c3b718b9c | 149 | printf("rc from disconnect was %d\n", rc); |
mazgch | 10:6f1c3b718b9c | 150 | } |
icraggs | 0:0cae29831d01 | 151 | |
mazgch | 10:6f1c3b718b9c | 152 | mdm.disconnect(); |
mazgch | 8:b32c94be6522 | 153 | mdm.powerOff(); |
mazgch | 8:b32c94be6522 | 154 | c027.mdmPower(false); |
icraggs | 2:638c854c0695 | 155 | |
sam_grove | 5:4a257f6ac09a | 156 | lcd.cls(); |
sam_grove | 5:4a257f6ac09a | 157 | lcd.locate(0,3); |
sam_grove | 5:4a257f6ac09a | 158 | lcd.printf("Finish: %d msgs\n", arrivedcount); |
sam_grove | 5:4a257f6ac09a | 159 | printf("Finishing with %d messages received\n", arrivedcount); |
icraggs | 2:638c854c0695 | 160 | |
mazgch | 8:b32c94be6522 | 161 | while(true); |
icraggs | 0:0cae29831d01 | 162 | } |