Programm erwartet auf der seriellen Schnittstelle eine Eingabe, die es an den IntrospectionEP des Servers schickt und bei erfolgreicher Überprüfung (Status=200) das Schloss öffnet.
Dependencies: EthernetInterface HTTPClient mbed-dev mbed-rtos wolfSSL MODSERIAL
main.cpp@2:1ffd9e2ce7df, 2016-06-18 (annotated)
- Committer:
- coolnik
- Date:
- Sat Jun 18 11:55:54 2016 +0000
- Revision:
- 2:1ffd9e2ce7df
- Parent:
- 1:5f0df6a77d62
- Child:
- 3:e91a03086c72
serial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
coolnik | 0:64d0faaa4e6e | 1 | #include "mbed.h" |
coolnik | 0:64d0faaa4e6e | 2 | #include "EthernetInterface.h" |
coolnik | 0:64d0faaa4e6e | 3 | #include "HTTPClient.h" |
coolnik | 0:64d0faaa4e6e | 4 | |
coolnik | 1:5f0df6a77d62 | 5 | DigitalOut R(PTB22); // PTB22 = Red pin |
coolnik | 1:5f0df6a77d62 | 6 | DigitalOut G(PTE26); // PTE26 = Green pin |
coolnik | 1:5f0df6a77d62 | 7 | DigitalOut B(PTB21); // PTB21 = Blue pin |
coolnik | 1:5f0df6a77d62 | 8 | |
coolnik | 1:5f0df6a77d62 | 9 | Serial serial(USBTX, USBRX); // tx, rx |
coolnik | 0:64d0faaa4e6e | 10 | EthernetInterface eth; |
coolnik | 0:64d0faaa4e6e | 11 | HTTPClient http; |
coolnik | 1:5f0df6a77d62 | 12 | char str[8192]; |
coolnik | 0:64d0faaa4e6e | 13 | |
coolnik | 0:64d0faaa4e6e | 14 | int main() { |
coolnik | 0:64d0faaa4e6e | 15 | setbuf(stdout, NULL); |
coolnik | 0:64d0faaa4e6e | 16 | eth.init(); //Use DHCP |
coolnik | 0:64d0faaa4e6e | 17 | eth.connect(); |
coolnik | 2:1ffd9e2ce7df | 18 | http.dumpResHeader(true); |
coolnik | 1:5f0df6a77d62 | 19 | |
coolnik | 1:5f0df6a77d62 | 20 | while(1){ |
coolnik | 1:5f0df6a77d62 | 21 | // Wait for Token on Serial and read to str |
coolnik | 1:5f0df6a77d62 | 22 | while(1){ |
coolnik | 1:5f0df6a77d62 | 23 | if(serial.readable()){ |
coolnik | 1:5f0df6a77d62 | 24 | serial.scanf("%s",&str); |
coolnik | 1:5f0df6a77d62 | 25 | break; |
coolnik | 1:5f0df6a77d62 | 26 | } |
coolnik | 1:5f0df6a77d62 | 27 | } |
coolnik | 1:5f0df6a77d62 | 28 | printf(str); |
coolnik | 1:5f0df6a77d62 | 29 | |
coolnik | 1:5f0df6a77d62 | 30 | // Send token to Server |
coolnik | 2:1ffd9e2ce7df | 31 | // POST date, more examples on https://developer.mbed.org/users/donatien/code/HTTPClient_HelloWorld/file/270e2d0bb85a/main.cpp |
coolnik | 1:5f0df6a77d62 | 32 | printf("\nTrying to fetch page...\n"); |
coolnik | 2:1ffd9e2ce7df | 33 | HTTPMap map; |
coolnik | 2:1ffd9e2ce7df | 34 | HTTPText inText(str, 512); |
coolnik | 2:1ffd9e2ce7df | 35 | map.put("Hello", "World"); |
coolnik | 2:1ffd9e2ce7df | 36 | map.put("test", "1234"); |
coolnik | 2:1ffd9e2ce7df | 37 | printf("\nTrying to post data...\n"); |
coolnik | 2:1ffd9e2ce7df | 38 | int ret = http.post("http://httpbin.org/post", map, &inText); |
coolnik | 1:5f0df6a77d62 | 39 | if (!ret) |
coolnik | 1:5f0df6a77d62 | 40 | { |
coolnik | 1:5f0df6a77d62 | 41 | printf("Page fetched successfully - read %d characters\n", strlen(str)); |
coolnik | 1:5f0df6a77d62 | 42 | printf("Result: %s\n", str); |
coolnik | 1:5f0df6a77d62 | 43 | // Unlock |
coolnik | 1:5f0df6a77d62 | 44 | R=!R; |
coolnik | 1:5f0df6a77d62 | 45 | wait(1); |
coolnik | 1:5f0df6a77d62 | 46 | R=!R; |
coolnik | 1:5f0df6a77d62 | 47 | } |
coolnik | 1:5f0df6a77d62 | 48 | else |
coolnik | 1:5f0df6a77d62 | 49 | { |
coolnik | 1:5f0df6a77d62 | 50 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
coolnik | 1:5f0df6a77d62 | 51 | } |
coolnik | 0:64d0faaa4e6e | 52 | } |
coolnik | 1:5f0df6a77d62 | 53 | |
coolnik | 0:64d0faaa4e6e | 54 | eth.disconnect(); |
coolnik | 0:64d0faaa4e6e | 55 | } |