Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
5 years, 7 months ago.
How to get data via socket on FRDM-K66F from laptop using ethernet cable ?
Hello, I am using FRDM-K66F Microcontroller. I want to use FRDM-K66F as a client and accept data from a laptop using Ethernet cable. The confusing part is, How to get the IP number of MIcrocontroller. I am trying to use the following program from mbed. At the server side, the python program is which sends the data.
******** Program Start************
- include "mbed.h"
- include "EthernetInterface.h"
const char* ECHO_SERVER_ADDRESS = "192.168.2.2"; const int ECHO_SERVER_PORT = 7;
int main() { EthernetInterface eth; eth.init(); Use DHCP eth.connect(); printf("\nClient IP Address is %s \n", eth.getIPAddress());
UDPSocket sock; sock.init();
Endpoint echo_server; echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
char out_buffer[] = "Hello World"; printf("Sending message '%s' to server (%s)\n",out_buffer,ECHO_SERVER_ADDRESS); sock.sendTo(echo_server, out_buffer, sizeof(out_buffer));
char in_buffer[256]; int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer));
in_buffer[n] = '\0'; printf("Received message from server: '%s'\n", in_buffer);
sock.close();
eth.disconnect(); while(1) {} } *******Program End**********
1) How to get the IP address of Microcontroller? 2) Can I use this program for FRDM-K66F? 3) Can I use threading in this program?
1 Answer
5 years, 7 months ago.
Hi, please open "Editing tips" and correctly mark your code with
"<<code title=Title>> Your Code <</code>>
" instead of " Program Start / Program End"
I'll get back to it later.
EDIT:
You use an old example so check this. It is not same but similar and more up-to-date.
Import programUDP_Echo
UDP echo with thread
- ad 1.: The "get_ip_address" method will return local IP address
- ad 2.: Probably yes, when your board is supported in the Mbed EthernetInterface library https://os.mbed.com/questions/85840/Does-FRDM-K66F-support-EthernertInterfac/
- ad 3.: Probably yes
For more information check https://os.mbed.com/docs/mbed-os/v5.12/apis/network-socket.html
Best regards
J.
Silly question: Are you connecting directly to your laptop? If so is there a DHCP server running on it?
You are already hard coding the IP address of the computer. It may be easier initially to also hard code the ip address of the mbed.
posted by Andy A 07 May 2019