Example project to publish messages to a MQTT-SN broker using the u-blox SARA-N200 NB-IoT modem
Dependencies: MQTTSNPacket X-NUCLEO-SARA-N200
main.cpp@6:16be6bf59d30, 2018-08-21 (annotated)
- Committer:
- KeystoneElectronic
- Date:
- Tue Aug 21 16:37:21 2018 +0200
- Revision:
- 6:16be6bf59d30
- Parent:
- 5:749a8fb7f27e
- Child:
- 7:ab32680de141
Move udp_interface to SARA-N200 library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 1 | #include <stdio.h> |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 2 | #include <Serial.h> |
KeystoneElectronic | 0:c0cf12ee5420 | 3 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 4 | #include "mbed.h" |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 5 | #include "sara_n2.h" |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 6 | #include "MQTTmbed.h" |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 7 | #include "MQTTSNUDP.h" |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 8 | #include "MQTTSNClient.h" |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 9 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 10 | Serial pc(SERIAL_TX, SERIAL_RX, 115200); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 11 | DigitalOut myled(LED1); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 12 | InterruptIn button(USER_BUTTON); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 13 | UARTSerial sara(PA_9, PA_10, 9600); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 14 | DigitalOut sara_reset(D7); |
KeystoneElectronic | 0:c0cf12ee5420 | 15 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 16 | //#define INFO_TRACE(_class, _string, ...) printf( "%8s: " _string, _class, ##__VA_ARGS__) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 17 | #undef TRACE |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 18 | #define TRACE(_string, ...) INFO_TRACE( "MQTT", _string, ##__VA_ARGS__ ) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 19 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 20 | float led_flash_rate = 0.5; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 21 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 22 | void messageArrived(MQTTSN::MessageData& md) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 23 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 24 | MQTTSN::Message &message = md.message; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 25 | TRACE("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 26 | message.qos, |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 27 | message.retained, |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 28 | message.dup, |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 29 | message.id); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 30 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 31 | TRACE("Payload %s\n", message.payload); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 32 | char *ptr; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 33 | float new_rate = strtof((const char*)message.payload, &ptr); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 34 | if(new_rate > 0) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 35 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 36 | TRACE("New rate %f\n", new_rate); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 37 | led_flash_rate = new_rate; |
KeystoneElectronic | 0:c0cf12ee5420 | 38 | } |
KeystoneElectronic | 0:c0cf12ee5420 | 39 | } |
KeystoneElectronic | 0:c0cf12ee5420 | 40 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 41 | enum eMQTTstates |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 42 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 43 | MQTT_SOCKET_DISCONNECTED, |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 44 | MQTT_SOCKET_CONNECTED, |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 45 | MQTT_CLIENT_CONNECTED, |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 46 | MQTT_CLIENT_PUBLISH, |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 47 | MQTT_CLIENT_SUBSCRIBED |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 48 | }mMQTTstate; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 49 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 50 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 51 | SARA_N2 sara_mdm(&sara, &sara_reset); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 52 | MQTTSNUDP ipsocket = MQTTSNUDP(&sara_mdm); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 53 | MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 54 | MQTTSN_topicid topicid; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 55 | MQTTSN::QoS grantedQoS; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 56 | char topicString[64]; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 57 | char publishMessage[100]; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 58 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 59 | bool buttonPressed = false; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 60 | |
KeystoneElectronic | 5:749a8fb7f27e | 61 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 62 | void mqtt_fsm(MQTTSN::Client<MQTTSNUDP, Countdown> *client) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 63 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 64 | static int pub_count = 0; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 65 | switch(mMQTTstate) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 66 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 67 | case MQTT_SOCKET_DISCONNECTED: |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 68 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 69 | const char* hostname = MBED_CONF_APP_MQTT_SN_HOST; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 70 | int port = MBED_CONF_APP_MQTT_SN_PORT; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 71 | TRACE("Connecting to %s:%d\n", hostname, port); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 72 | int socket = ipsocket.connect((char*)hostname, port); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 73 | if(socket >= 0) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 74 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 75 | TRACE("Socket connected - %d\n", socket); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 76 | mMQTTstate = MQTT_SOCKET_CONNECTED; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 77 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 78 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 79 | break; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 80 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 81 | case MQTT_SOCKET_CONNECTED: |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 82 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 83 | data.clientID.cstring = (char*)"NUCLEO_NB-IoT"; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 84 | data.duration = 100; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 85 | int rc = client->connect(data); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 86 | TRACE("Connect - %d\n", rc); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 87 | if(rc == MQTTSN::SUCCESS) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 88 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 89 | printf("Client Connected\n"); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 90 | mMQTTstate = MQTT_CLIENT_CONNECTED; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 91 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 92 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 93 | break; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 94 | case MQTT_CLIENT_CONNECTED: |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 95 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 96 | topicid.type = MQTTSN_TOPIC_TYPE_NORMAL; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 97 | topicid.data.long_.name = topicString; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 98 | topicid.data.long_.len = strlen(topicString); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 99 | int rc = client->subscribe(topicid, MQTTSN::QOS1, grantedQoS, messageArrived); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 100 | TRACE("Subscribe - %d\n", rc); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 101 | if(rc == MQTTSN::SUCCESS) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 102 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 103 | printf("Client Subscribed\n"); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 104 | mMQTTstate = MQTT_CLIENT_PUBLISH; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 105 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 106 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 107 | break; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 108 | case MQTT_CLIENT_PUBLISH: |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 109 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 110 | pub_count = 0; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 111 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 112 | MQTTSN::Message message; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 113 | message.qos = MQTTSN::QOS0; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 114 | message.retained = false; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 115 | message.dup = false; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 116 | message.payload = (void*)publishMessage; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 117 | message.payloadlen = strlen(publishMessage)+1; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 118 | int rc = client->publish(topicid, message); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 119 | if(rc == MQTTSN::SUCCESS) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 120 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 121 | TRACE("Publishes %s\n", publishMessage); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 122 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 123 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 124 | mMQTTstate = MQTT_CLIENT_SUBSCRIBED; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 125 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 126 | break; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 127 | case MQTT_CLIENT_SUBSCRIBED: |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 128 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 129 | client->yield(100); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 130 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 131 | if(buttonPressed) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 132 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 133 | buttonPressed = false; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 134 | mMQTTstate = MQTT_CLIENT_PUBLISH; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 135 | sprintf(publishMessage, "Button pressed"); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 136 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 137 | |
KeystoneElectronic |
6:16be6bf59d30 | 138 | if(pub_count++ > 100) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 139 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 140 | mMQTTstate = MQTT_CLIENT_PUBLISH; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 141 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 142 | static int aliveCount = 0; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 143 | sprintf(publishMessage, "I am alive %d", aliveCount++); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 144 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 145 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 146 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 147 | break; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 148 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 149 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 150 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 151 | void pressedEvent() |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 152 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 153 | buttonPressed = true; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 154 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 155 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 156 | int main(void) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 157 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 158 | printf("\n\nNUCLEO_X_SARA-N200 HelloMQTT-SN\n"); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 159 | printf("Version: 0x%08X\n", MBED_CONF_APP_VERSION); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 160 | printf("mbed-os: %d.%d.%d\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 161 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 162 | strcpy(topicString, MBED_CONF_APP_TOPIC); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 163 | sprintf(publishMessage, "NUCLEO NB-IoT online"); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 164 | MQTTSN::Client<MQTTSNUDP, Countdown> client = MQTTSN::Client<MQTTSNUDP, Countdown>(ipsocket); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 165 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 166 | button.fall(&pressedEvent); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 167 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 168 | while(1) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 169 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 170 | wait(led_flash_rate); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 171 | myled = 1; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 172 | wait(led_flash_rate); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 173 | myled = 0; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 174 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 175 | sara_mdm.fsm(); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 176 | mqtt_fsm(&client); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 177 | } |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 178 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 179 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 180 | return 0; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 181 | } |