Sample Web Client call on Nucleo-L476RG with W5500 Ethernet Shield by SeedStudio
Dependencies: WIZnetInterface mbed
main.cpp@0:d55f9d572257, 2017-06-29 (annotated)
- Committer:
- sgnezdov
- Date:
- Thu Jun 29 19:07:48 2017 +0000
- Revision:
- 0:d55f9d572257
initial import
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sgnezdov | 0:d55f9d572257 | 1 | #include "mbed.h" |
sgnezdov | 0:d55f9d572257 | 2 | #include "EthernetInterface.h" |
sgnezdov | 0:d55f9d572257 | 3 | |
sgnezdov | 0:d55f9d572257 | 4 | int main() { |
sgnezdov | 0:d55f9d572257 | 5 | |
sgnezdov | 0:d55f9d572257 | 6 | uint8_t mac_addr[6] = {0x00, 0x08, 0xdc, 0x45, 0x56, 0x67}; |
sgnezdov | 0:d55f9d572257 | 7 | |
sgnezdov | 0:d55f9d572257 | 8 | Serial pc(USBTX, USBRX); |
sgnezdov | 0:d55f9d572257 | 9 | |
sgnezdov | 0:d55f9d572257 | 10 | int err; |
sgnezdov | 0:d55f9d572257 | 11 | |
sgnezdov | 0:d55f9d572257 | 12 | pc.baud(115200); |
sgnezdov | 0:d55f9d572257 | 13 | wait(1); |
sgnezdov | 0:d55f9d572257 | 14 | pc.printf("\r\nSPI to Ethernet Slave\r\n"); |
sgnezdov | 0:d55f9d572257 | 15 | |
sgnezdov | 0:d55f9d572257 | 16 | SPI spi(PA_7,PA_6,PA_5); // mosi, miso, sclk |
sgnezdov | 0:d55f9d572257 | 17 | spi.format(32, 0); |
sgnezdov | 0:d55f9d572257 | 18 | spi.frequency(100000); |
sgnezdov | 0:d55f9d572257 | 19 | wait(3); |
sgnezdov | 0:d55f9d572257 | 20 | |
sgnezdov | 0:d55f9d572257 | 21 | pc.printf("Wait a second...\r\n"); |
sgnezdov | 0:d55f9d572257 | 22 | |
sgnezdov | 0:d55f9d572257 | 23 | pc.printf("Network Setting DHCP\r\n"); |
sgnezdov | 0:d55f9d572257 | 24 | EthernetInterface eth(&spi, PB_6, NC); // mosi, miso, sclk, cs, reset |
sgnezdov | 0:d55f9d572257 | 25 | err = eth.init(mac_addr); |
sgnezdov | 0:d55f9d572257 | 26 | if (err) { |
sgnezdov | 0:d55f9d572257 | 27 | pc.printf("Error in eth init\n\r"); |
sgnezdov | 0:d55f9d572257 | 28 | exit(0); |
sgnezdov | 0:d55f9d572257 | 29 | } |
sgnezdov | 0:d55f9d572257 | 30 | |
sgnezdov | 0:d55f9d572257 | 31 | pc.printf("Initialized, MY_MAC: %s\r\n", eth.getMACAddress()); |
sgnezdov | 0:d55f9d572257 | 32 | |
sgnezdov | 0:d55f9d572257 | 33 | err = eth.connect(); |
sgnezdov | 0:d55f9d572257 | 34 | if (err) { |
sgnezdov | 0:d55f9d572257 | 35 | pc.printf("Error in eth connect\r\n"); |
sgnezdov | 0:d55f9d572257 | 36 | exit(0); |
sgnezdov | 0:d55f9d572257 | 37 | } |
sgnezdov | 0:d55f9d572257 | 38 | pc.printf |
sgnezdov | 0:d55f9d572257 | 39 | ( |
sgnezdov | 0:d55f9d572257 | 40 | "Connected with IP: %s, NETMASK: %s, GATEWAY: %s\n\r", |
sgnezdov | 0:d55f9d572257 | 41 | eth.getIPAddress(), |
sgnezdov | 0:d55f9d572257 | 42 | eth.getNetworkMask(), |
sgnezdov | 0:d55f9d572257 | 43 | eth.getGateway() |
sgnezdov | 0:d55f9d572257 | 44 | ); |
sgnezdov | 0:d55f9d572257 | 45 | |
sgnezdov | 0:d55f9d572257 | 46 | while(1) |
sgnezdov | 0:d55f9d572257 | 47 | { |
sgnezdov | 0:d55f9d572257 | 48 | pc.printf("Check Ethernet Link\r\n"); |
sgnezdov | 0:d55f9d572257 | 49 | if(eth.link() == true) |
sgnezdov | 0:d55f9d572257 | 50 | { |
sgnezdov | 0:d55f9d572257 | 51 | pc.printf("Link up\r\n"); |
sgnezdov | 0:d55f9d572257 | 52 | break; |
sgnezdov | 0:d55f9d572257 | 53 | } |
sgnezdov | 0:d55f9d572257 | 54 | wait(1); |
sgnezdov | 0:d55f9d572257 | 55 | } |
sgnezdov | 0:d55f9d572257 | 56 | |
sgnezdov | 0:d55f9d572257 | 57 | const char* Target_addr = "192.168.40.60"; |
sgnezdov | 0:d55f9d572257 | 58 | const int Target_port = 8080; |
sgnezdov | 0:d55f9d572257 | 59 | |
sgnezdov | 0:d55f9d572257 | 60 | TCPSocketConnection socket; |
sgnezdov | 0:d55f9d572257 | 61 | err = socket.connect(Target_addr, Target_port); |
sgnezdov | 0:d55f9d572257 | 62 | if (err) { |
sgnezdov | 0:d55f9d572257 | 63 | pc.printf("Unable to connect to (%s) on port (%d). Error: %d\r\n", Target_addr, Target_port, err); |
sgnezdov | 0:d55f9d572257 | 64 | exit(0); |
sgnezdov | 0:d55f9d572257 | 65 | } |
sgnezdov | 0:d55f9d572257 | 66 | pc.printf("Connected to %s on port%d\r\n", Target_addr, Target_port); |
sgnezdov | 0:d55f9d572257 | 67 | |
sgnezdov | 0:d55f9d572257 | 68 | char* reqMsg = "GET /api/user HTTP/1.1\r\nHost: kama.blackhawk-lab.itron.com\r\n\r\n"; |
sgnezdov | 0:d55f9d572257 | 69 | err = socket.send_all(reqMsg, strlen(reqMsg)); |
sgnezdov | 0:d55f9d572257 | 70 | if (err == -1) { |
sgnezdov | 0:d55f9d572257 | 71 | pc.printf("send_all error: %d\r\n", err); |
sgnezdov | 0:d55f9d572257 | 72 | exit(0); |
sgnezdov | 0:d55f9d572257 | 73 | } |
sgnezdov | 0:d55f9d572257 | 74 | |
sgnezdov | 0:d55f9d572257 | 75 | char resp[10240]; |
sgnezdov | 0:d55f9d572257 | 76 | int count = socket.receive_all(resp, sizeof(resp)); |
sgnezdov | 0:d55f9d572257 | 77 | if (err == -1) { |
sgnezdov | 0:d55f9d572257 | 78 | pc.printf("receive_all error: %d\r\n", err); |
sgnezdov | 0:d55f9d572257 | 79 | exit(0); |
sgnezdov | 0:d55f9d572257 | 80 | } |
sgnezdov | 0:d55f9d572257 | 81 | pc.printf("%.*s\r\n", count, resp); |
sgnezdov | 0:d55f9d572257 | 82 | |
sgnezdov | 0:d55f9d572257 | 83 | exit(0); |
sgnezdov | 0:d55f9d572257 | 84 | } |