v1
Dependencies: MQTTSN mbed-http
network-helper.h@15:557d0008dd2d, 2019-03-11 (annotated)
- Committer:
- m_ahsan
- Date:
- Mon Mar 11 07:51:06 2019 +0000
- Revision:
- 15:557d0008dd2d
123
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
m_ahsan | 15:557d0008dd2d | 1 | #ifndef _MBED_HTTP_EXAMPLE_H_ |
m_ahsan | 15:557d0008dd2d | 2 | #define _MBED_HTTP_EXAMPLE_H_ |
m_ahsan | 15:557d0008dd2d | 3 | |
m_ahsan | 15:557d0008dd2d | 4 | #include "mbed.h" |
m_ahsan | 15:557d0008dd2d | 5 | #include "NetworkInterface.h" |
m_ahsan | 15:557d0008dd2d | 6 | |
m_ahsan | 15:557d0008dd2d | 7 | /** |
m_ahsan | 15:557d0008dd2d | 8 | * Connect to the network using the default networking interface, |
m_ahsan | 15:557d0008dd2d | 9 | * you can also swap this out with a driver for a different networking interface |
m_ahsan | 15:557d0008dd2d | 10 | * if you use WiFi: see mbed_app.json for the credentials |
m_ahsan | 15:557d0008dd2d | 11 | */ |
m_ahsan | 15:557d0008dd2d | 12 | NetworkInterface *connect_to_default_network_interface() { |
m_ahsan | 15:557d0008dd2d | 13 | printf("[NWKH] Connecting to network...\n"); |
m_ahsan | 15:557d0008dd2d | 14 | |
m_ahsan | 15:557d0008dd2d | 15 | NetworkInterface* network = NetworkInterface::get_default_instance(); |
m_ahsan | 15:557d0008dd2d | 16 | |
m_ahsan | 15:557d0008dd2d | 17 | if (!network) { |
m_ahsan | 15:557d0008dd2d | 18 | printf("[NWKH] No network interface found, select an interface in 'network-helper.h'\n"); |
m_ahsan | 15:557d0008dd2d | 19 | return NULL; |
m_ahsan | 15:557d0008dd2d | 20 | } |
m_ahsan | 15:557d0008dd2d | 21 | |
m_ahsan | 15:557d0008dd2d | 22 | nsapi_error_t connect_status = network->connect(); |
m_ahsan | 15:557d0008dd2d | 23 | |
m_ahsan | 15:557d0008dd2d | 24 | if (connect_status != NSAPI_ERROR_OK) { |
m_ahsan | 15:557d0008dd2d | 25 | printf("[NWKH] Failed to connect to network (%d)\n", connect_status); |
m_ahsan | 15:557d0008dd2d | 26 | return NULL; |
m_ahsan | 15:557d0008dd2d | 27 | } |
m_ahsan | 15:557d0008dd2d | 28 | |
m_ahsan | 15:557d0008dd2d | 29 | printf("[NWKH] Connected to the network\n"); |
m_ahsan | 15:557d0008dd2d | 30 | printf("[NWKH] IP address: %s\n", network->get_ip_address()); |
m_ahsan | 15:557d0008dd2d | 31 | return network; |
m_ahsan | 15:557d0008dd2d | 32 | } |
m_ahsan | 15:557d0008dd2d | 33 | |
m_ahsan | 15:557d0008dd2d | 34 | #endif // _MBED_HTTP_EXAMPLE_H_ |