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.
tcp_example.cpp
00001 // This example program will make a cellular connection, open a TCP socket then 00002 // send and receive data on the socket. After transmission, it will close 00003 // the socket and disconnect the cellular connection. 00004 // 00005 // ** Configure the apn[] value below for MTQ-H5 or MTQ-LAT3 radios. 00006 // ** INTERVAL sets the number of seconds between repeated cycles of this example. 00007 00008 #include "mbed.h" 00009 #include "MTSCellularInterface.h" 00010 #include "MTSLog.h" 00011 #include "example_config.h" 00012 00013 #if ACTIVE_EXAMPLE == TCP_EXAMPLE 00014 00015 // Dragonfly debug port. 00016 Serial debug_port(USBTX, USBRX); 00017 00018 #define INTERVAL 30 00019 00020 int main(){ 00021 //Sets the log level to INFO, higher log levels produce more log output. 00022 //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG and TRACE. 00023 //For the Dragonfly, installed on the UDK 2.0 board, messages are output on the 00024 // UDK 2.0 USB port “COMxx: STMicroelectronics STLink Virtual COM port (xx)” 00025 // at 9600bps. 00026 mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL); 00027 //Sets the debug port to 115200bps. 00028 debug_port.baud(115200); 00029 00030 // Create an MTSCellularInterface object. Serial pins for the Dragonfly board that connect 00031 // to the on board cellular radio: 00032 // RADIO_TX = pin PC_7, RADIO_RX = pin PC_6 00033 MTSCellularInterface *radio = new MTSCellularInterface(RADIO_TX, RADIO_RX); 00034 00035 // Print the MTSCellularInterface version 00036 logInfo("MTSCellularInterface Version %s", radio->get_library_version().c_str()); 00037 00038 //Modify to match your apn if you are using the MTQ-H5 or MTQ-LAT3. 00039 const char apn[] = ""; 00040 00041 // Basic HTTP request. 00042 std::string request = "GET / HTTP/1.1\r\nHost: developer.mbed.org\r\n\r\n"; 00043 00044 int cycle_count = 1; 00045 00046 while (true) { 00047 logInfo("-------- CYCLE #%d --------\r\n", cycle_count++); 00048 00049 while(!radio->is_registered()){ 00050 logWarning("radio not registered, try again in 2s"); 00051 wait(2); 00052 } 00053 00054 Timer tmr; //mbed Timer has a 30 minute maximum timeout. 00055 tmr.start(); 00056 while (radio->connect(apn) != NSAPI_ERROR_OK) { 00057 logWarning("Radio did not connect"); 00058 while (tmr.read() < 30); //make sure we wait at least 30s. 00059 tmr.reset(); 00060 } 00061 tmr.stop(); 00062 00063 // Show the network address 00064 const char *ip = radio->get_ip_address(); 00065 logInfo("IP address is: %s\n", ip ? ip : "No IP"); 00066 00067 // Open a socket on the network interface, and create a TCP connection to mbed.org 00068 TCPSocket socket; 00069 00070 // Open a socket on the network interface. 00071 if (socket.open(radio) != NSAPI_ERROR_OK) { 00072 logWarning("socket did not open"); 00073 socket.close(); 00074 continue; 00075 } 00076 00077 // Make a socket connection. 00078 if (socket.connect("developer.mbed.org", 80) != NSAPI_ERROR_OK) { 00079 logWarning("socket did not connect"); 00080 socket.close(); 00081 continue; 00082 } 00083 00084 // Send tcp data 00085 int scount = socket.send(request.c_str(), request.size()); 00086 logInfo("sent %d bytes: %s", scount, request.c_str()); 00087 00088 // Recieve and print. Give a couple seonds to receive. 00089 int size = 512; 00090 char rbuffer[size]; 00091 memset(rbuffer, 0, size); 00092 bool got_data = false; 00093 Timer rcv_timer; 00094 rcv_timer.start(); 00095 do { 00096 int rcount = socket.recv(rbuffer, size-1); //leave room for a null character 00097 if (rcount > 0) { 00098 got_data = true; 00099 while (rcount > 0) { 00100 logInfo("recv %d bytes: %s", rcount, rbuffer); 00101 memset(rbuffer, 0, size); 00102 rcount = socket.recv(rbuffer, size-1); 00103 } 00104 } 00105 } while (rcv_timer < 2 && !got_data); 00106 00107 // Close the socket to return its memory and bring down the network interface 00108 socket.close(); 00109 00110 radio->disconnect(); 00111 00112 logInfo("waiting %d seconds\r\n", INTERVAL); 00113 wait(INTERVAL); 00114 } 00115 00116 } 00117 00118 #endif
Generated on Thu Jul 14 2022 05:07:51 by
1.7.2