The Ethernet-Board

Dependencies:   EthernetInterface HTTPClient MODSERIAL mbed-dev mbed-rtos wolfSSL

Fork of sose2016_tr_oauth_pop by niko gillen

Committer:
coolnik
Date:
Thu Jun 23 20:58:07 2016 +0000
Revision:
3:e91a03086c72
Parent:
2:1ffd9e2ce7df
Child:
4:00b8d595aa33
Token/Daten werden auf UART erwartet; HttpsVerbindung steht; POST wird ausgef?hrt; Bei Response-Status 200 kann das Schloss ge?ffnet werden

Who changed what in which revision?

UserRevisionLine numberNew 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 3:e91a03086c72 5 DigitalOut R(LED_RED);
coolnik 3:e91a03086c72 6 DigitalOut G(LED_GREEN);
coolnik 3:e91a03086c72 7 DigitalOut B(LED_BLUE);
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 3:e91a03086c72 12 char str[32000];
coolnik 3:e91a03086c72 13 char str2[32000];
coolnik 0:64d0faaa4e6e 14
coolnik 0:64d0faaa4e6e 15 int main() {
coolnik 0:64d0faaa4e6e 16 setbuf(stdout, NULL);
coolnik 0:64d0faaa4e6e 17 eth.init(); //Use DHCP
coolnik 0:64d0faaa4e6e 18 eth.connect();
coolnik 2:1ffd9e2ce7df 19 http.dumpResHeader(true);
coolnik 3:e91a03086c72 20 R=!R;G=!G;B=!B;
coolnik 3:e91a03086c72 21 wait(1);
coolnik 1:5f0df6a77d62 22
coolnik 1:5f0df6a77d62 23 while(1){
coolnik 1:5f0df6a77d62 24 // Wait for Token on Serial and read to str
coolnik 3:e91a03086c72 25 // Light Blue
coolnik 3:e91a03086c72 26 B=!B;
coolnik 3:e91a03086c72 27 printf("Erwarte Eingabe: ");
coolnik 1:5f0df6a77d62 28 while(1){
coolnik 1:5f0df6a77d62 29 if(serial.readable()){
coolnik 1:5f0df6a77d62 30 serial.scanf("%s",&str);
coolnik 1:5f0df6a77d62 31 break;
coolnik 1:5f0df6a77d62 32 }
coolnik 1:5f0df6a77d62 33 }
coolnik 3:e91a03086c72 34 // Token Read, light Yellow
coolnik 3:e91a03086c72 35 R=!R;G=!G;B=!B;
coolnik 1:5f0df6a77d62 36 printf(str);
coolnik 1:5f0df6a77d62 37
coolnik 1:5f0df6a77d62 38 // Send token to Server
coolnik 2:1ffd9e2ce7df 39 // POST date, more examples on https://developer.mbed.org/users/donatien/code/HTTPClient_HelloWorld/file/270e2d0bb85a/main.cpp
coolnik 3:e91a03086c72 40 printf("\nTrying to post data...\n");
coolnik 2:1ffd9e2ce7df 41 HTTPMap map;
coolnik 3:e91a03086c72 42 HTTPText inText(str2, 32000);
coolnik 3:e91a03086c72 43 map.put("client_id", "a1b2c3");
coolnik 3:e91a03086c72 44 map.put("client_secret", "drickyoughurt");
coolnik 3:e91a03086c72 45 map.put("token_type_hint", "pop_token"); //Sptäer durch pop ersetzen!!
coolnik 3:e91a03086c72 46 map.put("token", str);
coolnik 3:e91a03086c72 47
coolnik 3:e91a03086c72 48 //int ret = http.post("http://requestb.in/14qajp81", map, &inText);
coolnik 3:e91a03086c72 49 int ret = http.post("https://kongsugar.de/introspection", map, &inText);
coolnik 1:5f0df6a77d62 50 if (!ret)
coolnik 1:5f0df6a77d62 51 {
coolnik 3:e91a03086c72 52 printf("Page fetched successfully - read %d characters\n", strlen(str2));
coolnik 3:e91a03086c72 53 printf("Result: %s\n", str2);
coolnik 3:e91a03086c72 54 // Unlock, Light Green 5s
coolnik 3:e91a03086c72 55 R=!R;wait(5);G=!G;
coolnik 1:5f0df6a77d62 56 }
coolnik 1:5f0df6a77d62 57 else
coolnik 1:5f0df6a77d62 58 {
coolnik 1:5f0df6a77d62 59 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
coolnik 3:e91a03086c72 60 // Light Red 5s
coolnik 3:e91a03086c72 61 G=!G;wait(5);R=!R;
coolnik 1:5f0df6a77d62 62 }
coolnik 0:64d0faaa4e6e 63 }
coolnik 1:5f0df6a77d62 64
coolnik 0:64d0faaa4e6e 65 eth.disconnect();
coolnik 0:64d0faaa4e6e 66 }