![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This is a demo that upload temperature info. to dweet.io
Dependencies: BLE_API DHT HTTPClient SDFileSystem WIZnet_Library mbed nRF51822
main.cpp@2:8b304d2b4ca5, 2015-06-30 (annotated)
- Committer:
- Kevin_Lee
- Date:
- Tue Jun 30 07:28:24 2015 +0000
- Revision:
- 2:8b304d2b4ca5
- Parent:
- 1:0f6a216263aa
- Child:
- 3:cfc6ff08a668
backup
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 | 2:8b304d2b4ca5 | 5 | #include "HTTPClient.h" |
Kevin_Lee | 0:29bbc7eec5e1 | 6 | |
Kevin_Lee | 2:8b304d2b4ca5 | 7 | #define USE_DHCP 0 |
Kevin_Lee | 0:29bbc7eec5e1 | 8 | #define LOOPBACKPORT 5000 |
Kevin_Lee | 0:29bbc7eec5e1 | 9 | int W5500_Test(void); |
Kevin_Lee | 1:0f6a216263aa | 10 | |
Kevin_Lee | 0:29bbc7eec5e1 | 11 | SPI spi(SPI_PSELMOSI0, SPI_PSELMISO0, SPI_PSELSCK0); |
Kevin_Lee | 0:29bbc7eec5e1 | 12 | WIZnetInterface ethernet(&spi, p24, p17); // Spi ,cs, reset |
Kevin_Lee | 0:29bbc7eec5e1 | 13 | int ret, dummy, lv = 1; |
Kevin_Lee | 0:29bbc7eec5e1 | 14 | const char * IP_Addr = "192.168.21.247"; |
Kevin_Lee | 0:29bbc7eec5e1 | 15 | const char * IP_Subnet = "255.255.255.0"; |
Kevin_Lee | 0:29bbc7eec5e1 | 16 | const char * IP_Gateway = "192.168.21.2"; |
Kevin_Lee | 0:29bbc7eec5e1 | 17 | unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x1C,0xAA,0xCA}; |
Kevin_Lee | 0:29bbc7eec5e1 | 18 | |
Kevin_Lee | 0:29bbc7eec5e1 | 19 | |
Kevin_Lee | 0:29bbc7eec5e1 | 20 | DigitalOut myled(LED1); |
Kevin_Lee | 0:29bbc7eec5e1 | 21 | //Arch Link |
Kevin_Lee | 0:29bbc7eec5e1 | 22 | Serial pc(USBTX, USBRX); /* uart */ |
Kevin_Lee | 0:29bbc7eec5e1 | 23 | |
Kevin_Lee | 0:29bbc7eec5e1 | 24 | |
Kevin_Lee | 0:29bbc7eec5e1 | 25 | int main() { |
Kevin_Lee | 1:0f6a216263aa | 26 | char buffer[256]; |
Kevin_Lee | 0:29bbc7eec5e1 | 27 | wait(1); |
Kevin_Lee | 0:29bbc7eec5e1 | 28 | pc.baud(115200); |
Kevin_Lee | 0:29bbc7eec5e1 | 29 | wait(1); |
Kevin_Lee | 0:29bbc7eec5e1 | 30 | printf("helloworld\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 31 | |
Kevin_Lee | 2:8b304d2b4ca5 | 32 | if(W5500_Test() == 0) { // Internet is ok |
Kevin_Lee | 0:29bbc7eec5e1 | 33 | printf("W5500 tested OK \r\n"); |
Kevin_Lee | 1:0f6a216263aa | 34 | |
Kevin_Lee | 2:8b304d2b4ca5 | 35 | char str[512]; |
Kevin_Lee | 2:8b304d2b4ca5 | 36 | char get_msg[128]= ""; |
Kevin_Lee | 2:8b304d2b4ca5 | 37 | |
Kevin_Lee | 2:8b304d2b4ca5 | 38 | /* |
Kevin_Lee | 2:8b304d2b4ca5 | 39 | http://dweet.io/follow/nameYouWant |
Kevin_Lee | 2:8b304d2b4ca5 | 40 | */ |
Kevin_Lee | 2:8b304d2b4ca5 | 41 | char nameYouWant[] = "nameYouWant"; |
Kevin_Lee | 2:8b304d2b4ca5 | 42 | while(1) |
Kevin_Lee | 2:8b304d2b4ca5 | 43 | { |
Kevin_Lee | 2:8b304d2b4ca5 | 44 | // sprintf(get_msg,"http://dweet.io/dweet/for/%s?a0=%d",nameYouWant,(int)(1000)); |
Kevin_Lee | 2:8b304d2b4ca5 | 45 | |
Kevin_Lee | 2:8b304d2b4ca5 | 46 | sprintf(get_msg,"http://seeedmsvps.cloudapp.net:10080/plantbox/upload_iot/"); |
Kevin_Lee | 2:8b304d2b4ca5 | 47 | HTTPClient http; |
Kevin_Lee | 1:0f6a216263aa | 48 | |
Kevin_Lee | 2:8b304d2b4ca5 | 49 | pc.printf("Send post message to dweet.io\r\n"); |
Kevin_Lee | 2:8b304d2b4ca5 | 50 | pc.printf("msg : %s\r\n",get_msg); |
Kevin_Lee | 2:8b304d2b4ca5 | 51 | ret = http.get(get_msg, str, sizeof(str)); |
Kevin_Lee | 2:8b304d2b4ca5 | 52 | if(!ret) |
Kevin_Lee | 2:8b304d2b4ca5 | 53 | { |
Kevin_Lee | 2:8b304d2b4ca5 | 54 | pc.printf("\r\nPage fetched successfully - read %d characters\r\n", strlen(str)); |
Kevin_Lee | 2:8b304d2b4ca5 | 55 | pc.printf("Result: %s\r\n", str); |
Kevin_Lee | 2:8b304d2b4ca5 | 56 | } |
Kevin_Lee | 2:8b304d2b4ca5 | 57 | else |
Kevin_Lee | 2:8b304d2b4ca5 | 58 | { |
Kevin_Lee | 2:8b304d2b4ca5 | 59 | pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
Kevin_Lee | 2:8b304d2b4ca5 | 60 | } |
Kevin_Lee | 2:8b304d2b4ca5 | 61 | wait(10); |
Kevin_Lee | 2:8b304d2b4ca5 | 62 | } |
Kevin_Lee | 2:8b304d2b4ca5 | 63 | |
Kevin_Lee | 2:8b304d2b4ca5 | 64 | |
Kevin_Lee | 2:8b304d2b4ca5 | 65 | // TCPSocketServer server; |
Kevin_Lee | 2:8b304d2b4ca5 | 66 | // server.bind(LOOPBACKPORT); |
Kevin_Lee | 2:8b304d2b4ca5 | 67 | // server.listen(); |
Kevin_Lee | 2:8b304d2b4ca5 | 68 | // while(1) { |
Kevin_Lee | 2:8b304d2b4ca5 | 69 | // printf("\nWait for new connection...\r\n"); |
Kevin_Lee | 2:8b304d2b4ca5 | 70 | // TCPSocketConnection client; |
Kevin_Lee | 2:8b304d2b4ca5 | 71 | // server.accept(client); |
Kevin_Lee | 2:8b304d2b4ca5 | 72 | // client.set_blocking(false,0); // Timeout = 0. |
Kevin_Lee | 2:8b304d2b4ca5 | 73 | // printf("Connection from: %s\r\n",client.get_address()); |
Kevin_Lee | 2:8b304d2b4ca5 | 74 | // while (client.is_connected() == true) { |
Kevin_Lee | 2:8b304d2b4ca5 | 75 | // int n = client.receive(buffer,sizeof(buffer)); |
Kevin_Lee | 2:8b304d2b4ca5 | 76 | // if (n > 0) { |
Kevin_Lee | 2:8b304d2b4ca5 | 77 | // client.send_all(buffer,n); |
Kevin_Lee | 2:8b304d2b4ca5 | 78 | // } else { |
Kevin_Lee | 2:8b304d2b4ca5 | 79 | // } |
Kevin_Lee | 2:8b304d2b4ca5 | 80 | // if (client.is_fin_received()) { |
Kevin_Lee | 2:8b304d2b4ca5 | 81 | // client.close(); |
Kevin_Lee | 2:8b304d2b4ca5 | 82 | // } else { |
Kevin_Lee | 2:8b304d2b4ca5 | 83 | // } |
Kevin_Lee | 1:0f6a216263aa | 84 | |
Kevin_Lee | 2:8b304d2b4ca5 | 85 | // } |
Kevin_Lee | 2:8b304d2b4ca5 | 86 | // printf("Disconnected.\r\n"); |
Kevin_Lee | 1:0f6a216263aa | 87 | |
Kevin_Lee | 2:8b304d2b4ca5 | 88 | // } |
Kevin_Lee | 0:29bbc7eec5e1 | 89 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 90 | |
Kevin_Lee | 0:29bbc7eec5e1 | 91 | while(1) { |
Kevin_Lee | 0:29bbc7eec5e1 | 92 | myled = 1; |
Kevin_Lee | 0:29bbc7eec5e1 | 93 | printf("light1\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 94 | wait(3); |
Kevin_Lee | 0:29bbc7eec5e1 | 95 | myled = 0; |
Kevin_Lee | 0:29bbc7eec5e1 | 96 | printf("light0\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 97 | wait(3); |
Kevin_Lee | 0:29bbc7eec5e1 | 98 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 99 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 100 | |
Kevin_Lee | 0:29bbc7eec5e1 | 101 | int W5500_Test(void) |
Kevin_Lee | 0:29bbc7eec5e1 | 102 | { |
Kevin_Lee | 0:29bbc7eec5e1 | 103 | mbed_mac_address((char *)MAC_Addr); //Use mbed mac addres |
Kevin_Lee | 0:29bbc7eec5e1 | 104 | wait(1); |
Kevin_Lee | 0:29bbc7eec5e1 | 105 | printf("Start to test ethernet!\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 106 | |
Kevin_Lee | 0:29bbc7eec5e1 | 107 | #if USE_DHCP |
Kevin_Lee | 0:29bbc7eec5e1 | 108 | printf("use DHCP\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 109 | ret = ethernet.init(MAC_Addr); |
Kevin_Lee | 0:29bbc7eec5e1 | 110 | #else |
Kevin_Lee | 0:29bbc7eec5e1 | 111 | printf("do NOT use DHCP\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 112 | int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway); |
Kevin_Lee | 0:29bbc7eec5e1 | 113 | #endif |
Kevin_Lee | 0:29bbc7eec5e1 | 114 | |
Kevin_Lee | 0:29bbc7eec5e1 | 115 | if (!ret) { |
Kevin_Lee | 0:29bbc7eec5e1 | 116 | pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress()); |
Kevin_Lee | 0:29bbc7eec5e1 | 117 | ret = ethernet.connect(); |
Kevin_Lee | 0:29bbc7eec5e1 | 118 | if (!ret) { |
Kevin_Lee | 0:29bbc7eec5e1 | 119 | pc.printf("IP: %s, MASK: %s, GW: %s\r\n", |
Kevin_Lee | 0:29bbc7eec5e1 | 120 | ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway()); |
Kevin_Lee | 0:29bbc7eec5e1 | 121 | return 0; |
Kevin_Lee | 0:29bbc7eec5e1 | 122 | } else { |
Kevin_Lee | 0:29bbc7eec5e1 | 123 | pc.printf("Error ethernet.connect() - ret = %d\r\n", ret); |
Kevin_Lee | 0:29bbc7eec5e1 | 124 | //exit(0); |
Kevin_Lee | 0:29bbc7eec5e1 | 125 | return -1; |
Kevin_Lee | 0:29bbc7eec5e1 | 126 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 127 | } else { |
Kevin_Lee | 0:29bbc7eec5e1 | 128 | pc.printf("Error ethernet.init() - ret = %d\r\n", ret); |
Kevin_Lee | 0:29bbc7eec5e1 | 129 | //exit(0); |
Kevin_Lee | 0:29bbc7eec5e1 | 130 | return -1; |
Kevin_Lee | 0:29bbc7eec5e1 | 131 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 132 | /* |
Kevin_Lee | 0:29bbc7eec5e1 | 133 | TCPSocketServer server; |
Kevin_Lee | 0:29bbc7eec5e1 | 134 | server.bind(LOOPBACKPORT); |
Kevin_Lee | 0:29bbc7eec5e1 | 135 | server.listen(); |
Kevin_Lee | 0:29bbc7eec5e1 | 136 | |
Kevin_Lee | 0:29bbc7eec5e1 | 137 | while (1) { |
Kevin_Lee | 0:29bbc7eec5e1 | 138 | pc.printf("\nWait for new connection...\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 139 | TCPSocketConnection client; |
Kevin_Lee | 0:29bbc7eec5e1 | 140 | server.accept(client); |
Kevin_Lee | 0:29bbc7eec5e1 | 141 | client.set_blocking(false, 0); // Timeout=0. |
Kevin_Lee | 0:29bbc7eec5e1 | 142 | pc.printf("Connection from: %s\r\n", client.get_address()); |
Kevin_Lee | 0:29bbc7eec5e1 | 143 | while (client.is_connected() == true) { |
Kevin_Lee | 0:29bbc7eec5e1 | 144 | int n = client.receive(buffer, sizeof(buffer)); |
Kevin_Lee | 0:29bbc7eec5e1 | 145 | if(n > 0) |
Kevin_Lee | 0:29bbc7eec5e1 | 146 | client.send_all(buffer, n); |
Kevin_Lee | 0:29bbc7eec5e1 | 147 | if(client.is_fin_received()) |
Kevin_Lee | 0:29bbc7eec5e1 | 148 | client.close(); |
Kevin_Lee | 0:29bbc7eec5e1 | 149 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 150 | pc.printf("Disconnected.\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 151 | }*/ |
Kevin_Lee | 0:29bbc7eec5e1 | 152 | } |