Last active
December 18, 2015 21:09
-
-
Save bugduino/5845439 to your computer and use it in GitHub Desktop.
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
/* | |
Google Stock (GOOG) quotation on LCD | |
Arduino + Ethernet shield + LCD Screen | |
*/ | |
// Include lib for Ethernet and LCD | |
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <LiquidCrystal.h> | |
// Variables | |
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address | |
char server[] = "www.google.com"; | |
EthernetClient client; | |
LiquidCrystal lcd(8, 9, 5, 7, 3, 2); //Don't use pin 4, 10, 11, 12, 13 (or A0, A1) | |
//because they are used by the ethernet shield | |
void setup() | |
{ | |
Serial.begin(9600); //Serial used for debugging | |
if(Ethernet.begin(mac) == 0) // Start ethernet connection usgin DHCP (no ip given) | |
{ | |
Serial.println("Error on DHCP configuration"); | |
while(true) ;//do nothing forever | |
} | |
delay(1000); //let ethernet connection starts | |
IPAddress ip = Ethernet.localIP(); | |
Serial.print("IP: "); | |
Serial.println(ip); | |
if (client.connect(server, 80) == 1) //connect to "server" (on port 80) | |
{ | |
Serial.println("Connected"); | |
client.println("GET /finance?q=google HTTP/1.0"); //get google stock quotation html page | |
client.println(); | |
lcd.begin(16, 2); | |
} | |
else | |
{ | |
Serial.println("Connection failed"); | |
} | |
lcd.print("Searching Google"); | |
lcd.setCursor(0, 1); | |
lcd.print("quotation ..."); | |
} | |
void loop() | |
{ | |
if (client.available()) | |
{ | |
char c = client.read(); //read html page from server | |
if (client.find("<span class=\"pr\">")) //find the html tag in the page www.google.com/finance?q=google | |
{ // right behind the quotation ( " in html becomes \" insiede the sketch | |
client.find(">"); | |
lcd.setCursor(0, 0); | |
lcd.clear(); //preparing to display datas | |
lcd.print("GOOGLE: "); | |
float value = client.parseFloat(); //read the value | |
Serial.print("Google Stock quotation: "); | |
Serial.println(value); | |
lcd.print(value); | |
} | |
} | |
if (!client.connected()) //disconnected | |
{ | |
Serial.println("Disconnected"); | |
lcd.setCursor(0, 1); | |
lcd.print("Disconnected"); | |
client.stop(); | |
while(true) ; | |
} | |
} | |
/* | |
Author: William Bergamo | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment