Mark Lambe
/
AsavieIoTConnectCloudConnector
Asavie IoT Connect cloud service connector example
main.cpp@0:ceabe0e90767, 2019-05-17 (annotated)
- Committer:
- Markl
- Date:
- Fri May 17 16:38:26 2019 +0000
- Revision:
- 0:ceabe0e90767
Sample MQTT proxy for cellular
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Markl | 0:ceabe0e90767 | 1 | /* |
Markl | 0:ceabe0e90767 | 2 | * @author Asavie - Mark Lambe |
Markl | 0:ceabe0e90767 | 3 | * @version V1.0.0 |
Markl | 0:ceabe0e90767 | 4 | * @date 25 March 2019 |
Markl | 0:ceabe0e90767 | 5 | * @brief Demonstration of Asavie IoT Connect cloud service connector and proxy |
Markl | 0:ceabe0e90767 | 6 | * @overview Securely connect MQTT data to IoT cloud services via Asavie proxy, |
Markl | 0:ceabe0e90767 | 7 | * further information on setup available at https://developer.asavie.com |
Markl | 0:ceabe0e90767 | 8 | * |
Markl | 0:ceabe0e90767 | 9 | ****************************************************************************** |
Markl | 0:ceabe0e90767 | 10 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Markl | 0:ceabe0e90767 | 11 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
Markl | 0:ceabe0e90767 | 12 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Markl | 0:ceabe0e90767 | 13 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
Markl | 0:ceabe0e90767 | 14 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
Markl | 0:ceabe0e90767 | 15 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
Markl | 0:ceabe0e90767 | 16 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
Markl | 0:ceabe0e90767 | 17 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
Markl | 0:ceabe0e90767 | 18 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
Markl | 0:ceabe0e90767 | 19 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Markl | 0:ceabe0e90767 | 20 | * |
Markl | 0:ceabe0e90767 | 21 | ****************************************************************************** |
Markl | 0:ceabe0e90767 | 22 | */ |
Markl | 0:ceabe0e90767 | 23 | |
Markl | 0:ceabe0e90767 | 24 | #include "mbed.h" |
Markl | 0:ceabe0e90767 | 25 | #include "common_functions.h" |
Markl | 0:ceabe0e90767 | 26 | #include "UDPSocket.h" |
Markl | 0:ceabe0e90767 | 27 | #include "CellularLog.h" |
Markl | 0:ceabe0e90767 | 28 | #include "MQTTNetwork.h" |
Markl | 0:ceabe0e90767 | 29 | #include "MQTTmbed.h" |
Markl | 0:ceabe0e90767 | 30 | #include "MQTTClient.h" |
Markl | 0:ceabe0e90767 | 31 | |
Markl | 0:ceabe0e90767 | 32 | |
Markl | 0:ceabe0e90767 | 33 | // MQTT host details |
Markl | 0:ceabe0e90767 | 34 | static const char mqtt_host[] = "mqtt.asavie.network"; |
Markl | 0:ceabe0e90767 | 35 | static int mqtt_port = 1883; |
Markl | 0:ceabe0e90767 | 36 | |
Markl | 0:ceabe0e90767 | 37 | // Define MQTT details |
Markl | 0:ceabe0e90767 | 38 | char mqtt_client[] = "Asavie"; |
Markl | 0:ceabe0e90767 | 39 | char mqtt_topic[] = "Asavie"; |
Markl | 0:ceabe0e90767 | 40 | char mqtt_pub_msg[] = "{\"message\": \"MQTT sent via Asavie IoT Connect\"}"; |
Markl | 0:ceabe0e90767 | 41 | |
Markl | 0:ceabe0e90767 | 42 | // Incoming MQTT message |
Markl | 0:ceabe0e90767 | 43 | int arrivedcount = 0; |
Markl | 0:ceabe0e90767 | 44 | |
Markl | 0:ceabe0e90767 | 45 | |
Markl | 0:ceabe0e90767 | 46 | //TCP test |
Markl | 0:ceabe0e90767 | 47 | #define UDP 0 |
Markl | 0:ceabe0e90767 | 48 | #define TCP 1 |
Markl | 0:ceabe0e90767 | 49 | |
Markl | 0:ceabe0e90767 | 50 | // Number of retries / |
Markl | 0:ceabe0e90767 | 51 | #define RETRY_COUNT 3 |
Markl | 0:ceabe0e90767 | 52 | |
Markl | 0:ceabe0e90767 | 53 | NetworkInterface *iface; |
Markl | 0:ceabe0e90767 | 54 | |
Markl | 0:ceabe0e90767 | 55 | // Echo server hostname |
Markl | 0:ceabe0e90767 | 56 | const char *host_name = MBED_CONF_APP_ECHO_SERVER_HOSTNAME; |
Markl | 0:ceabe0e90767 | 57 | |
Markl | 0:ceabe0e90767 | 58 | // Echo server port (same for TCP and UDP) |
Markl | 0:ceabe0e90767 | 59 | const int port = MBED_CONF_APP_ECHO_SERVER_PORT; |
Markl | 0:ceabe0e90767 | 60 | |
Markl | 0:ceabe0e90767 | 61 | static rtos::Mutex trace_mutex; |
Markl | 0:ceabe0e90767 | 62 | |
Markl | 0:ceabe0e90767 | 63 | #if MBED_CONF_MBED_TRACE_ENABLE |
Markl | 0:ceabe0e90767 | 64 | static void trace_wait() |
Markl | 0:ceabe0e90767 | 65 | { |
Markl | 0:ceabe0e90767 | 66 | trace_mutex.lock(); |
Markl | 0:ceabe0e90767 | 67 | } |
Markl | 0:ceabe0e90767 | 68 | |
Markl | 0:ceabe0e90767 | 69 | static void trace_release() |
Markl | 0:ceabe0e90767 | 70 | { |
Markl | 0:ceabe0e90767 | 71 | trace_mutex.unlock(); |
Markl | 0:ceabe0e90767 | 72 | } |
Markl | 0:ceabe0e90767 | 73 | |
Markl | 0:ceabe0e90767 | 74 | static char time_st[50]; |
Markl | 0:ceabe0e90767 | 75 | |
Markl | 0:ceabe0e90767 | 76 | static char* trace_time(size_t ss) |
Markl | 0:ceabe0e90767 | 77 | { |
Markl | 0:ceabe0e90767 | 78 | snprintf(time_st, 49, "[%08llums]", Kernel::get_ms_count()); |
Markl | 0:ceabe0e90767 | 79 | return time_st; |
Markl | 0:ceabe0e90767 | 80 | } |
Markl | 0:ceabe0e90767 | 81 | |
Markl | 0:ceabe0e90767 | 82 | static void trace_open() |
Markl | 0:ceabe0e90767 | 83 | { |
Markl | 0:ceabe0e90767 | 84 | mbed_trace_init(); |
Markl | 0:ceabe0e90767 | 85 | mbed_trace_prefix_function_set( &trace_time ); |
Markl | 0:ceabe0e90767 | 86 | |
Markl | 0:ceabe0e90767 | 87 | mbed_trace_mutex_wait_function_set(trace_wait); |
Markl | 0:ceabe0e90767 | 88 | mbed_trace_mutex_release_function_set(trace_release); |
Markl | 0:ceabe0e90767 | 89 | |
Markl | 0:ceabe0e90767 | 90 | mbed_cellular_trace::mutex_wait_function_set(trace_wait); |
Markl | 0:ceabe0e90767 | 91 | mbed_cellular_trace::mutex_release_function_set(trace_release); |
Markl | 0:ceabe0e90767 | 92 | } |
Markl | 0:ceabe0e90767 | 93 | |
Markl | 0:ceabe0e90767 | 94 | static void trace_close() |
Markl | 0:ceabe0e90767 | 95 | { |
Markl | 0:ceabe0e90767 | 96 | mbed_cellular_trace::mutex_wait_function_set(NULL); |
Markl | 0:ceabe0e90767 | 97 | mbed_cellular_trace::mutex_release_function_set(NULL); |
Markl | 0:ceabe0e90767 | 98 | |
Markl | 0:ceabe0e90767 | 99 | mbed_trace_free(); |
Markl | 0:ceabe0e90767 | 100 | } |
Markl | 0:ceabe0e90767 | 101 | #endif // #if MBED_CONF_MBED_TRACE_ENABLE |
Markl | 0:ceabe0e90767 | 102 | |
Markl | 0:ceabe0e90767 | 103 | Thread dot_thread(osPriorityNormal, 512); |
Markl | 0:ceabe0e90767 | 104 | |
Markl | 0:ceabe0e90767 | 105 | void print_function(const char *format, ...) |
Markl | 0:ceabe0e90767 | 106 | { |
Markl | 0:ceabe0e90767 | 107 | trace_mutex.lock(); |
Markl | 0:ceabe0e90767 | 108 | va_list arglist; |
Markl | 0:ceabe0e90767 | 109 | va_start( arglist, format ); |
Markl | 0:ceabe0e90767 | 110 | vprintf(format, arglist); |
Markl | 0:ceabe0e90767 | 111 | va_end( arglist ); |
Markl | 0:ceabe0e90767 | 112 | trace_mutex.unlock(); |
Markl | 0:ceabe0e90767 | 113 | } |
Markl | 0:ceabe0e90767 | 114 | |
Markl | 0:ceabe0e90767 | 115 | void dot_event() |
Markl | 0:ceabe0e90767 | 116 | { |
Markl | 0:ceabe0e90767 | 117 | while (true) { |
Markl | 0:ceabe0e90767 | 118 | ThisThread::sleep_for(4000); |
Markl | 0:ceabe0e90767 | 119 | if (iface && iface->get_connection_status() == NSAPI_STATUS_GLOBAL_UP) { |
Markl | 0:ceabe0e90767 | 120 | break; |
Markl | 0:ceabe0e90767 | 121 | } else { |
Markl | 0:ceabe0e90767 | 122 | trace_mutex.lock(); |
Markl | 0:ceabe0e90767 | 123 | printf("."); |
Markl | 0:ceabe0e90767 | 124 | fflush(stdout); |
Markl | 0:ceabe0e90767 | 125 | trace_mutex.unlock(); |
Markl | 0:ceabe0e90767 | 126 | } |
Markl | 0:ceabe0e90767 | 127 | } |
Markl | 0:ceabe0e90767 | 128 | } |
Markl | 0:ceabe0e90767 | 129 | |
Markl | 0:ceabe0e90767 | 130 | /** |
Markl | 0:ceabe0e90767 | 131 | * Connects to the Cellular Network |
Markl | 0:ceabe0e90767 | 132 | */ |
Markl | 0:ceabe0e90767 | 133 | nsapi_error_t do_connect() |
Markl | 0:ceabe0e90767 | 134 | { |
Markl | 0:ceabe0e90767 | 135 | nsapi_error_t retcode = NSAPI_ERROR_OK; |
Markl | 0:ceabe0e90767 | 136 | uint8_t retry_counter = 0; |
Markl | 0:ceabe0e90767 | 137 | |
Markl | 0:ceabe0e90767 | 138 | while (iface->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) { |
Markl | 0:ceabe0e90767 | 139 | retcode = iface->connect(); |
Markl | 0:ceabe0e90767 | 140 | if (retcode == NSAPI_ERROR_AUTH_FAILURE) { |
Markl | 0:ceabe0e90767 | 141 | print_function("\n\nAuthentication Failure. Exiting application\n"); |
Markl | 0:ceabe0e90767 | 142 | } else if (retcode == NSAPI_ERROR_OK) { |
Markl | 0:ceabe0e90767 | 143 | print_function("\n\nConnection Established.\n"); |
Markl | 0:ceabe0e90767 | 144 | } else if (retry_counter > RETRY_COUNT) { |
Markl | 0:ceabe0e90767 | 145 | print_function("\n\nFatal connection failure: %d\n", retcode); |
Markl | 0:ceabe0e90767 | 146 | } else { |
Markl | 0:ceabe0e90767 | 147 | print_function("\n\nCouldn't connect: %d, will retry\n", retcode); |
Markl | 0:ceabe0e90767 | 148 | retry_counter++; |
Markl | 0:ceabe0e90767 | 149 | continue; |
Markl | 0:ceabe0e90767 | 150 | } |
Markl | 0:ceabe0e90767 | 151 | break; |
Markl | 0:ceabe0e90767 | 152 | } |
Markl | 0:ceabe0e90767 | 153 | return retcode; |
Markl | 0:ceabe0e90767 | 154 | } |
Markl | 0:ceabe0e90767 | 155 | |
Markl | 0:ceabe0e90767 | 156 | /** |
Markl | 0:ceabe0e90767 | 157 | * MQTT message receiver |
Markl | 0:ceabe0e90767 | 158 | */ |
Markl | 0:ceabe0e90767 | 159 | |
Markl | 0:ceabe0e90767 | 160 | void messageArrived(MQTT::MessageData& md) |
Markl | 0:ceabe0e90767 | 161 | { |
Markl | 0:ceabe0e90767 | 162 | MQTT::Message &message = md.message; |
Markl | 0:ceabe0e90767 | 163 | print_function("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); |
Markl | 0:ceabe0e90767 | 164 | print_function("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); |
Markl | 0:ceabe0e90767 | 165 | ++arrivedcount; |
Markl | 0:ceabe0e90767 | 166 | } |
Markl | 0:ceabe0e90767 | 167 | |
Markl | 0:ceabe0e90767 | 168 | |
Markl | 0:ceabe0e90767 | 169 | int mqtt_send_receive() { |
Markl | 0:ceabe0e90767 | 170 | |
Markl | 0:ceabe0e90767 | 171 | MQTTNetwork mqttNetwork(iface); |
Markl | 0:ceabe0e90767 | 172 | |
Markl | 0:ceabe0e90767 | 173 | MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); |
Markl | 0:ceabe0e90767 | 174 | |
Markl | 0:ceabe0e90767 | 175 | print_function("Connecting to %s:%d\r\n", mqtt_host, mqtt_port); |
Markl | 0:ceabe0e90767 | 176 | int rc = mqttNetwork.connect(mqtt_host, mqtt_port); |
Markl | 0:ceabe0e90767 | 177 | if (rc != 0) |
Markl | 0:ceabe0e90767 | 178 | print_function("rc from TCP connect is %d\r\n", rc); |
Markl | 0:ceabe0e90767 | 179 | |
Markl | 0:ceabe0e90767 | 180 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
Markl | 0:ceabe0e90767 | 181 | data.MQTTVersion = 3; |
Markl | 0:ceabe0e90767 | 182 | data.clientID.cstring = mqtt_client; |
Markl | 0:ceabe0e90767 | 183 | |
Markl | 0:ceabe0e90767 | 184 | if ((rc = client.connect(data)) != 0) |
Markl | 0:ceabe0e90767 | 185 | print_function("rc from MQTT connect is %d\r\n", rc); |
Markl | 0:ceabe0e90767 | 186 | |
Markl | 0:ceabe0e90767 | 187 | if ((rc = client.subscribe(mqtt_topic, MQTT::QOS0, messageArrived)) != 0) |
Markl | 0:ceabe0e90767 | 188 | print_function("rc from MQTT subscribe is %d\r\n", rc); |
Markl | 0:ceabe0e90767 | 189 | |
Markl | 0:ceabe0e90767 | 190 | MQTT::Message message; |
Markl | 0:ceabe0e90767 | 191 | |
Markl | 0:ceabe0e90767 | 192 | // QoS 0 |
Markl | 0:ceabe0e90767 | 193 | char buf[100]; |
Markl | 0:ceabe0e90767 | 194 | sprintf(buf, "Hello World! QoS 0 message from Asavie \r\n"); |
Markl | 0:ceabe0e90767 | 195 | message.qos = MQTT::QOS0; |
Markl | 0:ceabe0e90767 | 196 | message.retained = false; |
Markl | 0:ceabe0e90767 | 197 | message.dup = false; |
Markl | 0:ceabe0e90767 | 198 | message.payload = (void*)buf; |
Markl | 0:ceabe0e90767 | 199 | message.payloadlen = strlen(buf)+1; |
Markl | 0:ceabe0e90767 | 200 | rc = client.publish(mqtt_topic, message); |
Markl | 0:ceabe0e90767 | 201 | while (arrivedcount < 1) |
Markl | 0:ceabe0e90767 | 202 | client.yield(100); |
Markl | 0:ceabe0e90767 | 203 | |
Markl | 0:ceabe0e90767 | 204 | // QoS 1 |
Markl | 0:ceabe0e90767 | 205 | sprintf(buf, "Hello World! QoS 1 message from Asavie \r\n"); |
Markl | 0:ceabe0e90767 | 206 | message.qos = MQTT::QOS1; |
Markl | 0:ceabe0e90767 | 207 | message.payloadlen = strlen(buf)+1; |
Markl | 0:ceabe0e90767 | 208 | rc = client.publish(mqtt_topic, message); |
Markl | 0:ceabe0e90767 | 209 | while (arrivedcount < 2) |
Markl | 0:ceabe0e90767 | 210 | client.yield(100); |
Markl | 0:ceabe0e90767 | 211 | |
Markl | 0:ceabe0e90767 | 212 | // QoS 2 |
Markl | 0:ceabe0e90767 | 213 | sprintf(buf, "Hello World! QoS 2 message from Asavie \r\n"); |
Markl | 0:ceabe0e90767 | 214 | message.qos = MQTT::QOS2; |
Markl | 0:ceabe0e90767 | 215 | message.payloadlen = strlen(buf)+1; |
Markl | 0:ceabe0e90767 | 216 | rc = client.publish(mqtt_topic, message); |
Markl | 0:ceabe0e90767 | 217 | while (arrivedcount < 3) |
Markl | 0:ceabe0e90767 | 218 | client.yield(100); |
Markl | 0:ceabe0e90767 | 219 | |
Markl | 0:ceabe0e90767 | 220 | if ((rc = client.unsubscribe(mqtt_topic)) != 0) |
Markl | 0:ceabe0e90767 | 221 | print_function("rc from unsubscribe was %d\r\n", rc); |
Markl | 0:ceabe0e90767 | 222 | |
Markl | 0:ceabe0e90767 | 223 | if ((rc = client.disconnect()) != 0) |
Markl | 0:ceabe0e90767 | 224 | print_function("rc from disconnect was %d\r\n", rc); |
Markl | 0:ceabe0e90767 | 225 | |
Markl | 0:ceabe0e90767 | 226 | mqttNetwork.disconnect(); |
Markl | 0:ceabe0e90767 | 227 | |
Markl | 0:ceabe0e90767 | 228 | print_function("MQTT finished, received %d msgs\r\n", arrivedcount); |
Markl | 0:ceabe0e90767 | 229 | |
Markl | 0:ceabe0e90767 | 230 | return 0; |
Markl | 0:ceabe0e90767 | 231 | } |
Markl | 0:ceabe0e90767 | 232 | |
Markl | 0:ceabe0e90767 | 233 | |
Markl | 0:ceabe0e90767 | 234 | |
Markl | 0:ceabe0e90767 | 235 | /** |
Markl | 0:ceabe0e90767 | 236 | * Opens a UDP or a TCP socket with the given echo server and performs an echo |
Markl | 0:ceabe0e90767 | 237 | * transaction retrieving current. |
Markl | 0:ceabe0e90767 | 238 | */ |
Markl | 0:ceabe0e90767 | 239 | nsapi_error_t test_send_recv() |
Markl | 0:ceabe0e90767 | 240 | { |
Markl | 0:ceabe0e90767 | 241 | nsapi_size_or_error_t retcode; |
Markl | 0:ceabe0e90767 | 242 | #if MBED_CONF_APP_SOCK_TYPE == TCP |
Markl | 0:ceabe0e90767 | 243 | TCPSocket sock; |
Markl | 0:ceabe0e90767 | 244 | #else |
Markl | 0:ceabe0e90767 | 245 | UDPSocket sock; |
Markl | 0:ceabe0e90767 | 246 | #endif |
Markl | 0:ceabe0e90767 | 247 | |
Markl | 0:ceabe0e90767 | 248 | retcode = sock.open(iface); |
Markl | 0:ceabe0e90767 | 249 | if (retcode != NSAPI_ERROR_OK) { |
Markl | 0:ceabe0e90767 | 250 | #if MBED_CONF_APP_SOCK_TYPE == TCP |
Markl | 0:ceabe0e90767 | 251 | print_function("TCPSocket.open() fails, code: %d\n", retcode); |
Markl | 0:ceabe0e90767 | 252 | #else |
Markl | 0:ceabe0e90767 | 253 | print_function("UDPSocket.open() fails, code: %d\n", retcode); |
Markl | 0:ceabe0e90767 | 254 | #endif |
Markl | 0:ceabe0e90767 | 255 | return -1; |
Markl | 0:ceabe0e90767 | 256 | } |
Markl | 0:ceabe0e90767 | 257 | |
Markl | 0:ceabe0e90767 | 258 | SocketAddress sock_addr; |
Markl | 0:ceabe0e90767 | 259 | retcode = iface->gethostbyname(host_name, &sock_addr); |
Markl | 0:ceabe0e90767 | 260 | if (retcode != NSAPI_ERROR_OK) { |
Markl | 0:ceabe0e90767 | 261 | print_function("Couldn't resolve remote host: %s, code: %d\n", host_name, retcode); |
Markl | 0:ceabe0e90767 | 262 | return -1; |
Markl | 0:ceabe0e90767 | 263 | } |
Markl | 0:ceabe0e90767 | 264 | |
Markl | 0:ceabe0e90767 | 265 | sock_addr.set_port(port); |
Markl | 0:ceabe0e90767 | 266 | |
Markl | 0:ceabe0e90767 | 267 | sock.set_timeout(15000); |
Markl | 0:ceabe0e90767 | 268 | int n = 0; |
Markl | 0:ceabe0e90767 | 269 | const char *echo_string = "TEST"; |
Markl | 0:ceabe0e90767 | 270 | char recv_buf[4]; |
Markl | 0:ceabe0e90767 | 271 | #if MBED_CONF_APP_SOCK_TYPE == TCP |
Markl | 0:ceabe0e90767 | 272 | retcode = sock.connect(sock_addr); |
Markl | 0:ceabe0e90767 | 273 | if (retcode < 0) { |
Markl | 0:ceabe0e90767 | 274 | print_function("TCPSocket.connect() fails, code: %d\n", retcode); |
Markl | 0:ceabe0e90767 | 275 | return -1; |
Markl | 0:ceabe0e90767 | 276 | } else { |
Markl | 0:ceabe0e90767 | 277 | print_function("TCP: connected with %s server\n", host_name); |
Markl | 0:ceabe0e90767 | 278 | } |
Markl | 0:ceabe0e90767 | 279 | retcode = sock.send((void*) echo_string, sizeof(echo_string)); |
Markl | 0:ceabe0e90767 | 280 | if (retcode < 0) { |
Markl | 0:ceabe0e90767 | 281 | print_function("TCPSocket.send() fails, code: %d\n", retcode); |
Markl | 0:ceabe0e90767 | 282 | return -1; |
Markl | 0:ceabe0e90767 | 283 | } else { |
Markl | 0:ceabe0e90767 | 284 | print_function("TCP: Sent %d Bytes to %s\n", retcode, host_name); |
Markl | 0:ceabe0e90767 | 285 | } |
Markl | 0:ceabe0e90767 | 286 | |
Markl | 0:ceabe0e90767 | 287 | n = sock.recv((void*) recv_buf, sizeof(recv_buf)); |
Markl | 0:ceabe0e90767 | 288 | #else |
Markl | 0:ceabe0e90767 | 289 | |
Markl | 0:ceabe0e90767 | 290 | retcode = sock.sendto(sock_addr, (void*) echo_string, sizeof(echo_string)); |
Markl | 0:ceabe0e90767 | 291 | if (retcode < 0) { |
Markl | 0:ceabe0e90767 | 292 | print_function("UDPSocket.sendto() fails, code: %d\n", retcode); |
Markl | 0:ceabe0e90767 | 293 | return -1; |
Markl | 0:ceabe0e90767 | 294 | } else { |
Markl | 0:ceabe0e90767 | 295 | print_function("UDP: Sent %d Bytes to %s\n", retcode, host_name); |
Markl | 0:ceabe0e90767 | 296 | } |
Markl | 0:ceabe0e90767 | 297 | |
Markl | 0:ceabe0e90767 | 298 | n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf)); |
Markl | 0:ceabe0e90767 | 299 | #endif |
Markl | 0:ceabe0e90767 | 300 | |
Markl | 0:ceabe0e90767 | 301 | sock.close(); |
Markl | 0:ceabe0e90767 | 302 | |
Markl | 0:ceabe0e90767 | 303 | if (n > 0) { |
Markl | 0:ceabe0e90767 | 304 | print_function("Received from echo server %d Bytes\n", n); |
Markl | 0:ceabe0e90767 | 305 | return 0; |
Markl | 0:ceabe0e90767 | 306 | } |
Markl | 0:ceabe0e90767 | 307 | |
Markl | 0:ceabe0e90767 | 308 | return -1; |
Markl | 0:ceabe0e90767 | 309 | } |
Markl | 0:ceabe0e90767 | 310 | |
Markl | 0:ceabe0e90767 | 311 | int main() |
Markl | 0:ceabe0e90767 | 312 | { |
Markl | 0:ceabe0e90767 | 313 | |
Markl | 0:ceabe0e90767 | 314 | print_function("\n\nAsavie IoT Connect cloud service connector example\n"); |
Markl | 0:ceabe0e90767 | 315 | print_function("\n\nBuilt: %s, %s\n", __DATE__, __TIME__); |
Markl | 0:ceabe0e90767 | 316 | #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN |
Markl | 0:ceabe0e90767 | 317 | print_function("\n\n[MAIN], plmn: %s\n", MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN); |
Markl | 0:ceabe0e90767 | 318 | #endif |
Markl | 0:ceabe0e90767 | 319 | |
Markl | 0:ceabe0e90767 | 320 | print_function("Establishing connection\n"); |
Markl | 0:ceabe0e90767 | 321 | #if MBED_CONF_MBED_TRACE_ENABLE |
Markl | 0:ceabe0e90767 | 322 | trace_open(); |
Markl | 0:ceabe0e90767 | 323 | #else |
Markl | 0:ceabe0e90767 | 324 | dot_thread.start(dot_event); |
Markl | 0:ceabe0e90767 | 325 | #endif // #if MBED_CONF_MBED_TRACE_ENABLE |
Markl | 0:ceabe0e90767 | 326 | |
Markl | 0:ceabe0e90767 | 327 | // sim pin, apn, credentials and possible plmn are taken atuomtically from json when using get_default_instance() |
Markl | 0:ceabe0e90767 | 328 | iface = NetworkInterface::get_default_instance(); |
Markl | 0:ceabe0e90767 | 329 | MBED_ASSERT(iface); |
Markl | 0:ceabe0e90767 | 330 | |
Markl | 0:ceabe0e90767 | 331 | nsapi_error_t retcode = NSAPI_ERROR_NO_CONNECTION; |
Markl | 0:ceabe0e90767 | 332 | |
Markl | 0:ceabe0e90767 | 333 | /* Attempt to connect to a cellular network */ |
Markl | 0:ceabe0e90767 | 334 | while (do_connect() == NSAPI_ERROR_OK) { |
Markl | 0:ceabe0e90767 | 335 | // Validate TCP session rules in Asavie IoT Connect |
Markl | 0:ceabe0e90767 | 336 | retcode = test_send_recv(); |
Markl | 0:ceabe0e90767 | 337 | // Validate Asavie IoT Connect cloud service connector |
Markl | 0:ceabe0e90767 | 338 | retcode = mqtt_send_receive(); |
Markl | 0:ceabe0e90767 | 339 | |
Markl | 0:ceabe0e90767 | 340 | } |
Markl | 0:ceabe0e90767 | 341 | |
Markl | 0:ceabe0e90767 | 342 | |
Markl | 0:ceabe0e90767 | 343 | if (iface->disconnect() != NSAPI_ERROR_OK) { |
Markl | 0:ceabe0e90767 | 344 | print_function("\n\n disconnect failed.\n\n"); |
Markl | 0:ceabe0e90767 | 345 | } |
Markl | 0:ceabe0e90767 | 346 | |
Markl | 0:ceabe0e90767 | 347 | if (retcode == NSAPI_ERROR_OK) { |
Markl | 0:ceabe0e90767 | 348 | print_function("\n\nSuccess. Exiting \n\n"); |
Markl | 0:ceabe0e90767 | 349 | } else { |
Markl | 0:ceabe0e90767 | 350 | print_function("\n\nFailure. Exiting \n\n"); |
Markl | 0:ceabe0e90767 | 351 | } |
Markl | 0:ceabe0e90767 | 352 | |
Markl | 0:ceabe0e90767 | 353 | while(do_connect() == NSAPI_ERROR_OK); |
Markl | 0:ceabe0e90767 | 354 | #if MBED_CONF_MBED_TRACE_ENABLE |
Markl | 0:ceabe0e90767 | 355 | trace_close(); |
Markl | 0:ceabe0e90767 | 356 | #else |
Markl | 0:ceabe0e90767 | 357 | dot_thread.terminate(); |
Markl | 0:ceabe0e90767 | 358 | #endif // #if MBED_CONF_MBED_TRACE_ENABLE |
Markl | 0:ceabe0e90767 | 359 | |
Markl | 0:ceabe0e90767 | 360 | return 0; |
Markl | 0:ceabe0e90767 | 361 | } |