The Ethernet-Board

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

Fork of sose2016_tr_oauth_pop by niko gillen

main.cpp

Committer:
coolnik
Date:
2016-06-23
Revision:
3:e91a03086c72
Parent:
2:1ffd9e2ce7df
Child:
4:00b8d595aa33

File content as of revision 3:e91a03086c72:

#include "mbed.h"
#include "EthernetInterface.h"
#include "HTTPClient.h"

DigitalOut R(LED_RED);
DigitalOut G(LED_GREEN);
DigitalOut B(LED_BLUE);

Serial serial(USBTX, USBRX); // tx, rx
EthernetInterface eth;
HTTPClient http;
char str[32000];
char str2[32000];

int main() {
    setbuf(stdout, NULL);
    eth.init(); //Use DHCP
    eth.connect();
    http.dumpResHeader(true);
    R=!R;G=!G;B=!B;
    wait(1);

    while(1){
        // Wait for Token on Serial and read to str
        // Light Blue
        B=!B;
        printf("Erwarte Eingabe: ");        
        while(1){
               if(serial.readable()){
                    serial.scanf("%s",&str);
                    break;
                }
        }
        // Token Read, light Yellow
        R=!R;G=!G;B=!B;
        printf(str);
        
        // Send token to Server
        // POST date, more examples on https://developer.mbed.org/users/donatien/code/HTTPClient_HelloWorld/file/270e2d0bb85a/main.cpp
        printf("\nTrying to post data...\n");
        HTTPMap map;
        HTTPText inText(str2, 32000);
        map.put("client_id", "a1b2c3");
        map.put("client_secret", "drickyoughurt");
        map.put("token_type_hint", "pop_token"); //Sptäer durch pop ersetzen!!
        map.put("token", str);
        
        //int ret = http.post("http://requestb.in/14qajp81", map, &inText);    
        int ret = http.post("https://kongsugar.de/introspection", map, &inText); 
        if (!ret)
        {
          printf("Page fetched successfully - read %d characters\n", strlen(str2));
          printf("Result: %s\n", str2);
          // Unlock, Light Green 5s
          R=!R;wait(5);G=!G;
        }
        else
        {
          printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
          // Light Red 5s
          G=!G;wait(5);R=!R;
        }
    }
    
    eth.disconnect();  
}