Last active
October 3, 2015 03:05
-
-
Save a-r-d/40cc5d3ddf39a4b2556d to your computer and use it in GitHub Desktop.
Pull data from markitondemand and send it to my arduino's LCD screen that sits above my dining room table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import math | |
import urllib2 | |
import json | |
import traceback | |
ser = None | |
MARKIT_PAUSE = 3 | |
#MOUNT = "/dev/ttyACM0" #raspi | |
MOUNT = "COM3" #windows | |
URL_BASE = "http://dev.markitondemand.com/Api/v2/Quote/json" | |
SYMBOLS_1 = ["AAPL" , "MSFT", "NFLX"] | |
SYMBOLS_2 = ["JNJ", "GE", "KR"] | |
SYMBOLS_3 = ["CTAS", "BP", "CVX"] | |
SYMBOLS_4 = ["SCTY", "FB", "AMZN"] | |
SYMBOLS_A = [SYMBOLS_1, SYMBOLS_2, SYMBOLS_3, SYMBOLS_4] | |
SOCKET_ON = True | |
if SOCKET_ON: | |
import serial | |
ser = serial.Serial(MOUNT, 9600) | |
def send( a_string ): | |
global ser | |
# Markit gives some unicode but we dont actually need it | |
a_string = a_string.encode('ascii','ignore') | |
print a_string | |
if SOCKET_ON: | |
ser.write(a_string) | |
def sendLine(line, val): | |
print "Sending line " + line + ", " + val | |
tosend = line + ":" + val | |
if(len(tosend) > 64): | |
print "WARNING: string will be cut off by serial bufffer (64 bytes)" | |
send(tosend) | |
def getMarkitData(symbols): | |
quotes = [] | |
for s in symbols: | |
url = URL_BASE + "?symbol=" + s | |
print "Query URL: " + url | |
req = urllib2.Request(url) | |
response = urllib2.urlopen(req) | |
quotes.append(json.loads(response.read())) | |
# Don't hit their API super hard, they have a req/sec limit | |
time.sleep(MARKIT_PAUSE) | |
return quotes | |
def processMarkitData(data): | |
quote_strings = "" | |
for quote in data: | |
#print quote | |
if not "Symbol" in quote: | |
continue | |
quote_strings += quote["Symbol"] + ":" + \ | |
str(round(quote["LastPrice"], 2)) + \ | |
", " + str(round(quote["ChangePercent"], 2)) + "% " | |
return quote_strings | |
def sendSymbols(symbols, line): | |
data = getMarkitData(symbols) | |
strings = processMarkitData(data) | |
sendLine(line, strings) | |
""" | |
Give it a second or two between sending lines, otherwise it will interpret them | |
as the same line. Easier to wait, than programming in line split chars | |
and a method to do that on the board. | |
""" | |
def doUpdate(): | |
time.sleep(2) | |
try: | |
i = 0 | |
for s in SYMBOLS_A: | |
i += 1 | |
sendSymbols(s, str(i)) | |
time.sleep(3) | |
except Exception, e: | |
print str(e) | |
print(traceback.format_exc()) | |
time.sleep(2) | |
sendLine("4", str(e)) | |
if __name__ == "__main__": | |
doUpdate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LiquidCrystal_SR3W.h> | |
#include <LiquidCrystal_SR2W.h> | |
#include <FastIO.h> | |
#include <LiquidCrystal.h> | |
#include <String.h> | |
#include <LCD.h> | |
#include <I2CIO.h> | |
#include <LiquidCrystal_I2C.h> | |
#include <LiquidCrystal_SR.h> | |
#include <Wire.h> | |
#include <LCD.h> | |
#include <LiquidCrystal_I2C.h> | |
/* | |
** Example Arduino sketch for SainSmart I2C LCD2004 adapter for HD44780 LCD screens | |
** Readily found on eBay or http://www.sainsmart.com/ | |
** The LCD2004 module appears to be identical to one marketed by YwRobot | |
** | |
** Address pins 0,1 & 2 are all permenantly tied high so the address is fixed at 0x27 | |
** | |
** Written for and tested with Arduino 1.0 | |
** This example uses F Malpartida's NewLiquidCrystal library. Obtain from: | |
** https://bitbucket.org/fmalpartida/new-liquidcrystal | |
** | |
** Edward Comer | |
** LICENSE: GNU General Public License, version 3 (GPL-3.0) | |
** | |
** NOTE: TEsted on Arduino NANO whose I2C pins are A4==SDA, A5==SCL | |
* | |
* 10/2/2015 - ard | |
* NOTE: remove the included ardiuno library called "LiquidCrystal" and include the NewLiquidCrystal library (I used tag 'V 1.3.1') | |
* https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads | |
* | |
* Also, don't re-order the imports, they need to go in this order | |
* | |
* | |
*/ | |
#define I2C_ADDR 0x27 // Define I2C Address where the PCF8574A is | |
#define BACKLIGHT_PIN 3 | |
#define En_pin 2 | |
#define Rw_pin 1 | |
#define Rs_pin 0 | |
#define D4_pin 4 | |
#define D5_pin 5 | |
#define D6_pin 6 | |
#define D7_pin 7 | |
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); | |
// led controller: | |
int num_leds = 10; | |
int led10 = 13; | |
int led9 = 12; | |
int led8 = 11; | |
int led7 = 10; | |
int led6 = 9; | |
int led5 = 8; | |
int led4 = 7; | |
int led3 = 6; | |
int led2 = 5; | |
int led1 = 4; | |
int led_side_light = 2; | |
int led_indicator_array[] = { | |
led10, | |
led9, | |
led8, | |
led7, | |
led6, | |
led5, | |
led4, | |
led3, | |
led2, | |
led1 | |
}; | |
void setup() | |
{ | |
// init lcd screen | |
lcdInit(); | |
// init leds: | |
initLedPins(); | |
// init serial cxn. | |
sendSerialDataInit(); | |
} | |
void lcdInit(){ | |
lcd.begin (20,4); | |
// Switch on the backlight | |
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); | |
lcd.setBacklight(HIGH); | |
lcd.home (); | |
} | |
void initLedPins(){ | |
for( int i=0; i < sizeof(led_indicator_array); i++) { | |
pinMode( led_indicator_array[i], OUTPUT); | |
digitalWrite( led_indicator_array[i], LOW); | |
} | |
pinMode( led_side_light, OUTPUT); | |
digitalWrite( led_side_light, LOW); | |
} | |
void sendSerialDataInit(){ | |
Serial.begin(9600); | |
Serial.println("\nThe LCD is on, serial listening for data."); | |
} | |
String getLine( String input_str, int iter, int width){ | |
int len_str = input_str.length(); | |
// e.g. 34983 % 50 | |
int mod = iter % len_str; // n can be up max integer | |
int position_start = mod; | |
int position_end = mod + width; | |
//Serial.println("start " + position_start); | |
//Serial.println("end " + position_end); | |
// case short string | |
if( len_str <= width ) { | |
return input_str; | |
} | |
// case @ end of string- get some of the first part of the string also. | |
if(position_end > len_str) { | |
String toDisplay = input_str.substring(position_start, input_str.length()) + input_str.substring(0, position_end - len_str); | |
return toDisplay; | |
} | |
// case @ medium | |
return input_str.substring( position_start, position_end); | |
} | |
// other misc globals: | |
int n = 0; | |
// Pull strings off of serial and read-em! | |
String line1 = "no data ."; | |
String line2 = ".:--:_=|-_-^-_-^-_-^-_-^-_-^-___-^-()_"; | |
String line3 = "no data ..."; | |
String line4 = "no data ..."; | |
void setLine(String content){ | |
if(content.substring(0, 2) == "1:"){ | |
line1 = content.substring(2, content.length()); | |
Serial.println("Line 1: " + line1); | |
} else if(content.substring(0, 2) == "2:"){ | |
line2 = content.substring(2, content.length()); | |
Serial.println("Line 2: " + line2); | |
} else if(content.substring(0, 2) == "3:"){ | |
line3 = content.substring(2, content.length()); | |
Serial.println("Line 3: " + line3); | |
} else if(content.substring(0, 2) == "4:"){ | |
line4 = content.substring(2, content.length()); | |
Serial.println("Line 4: " + line4); | |
} | |
} | |
void readSerialAndSetLine(){ | |
String content = ""; | |
char character; | |
while(Serial.available()) { | |
character = Serial.read(); | |
content.concat(character); | |
} | |
// send it back to be sure: | |
if (content != "") { | |
Serial.println(content); | |
} | |
setLine(content); | |
} | |
void loop() | |
{ | |
readSerialAndSetLine(); | |
if(n <= 0) n = 0; // not a uint | |
lcd.setCursor ( 0, 0 ); | |
lcd.print(getLine(line1, n, 20)); | |
lcd.setCursor ( 0, 1 ); // go to the 2nd line | |
lcd.print(getLine(line2, n, 20)); | |
lcd.setCursor ( 0, 2 ); // go to the third line | |
lcd.print(getLine(line3, n, 20)); | |
lcd.setCursor ( 0, 3 ); // go to the fourth line | |
lcd.print(getLine(line4, n, 20)); | |
// example: | |
//lcd.setCursor (14,3); // go col 14 of line 3 | |
//lcd.print(n++,DEC); | |
Serial.println("\n Iteration: " + (String)n); | |
//lcd.setBacklight(HIGH); // Backlight on | |
delay(1500); | |
n++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment