![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This is a demo to show how to use Arch Link and dweet.io
Dependencies: BLE_API DHT HTTPClient SDFileSystem WIZnet_Library mbed nRF51822
Fork of archlink_Temperture_ethernet_http by
main.cpp@0:29bbc7eec5e1, 2015-06-30 (annotated)
- Committer:
- Kevin_Lee
- Date:
- Tue Jun 30 03:40:02 2015 +0000
- Revision:
- 0:29bbc7eec5e1
- Child:
- 1:0f6a216263aa
ethernet;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kevin_Lee | 0:29bbc7eec5e1 | 1 | #include "mbed.h" |
Kevin_Lee | 0:29bbc7eec5e1 | 2 | #include "SDFileSystem.h" |
Kevin_Lee | 0:29bbc7eec5e1 | 3 | #include "BLE.h" |
Kevin_Lee | 0:29bbc7eec5e1 | 4 | #include "WIZnetInterface.h" |
Kevin_Lee | 0:29bbc7eec5e1 | 5 | |
Kevin_Lee | 0:29bbc7eec5e1 | 6 | |
Kevin_Lee | 0:29bbc7eec5e1 | 7 | #define USE_DHCP 0 |
Kevin_Lee | 0:29bbc7eec5e1 | 8 | #define LOOPBACKPORT 5000 |
Kevin_Lee | 0:29bbc7eec5e1 | 9 | int W5500_Test(void); |
Kevin_Lee | 0:29bbc7eec5e1 | 10 | SPI spi(SPI_PSELMOSI0, SPI_PSELMISO0, SPI_PSELSCK0); |
Kevin_Lee | 0:29bbc7eec5e1 | 11 | WIZnetInterface ethernet(&spi, p24, p17); // Spi ,cs, reset |
Kevin_Lee | 0:29bbc7eec5e1 | 12 | int ret, dummy, lv = 1; |
Kevin_Lee | 0:29bbc7eec5e1 | 13 | const char * IP_Addr = "192.168.21.247"; |
Kevin_Lee | 0:29bbc7eec5e1 | 14 | const char * IP_Subnet = "255.255.255.0"; |
Kevin_Lee | 0:29bbc7eec5e1 | 15 | const char * IP_Gateway = "192.168.21.2"; |
Kevin_Lee | 0:29bbc7eec5e1 | 16 | unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x1C,0xAA,0xCA}; |
Kevin_Lee | 0:29bbc7eec5e1 | 17 | |
Kevin_Lee | 0:29bbc7eec5e1 | 18 | |
Kevin_Lee | 0:29bbc7eec5e1 | 19 | DigitalOut myled(LED1); |
Kevin_Lee | 0:29bbc7eec5e1 | 20 | //Arch Link |
Kevin_Lee | 0:29bbc7eec5e1 | 21 | Serial pc(USBTX, USBRX); /* uart */ |
Kevin_Lee | 0:29bbc7eec5e1 | 22 | |
Kevin_Lee | 0:29bbc7eec5e1 | 23 | |
Kevin_Lee | 0:29bbc7eec5e1 | 24 | int main() { |
Kevin_Lee | 0:29bbc7eec5e1 | 25 | wait(1); |
Kevin_Lee | 0:29bbc7eec5e1 | 26 | pc.baud(115200); |
Kevin_Lee | 0:29bbc7eec5e1 | 27 | wait(1); |
Kevin_Lee | 0:29bbc7eec5e1 | 28 | printf("helloworld\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 29 | |
Kevin_Lee | 0:29bbc7eec5e1 | 30 | if(W5500_Test() == 0) { |
Kevin_Lee | 0:29bbc7eec5e1 | 31 | printf("W5500 tested OK \r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 32 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 33 | |
Kevin_Lee | 0:29bbc7eec5e1 | 34 | while(1) { |
Kevin_Lee | 0:29bbc7eec5e1 | 35 | myled = 1; |
Kevin_Lee | 0:29bbc7eec5e1 | 36 | printf("light1\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 37 | wait(3); |
Kevin_Lee | 0:29bbc7eec5e1 | 38 | myled = 0; |
Kevin_Lee | 0:29bbc7eec5e1 | 39 | printf("light0\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 40 | wait(3); |
Kevin_Lee | 0:29bbc7eec5e1 | 41 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 42 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 43 | |
Kevin_Lee | 0:29bbc7eec5e1 | 44 | int W5500_Test(void) |
Kevin_Lee | 0:29bbc7eec5e1 | 45 | { |
Kevin_Lee | 0:29bbc7eec5e1 | 46 | mbed_mac_address((char *)MAC_Addr); //Use mbed mac addres |
Kevin_Lee | 0:29bbc7eec5e1 | 47 | wait(1); |
Kevin_Lee | 0:29bbc7eec5e1 | 48 | printf("Start to test ethernet!\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 49 | |
Kevin_Lee | 0:29bbc7eec5e1 | 50 | #if USE_DHCP |
Kevin_Lee | 0:29bbc7eec5e1 | 51 | printf("use DHCP\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 52 | ret = ethernet.init(MAC_Addr); |
Kevin_Lee | 0:29bbc7eec5e1 | 53 | #else |
Kevin_Lee | 0:29bbc7eec5e1 | 54 | printf("do NOT use DHCP\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 55 | int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway); |
Kevin_Lee | 0:29bbc7eec5e1 | 56 | #endif |
Kevin_Lee | 0:29bbc7eec5e1 | 57 | |
Kevin_Lee | 0:29bbc7eec5e1 | 58 | if (!ret) { |
Kevin_Lee | 0:29bbc7eec5e1 | 59 | pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress()); |
Kevin_Lee | 0:29bbc7eec5e1 | 60 | ret = ethernet.connect(); |
Kevin_Lee | 0:29bbc7eec5e1 | 61 | if (!ret) { |
Kevin_Lee | 0:29bbc7eec5e1 | 62 | pc.printf("IP: %s, MASK: %s, GW: %s\r\n", |
Kevin_Lee | 0:29bbc7eec5e1 | 63 | ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway()); |
Kevin_Lee | 0:29bbc7eec5e1 | 64 | return 0; |
Kevin_Lee | 0:29bbc7eec5e1 | 65 | } else { |
Kevin_Lee | 0:29bbc7eec5e1 | 66 | pc.printf("Error ethernet.connect() - ret = %d\r\n", ret); |
Kevin_Lee | 0:29bbc7eec5e1 | 67 | //exit(0); |
Kevin_Lee | 0:29bbc7eec5e1 | 68 | return -1; |
Kevin_Lee | 0:29bbc7eec5e1 | 69 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 70 | } else { |
Kevin_Lee | 0:29bbc7eec5e1 | 71 | pc.printf("Error ethernet.init() - ret = %d\r\n", ret); |
Kevin_Lee | 0:29bbc7eec5e1 | 72 | //exit(0); |
Kevin_Lee | 0:29bbc7eec5e1 | 73 | return -1; |
Kevin_Lee | 0:29bbc7eec5e1 | 74 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 75 | /* |
Kevin_Lee | 0:29bbc7eec5e1 | 76 | TCPSocketServer server; |
Kevin_Lee | 0:29bbc7eec5e1 | 77 | server.bind(LOOPBACKPORT); |
Kevin_Lee | 0:29bbc7eec5e1 | 78 | server.listen(); |
Kevin_Lee | 0:29bbc7eec5e1 | 79 | |
Kevin_Lee | 0:29bbc7eec5e1 | 80 | while (1) { |
Kevin_Lee | 0:29bbc7eec5e1 | 81 | pc.printf("\nWait for new connection...\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 82 | TCPSocketConnection client; |
Kevin_Lee | 0:29bbc7eec5e1 | 83 | server.accept(client); |
Kevin_Lee | 0:29bbc7eec5e1 | 84 | client.set_blocking(false, 0); // Timeout=0. |
Kevin_Lee | 0:29bbc7eec5e1 | 85 | pc.printf("Connection from: %s\r\n", client.get_address()); |
Kevin_Lee | 0:29bbc7eec5e1 | 86 | while (client.is_connected() == true) { |
Kevin_Lee | 0:29bbc7eec5e1 | 87 | int n = client.receive(buffer, sizeof(buffer)); |
Kevin_Lee | 0:29bbc7eec5e1 | 88 | if(n > 0) |
Kevin_Lee | 0:29bbc7eec5e1 | 89 | client.send_all(buffer, n); |
Kevin_Lee | 0:29bbc7eec5e1 | 90 | if(client.is_fin_received()) |
Kevin_Lee | 0:29bbc7eec5e1 | 91 | client.close(); |
Kevin_Lee | 0:29bbc7eec5e1 | 92 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 93 | pc.printf("Disconnected.\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 94 | }*/ |
Kevin_Lee | 0:29bbc7eec5e1 | 95 | } |