DISCO-STM32L475VG-IOT01A WiFi Sensor Thingspeak
Dependencies: LPS22HB LSM6DSL HTS221 LIS3MDL VL53L0X
main.cpp@57:c8c960b825f0, 2018-02-27 (annotated)
- Committer:
- adustm
- Date:
- Tue Feb 27 10:59:23 2018 +0100
- Revision:
- 57:c8c960b825f0
- Parent:
- 55:23a0d1e8270a
- Child:
- 58:8d4bde75ebb9
Remove ESP8266 from this example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:857719181846 | 1 | /* WiFi Example |
adustm | 57:c8c960b825f0 | 2 | * Copyright (c) 2018 ARM Limited |
mbed_official | 0:857719181846 | 3 | * |
mbed_official | 0:857719181846 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 0:857719181846 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 0:857719181846 | 6 | * You may obtain a copy of the License at |
mbed_official | 0:857719181846 | 7 | * |
mbed_official | 0:857719181846 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 0:857719181846 | 9 | * |
mbed_official | 0:857719181846 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 0:857719181846 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 0:857719181846 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 0:857719181846 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 0:857719181846 | 14 | * limitations under the License. |
mbed_official | 0:857719181846 | 15 | */ |
mbed_official | 0:857719181846 | 16 | |
mbed_official | 0:857719181846 | 17 | #include "mbed.h" |
mbed_official | 0:857719181846 | 18 | #include "TCPSocket.h" |
mbed_official | 0:857719181846 | 19 | |
mbed_official | 44:63be19b7a3db | 20 | #define WIFI_IDW0XX1 2 |
mbed_official | 32:bca3f5f442b3 | 21 | |
adustm | 57:c8c960b825f0 | 22 | #if defined(TARGET_DISCO_L475VG_IOT01A) |
adustm adustm@gmail.com | 55:23a0d1e8270a | 23 | #include "ISM43362Interface.h" |
adustm adustm@gmail.com | 55:23a0d1e8270a | 24 | ISM43362Interface wifi(MBED_CONF_APP_WIFI_SPI_MOSI, MBED_CONF_APP_WIFI_SPI_MISO, MBED_CONF_APP_WIFI_SPI_SCLK, MBED_CONF_APP_WIFI_SPI_NSS, MBED_CONF_APP_WIFI_RESET, MBED_CONF_APP_WIFI_DATAREADY, MBED_CONF_APP_WIFI_WAKEUP, false); |
adustm adustm@gmail.com | 55:23a0d1e8270a | 25 | |
mbed_official | 33:12f0df4d51d7 | 26 | #else // External WiFi modules |
mbed_official | 21:b2f2f6a840b4 | 27 | |
adustm | 57:c8c960b825f0 | 28 | #if MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1 |
mbed_official | 32:bca3f5f442b3 | 29 | #include "SpwfSAInterface.h" |
mbed_official | 32:bca3f5f442b3 | 30 | SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
mbed_official | 44:63be19b7a3db | 31 | #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1 |
mbed_official | 10:5b5beb106156 | 32 | |
mbed_official | 33:12f0df4d51d7 | 33 | #endif |
mbed_official | 0:857719181846 | 34 | |
mbed_official | 0:857719181846 | 35 | const char *sec2str(nsapi_security_t sec) |
mbed_official | 0:857719181846 | 36 | { |
mbed_official | 0:857719181846 | 37 | switch (sec) { |
mbed_official | 0:857719181846 | 38 | case NSAPI_SECURITY_NONE: |
mbed_official | 0:857719181846 | 39 | return "None"; |
mbed_official | 0:857719181846 | 40 | case NSAPI_SECURITY_WEP: |
mbed_official | 0:857719181846 | 41 | return "WEP"; |
mbed_official | 0:857719181846 | 42 | case NSAPI_SECURITY_WPA: |
mbed_official | 0:857719181846 | 43 | return "WPA"; |
mbed_official | 0:857719181846 | 44 | case NSAPI_SECURITY_WPA2: |
mbed_official | 0:857719181846 | 45 | return "WPA2"; |
mbed_official | 0:857719181846 | 46 | case NSAPI_SECURITY_WPA_WPA2: |
mbed_official | 0:857719181846 | 47 | return "WPA/WPA2"; |
mbed_official | 0:857719181846 | 48 | case NSAPI_SECURITY_UNKNOWN: |
mbed_official | 0:857719181846 | 49 | default: |
mbed_official | 0:857719181846 | 50 | return "Unknown"; |
mbed_official | 0:857719181846 | 51 | } |
mbed_official | 0:857719181846 | 52 | } |
mbed_official | 0:857719181846 | 53 | |
mbed_official | 37:3a31525e2971 | 54 | int scan_demo(WiFiInterface *wifi) |
mbed_official | 0:857719181846 | 55 | { |
mbed_official | 0:857719181846 | 56 | WiFiAccessPoint *ap; |
mbed_official | 0:857719181846 | 57 | |
mbed_official | 35:052c1ba06ce7 | 58 | printf("Scan:\n"); |
mbed_official | 0:857719181846 | 59 | |
mbed_official | 0:857719181846 | 60 | int count = wifi->scan(NULL,0); |
adustm adustm@gmail.com | 55:23a0d1e8270a | 61 | printf("%d networks available.\n", count); |
mbed_official | 0:857719181846 | 62 | |
mbed_official | 0:857719181846 | 63 | /* Limit number of network arbitrary to 15 */ |
mbed_official | 0:857719181846 | 64 | count = count < 15 ? count : 15; |
mbed_official | 0:857719181846 | 65 | |
mbed_official | 0:857719181846 | 66 | ap = new WiFiAccessPoint[count]; |
mbed_official | 0:857719181846 | 67 | count = wifi->scan(ap, count); |
mbed_official | 0:857719181846 | 68 | for (int i = 0; i < count; i++) |
mbed_official | 0:857719181846 | 69 | { |
mbed_official | 35:052c1ba06ce7 | 70 | printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), |
mbed_official | 0:857719181846 | 71 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
mbed_official | 0:857719181846 | 72 | ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); |
mbed_official | 0:857719181846 | 73 | } |
mbed_official | 0:857719181846 | 74 | |
mbed_official | 0:857719181846 | 75 | delete[] ap; |
mbed_official | 37:3a31525e2971 | 76 | return count; |
mbed_official | 0:857719181846 | 77 | } |
mbed_official | 0:857719181846 | 78 | |
mbed_official | 0:857719181846 | 79 | void http_demo(NetworkInterface *net) |
mbed_official | 0:857719181846 | 80 | { |
mbed_official | 0:857719181846 | 81 | TCPSocket socket; |
mbed_official | 24:323569a565ec | 82 | nsapi_error_t response; |
mbed_official | 0:857719181846 | 83 | |
mbed_official | 35:052c1ba06ce7 | 84 | printf("Sending HTTP request to www.arm.com...\n"); |
mbed_official | 0:857719181846 | 85 | |
mbed_official | 0:857719181846 | 86 | // Open a socket on the network interface, and create a TCP connection to www.arm.com |
mbed_official | 0:857719181846 | 87 | socket.open(net); |
mbed_official | 24:323569a565ec | 88 | response = socket.connect("www.arm.com", 80); |
mbed_official | 24:323569a565ec | 89 | if(0 != response) { |
mbed_official | 35:052c1ba06ce7 | 90 | printf("Error connecting: %d\n", response); |
mbed_official | 24:323569a565ec | 91 | socket.close(); |
mbed_official | 24:323569a565ec | 92 | return; |
mbed_official | 24:323569a565ec | 93 | } |
mbed_official | 0:857719181846 | 94 | |
mbed_official | 0:857719181846 | 95 | // Send a simple http request |
mbed_official | 0:857719181846 | 96 | char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n"; |
mbed_official | 33:12f0df4d51d7 | 97 | nsapi_size_t size = strlen(sbuffer); |
mbed_official | 24:323569a565ec | 98 | response = 0; |
mbed_official | 24:323569a565ec | 99 | while(size) |
mbed_official | 24:323569a565ec | 100 | { |
mbed_official | 24:323569a565ec | 101 | response = socket.send(sbuffer+response, size); |
mbed_official | 24:323569a565ec | 102 | if (response < 0) { |
mbed_official | 35:052c1ba06ce7 | 103 | printf("Error sending data: %d\n", response); |
mbed_official | 24:323569a565ec | 104 | socket.close(); |
mbed_official | 24:323569a565ec | 105 | return; |
mbed_official | 24:323569a565ec | 106 | } else { |
mbed_official | 24:323569a565ec | 107 | size -= response; |
mbed_official | 24:323569a565ec | 108 | // Check if entire message was sent or not |
mbed_official | 35:052c1ba06ce7 | 109 | printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
mbed_official | 24:323569a565ec | 110 | } |
mbed_official | 24:323569a565ec | 111 | } |
mbed_official | 0:857719181846 | 112 | |
mbed_official | 0:857719181846 | 113 | // Recieve a simple http response and print out the response line |
mbed_official | 0:857719181846 | 114 | char rbuffer[64]; |
mbed_official | 24:323569a565ec | 115 | response = socket.recv(rbuffer, sizeof rbuffer); |
mbed_official | 24:323569a565ec | 116 | if (response < 0) { |
mbed_official | 35:052c1ba06ce7 | 117 | printf("Error receiving data: %d\n", response); |
mbed_official | 24:323569a565ec | 118 | } else { |
mbed_official | 35:052c1ba06ce7 | 119 | printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); |
mbed_official | 24:323569a565ec | 120 | } |
mbed_official | 0:857719181846 | 121 | |
mbed_official | 0:857719181846 | 122 | // Close the socket to return its memory and bring down the network interface |
mbed_official | 0:857719181846 | 123 | socket.close(); |
mbed_official | 0:857719181846 | 124 | } |
mbed_official | 0:857719181846 | 125 | |
mbed_official | 0:857719181846 | 126 | int main() |
mbed_official | 0:857719181846 | 127 | { |
mbed_official | 37:3a31525e2971 | 128 | int count = 0; |
mbed_official | 37:3a31525e2971 | 129 | |
mbed_official | 35:052c1ba06ce7 | 130 | printf("WiFi example\n\n"); |
mbed_official | 0:857719181846 | 131 | |
mbed_official | 37:3a31525e2971 | 132 | count = scan_demo(&wifi); |
mbed_official | 37:3a31525e2971 | 133 | if (count == 0) { |
mbed_official | 37:3a31525e2971 | 134 | printf("No WIFI APNs found - can't continue further.\n"); |
mbed_official | 37:3a31525e2971 | 135 | return -1; |
mbed_official | 37:3a31525e2971 | 136 | } |
mbed_official | 0:857719181846 | 137 | |
mbed_official | 37:3a31525e2971 | 138 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
mbed_official | 0:857719181846 | 139 | int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
mbed_official | 0:857719181846 | 140 | if (ret != 0) { |
mbed_official | 35:052c1ba06ce7 | 141 | printf("\nConnection error\n"); |
mbed_official | 0:857719181846 | 142 | return -1; |
mbed_official | 0:857719181846 | 143 | } |
mbed_official | 0:857719181846 | 144 | |
mbed_official | 35:052c1ba06ce7 | 145 | printf("Success\n\n"); |
mbed_official | 35:052c1ba06ce7 | 146 | printf("MAC: %s\n", wifi.get_mac_address()); |
mbed_official | 35:052c1ba06ce7 | 147 | printf("IP: %s\n", wifi.get_ip_address()); |
mbed_official | 35:052c1ba06ce7 | 148 | printf("Netmask: %s\n", wifi.get_netmask()); |
mbed_official | 35:052c1ba06ce7 | 149 | printf("Gateway: %s\n", wifi.get_gateway()); |
mbed_official | 35:052c1ba06ce7 | 150 | printf("RSSI: %d\n\n", wifi.get_rssi()); |
mbed_official | 0:857719181846 | 151 | |
mbed_official | 0:857719181846 | 152 | http_demo(&wifi); |
mbed_official | 0:857719181846 | 153 | |
mbed_official | 0:857719181846 | 154 | wifi.disconnect(); |
mbed_official | 0:857719181846 | 155 | |
mbed_official | 35:052c1ba06ce7 | 156 | printf("\nDone\n"); |
mbed_official | 0:857719181846 | 157 | } |