This is a very simple guide, reviewing the steps required to get Blinky working on an Mbed OS platform.

Dependencies:   RemoteIR

Committer:
sb8718
Date:
Tue May 26 00:30:01 2020 +0000
Revision:
137:81b5a1672c6a
Parent:
136:53a83b91854c
Child:
138:e829be898713
lab12_1;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sb8718 109:5274dd9bebe1 1 #include "mbed.h"
sb8718 137:81b5a1672c6a 2 #define SERVER_IP "192.168.0.10"
sb8718 137:81b5a1672c6a 3 #define SERVER_PORT 50000
sb8718 137:81b5a1672c6a 4 Serial pc(USBTX, USBRX, 115200);
sb8718 137:81b5a1672c6a 5 WiFiInterface *wifi;
sb8718 137:81b5a1672c6a 6 TCPSocket socket;
sb8718 137:81b5a1672c6a 7 Thread sock_thread;
sb8718 137:81b5a1672c6a 8 char rx_buf_pc[100];
sb8718 137:81b5a1672c6a 9 int index = 0;
sb8718 137:81b5a1672c6a 10 volatile int flag ;
sb8718 128:29911670c7fd 11
sb8718 137:81b5a1672c6a 12 void rx_cb(void) // ISR for receiving data from the PC keyboard {
sb8718 137:81b5a1672c6a 13 {
sb8718 137:81b5a1672c6a 14 char ch;
sb8718 137:81b5a1672c6a 15 ch = pc.getc();
sb8718 137:81b5a1672c6a 16 pc.putc(ch);
sb8718 137:81b5a1672c6a 17 rx_buf_pc[index++] = ch;
sb8718 137:81b5a1672c6a 18 if (ch == 0x0D) { //LF
sb8718 137:81b5a1672c6a 19 pc.putc(0x0A);
sb8718 137:81b5a1672c6a 20 rx_buf_pc[--index] = '\0';
sb8718 137:81b5a1672c6a 21 index = 0;
sb8718 137:81b5a1672c6a 22 flag = 1;
sb8718 137:81b5a1672c6a 23 }
sb8718 137:81b5a1672c6a 24 }
sb8718 135:03997cc206a4 25
sb8718 137:81b5a1672c6a 26 // rx_thread: a thread to receive data from the TCP server
sb8718 137:81b5a1672c6a 27 void rx_thread()
sb8718 137:81b5a1672c6a 28 {
sb8718 137:81b5a1672c6a 29 char* buf = (char*)malloc(1024);
sb8718 137:81b5a1672c6a 30 while (true) {
sb8718 137:81b5a1672c6a 31 nsapi_size_or_error_t size = socket.recv(buf, 1024);
sb8718 137:81b5a1672c6a 32 if (size <= 0) {
sb8718 137:81b5a1672c6a 33 if (size == NSAPI_ERROR_WOULD_BLOCK) continue;
sb8718 137:81b5a1672c6a 34 // (-3001) no data for the case of Non-blocking mode
sb8718 137:81b5a1672c6a 35 pc.printf("Error while receiving data from TCP socket (%d)\r\n", size);
sb8718 137:81b5a1672c6a 36 return;
sb8718 137:81b5a1672c6a 37 }
sb8718 137:81b5a1672c6a 38 buf[size] = '\0'; // turn into valid C string
sb8718 137:81b5a1672c6a 39 pc.printf("\r\nRX data: (%d) %s \r\n", size, buf);
sb8718 137:81b5a1672c6a 40 }
sb8718 137:81b5a1672c6a 41 }
sb8718 130:d19783810c05 42
sb8718 135:03997cc206a4 43 int main() {
sb8718 137:81b5a1672c6a 44 SocketAddress sockAddr;
sb8718 137:81b5a1672c6a 45 SocketAddress serverAddr(SERVER_IP, SERVER_PORT);
sb8718 137:81b5a1672c6a 46 pc.printf("\r\n WiFi TCP Client example\r\n");
sb8718 137:81b5a1672c6a 47 pc.attach(&rx_cb);
sb8718 137:81b5a1672c6a 48 wifi = WiFiInterface::get_default_instance();
sb8718 137:81b5a1672c6a 49 if (!wifi)
sb8718 137:81b5a1672c6a 50 {
sb8718 137:81b5a1672c6a 51 pc.printf("ERROR: No WiFiInterface found.\n");
sb8718 137:81b5a1672c6a 52 while(1);
sb8718 137:81b5a1672c6a 53 }
sb8718 137:81b5a1672c6a 54 pc.printf("Connecting to %s...\r\n", MBED_CONF_APP_WIFI_SSID);
sb8718 137:81b5a1672c6a 55 int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID,
sb8718 137:81b5a1672c6a 56 MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
sb8718 137:81b5a1672c6a 57 if (ret != 0)
sb8718 137:81b5a1672c6a 58 {
sb8718 137:81b5a1672c6a 59 pc.printf("Connection error!!\r\n");
sb8718 137:81b5a1672c6a 60 while(1);
sb8718 137:81b5a1672c6a 61 }
sb8718 137:81b5a1672c6a 62
sb8718 137:81b5a1672c6a 63 pc.printf("Success!!\r\n");
sb8718 137:81b5a1672c6a 64 pc.printf("RSSI: %d\r\n", wifi->get_rssi());
sb8718 137:81b5a1672c6a 65 wifi->get_ip_address();
sb8718 137:81b5a1672c6a 66 pc.printf("IP: %s, ", sockAddr.get_ip_address());
sb8718 137:81b5a1672c6a 67 //wifi->get_netmask(&sockAddr);
sb8718 137:81b5a1672c6a 68 //pc.printf("Netmask: %s, ", sockAddr.get_ip_address()); pc.printf("Netmask: %s, ", wifi->get_netmask());
sb8718 137:81b5a1672c6a 69 wifi->get_gateway();
sb8718 137:81b5a1672c6a 70 pc.printf("Gateway: %s\r\n", sockAddr.get_ip_address());
sb8718 137:81b5a1672c6a 71
sb8718 137:81b5a1672c6a 72 // Open a TCP socket on the network interface, and create a TCP connection to a Server
sb8718 137:81b5a1672c6a 73 socket.open(wifi);
sb8718 137:81b5a1672c6a 74 int response = socket.connect(serverAddr);
sb8718 137:81b5a1672c6a 75 if(0 != response)
sb8718 137:81b5a1672c6a 76 {
sb8718 137:81b5a1672c6a 77 pc.printf("Error connecting: %d\r\n", response);
sb8718 137:81b5a1672c6a 78 socket.close();
sb8718 137:81b5a1672c6a 79 wifi->disconnect();
sb8718 137:81b5a1672c6a 80 while(1);
sb8718 137:81b5a1672c6a 81 }
sb8718 137:81b5a1672c6a 82 sock_thread.start(&rx_thread);
sb8718 137:81b5a1672c6a 83 while (true)
sb8718 137:81b5a1672c6a 84 {
sb8718 137:81b5a1672c6a 85 flag = 0;
sb8718 137:81b5a1672c6a 86 pc.printf("Enter characters to send to a server: ");
sb8718 137:81b5a1672c6a 87 while (flag != 1) {
sb8718 137:81b5a1672c6a 88 }
sb8718 137:81b5a1672c6a 89 if (!strcmp(rx_buf_pc, "q") || !strcmp(rx_buf_pc, "Q")) {
sb8718 137:81b5a1672c6a 90 break;
sb8718 137:81b5a1672c6a 91 } else {
sb8718 137:81b5a1672c6a 92 int len = strlen(rx_buf_pc);
sb8718 137:81b5a1672c6a 93 pc.printf("Sent: %s (%d)\r\n", rx_buf_pc, len);
sb8718 137:81b5a1672c6a 94 rx_buf_pc[len++] = 0x0D;
sb8718 137:81b5a1672c6a 95 rx_buf_pc[len++] = 0x0A;
sb8718 137:81b5a1672c6a 96 socket.send((const char *)rx_buf_pc, len);
sb8718 137:81b5a1672c6a 97 Thread::wait(500); // 0.5sec
sb8718 130:d19783810c05 98 }
sb8718 131:8fb226cc407c 99 }
sb8718 137:81b5a1672c6a 100 socket.close();
sb8718 137:81b5a1672c6a 101 wifi->disconnect();
sb8718 137:81b5a1672c6a 102 sock_thread.join();
sb8718 137:81b5a1672c6a 103 pc.printf("RX sock_thread joined!!\r\n");
sb8718 137:81b5a1672c6a 104 pc.printf("\nDone\r\n");
sb8718 137:81b5a1672c6a 105 Thread::wait(osWaitForever);
sb8718 135:03997cc206a4 106 }