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@1:70b751b7a189, 2018-08-20 (annotated)
- Committer:
- keystone.electronic.solutions@gmail.com
- Date:
- Mon Aug 20 16:42:45 2018 +0200
- Revision:
- 1:70b751b7a189
- Parent:
- 0:c0cf12ee5420
- Child:
- 4:df0aab6b9120
Add example files
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 | #include "mbed_config.h" |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 10 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 11 | Serial pc(SERIAL_TX, SERIAL_RX, 115200); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 12 | DigitalOut myled(LED1); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 13 | InterruptIn button(USER_BUTTON); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 14 | UARTSerial sara(PA_9, PA_10, 9600); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 15 | DigitalOut sara_reset(D7); |
KeystoneElectronic | 0:c0cf12ee5420 | 16 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 17 | //#define INFO_TRACE(_class, _string, ...) printf( "%8s: " _string, _class, ##__VA_ARGS__) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 18 | #undef TRACE |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 19 | #define TRACE(_string, ...) INFO_TRACE( "MQTT", _string, ##__VA_ARGS__ ) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 20 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 21 | float led_flash_rate = 0.5; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 22 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 23 | void messageArrived(MQTTSN::MessageData& md) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 24 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 25 | MQTTSN::Message &message = md.message; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 26 | TRACE("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 27 | message.qos, |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 28 | message.retained, |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 29 | message.dup, |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 30 | message.id); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 31 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 32 | TRACE("Payload %s\n", message.payload); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 33 | char *ptr; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 34 | float new_rate = strtof((const char*)message.payload, &ptr); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 35 | if(new_rate > 0) |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 36 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 37 | TRACE("New rate %f\n", new_rate); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 38 | led_flash_rate = new_rate; |
KeystoneElectronic | 0:c0cf12ee5420 | 39 | } |
KeystoneElectronic | 0:c0cf12ee5420 | 40 | } |
KeystoneElectronic | 0:c0cf12ee5420 | 41 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 42 | enum eMQTTstates |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 43 | { |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 44 | MQTT_SOCKET_DISCONNECTED, |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 45 | MQTT_SOCKET_CONNECTED, |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 46 | MQTT_CLIENT_CONNECTED, |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 47 | MQTT_CLIENT_PUBLISH, |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 48 | MQTT_CLIENT_SUBSCRIBED |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 49 | }mMQTTstate; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 50 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 51 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 52 | SARA_N2 sara_mdm(&sara, &sara_reset); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 53 | MQTTSNUDP ipsocket = MQTTSNUDP(&sara_mdm); |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 54 | MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 55 | MQTTSN_topicid topicid; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 56 | MQTTSN::QoS grantedQoS; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 57 | char topicString[64]; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 58 | char publishMessage[100]; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 59 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 60 | bool buttonPressed = false; |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 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 | |
keystone.electronic.solutions@gmail.com | 1:70b751b7a189 | 138 | if(pub_count++ > 50) |
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 | } |