Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * NewTimeSerial.pde
- * example code illustrating Time library date strings
- *
- * Time can be set using an 11 character message consisiting of the letter 'T'
- * followed by the ten digit number of seconds since Jan 1 1970 (Unix time)
- *
- * The time can also be set as follows:
- * S[hh:mm:ss dd mm yy]
- * Values must be given in order starting from hh
- * S[14] sets the hour to 2pm, S[14:10] sets 2:10 pm
- * values must be two digits with preceeding 0 if necessary (eg 05 to set the value 5)
- * year must be two digits, 2010 is entered as 10
- * opening and closing square brackest are required, colon and spaces are optional
- *
- *
- */
- #include "Wire.h"
- #define DS1307_I2C_ADDRESS 0x68 // This is the I2C address
- // Arduino version compatibility Pre-Compiler Directives
- #if defined(ARDUINO) && ARDUINO >= 100 // Arduino v1.0 and newer
- #define I2C_WRITE Wire.write
- #define I2C_READ Wire.read
- #else // Arduino Prior to v1.0
- #define I2C_WRITE Wire.send
- #define I2C_READ Wire.receive
- #endif
- #include <Time.h>
- #include <LiquidCrystal.h>
- #include <SPI.h>
- #include <Ethernet.h>
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
- char serverName[] = "www.cryptofolio.info"; // web page
- EthernetClient client;
- #define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
- #define TIME_HEADER 'T' // Header tag for serial time sync message
- #define TIME_SET_TAG 'S' // Indicates start of time set string
- #define TIME_SET_StrLen 15 // the maximum length of a time set string [hhmmssddmmyyyy]
- #define TIME_REQUEST 7 // ASCII bell character requests a time sync message
- TimeElements te; // holds the time elements for setting the time
- // initialize the library with the numbers of the interface pins
- LiquidCrystal lcd(9, 8, 6, 5, 4, 3);
- int ledPin = 7; // LED connected to digital pin 13
- int inPin = 2; // IR Button connected to digital pin 7
- // Variables will change:
- volatile int ledState = HIGH; // ledState used to set the LED
- //long previousMillis = 0; // will store last time LED was updated
- // the follow variables is a long because the time, measured in miliseconds,
- // will quickly become a bigger number than can be stored in an int.
- long interval = 250; // interval at which to blink (milliseconds)
- // Global Variables
- int command = 0; // This is the command char, in ascii form, sent from the serial port
- int i;
- long previousMillis = 0; // will store last time Temp was updated
- //byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
- byte test;
- byte zero;
- char *Day[] = {"","Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
- char *Mon[] = {"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
- String TotalBTC = "TotalBTC";
- String GrandTotal = "GrandTotal";
- int timeCount = 0;
- void setup()
- {
- Wire.begin();
- //Serial.begin(57600);
- zero=0x00;
- pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
- pinMode(inPin, INPUT); // sets the digital pin 7 as input
- attachInterrupt(0, backlight, LOW); // 0 = digital pin 2
- // set up the LCD's number of columns and rows:
- lcd.begin(16, 2);
- Serial.begin(9600);
- setSyncInterval(1800);
- setSyncProvider(requestSync); //set function to call when sync required
- Ethernet.begin(mac);
- Serial.println(Ethernet.localIP());
- Startup();
- }
- void loop()
- {
- if (Serial.available()) { // Look for char in serial que and process if found
- command = Serial.read();
- if (command == 84 || command == 116) { //If command = "Tt" Set Date
- setDateDs1307();
- getDateDs1307();
- Serial.println(" ");
- }
- else if (command == 82 || command == 114) { //If command = "Rr" Read Date ... BBR
- getDateDs1307();
- Serial.println(" ");
- }
- }
- processSetTime();
- if(timeStatus()!= timeNotSet)
- {
- digitalClockDisplay();
- }
- timeCount++;
- Serial.println(timeCount);
- if(timeCount == 60)
- {
- GetData("TotalBTC");
- }
- else if( timeCount == 120)
- {
- timeCount = 0;
- GetData("GrandTotal");
- }
- delay(1000);
- //backlight();
- }
- void GetData(String type)
- {
- if (client.connect(serverName, 80))
- { //starts client connection, checks for connection
- Serial.println("connected");
- client.println("GET /api.aspx?User=<!!!!passprhase here!!!>&Exchange=BTC-E&LocalCurrency=AUD&Result="+type+" HTTP/1.0"); //download text
- client.println("Host: www.cryptofolio.info");
- client.println(); //end of get request
- int counter = 0;
- boolean goTime = false;
- while(client.connected() && !client.available()) delay(1); //waits for data
- while (client.connected() || client.available())
- { //connected or data available
- char c = client.read(); //gets byte from ethernet buffer
- Serial.print(c); //prints byte to serial monitor
- if( c == char(124))
- {
- goTime = !goTime;
- if(goTime)
- {
- if(type == "TotalBTC")
- TotalBTC = "";
- else
- GrandTotal = "";
- }
- }
- if(goTime)
- {
- if(c != char(124))
- {
- if(type == "TotalBTC")
- {
- TotalBTC += c ;
- }
- else
- {
- GrandTotal += c ;
- }
- counter = counter + 1;
- }
- }
- }
- }
- else
- {
- Serial.println("connection failed");
- Serial.println("disconnecting");
- client.stop();
- GetData(type);
- }
- }
- void backlight()
- {
- //tone1.stop();
- unsigned long currentMillis = millis();
- if(currentMillis - previousMillis > interval) {
- // save the last time you blinked the LED
- previousMillis = currentMillis;
- // if the LED is off turn it on and vice-versa:
- if (ledState == LOW)
- ledState = HIGH;
- else
- ledState = LOW;
- // set the LED with the ledState of the variable:
- digitalWrite(ledPin, ledState);
- }
- }
- void Startup()
- {
- digitalWrite(ledPin, LOW );
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("WELCOME");
- delay(50);
- lcd.setCursor(0, 0);
- lcd.print(" WELCOME");
- delay(100);
- lcd.setCursor(0, 0);
- lcd.print(" WELCOME");
- delay(200);
- lcd.setCursor(0, 0);
- lcd.print(" WELCOME");
- delay(250);
- lcd.setCursor(0, 0);
- lcd.print(" WELCOME");
- delay(450);
- lcd.clear();
- digitalWrite(ledPin, HIGH );
- lcd.print("Waiting...");
- }
- void digitalClockDisplay()
- {
- if(second() < 10 || (second() > 20 && second() <30) || (second() > 40 && second() < 50))
- {
- lcd.setCursor(0, 0);
- lcd.print( " "+TotalBTC+" ");
- Serial.println(TotalBTC);
- //bottom of display
- lcd.setCursor(0, 1);
- String dmy = " " + strWeekday() + " "+printDigits(day())+"/"+printDigits(month())+"/"+(String)year();
- lcd.print( dmy);
- }
- else
- {
- lcd.setCursor(0, 0);
- lcd.print( " "+GrandTotal+" ");
- Serial.println( GrandTotal);
- //bottom of display
- lcd.setCursor(0, 1);
- int hour12 = hourFormat12();
- if(isAM())
- lcd.print(" "+printDigits(hour12) +":" + printDigits(minute()) +" AM" + " ");// + ":" + printDigits(second())
- else
- lcd.print(" "+printDigits(hour12) +":" + printDigits(minute()) +" PM" + " ");//+ ":" + printDigits(second())
- }
- }
- String strWeekday()
- {
- String week = "";
- switch (weekday())
- {
- case 1:
- week = "Sun";
- break;
- case 2:
- week = "Mon";
- break;
- case 3:
- week = "Tue";
- break;
- case 4:
- week = "Wed";
- break;
- case 5:
- week = "Thu";
- break;
- case 6:
- week = "Fri";
- break;
- case 7:
- week = "Sat";
- break;
- }
- return week;
- }
- String printDigits(int digits)
- {
- // utility function for digital clock display: prints preceding colon and leading 0
- String result = "";
- if(digits < 10)
- result += "0";
- result += digits;
- return result;
- }
- void processSetTime(){
- // //[hh:mm:ss dd mm yyyy]
- // char setString[TIME_SET_StrLen];
- // int index = 0;
- // char c = Serial.read();
- // if( c != '[')
- // return; // first character must be opening square brackets
- // do
- // {
- // c = Serial.read();
- // if( isdigit(c)) // non numeric characters are discarded
- // setString[index++] = c -'0'; // convert from ascii
- // }
- // while (c != ']'); // wait for trailing square brackets
- //
- // breakTime(now(), te); // put the time now into the TimeElements structure
- //
- // int count = index;
- // int element = 0;
- // for( index = 0; index < count; index += 2) // step through each pair of digits
- // {
- // int val = setString[index] * 10 + setString[index+1] ; // get the numeric value of the next pair of numbers
- // switch( element++){
- // case 0 : te.Hour = val; break;
- // case 1 : te.Minute = val; break;
- // case 2 : te.Second = val; break;
- // case 3 : te.Day = val; break;
- // case 4 : te.Month= val; break;
- // case 5 : te.Year = val + 30; break; // year 0 is 1970
- // }
- // }
- // setTime( makeTime(te));
- // Reset the register pointer
- Wire.beginTransmission(DS1307_I2C_ADDRESS);
- I2C_WRITE(zero);
- Wire.endTransmission();
- Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
- // A few of these need masks because certain bits are control bits
- te.Second = bcdToDec(I2C_READ() & 0x7f);
- te.Minute = bcdToDec(I2C_READ());
- te.Hour = bcdToDec(I2C_READ() & 0x3f); // Need to change this if 12 hour am/pm
- int dayOfWeek = bcdToDec(I2C_READ());
- //dayOfMonth = bcdToDec(I2C_READ());
- te.Day = bcdToDec(I2C_READ());
- te.Month = bcdToDec(I2C_READ());
- te.Year = bcdToDec(I2C_READ()) + 30;
- setTime( makeTime(te));
- }
- void processSyncMessage() {
- // // wait for ten digts, convert to time and update systemTime
- // // todo - timeout after say 5 seconds
- // time_t pctime = 0;
- //
- //
- // while(Serial.available() < TIME_MSG_LEN-1 ) // time message consists of a header and ten ascii digits
- // ;
- // for(int i=0; i < TIME_MSG_LEN -1; i++)
- // {
- // char c = Serial.read();
- // if( c >= '0' && c <= '9'){
- // pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
- // }
- // }
- // setTime(pctime); // Sync Arduino clock to the time received on the serial port
- }
- time_t requestSync()
- {
- //Serial.print(TIME_REQUEST,BYTE);
- //Serial.print(1);
- return 0; // the time will be sent later in response to serial mesg
- }
- // Convert binary coded decimal to normal decimal numbers
- byte bcdToDec(byte val)
- {
- return ( (val/16*10) + (val%16) );
- }
- // Convert normal decimal numbers to binary coded decimal
- byte decToBcd(byte val)
- {
- return ( (val/10*16) + (val%10) );
- }
- // 1) Sets the date and time on the ds1307
- // 2) Starts the clock
- // 3) Sets hour mode to 24 hour clock
- // Assumes you're passing in valid numbers, Probably need to put in checks for valid numbers.
- void setDateDs1307()
- {
- byte second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.
- byte minute = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
- byte hour = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
- byte dayOfWeek = (byte) (Serial.read() - 48);
- byte dayOfMonth = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
- byte month = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
- byte year= (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
- Wire.beginTransmission(DS1307_I2C_ADDRESS);
- I2C_WRITE(zero);
- I2C_WRITE(decToBcd(second) & 0x7f); // 0 to bit 7 starts the clock
- I2C_WRITE(decToBcd(minute));
- I2C_WRITE(decToBcd(hour)); // If you want 12 hour am/pm you need to set
- // bit 6 (also need to change readDateDs1307)
- I2C_WRITE(decToBcd(dayOfWeek));
- I2C_WRITE(decToBcd(dayOfMonth));
- I2C_WRITE(decToBcd(month));
- I2C_WRITE(decToBcd(year));
- Wire.endTransmission();
- }
- // Gets the date and time from the ds1307 and prints result
- void getDateDs1307()
- {
- // Reset the register pointer
- Wire.beginTransmission(DS1307_I2C_ADDRESS);
- I2C_WRITE(zero);
- Wire.endTransmission();
- Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
- // A few of these need masks because certain bits are control bits
- byte second = bcdToDec(I2C_READ() & 0x7f);
- byte minute = bcdToDec(I2C_READ());
- byte hour = bcdToDec(I2C_READ() & 0x3f); // Need to change this if 12 hour am/pm
- byte dayOfWeek = bcdToDec(I2C_READ());
- byte dayOfMonth = bcdToDec(I2C_READ());
- byte month = bcdToDec(I2C_READ());
- byte year = bcdToDec(I2C_READ());
- if (hour < 10)
- Serial.print("0");
- Serial.print(hour, DEC);
- Serial.print(":");
- if (minute < 10)
- Serial.print("0");
- Serial.print(minute, DEC);
- Serial.print(":");
- if (second < 10)
- Serial.print("0");
- Serial.print(second, DEC);
- Serial.print(" ");
- Serial.print(Day[dayOfWeek]);
- Serial.print(", ");
- Serial.print(dayOfMonth, DEC);
- Serial.print(" ");
- Serial.print(Mon[month]);
- Serial.print(" 20");
- if (year < 10)
- Serial.print("0");
- Serial.println(year, DEC);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement