Ryo Yonezawa / Mbed OS ublox_NADA

Fork of mbed-os-example-wifi-ThingSpeak by Hiroaki Okoshi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* WiFi Example
00002  * Copyright (c) 2016 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "TCPSocket.h"
00019 
00020 #if TARGET_UBLOX_EVK_ODIN_W2
00021 #include "OdinWiFiInterface.h"
00022 OdinWiFiInterface wifi;
00023 #else
00024 #if !TARGET_FF_ARDUINO
00025 #error [NOT_SUPPORTED] Only Arduino form factor devices are supported at this time
00026 #endif
00027 #include "ESP8266Interface.h"
00028 ESP8266Interface wifi(D1, D0);
00029 #endif
00030 
00031 DigitalOut led_r(PE_0);
00032 DigitalOut led_b(PB_8);
00033 
00034 DigitalIn sw_0(PF_2);
00035 DigitalIn sw_1(PB_6);
00036 
00037 Serial device(PD_8, PD_9);  // tx(p28), rx(p27)
00038 
00039 const char *sec2str(nsapi_security_t sec)
00040 {
00041     switch (sec) {
00042         case NSAPI_SECURITY_NONE:
00043             return "None";
00044         case NSAPI_SECURITY_WEP:
00045             return "WEP";
00046         case NSAPI_SECURITY_WPA:
00047             return "WPA";
00048         case NSAPI_SECURITY_WPA2:
00049             return "WPA2";
00050         case NSAPI_SECURITY_WPA_WPA2:
00051             return "WPA/WPA2";
00052         case NSAPI_SECURITY_UNKNOWN:
00053         default:
00054             return "Unknown";
00055     }
00056 }
00057 
00058 void scan_demo(WiFiInterface *wifi)
00059 {
00060     WiFiAccessPoint *ap;
00061 
00062     printf("Scan:\r\n");
00063 
00064     int count = wifi->scan(NULL,0);
00065 
00066     /* Limit number of network arbitrary to 15 */
00067     count = count < 15 ? count : 15;
00068 
00069     ap = new WiFiAccessPoint[count];
00070     count = wifi->scan(ap, count);
00071     for (int i = 0; i < count; i++)
00072     {
00073         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
00074                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
00075                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
00076     }
00077     printf("%d networks available.\r\n", count);
00078 
00079     delete[] ap;
00080 }
00081 
00082 void http_demo(NetworkInterface *net)
00083 {
00084     TCPSocket socket;
00085 
00086     printf("Sending HTTP request to flashair...\r\n");
00087 
00088     // Open a socket on the network interface, and create a TCP connection to www.arm.com
00089     socket.open(net);
00090     socket.connect("flashair", 80);
00091 
00092     // Send a simple http request
00093     char sbuffer[] = "GET /test.txt HTTP/1.1\r\nHost: flashair\r\nConnection: Keep-Alive\r\n\r\n";
00094 
00095     int scount = socket.send(sbuffer, sizeof sbuffer-1);
00096     printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
00097     printf("%s\r\n\r\n", sbuffer);
00098 
00099     // Recieve a simple http response and print out the response line
00100     char rbuffer[400];
00101     int rcount = socket.recv(rbuffer, sizeof rbuffer);
00102     printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
00103    
00104     //printf("test\r\n");
00105     //printf("strstr(rbuffer, text/plain):%d\r\n",strstr(rbuffer, "text/plain"));
00106     //printf("strstr(rbuffer, text/plain)-rb:%d\r\n",strstr(rbuffer, "text/plain")-rbuffer);
00107     // printf("%.*s\r\n", strstr(rbuffer, "text/plain")-rbuffer, rbuffer);
00108     printf("%s\r\n", strstr(rbuffer, "text/plain")+10);
00109     // printf("%s\r\n", rbuffer);
00110     //device.printf("%s\r\n", rbuffer);
00111 
00112     /* Text Print */
00113     device.printf("* pomera printer *");
00114 
00115     device.printf("%s\r\n", strstr(rbuffer, "text/plain")+10);
00116     device.printf("\r\r\r\r\r\r");      // Line Feed x 6
00117 
00118     // Close the socket to return its memory and bring down the network interface
00119     socket.close();
00120 }
00121 
00122 int main()
00123 {
00124     // LED & SW init
00125     led_r=0;
00126     led_b=1;
00127     sw_0.mode(PullUp);
00128     sw_1.mode(PullUp);
00129 
00130     printf("WiFi example\r\n\r\n");
00131 
00132     // scan_demo(&wifi);
00133 
00134     printf("\r\nConnecting...\r\n");
00135     int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00136     if (ret != 0) {
00137         printf("\r\nConnection error\r\n");
00138         return -1;
00139     }
00140     // Wireless LAN connected
00141     //while(sw_0){}
00142     printf("\r\nWireless LAN connected\r\n");
00143     led_r = 1;
00144     led_b = 0;
00145     
00146     printf("Success\r\n\r\n");
00147     printf("MAC: %s\r\n", wifi.get_mac_address());
00148     printf("IP: %s\r\n", wifi.get_ip_address());
00149     printf("Netmask: %s\r\n", wifi.get_netmask());
00150     printf("Gateway: %s\r\n", wifi.get_gateway());
00151     printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
00152 
00153     http_demo(&wifi);
00154 
00155     wifi.disconnect();
00156 
00157     printf("\r\nDone\r\n");
00158     
00159 
00160 }