Skip to content

Instantly share code, notes, and snippets.

@weex
Created June 8, 2016 03:18
Show Gist options
  • Save weex/f7abd311f9fd27dade1a191a679622a0 to your computer and use it in GitHub Desktop.
Save weex/f7abd311f9fd27dade1a191a679622a0 to your computer and use it in GitHub Desktop.
Particle Photon IoT board - Blink Bitcoin Price
#include "application.h"
#include "HttpClient/HttpClient.h"
int led2 = D7;
int count = 0;
int thousands;
int hundreds;
int tens;
int ones;
/**
* Declaring the variables.
*/
unsigned int nextTime = 0; // Next time to contact the server
HttpClient http;
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
// { "Content-Type", "application/json" },
// { "Accept" , "application/json" },
{ "Accept" , "*/*"},
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
void setup() {
pinMode(led2, OUTPUT);
}
void blink_count(int number, int ms){
for(int i = 0; i < number; i++){
digitalWrite(led2, HIGH);
delay(ms);
digitalWrite(led2, LOW);
delay(ms);
}
}
void loop() {
if (nextTime > millis()) {
return;
}
// Request path and body can be set at runtime or at setup.
request.hostname = "www.bitcoinexchangerate.org";
request.port = 80;
request.path = "/price-bitstamp.inc";
// Get request
if (count % 6 == 0) {
http.get(request, response, headers);
int price = (int)atof(response.body);
thousands = (price / 10000) % 10;
hundreds = (price / 100) % 10;
tens = (price / 10) % 10;
ones = price % 10;
}
blink_count(thousands, 1000);
delay(2000);
blink_count(hundreds, 250);
delay(2000);
blink_count(tens, 250);
delay(2000);
blink_count(ones, 250);
nextTime = millis() + 10000;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment