/************************************************** Det her er lige et arbejdsplads. Det skal blive en lille dims, som viser den nuværende pris på Bitcoins. Til at starte med skal den blot vise prisen (i USD), men senere kan det være at den skal begynde at vise flere valuta, og evt. også om prisen er steget eller faldet på det sidste. Måske både de sidste 15 min men også 24h. Dette er blot en prototype version med Arduino Uno og Adafruit CC3000 API Bitcoin market price data: https://bitcoinaverage.com/api.htm currencySelector values: 0 = USD 1 = GBP 2 = EUR ****************************************************/ #include #include #include #include #include "utility/debug.h" #include LiquidCrystal lcd(8, 7, 6, 4, 9, 2); // These are the interrupt and control pins #define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin! // These can be any two pins #define ADAFRUIT_CC3000_VBAT 5 #define ADAFRUIT_CC3000_CS 10 // Use hardware SPI for the remaining pins // On an UNO, SCK = 13, MISO = 12, and MOSI = 11 Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER); // you can change this clock speed #define WLAN_SSID "Network_name" // cannot be longer than 32 characters! #define WLAN_PASS "Passphrase" // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 #define WLAN_SECURITY WLAN_SEC_WPA2 #define IDLE_TIMEOUT_MS 1000 // Amount of time to wait (in milliseconds) with no data // received before closing the connection. If your internet connection // is faster than mine, you can decrease this number. You will have to try // and see what works best for you. // What page to grab! #define WEBSITE "api.coindesk.com" #define WEBPAGE "/v1/bpi/currentprice.json" #define WEBPAGEUSD "/ticker/USD/last" #define WEBPAGEEUR "/ticker/EUR/last" #define WEBPAGEGBP "/ticker/GBP/last" uint32_t ip; // Definér variabler her! // Knapper: const int buttonSettingsPin = A2; const int buttonDownPin = A1; const int buttonUpPin = A0; const int buttonAcceptPin = A4; int buttonSettingsState = LOW; int buttonUpState = LOW; int buttonDownState = LOW; int buttonAcceptState = LOW; unsigned long debounceSettings = 0; unsigned long debounceUp = 0; unsigned long debounceDown = 0; const int debounceDelay = 50; // Valuta: int currencySelect = 0; int currencySelectOld = 0; // Resten: boolean settingsOn = false; boolean isConnectedToSite = false; unsigned long waitTest = 0; const unsigned long waitTime = 60000; void setup(void){ pinMode(buttonSettingsPin, INPUT); pinMode(buttonUpPin, INPUT); pinMode(buttonDownPin, INPUT); lcd.begin(16, 2); lcd.print(F("BTC Price Ticker")); lcd.setCursor(0, 1); lcd.print(F("By Cavaleri")); delay(5000); Serial.begin(115200); /* Initialise the module */ lcd.clear(); lcd.setCursor(0, 0); lcd.print(F("Setting up")); lcd.setCursor(0, 1); lcd.print(F("WIFI connection")); connectToWifi(); ip = 0; // Try looking up the website's IP address while (ip == 0) { if (! cc3000.getHostByName(WEBSITE, &ip)) { Serial.println(F("Couldn't resolve!")); } delay(500); } Serial.println(F("Entering void loop")); } void loop(){ int readingSettings = digitalRead(buttonSettingsPin); if(readingSettings == HIGH){ lcd.clear(); lcd.setCursor(0, 0); lcd.print(F("Change currency")); lcd.setCursor(0, 1); if(currencySelect == 0){ lcd.print(F("US Dollars")); }else if(currencySelect == 1){ lcd.print(F("GB Pounds")); }else if(currencySelect == 2){ lcd.print(F("Euro")); } settingsOn = true; while(settingsOn){ // Read the UP button and change currencySelector! int readingUp = digitalRead(buttonUpPin); buttonUpState = readingUp; delay(50); readingUp = digitalRead(buttonUpPin); if(readingUp == buttonUpState && readingUp == HIGH){ if(currencySelect == 0){ currencySelect = 2; }else{ currencySelect = currencySelect - 1; } } // Read the DOWN button and change currencySelector! int readingDown = digitalRead(buttonDownPin); buttonDownState = readingDown; delay(50); readingDown = digitalRead(buttonDownPin); if(readingDown == buttonDownState && readingDown == HIGH){ if(currencySelect == 2){ currencySelect = 0; }else{ currencySelect = currencySelect + 1; } } // Change the LCD text to match new currency! if(currencySelect != currencySelectOld){ lcd.clear(); lcd.setCursor(0, 0); lcd.print(F("Change currency")); lcd.setCursor(0, 1); if(currencySelect == 0){ lcd.print(F("US Dollars")); }else if(currencySelect == 1){ lcd.print(F("GB Pounds")); }else if(currencySelect == 2){ lcd.print(F("Euro")); } currencySelectOld = currencySelect; } // Accept the new settings! int readingAccept = digitalRead(buttonAcceptPin); buttonAcceptState = readingAccept; delay(50); readingAccept = digitalRead(buttonAcceptPin); if(readingAccept == buttonAcceptState && readingAccept == HIGH){ waitTest = 0; settingsOn = false; } } } // Hvis den lige er kommet ud af settings eller // hvis det er 60 sekunder efter sidste tjek? // Brug funktion der sender HTTP GET request, // og henter prisen for den valute som er valgt. // Den skal vise på displayet at den opdaterer pris! if(waitTest == 0 || millis() - waitTest > waitTime || millis() - waitTime < 0){ lcd.clear(); lcd.setCursor(0, 0); lcd.print(F("Updating price!")); lcd.setCursor(0, 1); lcd.print(F("Please wait...")); /* Try connecting to the website. */ Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80); if (www.connected()) { www.fastrprint(F("GET ")); www.fastrprint(WEBPAGE); www.fastrprint(F(" HTTP/1.1\r\n")); www.fastrprint(F("Host: ")); www.fastrprint(WEBSITE); www.fastrprint(F("\r\n")); www.fastrprint(F("\r\n")); Serial.println(F("Connected to site")); isConnectedToSite = true; }else { Serial.println(F("Connection to site failed")); Serial.println(F("Trying to reconnect to Wifi")); lcd.clear(); lcd.setCursor(0, 0); lcd.print(F("Connection error")); lcd.setCursor(0, 1); lcd.print(F("Reconnecting")); cc3000.disconnect(); connectToWifi(); return; } /* Read data until either the connection is closed, or the idle timeout is reached. */ /* Only print out the body content */ /* and only if connected to site */ if (isConnectedToSite == true){ int newLines = 0; int charCount = 0; boolean stopWriting = false; String returnedContent = ""; unsigned long lastRead = millis(); while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) { while (www.available()) { char c = www.read(); if (c == 59){ newLines = newLines + 1; } if (newLines == currencySelect + 1){ charCount = charCount + 1; if(charCount > 11){ if(c == 34){ stopWriting = true; } if(stopWriting == false){ returnedContent += c; } } } lastRead = millis(); } } Serial.println(returnedContent); lcd.clear(); lcd.setCursor(0, 0); lcd.print(F("Current price")); lcd.setCursor(0, 1); if(currencySelect == 0){ lcd.print(returnedContent += " USD"); }else if(currencySelect == 1){ lcd.print(returnedContent += " GBP"); }else if(currencySelect == 2){ lcd.print(returnedContent += " EUR"); } www.close(); Serial.println(F("\r\nSleeping for 1 minute")); waitTest = millis(); } isConnectedToSite == false; } } void connectToWifi(void){ if (!cc3000.begin()) { Serial.println(F("Couldn't begin()! Check your wiring?")); while(1); } /* Connect to LAN & Internet */ if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { Serial.println(F("Failed!")); while(1); } /* Wait for DHCP to complete */ Serial.println(F("Request DHCP")); boolean DHCPfail = false; int timeoutDHCP = 0; while (!cc3000.checkDHCP() && DHCPfail == false) { delay(100); // ToDo: Insert a DHCP timeout! timeoutDHCP = timeoutDHCP + 1; if(timeoutDHCP > 300){ DHCPfail = true; } } if(DHCPfail == true){ Serial.println(F("DHCP failed!")); lcd.clear(); lcd.setCursor(0, 0); lcd.print(F("DHCP failed!")); lcd.setCursor(0, 1); lcd.print(F("Please restart")); while(1); } Serial.println(F("Connected!")); }