Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 141:ba61e3c64a6d, committed 2020-06-02
- Comitter:
- sb8718
- Date:
- Tue Jun 02 08:31:06 2020 +0000
- Parent:
- 140:2252b2df4bc6
- Child:
- 142:cb9e8d32d1b9
- Commit message:
- wifi setting
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Jun 02 01:43:23 2020 +0000 +++ b/main.cpp Tue Jun 02 08:31:06 2020 +0000 @@ -1,138 +1,22 @@ #include "mbed.h" -#define SERVER_IP "192.168.0.53" -#define SERVER_PORT 50000 - Serial pc(USBTX, USBRX, 115200); -WiFiInterface *wifi; - -TCPSocket socket; -Thread sock_thread; +RawSerial esp(D8, D2, 115200); -char rx_buf_pc[100]; -int index = 0; -volatile int flag; - -void rx_cb(void) +int main() { - char ch; - ch = pc.getc(); - pc.putc(ch); - rx_buf_pc[index++] = ch; - if (ch == 0x0D) { - pc.putc(0x0A); - rx_buf_pc[--index] = '\0'; - index = 0; - flag = 1; - } -} - -void rx_thread() -{ - char* buf = (char*)malloc(1024); - - while(true) - { - nsapi_size_or_error_t size = socket.recv(buf, 1024); - if (size <= 0) - { - if (size == NSAPI_ERROR_WOULD_BLOCK) continue; - - pc.printf("Error while receiving data from TCP socket (%d)\r\n", size); - return; - } - buf[size] = '\0'; - pc.printf("\r\nRX data: (%d) %s \r\n", size, buf); - } -} - + char c; -int main() { - SocketAddress sockAddr; - SocketAddress serverAddr(SERVER_IP, SERVER_PORT); - - pc.printf("\r\n WiFi TCP Client example\r\n"); - - wifi = WiFiInterface::get_default_instance(); - if (!wifi) - { - pc.printf("ERROR: No WiFiInterface found.\n"); - while(1); - } - - pc.printf("Connecting to %s...\r\n", MBED_CONF_APP_WIFI_SSID); - int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); - if (ret != 0) - { - pc.printf("Connection error!!\r\n"); - while(1); - } - - pc.printf("Success!!\r\n"); - pc.printf("RSSI: %d\r\n", wifi->get_rssi()); - - wifi->get_ip_address(&sockAddr); - pc.printf("IP : %s, ", sockAddr.get_ip_address()); - - pc.printf("Netmask: %s, ", wifi->get_netmask()); - - wifi->get_gateway(&sockAddr); - pc.printf("Gateway: %s\r\n", sockAddr.get_ip_address()); - - socket.open(wifi); - int response = socket.connect(serverAddr); - if (0 != response) - { - pc.printf("Enter connecting: %d\r\n", response); - socket.close(); - wifi->disconnect(); - while(1); - } - - sock_thread.start(&rx_thread); - //pc.attach(&rx_cb); - - while(1) - { - flag = 0; - pc.printf("Enter characters to send to a server: "); - while (flag != 1) - { - char ch; - if (pc.readable()) - { - ch = pc.getc(); - pc.putc(ch); - rx_buf_pc[index++] = ch; - if (ch == 0x0D) { - pc.putc(0x0A); - rx_buf_pc[--index] = '\0'; - index = 0; - flag = 1; - } - } + while(1) { + if(pc.readable()) { + c = pc.getc(); + esp.putc(c); } - - if (!strcmp(rx_buf_pc, "q") || !strcmp(rx_buf_pc, "Q")) - { - break; - } - else - { - int len = strlen(rx_buf_pc); - pc.printf("Sent: %s (%d)\r\n", rx_buf_pc, len); - rx_buf_pc[len++] = 0x0D; - rx_buf_pc[len++] = 0x0A; - socket.send((const char *) rx_buf_pc, len); - Thread::wait(500); + if(esp.readable()) { + c = esp.getc(); //while + pc.putc(c); } } - - socket.close(); - wifi->disconnect(); - sock_thread.join(); - pc.printf("RX sock_thread joined!!\r\n"); - pc.printf("\nDone\r\n"); - Thread::wait(osWaitForever); + }