pomera + FlashAir + ODIN-W2 + AS289R2 demo
Fork of mbed-os-example-wifi-ThingSpeak by
main.cpp@11:bad115ba016c, 2017-06-05 (annotated)
- Committer:
- yone2
- Date:
- Mon Jun 05 01:23:05 2017 +0000
- Revision:
- 11:bad115ba016c
- Parent:
- 8:72d49a084297
size of sbuffer -1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yone2 | 11:bad115ba016c | 1 | /* WiFi Example |
mbed_official | 0:857719181846 | 2 | * Copyright (c) 2016 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 | 0:857719181846 | 20 | #if TARGET_UBLOX_EVK_ODIN_W2 |
mbed_official | 1:aea78e21a7da | 21 | #include "OdinWiFiInterface.h" |
mbed_official | 1:aea78e21a7da | 22 | OdinWiFiInterface wifi; |
mbed_official | 0:857719181846 | 23 | #else |
mbed_official | 1:aea78e21a7da | 24 | #if !TARGET_FF_ARDUINO |
mbed_official | 1:aea78e21a7da | 25 | #error [NOT_SUPPORTED] Only Arduino form factor devices are supported at this time |
mbed_official | 1:aea78e21a7da | 26 | #endif |
mbed_official | 1:aea78e21a7da | 27 | #include "ESP8266Interface.h" |
mbed_official | 1:aea78e21a7da | 28 | ESP8266Interface wifi(D1, D0); |
mbed_official | 0:857719181846 | 29 | #endif |
mbed_official | 0:857719181846 | 30 | |
yone2 | 11:bad115ba016c | 31 | DigitalOut led_r(PE_0); |
yone2 | 11:bad115ba016c | 32 | DigitalOut led_b(PB_8); |
Okoshi | 8:72d49a084297 | 33 | |
yone2 | 11:bad115ba016c | 34 | DigitalIn sw_0(PF_2); |
yone2 | 11:bad115ba016c | 35 | DigitalIn sw_1(PB_6); |
yone2 | 11:bad115ba016c | 36 | |
yone2 | 11:bad115ba016c | 37 | Serial device(PD_8, PD_9); // tx(p28), rx(p27) |
Okoshi | 8:72d49a084297 | 38 | |
mbed_official | 0:857719181846 | 39 | const char *sec2str(nsapi_security_t sec) |
mbed_official | 0:857719181846 | 40 | { |
mbed_official | 0:857719181846 | 41 | switch (sec) { |
mbed_official | 0:857719181846 | 42 | case NSAPI_SECURITY_NONE: |
mbed_official | 0:857719181846 | 43 | return "None"; |
mbed_official | 0:857719181846 | 44 | case NSAPI_SECURITY_WEP: |
mbed_official | 0:857719181846 | 45 | return "WEP"; |
mbed_official | 0:857719181846 | 46 | case NSAPI_SECURITY_WPA: |
mbed_official | 0:857719181846 | 47 | return "WPA"; |
mbed_official | 0:857719181846 | 48 | case NSAPI_SECURITY_WPA2: |
mbed_official | 0:857719181846 | 49 | return "WPA2"; |
mbed_official | 0:857719181846 | 50 | case NSAPI_SECURITY_WPA_WPA2: |
mbed_official | 0:857719181846 | 51 | return "WPA/WPA2"; |
mbed_official | 0:857719181846 | 52 | case NSAPI_SECURITY_UNKNOWN: |
mbed_official | 0:857719181846 | 53 | default: |
mbed_official | 0:857719181846 | 54 | return "Unknown"; |
mbed_official | 0:857719181846 | 55 | } |
mbed_official | 0:857719181846 | 56 | } |
mbed_official | 0:857719181846 | 57 | |
mbed_official | 0:857719181846 | 58 | void scan_demo(WiFiInterface *wifi) |
mbed_official | 0:857719181846 | 59 | { |
mbed_official | 0:857719181846 | 60 | WiFiAccessPoint *ap; |
mbed_official | 0:857719181846 | 61 | |
mbed_official | 0:857719181846 | 62 | printf("Scan:\r\n"); |
mbed_official | 0:857719181846 | 63 | |
mbed_official | 0:857719181846 | 64 | int count = wifi->scan(NULL,0); |
mbed_official | 0:857719181846 | 65 | |
mbed_official | 0:857719181846 | 66 | /* Limit number of network arbitrary to 15 */ |
mbed_official | 0:857719181846 | 67 | count = count < 15 ? count : 15; |
mbed_official | 0:857719181846 | 68 | |
mbed_official | 0:857719181846 | 69 | ap = new WiFiAccessPoint[count]; |
mbed_official | 0:857719181846 | 70 | count = wifi->scan(ap, count); |
yone2 | 11:bad115ba016c | 71 | for (int i = 0; i < count; i++) |
yone2 | 11:bad115ba016c | 72 | { |
mbed_official | 0:857719181846 | 73 | printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(), |
mbed_official | 0:857719181846 | 74 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
mbed_official | 0:857719181846 | 75 | 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 | 76 | } |
mbed_official | 0:857719181846 | 77 | printf("%d networks available.\r\n", count); |
mbed_official | 0:857719181846 | 78 | |
mbed_official | 0:857719181846 | 79 | delete[] ap; |
mbed_official | 0:857719181846 | 80 | } |
mbed_official | 0:857719181846 | 81 | |
yone2 | 11:bad115ba016c | 82 | void http_demo(NetworkInterface *net) |
mbed_official | 0:857719181846 | 83 | { |
mbed_official | 0:857719181846 | 84 | TCPSocket socket; |
yone2 | 11:bad115ba016c | 85 | |
yone2 | 11:bad115ba016c | 86 | printf("Sending HTTP request to flashair...\r\n"); |
mbed_official | 0:857719181846 | 87 | |
yone2 | 11:bad115ba016c | 88 | // Open a socket on the network interface, and create a TCP connection to www.arm.com |
yone2 | 11:bad115ba016c | 89 | socket.open(net); |
yone2 | 11:bad115ba016c | 90 | socket.connect("flashair", 80); |
mbed_official | 0:857719181846 | 91 | |
yone2 | 11:bad115ba016c | 92 | // Send a simple http request |
yone2 | 11:bad115ba016c | 93 | char sbuffer[] = "GET /test.txt HTTP/1.1\r\nHost: flashair\r\nConnection: Keep-Alive\r\n\r\n"; |
Okoshi | 8:72d49a084297 | 94 | |
yone2 | 11:bad115ba016c | 95 | int scount = socket.send(sbuffer, sizeof sbuffer-1); |
yone2 | 11:bad115ba016c | 96 | printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
yone2 | 11:bad115ba016c | 97 | printf("%s\r\n\r\n", sbuffer); |
mbed_official | 0:857719181846 | 98 | |
yone2 | 11:bad115ba016c | 99 | // Recieve a simple http response and print out the response line |
yone2 | 11:bad115ba016c | 100 | char rbuffer[400]; |
yone2 | 11:bad115ba016c | 101 | int rcount = socket.recv(rbuffer, sizeof rbuffer); |
yone2 | 11:bad115ba016c | 102 | printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); |
yone2 | 11:bad115ba016c | 103 | |
yone2 | 11:bad115ba016c | 104 | //printf("test\r\n"); |
yone2 | 11:bad115ba016c | 105 | //printf("strstr(rbuffer, text/plain):%d\r\n",strstr(rbuffer, "text/plain")); |
yone2 | 11:bad115ba016c | 106 | //printf("strstr(rbuffer, text/plain)-rb:%d\r\n",strstr(rbuffer, "text/plain")-rbuffer); |
yone2 | 11:bad115ba016c | 107 | // printf("%.*s\r\n", strstr(rbuffer, "text/plain")-rbuffer, rbuffer); |
yone2 | 11:bad115ba016c | 108 | printf("%s\r\n", strstr(rbuffer, "text/plain")+10); |
yone2 | 11:bad115ba016c | 109 | // printf("%s\r\n", rbuffer); |
yone2 | 11:bad115ba016c | 110 | //device.printf("%s\r\n", rbuffer); |
mbed_official | 0:857719181846 | 111 | |
yone2 | 11:bad115ba016c | 112 | /* Text Print */ |
yone2 | 11:bad115ba016c | 113 | device.printf("* pomera printer *"); |
mbed_official | 0:857719181846 | 114 | |
yone2 | 11:bad115ba016c | 115 | device.printf("%s\r\n", strstr(rbuffer, "text/plain")+10); |
yone2 | 11:bad115ba016c | 116 | device.printf("\r\r\r\r\r\r"); // Line Feed x 6 |
yone2 | 11:bad115ba016c | 117 | |
yone2 | 11:bad115ba016c | 118 | // Close the socket to return its memory and bring down the network interface |
yone2 | 11:bad115ba016c | 119 | socket.close(); |
mbed_official | 0:857719181846 | 120 | } |
mbed_official | 0:857719181846 | 121 | |
mbed_official | 0:857719181846 | 122 | int main() |
mbed_official | 0:857719181846 | 123 | { |
yone2 | 11:bad115ba016c | 124 | // LED & SW init |
yone2 | 11:bad115ba016c | 125 | led_r=0; |
yone2 | 11:bad115ba016c | 126 | led_b=1; |
yone2 | 11:bad115ba016c | 127 | sw_0.mode(PullUp); |
yone2 | 11:bad115ba016c | 128 | sw_1.mode(PullUp); |
yone2 | 11:bad115ba016c | 129 | |
mbed_official | 0:857719181846 | 130 | printf("WiFi example\r\n\r\n"); |
mbed_official | 0:857719181846 | 131 | |
yone2 | 11:bad115ba016c | 132 | // scan_demo(&wifi); |
mbed_official | 0:857719181846 | 133 | |
mbed_official | 0:857719181846 | 134 | printf("\r\nConnecting...\r\n"); |
mbed_official | 0:857719181846 | 135 | int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
mbed_official | 0:857719181846 | 136 | if (ret != 0) { |
mbed_official | 0:857719181846 | 137 | printf("\r\nConnection error\r\n"); |
mbed_official | 0:857719181846 | 138 | return -1; |
mbed_official | 0:857719181846 | 139 | } |
yone2 | 11:bad115ba016c | 140 | // Wireless LAN connected |
yone2 | 11:bad115ba016c | 141 | //while(sw_0){} |
yone2 | 11:bad115ba016c | 142 | printf("\r\nWireless LAN connected\r\n"); |
yone2 | 11:bad115ba016c | 143 | led_r = 1; |
yone2 | 11:bad115ba016c | 144 | led_b = 0; |
yone2 | 11:bad115ba016c | 145 | |
mbed_official | 0:857719181846 | 146 | printf("Success\r\n\r\n"); |
mbed_official | 0:857719181846 | 147 | printf("MAC: %s\r\n", wifi.get_mac_address()); |
mbed_official | 0:857719181846 | 148 | printf("IP: %s\r\n", wifi.get_ip_address()); |
mbed_official | 0:857719181846 | 149 | printf("Netmask: %s\r\n", wifi.get_netmask()); |
mbed_official | 0:857719181846 | 150 | printf("Gateway: %s\r\n", wifi.get_gateway()); |
mbed_official | 0:857719181846 | 151 | printf("RSSI: %d\r\n\r\n", wifi.get_rssi()); |
mbed_official | 0:857719181846 | 152 | |
yone2 | 11:bad115ba016c | 153 | http_demo(&wifi); |
mbed_official | 0:857719181846 | 154 | |
mbed_official | 0:857719181846 | 155 | wifi.disconnect(); |
mbed_official | 0:857719181846 | 156 | |
yone2 | 11:bad115ba016c | 157 | printf("\r\nDone\r\n"); |
yone2 | 11:bad115ba016c | 158 | |
yone2 | 11:bad115ba016c | 159 | |
yone2 | 11:bad115ba016c | 160 | } |