Skip to content

Instantly share code, notes, and snippets.

@bryanmonti
Created November 27, 2017 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanmonti/d3c3ddb0897ca62ded399a2c331ceab3 to your computer and use it in GitHub Desktop.
Save bryanmonti/d3c3ddb0897ca62ded399a2c331ceab3 to your computer and use it in GitHub Desktop.
/**
The MIT License (MIT)
Copyright (c) 2016 by Daniel Eichhorn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This sketch created by JohnnyFRX 17Jun17
*/
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"'
#include <WiFi.h>
#include <WiFiMulti.h>
#include <string>
#include <math.h>
//#include <utility\Fonts\FreeMonoBold24pt7b.h>
//#include <utility\Fonts\FreeSans9pt7b.h>
#include <vector>
#include <string>
using namespace std;
WiFiMulti WiFiMulti;
int status = WL_IDLE_STATUS;
int lastPrice = 0;
int currentPrice;
int minPrice = 9999999;
int maxPrice = 0;
char servername[] = "api.coindesk.com"; // Google
String answer;
WiFiClient client;
int sensorPin = 36; // select the input pin for ldr
int sensorValue = 0; // variable to store the value coming from the sensor
// Initialize the OLED display using Wire library
SSD1306 display(0x3c, 5, 4);
vector<string> split(const char *str, char c = '|')
{
vector<string> result;
do
{
const char *begin = str;
while (*str != c && *str)
str++;
result.push_back(string(begin, str));
} while (0 != *str++);
return result;
}
void ConnectToClient() {
if (client.connect(servername, 80)) {
// Make a HTTP request:
Serial.println("Connecting to coindesk.com");
client.print(String("GET ") + "/v1/bpi/currentprice.json HTTP/1.1\r\n" +
"Host: api.coindesk.com\r\n" +
"Connection: close\r\n\r\n");
Serial.println("Connected to host");
}
}
void setup() {
pinMode(sensorPin, INPUT);
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_16);
WiFiMulti.addAP("Cisco-2.4Ghz", "bryanjmonti");
Serial.begin(115200);
Serial.println("Connected");
while (WiFiMulti.run() != WL_CONNECTED) {
delay(500);
Serial.println("Waiting to connect to wifi");
}
Serial.println("About to connect to client");
ConnectToClient();
}
void loop() {
//sensorValue = analogRead(sensorPin);
//delay(1000);
display.clear();
Serial.println("In the loop");
while (client.available()) {
char c = client.read();
answer += c;
}
Serial.println("Fucking finally disconnected");
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println("Server disconnected");
client.stop();
String jsonAnswer;
int jsonIndex;
for (int i = 0; i < answer.length(); i++) {
if (answer[i] == '{') {
jsonIndex = i;
break;
}
}
jsonAnswer = answer.substring(jsonIndex);
jsonAnswer.trim();
int rateIndex = jsonAnswer.indexOf("rate_float");
String priceString = jsonAnswer.substring(rateIndex + 12, rateIndex + 19);
Serial.println(jsonAnswer);
Serial.println("");
Serial.println(priceString);
priceString.trim();
int decimalplace = priceString.indexOf(".");
String Dollars = priceString.substring(0, decimalplace);
String Cents = priceString.substring(decimalplace + 1);
while (Cents.length() < 2) {
Cents += "0";
}
String Amount = "$" + Dollars + "." + Cents;
currentPrice = (Dollars + Cents).toInt();
if (currentPrice < minPrice || currentPrice > maxPrice ) {
Serial.println("determined new min/max price");
}
minPrice = std::min(minPrice, currentPrice);
maxPrice = std::max(maxPrice, currentPrice);
double maxxPrice = maxPrice * 0.01;
double minnPrice = minPrice * 0.01;
String min_text = "Min: ";
String minPriceConv = String(minnPrice);
String minPricePrint = min_text + minPriceConv;
String max_text = "Max: ";
String maxPriceConv = String(maxxPrice);
String maxPricePrint = max_text + maxPriceConv;
display.setColor(WHITE);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(0, 0, Amount);
display.drawString(0, 16, minPricePrint);
display.drawString(0, 32, maxPricePrint);
display.setFont(ArialMT_Plain_16);
display.display();
delay(10);
answer = "";
Amount = "";
currentPrice = 0;
ConnectToClient();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment