Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface TMP175 mbed-rtos mbed
Fork of 2-07-04-Uebung by
main.cpp@6:48a4698a3416, 2015-06-07 (annotated)
- Committer:
- stefan1691
- Date:
- Sun Jun 07 18:42:23 2015 +0000
- Revision:
- 6:48a4698a3416
- Parent:
- 5:13c63366d788
- Child:
- 7:4972e81b4b17
Alternative Variante mit lokalem Raspberry Pi und CGI-Script
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stefan1691 | 5:13c63366d788 | 1 | /** HTTP POST Beispiel |
stefan1691 | 5:13c63366d788 | 2 | */ |
WiredHome | 0:fb5060c39dd1 | 3 | #include "mbed.h" |
WiredHome | 0:fb5060c39dd1 | 4 | #include "HTTPClient.h" |
marcel1691 | 1:2e29a33cd918 | 5 | #include "HTTPText.h" |
WiredHome | 0:fb5060c39dd1 | 6 | #include "EthernetInterface.h" |
WiredHome | 0:fb5060c39dd1 | 7 | |
WiredHome | 0:fb5060c39dd1 | 8 | EthernetInterface eth; |
stefan1691 | 3:aad64a4b6ff6 | 9 | // HTTPClient Hilfsklasse |
WiredHome | 0:fb5060c39dd1 | 10 | HTTPClient http; |
stefan1691 | 3:aad64a4b6ff6 | 11 | // I/O Buffer |
stefan1691 | 5:13c63366d788 | 12 | char str[512]; |
WiredHome | 0:fb5060c39dd1 | 13 | |
WiredHome | 0:fb5060c39dd1 | 14 | DigitalOut myled(LED1); |
WiredHome | 0:fb5060c39dd1 | 15 | |
marcel1691 | 1:2e29a33cd918 | 16 | int main() |
marcel1691 | 1:2e29a33cd918 | 17 | { |
stefan1691 | 5:13c63366d788 | 18 | printf("HTTP Client - POST\n"); |
WiredHome | 0:fb5060c39dd1 | 19 | eth.init(); |
WiredHome | 0:fb5060c39dd1 | 20 | eth.connect(); |
WiredHome | 0:fb5060c39dd1 | 21 | |
marcel1691 | 1:2e29a33cd918 | 22 | while(1) |
marcel1691 | 1:2e29a33cd918 | 23 | { |
WiredHome | 0:fb5060c39dd1 | 24 | myled = 1; |
stefan1691 | 5:13c63366d788 | 25 | |
stefan1691 | 5:13c63366d788 | 26 | // Hilfsklasse um die Response vom Server zu formatieren |
stefan1691 | 5:13c63366d788 | 27 | HTTPText inText(str, 512); |
stefan1691 | 5:13c63366d788 | 28 | |
stefan1691 | 5:13c63366d788 | 29 | // MAP (Argument=Wert) |
stefan1691 | 5:13c63366d788 | 30 | HTTPMap map; |
stefan1691 | 5:13c63366d788 | 31 | map.put("DigitalOut", "LED1"); |
stefan1691 | 5:13c63366d788 | 32 | map.put("write", "1"); |
stefan1691 | 5:13c63366d788 | 33 | |
stefan1691 | 5:13c63366d788 | 34 | int ret = http.post("http://httpbin.org/post", map, &inText); |
stefan1691 | 6:48a4698a3416 | 35 | // lokale Variante mit CGI-Script auf Raspberry Pi. Wenn nicht Funktioniert: raspi2x durch IP-Adresse ersetzen |
stefan1691 | 6:48a4698a3416 | 36 | // int ret = http.post("http://raspi2x/cgi-bin/action", map, &inText); |
stefan1691 | 5:13c63366d788 | 37 | if ( ! ret ) |
marcel1691 | 1:2e29a33cd918 | 38 | { |
stefan1691 | 5:13c63366d788 | 39 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
stefan1691 | 5:13c63366d788 | 40 | printf("Result: %s\n", str); |
stefan1691 | 5:13c63366d788 | 41 | } |
stefan1691 | 5:13c63366d788 | 42 | else |
stefan1691 | 5:13c63366d788 | 43 | { |
stefan1691 | 5:13c63366d788 | 44 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
WiredHome | 0:fb5060c39dd1 | 45 | } |
marcel1691 | 1:2e29a33cd918 | 46 | |
WiredHome | 0:fb5060c39dd1 | 47 | myled = 0; |
marcel1691 | 1:2e29a33cd918 | 48 | |
WiredHome | 0:fb5060c39dd1 | 49 | wait(10); |
WiredHome | 0:fb5060c39dd1 | 50 | } |
stefan1691 | 5:13c63366d788 | 51 | //eth.disconnect(); |
stefan1691 | 5:13c63366d788 | 52 | } |