Mbed to IFTTT
Dependencies: EthernetInterface IFTTT mbed-rtos mbed
Fork of TCPSocket_Ethernet by
main.cpp@1:b1506df02fe6, 2016-09-11 (annotated)
- Committer:
- yu10078999
- Date:
- Sun Sep 11 01:23:05 2016 +0000
- Revision:
- 1:b1506df02fe6
- Parent:
- 0:ea9c19e28331
- Child:
- 2:807d696cf266
0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yu10078999 | 0:ea9c19e28331 | 1 | #include "mbed.h" |
yu10078999 | 0:ea9c19e28331 | 2 | #include "EthernetInterface.h" |
yu10078999 | 1:b1506df02fe6 | 3 | #include "TCPSocketConnection.h" |
yu10078999 | 0:ea9c19e28331 | 4 | |
yu10078999 | 1:b1506df02fe6 | 5 | EthernetInterface eth; |
yu10078999 | 1:b1506df02fe6 | 6 | RawSerial pc(USBTX, USBRX); // tx, rx |
yu10078999 | 1:b1506df02fe6 | 7 | |
yu10078999 | 1:b1506df02fe6 | 8 | int main() |
yu10078999 | 1:b1506df02fe6 | 9 | { |
yu10078999 | 1:b1506df02fe6 | 10 | pc.baud(9600); |
yu10078999 | 1:b1506df02fe6 | 11 | eth.init(); //Use DHCP |
yu10078999 | 0:ea9c19e28331 | 12 | eth.connect(); |
yu10078999 | 1:b1506df02fe6 | 13 | printf("IP Address is %s \n\r", eth.getIPAddress()); |
yu10078999 | 1:b1506df02fe6 | 14 | TCPSocketConnection socket; |
yu10078999 | 0:ea9c19e28331 | 15 | |
yu10078999 | 1:b1506df02fe6 | 16 | socket.connect("httpbin.org", 80); |
yu10078999 | 0:ea9c19e28331 | 17 | |
yu10078999 | 0:ea9c19e28331 | 18 | char http_cmd[] = "GET /get?helloworld HTTP/1.0\r\n\r\n"; |
yu10078999 | 1:b1506df02fe6 | 19 | socket.send_all(http_cmd, sizeof(http_cmd)-1); |
yu10078999 | 0:ea9c19e28331 | 20 | |
yu10078999 | 0:ea9c19e28331 | 21 | char buffer[300]; |
yu10078999 | 0:ea9c19e28331 | 22 | int ret; |
yu10078999 | 0:ea9c19e28331 | 23 | while (true) { |
yu10078999 | 1:b1506df02fe6 | 24 | ret = socket.receive(buffer, sizeof(buffer)-1); |
yu10078999 | 0:ea9c19e28331 | 25 | if (ret <= 0) |
yu10078999 | 0:ea9c19e28331 | 26 | break; |
yu10078999 | 0:ea9c19e28331 | 27 | buffer[ret] = '\0'; |
yu10078999 | 0:ea9c19e28331 | 28 | printf("Received %d chars from server:\n%s\n", ret, buffer); |
yu10078999 | 0:ea9c19e28331 | 29 | } |
yu10078999 | 0:ea9c19e28331 | 30 | |
yu10078999 | 1:b1506df02fe6 | 31 | printf("DONE!\n"); |
yu10078999 | 1:b1506df02fe6 | 32 | |
yu10078999 | 1:b1506df02fe6 | 33 | socket.close(); |
yu10078999 | 0:ea9c19e28331 | 34 | |
yu10078999 | 0:ea9c19e28331 | 35 | eth.disconnect(); |
yu10078999 | 0:ea9c19e28331 | 36 | |
yu10078999 | 0:ea9c19e28331 | 37 | while(1) {} |
yu10078999 | 0:ea9c19e28331 | 38 | } |