Simple code demonstrating how to configure MBed Network interface with DHCP or StaticIP (comment #define to switch between modes). Note: Demo program to be used on the GeekSessionLab Talk (November 2011). http://devrendezvous.com/?lang=en
Dependencies: EthernetNetIf mbed
main.cpp@0:22c18588092c, 2011-10-27 (annotated)
- Committer:
- botdream
- Date:
- Thu Oct 27 22:22:06 2011 +0000
- Revision:
- 0:22c18588092c
GeekSessionLab: Network Config Example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
botdream | 0:22c18588092c | 1 | //--------------------------------------------------------------------------------------------- |
botdream | 0:22c18588092c | 2 | #include "mbed.h" |
botdream | 0:22c18588092c | 3 | #include "EthernetNetIf.h" |
botdream | 0:22c18588092c | 4 | //--------------------------------------------------------------------------------------------- |
botdream | 0:22c18588092c | 5 | DigitalOut myled(LED1); |
botdream | 0:22c18588092c | 6 | Serial pc(USBTX, USBRX); // tx, rx |
botdream | 0:22c18588092c | 7 | //--------------------------------------------------------------------------------------------- |
botdream | 0:22c18588092c | 8 | #define internaldebug // send debug messages to USB Serial port (9600,1,N) |
botdream | 0:22c18588092c | 9 | #define dhcpenable // auto-setup IP Address from DHCP router |
botdream | 0:22c18588092c | 10 | //--------------------------------------------------------------------------------------------- |
botdream | 0:22c18588092c | 11 | // Ethernet Object Setup |
botdream | 0:22c18588092c | 12 | //--------------------------------------------------------------------------------------------- |
botdream | 0:22c18588092c | 13 | #ifdef dhcpenable |
botdream | 0:22c18588092c | 14 | EthernetNetIf eth; |
botdream | 0:22c18588092c | 15 | #else |
botdream | 0:22c18588092c | 16 | EthernetNetIf eth( |
botdream | 0:22c18588092c | 17 | IpAddr(192,168,1,100), //IP Address |
botdream | 0:22c18588092c | 18 | IpAddr(255,255,255,0), //Network Mask |
botdream | 0:22c18588092c | 19 | IpAddr(192,168,1,254), //Gateway |
botdream | 0:22c18588092c | 20 | IpAddr(192,168,1,254) //DNS |
botdream | 0:22c18588092c | 21 | ); |
botdream | 0:22c18588092c | 22 | #endif |
botdream | 0:22c18588092c | 23 | //--------------------------------------------------------------------------------------------- |
botdream | 0:22c18588092c | 24 | // MAIN |
botdream | 0:22c18588092c | 25 | //--------------------------------------------------------------------------------------------- |
botdream | 0:22c18588092c | 26 | int main() |
botdream | 0:22c18588092c | 27 | { |
botdream | 0:22c18588092c | 28 | //-------------------------------------------------------- |
botdream | 0:22c18588092c | 29 | // Set Serial Port Transfer Rate |
botdream | 0:22c18588092c | 30 | pc.baud(115200); |
botdream | 0:22c18588092c | 31 | //-------------------------------------------------------- |
botdream | 0:22c18588092c | 32 | |
botdream | 0:22c18588092c | 33 | //-------------------------------------------------------- |
botdream | 0:22c18588092c | 34 | // Setting Ethernet |
botdream | 0:22c18588092c | 35 | //-------------------------------------------------------- |
botdream | 0:22c18588092c | 36 | #ifdef internaldebug |
botdream | 0:22c18588092c | 37 | printf("\r\nSetting up Ethernet interface!\r\n"); |
botdream | 0:22c18588092c | 38 | #endif |
botdream | 0:22c18588092c | 39 | // Create return object for error check |
botdream | 0:22c18588092c | 40 | EthernetErr ethErr = eth.setup(); |
botdream | 0:22c18588092c | 41 | if(ethErr) |
botdream | 0:22c18588092c | 42 | { |
botdream | 0:22c18588092c | 43 | #ifdef internaldebug |
botdream | 0:22c18588092c | 44 | printf("\r\nError %d in Ethernet setup.\r\n", ethErr); |
botdream | 0:22c18588092c | 45 | #endif |
botdream | 0:22c18588092c | 46 | return -1; |
botdream | 0:22c18588092c | 47 | } |
botdream | 0:22c18588092c | 48 | #ifdef internaldebug |
botdream | 0:22c18588092c | 49 | printf("\r\nEthernet setup completed with success!\r\n"); |
botdream | 0:22c18588092c | 50 | #endif |
botdream | 0:22c18588092c | 51 | //-------------------------------------------------------- |
botdream | 0:22c18588092c | 52 | |
botdream | 0:22c18588092c | 53 | // main loop |
botdream | 0:22c18588092c | 54 | while(1) |
botdream | 0:22c18588092c | 55 | { |
botdream | 0:22c18588092c | 56 | myled = 1; |
botdream | 0:22c18588092c | 57 | wait(0.5); |
botdream | 0:22c18588092c | 58 | myled = 0; |
botdream | 0:22c18588092c | 59 | wait(0.5); |
botdream | 0:22c18588092c | 60 | } |
botdream | 0:22c18588092c | 61 | } |
botdream | 0:22c18588092c | 62 | //--------------------------------------------------------------------------------------------- |