nucleo f767zi onenet edp test
Dependencies: cJSON_lib Common_lib EdpKit_lib
main.cpp@0:17bd84fc5087, 2016-10-26 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Oct 26 12:00:09 2016 +0100
- Revision:
- 0:17bd84fc5087
- Child:
- 5:3e952c60d705
Initial commit
Commit copied from https://github.com/ARMmbed/mbed-os-example-sockets
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:17bd84fc5087 | 1 | #include "mbed.h" |
mbed_official | 0:17bd84fc5087 | 2 | #include "TCPSocket.h" |
mbed_official | 0:17bd84fc5087 | 3 | |
mbed_official | 0:17bd84fc5087 | 4 | #define STR(x) STR2(x) |
mbed_official | 0:17bd84fc5087 | 5 | #define STR2(x) #x |
mbed_official | 0:17bd84fc5087 | 6 | |
mbed_official | 0:17bd84fc5087 | 7 | |
mbed_official | 0:17bd84fc5087 | 8 | // Socket demo |
mbed_official | 0:17bd84fc5087 | 9 | void http_demo(NetworkInterface *net) { |
mbed_official | 0:17bd84fc5087 | 10 | TCPSocket socket; |
mbed_official | 0:17bd84fc5087 | 11 | |
mbed_official | 0:17bd84fc5087 | 12 | // Show the network address |
mbed_official | 0:17bd84fc5087 | 13 | const char *ip = net->get_ip_address(); |
mbed_official | 0:17bd84fc5087 | 14 | printf("IP address is: %s\n", ip ? ip : "No IP"); |
mbed_official | 0:17bd84fc5087 | 15 | |
mbed_official | 0:17bd84fc5087 | 16 | // Open a socket on the network interface, and create a TCP connection to mbed.org |
mbed_official | 0:17bd84fc5087 | 17 | socket.open(net); |
mbed_official | 0:17bd84fc5087 | 18 | socket.connect("developer.mbed.org", 80); |
mbed_official | 0:17bd84fc5087 | 19 | |
mbed_official | 0:17bd84fc5087 | 20 | // Send a simple http request |
mbed_official | 0:17bd84fc5087 | 21 | char sbuffer[] = "GET / HTTP/1.1\r\nHost: developer.mbed.org\r\n\r\n"; |
mbed_official | 0:17bd84fc5087 | 22 | int scount = socket.send(sbuffer, sizeof sbuffer); |
mbed_official | 0:17bd84fc5087 | 23 | printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
mbed_official | 0:17bd84fc5087 | 24 | |
mbed_official | 0:17bd84fc5087 | 25 | // Recieve a simple http response and print out the response line |
mbed_official | 0:17bd84fc5087 | 26 | char rbuffer[64]; |
mbed_official | 0:17bd84fc5087 | 27 | int rcount = socket.recv(rbuffer, sizeof rbuffer); |
mbed_official | 0:17bd84fc5087 | 28 | printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); |
mbed_official | 0:17bd84fc5087 | 29 | |
mbed_official | 0:17bd84fc5087 | 30 | // Close the socket to return its memory and bring down the network interface |
mbed_official | 0:17bd84fc5087 | 31 | socket.close(); |
mbed_official | 0:17bd84fc5087 | 32 | } |
mbed_official | 0:17bd84fc5087 | 33 | |
mbed_official | 0:17bd84fc5087 | 34 | |
mbed_official | 0:17bd84fc5087 | 35 | // Example with the ESP8266 interface |
mbed_official | 0:17bd84fc5087 | 36 | #if defined(MBED_DEMO_WIFI) |
mbed_official | 0:17bd84fc5087 | 37 | #include "ESP8266Interface.h" |
mbed_official | 0:17bd84fc5087 | 38 | |
mbed_official | 0:17bd84fc5087 | 39 | ESP8266Interface wifi(D1, D0); |
mbed_official | 0:17bd84fc5087 | 40 | |
mbed_official | 0:17bd84fc5087 | 41 | int main() { |
mbed_official | 0:17bd84fc5087 | 42 | // Brings up the esp8266 |
mbed_official | 0:17bd84fc5087 | 43 | printf("ESP8266 socket example\n"); |
mbed_official | 0:17bd84fc5087 | 44 | wifi.connect(STR(MBED_DEMO_WIFI_SSID), STR(MBED_DEMO_WIFI_PASS)); |
mbed_official | 0:17bd84fc5087 | 45 | |
mbed_official | 0:17bd84fc5087 | 46 | // Invoke the demo |
mbed_official | 0:17bd84fc5087 | 47 | http_demo(&wifi); |
mbed_official | 0:17bd84fc5087 | 48 | |
mbed_official | 0:17bd84fc5087 | 49 | // Brings down the esp8266 |
mbed_official | 0:17bd84fc5087 | 50 | wifi.disconnect(); |
mbed_official | 0:17bd84fc5087 | 51 | |
mbed_official | 0:17bd84fc5087 | 52 | printf("Done\n"); |
mbed_official | 0:17bd84fc5087 | 53 | } |
mbed_official | 0:17bd84fc5087 | 54 | |
mbed_official | 0:17bd84fc5087 | 55 | // Example using the builtin ethernet interface |
mbed_official | 0:17bd84fc5087 | 56 | #else |
mbed_official | 0:17bd84fc5087 | 57 | #include "EthernetInterface.h" |
mbed_official | 0:17bd84fc5087 | 58 | |
mbed_official | 0:17bd84fc5087 | 59 | EthernetInterface eth; |
mbed_official | 0:17bd84fc5087 | 60 | |
mbed_official | 0:17bd84fc5087 | 61 | int main() { |
mbed_official | 0:17bd84fc5087 | 62 | // Brings up the ethernet interface |
mbed_official | 0:17bd84fc5087 | 63 | printf("Ethernet socket example\n"); |
mbed_official | 0:17bd84fc5087 | 64 | eth.connect(); |
mbed_official | 0:17bd84fc5087 | 65 | |
mbed_official | 0:17bd84fc5087 | 66 | // Invoke the demo |
mbed_official | 0:17bd84fc5087 | 67 | http_demo(ð); |
mbed_official | 0:17bd84fc5087 | 68 | |
mbed_official | 0:17bd84fc5087 | 69 | // Brings down the ethernet interface |
mbed_official | 0:17bd84fc5087 | 70 | eth.disconnect(); |
mbed_official | 0:17bd84fc5087 | 71 | |
mbed_official | 0:17bd84fc5087 | 72 | printf("Done\n"); |
mbed_official | 0:17bd84fc5087 | 73 | } |
mbed_official | 0:17bd84fc5087 | 74 | #endif |