New Paste

 

Recent Pastes

Administrate




Pastebin on paste.luisaranguren.com

PasteID: 2ag
Pasted by Anonymous, 2017-08-14 01:17:13 GMT
Expires Never
Paste size 4.13 Kb
Tools Raw   Download
 
  1. cat coinjar.cpp   
  2. // Output libs and curl  
  3. #include <iostream>  
  4. #include <string>  
  5. #include <curl/curl.h>  
  6. // Include Rpi-hw headers  
  7. #include <rpi-hw.hpp>  
  8. #include <rpi-hw/time.hpp>  
  9. #include <rpi-hw/iface/decoder-out.hpp>  
  10. #include <rpi-hw/display/m7seg.hpp>  
  11. // Include the jsoncpp for JSON parsing  
  12. #include <jsoncpp/json/json.h>  
  13. // Use the Rpi-hw namespace  
  14. using namespace rpihw;  
  15. using namespace std;  
  16. // Magically convert curl output into a string  
  17. static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)  
  18. {  
  19.     ((std::string*)userp)->append((char*)contents, size * nmemb);  
  20.     return size * nmemb;  
  21. }  
  22. int main( int argc, char *args[] )  
  23. {  
  24.     // Multiple seven-segment display controller  
  25.     display::m7seg dev( 1, 4, 18, 15, 14, 0, 17, 23 );  
  26.     // Create the enabler interface  
  27.     iface::decoderOut enabler( { 21, 22, 24, 10 } );  
  28.     // Set the number of displays and the enabler interface  
  29.     dev.setDisplays( 4, enabler );  
  30.     // Set the updating frequency (Hz)  
  31.     dev.setFreq( 200.0 );  
  32.     // Initialization display  
  33.     dev.set ( 8888 );  
  34.     // Set the format of the display  
  35.     dev.format( 0, true );  
  36.     // Counter  
  37.     // Main loop  
  38.     float i = 0.0;  
  39.     for ( ;; )  
  40.     {  
  41.         CURL *curl;  
  42.         CURLcode res;  
  43.         // Declare variable where cURL will write contents to  
  44.         std::string readBuffer;  
  45.         curl = curl_easy_init();  
  46.         if(curl)  
  47.         {  
  48.             // Set options for curl easy handle  
  49. 	    // Verbose, YEAY !  
  50. 	    curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);	  
  51.             // Adds URL to CURLOPT_URL  
  52.             curl_easy_setopt(curl, CURLOPT_URL, "https://w7mgDPuEtXe7Cu8cus7dmUFNDqA2tXmojmetysmpZi8b4yEE:@api.coinjar.io/v1/fair_rate/AUD.json");  
  53. 	    // HTTPS Handshake  
  54. 	    curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);  
  55. 	    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);  
  56. 	    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);  
  57.             //To copy curl results into a string  
  58.             curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);  
  59.             curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);  
  60.             // Adds Follow any location = 1 -> CURLOPT_FOLLOWLOCATION  
  61.             curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);  
  62.             // Perform the request, res will get the return code  
  63.             res = curl_easy_perform(curl);  
  64.             // Check for errors  
  65.             if(res != CURLE_OK) {  
  66.                 /* fprintf(stderr, "curl_easy_perform() failed: %s\n",  
  67.                         curl_easy_strerror(res)); */  
  68.                 cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << endl;  
  69. 		dev.set ( 8888 );  
  70.                 time::msleep( 10000 );  
  71.                 continue;  
  72.             }  
  73.             //Results to stdout  
  74. //             cout << readBuffer << endl;  
  75.             // always cleanup  
  76.             curl_easy_cleanup(curl);  
  77.         }  
  78.         // Assing variables  
  79.         Json::Value root;  
  80.         Json::Reader reader;  
  81.         // Start parsing of the curl -> string  
  82.         bool parsedSuccess = reader.parse( readBuffer, root );  
  83.         if( !parsedSuccess )  
  84.         {  
  85.             //Didn't parse it  
  86.             cout << "Failed" << endl;  
  87.             return 1;  
  88.         }  
  89.         // Extract BTC price  from the parsed result  
  90.         const Json::Value BTCprice = root["ask"];  
  91.         // Display to stdout the parsed result  
  92. //        cout << "price: " << BTCprice.asString() << endl;  
  93.         // Convert price to integer  
  94.         double price = stod(BTCprice.asString());  
  95.         // Print price  
  96.         cout << "Price: " << price << endl;  
  97.         // Print the modulo of the int  
  98.         //printf("Modulo: ");  
  99.         //printf("%2.0fn", price);  
  100. 	// Reset display temporarily  
  101. 	dev.set ( 8888 );  
  102. 	time::msleep ( 200 );  
  103.         // Set the value of the display  
  104.         dev.set( price );  
  105.         // Increment value of the display  
  106.         i += 0.1;  
  107.         // Wait some time  
  108.         time::msleep( 10000 );  
  109.     }  
  110.     return 0;  
  111. }  
  112. /*  
  113.           1  
  114.         ######  
  115.     0  #      # 4  
  116.        #  17  #  
  117.         ######  
  118.     14 #      # 18  
  119.        #      #  
  120.         ######  # 23  
  121.          15  
  122. */  
  123.  
 
 
 
 
Written by Xan Manning, 2010.