Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- [] [] [] [] Mail Box You got new Mail Logging [] [] [] []
- 2015-02-09 1117
- - PHASE 2
- - Working on getting the read/write and display of the time stamps correct.
- - Learend how to find the file size.
- - Learned that a myFile.position() tells you where the cursor is from the first time you read it.
- - Figured out how to get it to read the following 28 bytes.
- 2015-02-05 1816
- - Got webpage up and running.
- - When button is pushed. It writes a line to the maillog.txt file
- - LED also lights up when 'door is open'
- 2015-02-05 1558 Build 1.0
- - Testing out webserver.
- 2015-07-07 1150
- - Attached RTC chip to circuit. Will be attempting to read the RTC data and log it on button presses
- 1504
- - I have working code to read from the RTC and generate a string. Thou it has added alot of data to the code, and I am very limited on dynamic memory. Thus causing the read/write to SD to fail.
- Goals:
- - [X] Get a basic web page to work. - 2015-02-05
- - [X] Get Arduino to respond to a button push (Representing the Xbee's signal) - 2015-02-05
- - [X] Get Arduino to read from an SD card - 2015-02-05
- - [X] Get Arduino to write to the SD Card when button is pushed - 2015-02-05
- - [X] Write to a TXT file. - 2015-02-05
- - [X] Get SD Card Write to write to an HTML file instead. (Insert line, not write line at end) - 2015-02-05
- - [ ] Get Arduino to read from the SD Card maillog.txt backwards. So most recent will be at the top.
- - [X] Get Arduino to read RTC (Real Time Clock) 2015-07-01 1505
- - [ ] Get Arduino to write to SD Card with Time code when a button is pushed.
- - [ ] Confirm Arduino webpage shows the logged info
- - [ ] Setup Xbee's to talk to each other.
- - [ ] Make Xbee test circuit to verify the distance.
- - [ ] Figure out the best way for XBee to communicate to Arduino
- Either:
- -- When Mailbox XBee gets power. It immedely sends command or acts as button push.
- -- OR When Mailbox XBee gets power. It waits and sends a specific command/code down. (aka a specific resistance value) Perhase to prevent accidental error.
- (Recieving XBee needs to read a 120ohm value to figure out it is true. If not, it will find it to be false error)
- - [ ] Program Arudino to respond to XBee (XBee replaces the push button)
- Advanced Goals:
- - [ ] Have Arduino play an audio code when the mail comes.
- - [ ] Have Arduino show previous 20 in backwards order
- */
- /*
- Circuit:
- * Ethernet shield attached to pins 10, 11, 12, 13
- * Analog inputs attached to pins A0 through A5 (optional)
- * RTC CHIP:
- ** RTC Chip Uses A4 and A5
- ** RTC CHIP SDA -> A4
- ** RTC CHIP SCL -> A5
- * SD card attached to SPI bus as follows:
- ** MOSI - pin 11
- ** MISO - pin 12
- ** CLK - pin 13
- ** CS - pin 4
- */
- // = = = = = = = = = = Includes = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- #include <SPI.h> // Needed to us the 6 special pins for the eithernet card
- #include <Ethernet.h> // Needed to use the eithernet card
- #include <SD.h> // Needed to read/write to the SD Card
- #include "Wire.h" // Needed for RTC Chip
- /*
- */
- // ============ Ethernet ============
- // Enter a MAC address and IP address for your controller below.
- // The IP address will be dependent on your local network:
- byte mac[] = {
- 0xDE, 0xAD, 0xDA, 0x00, 0xC1, 0xDE
- };
- IPAddress ip(10, 0, 0, 88);
- // Initialize the Ethernet server library
- // with the IP address and port you want to use
- // (port 80 is default for HTTP):
- EthernetServer server(80);
- // ============ SD Card ============
- File myFile; // Name of the file we will be using
- // ============ Push Button ============
- // push button and LED indicator circuit
- const int buttonDelete = 5; // the number of the pushbutton pin (Delete button of the file)
- const int buttonPin = 6; // the number of the pushbutton pin
- const int ledPin = 7; // the number of the LED pin
- // variables will change:
- boolean buttonState = 0; // interger for reading the pushbutton status
- boolean doorState = 0; // interger to see if the door is open. (Prevents code from looping and writing tons of lines for each door open)
- // ============ RTC - Real Time Clock ============
- #define DS1307_ADDRESS 0x68 // RTC Address.
- char timestamp[27]; // Sets up the 28 char count for the write to SD card line. EX: char timestamp[27] = ("2015-07-01 WED 12:31 </br>");
- // = = = = = = = = = = SETUP = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- void setup() {
- // ============ ETHERNET ============
- // start the Ethernet connection and the server:
- Ethernet.begin(mac, ip);
- server.begin();
- // ============ SERIAL ============
- // Open serial communications and wait for port to open:
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- Serial.print("server is at ");
- Serial.println(Ethernet.localIP());
- // ============ SD Card ============
- Serial.print("Initializing SD card...");
- // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
- // Note that even if it's not used as the CS pin, the hardware SS pin
- // (10 on most Arduino boards, 53 on the Mega) must be left as an output
- // or the SD library functions will not work.
- pinMode(10, OUTPUT);
- if (!SD.begin(4)) {
- Serial.println("initialization failed!");
- return;
- }
- Serial.println("initialization done.");
- // ============ Push Button / XBee button ===============
- // initialize the LED pin as an output:
- pinMode(ledPin, OUTPUT);
- // initialize the pushbutton pin as an input:
- pinMode(buttonPin, INPUT);
- pinMode(buttonDelete, INPUT);
- // ============ RTC - Real Time Clock ============
- Wire.begin(); // Sets up the Wire.h to be used.
- }
- // = = = = = = = = = = LOOP = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- void loop() {
- // ============ ETHERNET ============
- // listen for incoming clients
- EthernetClient client = server.available();
- if (client) {
- webpage(); // Directs to the VOID WEBPAGE Below. This will help keep the loop empty.
- }
- // ================ Button Action ====================
- // read the state of the pushbutton value:
- buttonState = digitalRead(buttonPin);
- // check if the pushbutton is pressed.
- // if it is, the buttonState is HIGH:
- if (buttonState == HIGH) {
- if (doorState == LOW) { // Checks to see if the door was previously closed. If still HIGH (door still open, prevents the time log to time stamp it.)
- dooropen(); // Jumps down to the VOID DOOROPEN to do actions.
- }
- }
- else if (buttonState == LOW) {
- // turn LED off:
- digitalWrite(ledPin, LOW); // When button is LOW. It turns off the LED
- doorState = LOW; // Resets doorState to LOW so the above loop will be good to go next time the door is opened.
- }
- // ================ Delete File Action ====================
- if (digitalRead(buttonDelete) == HIGH) {
- SD.remove("maillog.txt");
- Serial.println("maillog.txt has been deleted");
- }
- }
- // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- // - - - - - - - - - - WEBPAGE - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- void webpage() {
- EthernetClient client = server.available();
- Serial.println("new client"); // Testing - To verify in serial that there is a connection made.
- // an http request ends with a blank line
- boolean currentLineIsBlank = true;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- // Serial.write(c); // print HTTP request character to serial monitor
- // if you've gotten to the end of the line (received a newline
- // character) and the line is blank, the http request has ended,
- // so you can send a reply
- if (c == '\n' && currentLineIsBlank) {
- // send a standard http response header
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: close"); // the connection will be closed after completion of the response
- // client.println("Refresh: 5"); // refresh the page automatically every 5 sec
- client.println();
- client.println("<!DOCTYPE HTML>");
- client.println("<html>");
- // - - - - - - - READ SD CARD FILE - - - - - - - -
- // [X] myFileGet this section to read from each line of the SD Card. -2015-02-05
- /*
- - Could set it up as a loop.
- - 1) Count the lines in txt file.
- - 2) While the line number is less than the total lines in the txt file. Then keep client.print
- - - Only spit out the last 20 days?
- - 1) Count total lines. (Say 345)
- - 2) Subtract 20 from Total lines (345) = 325
- - 3) client.print line 325
- - 4) client.println("<br />");
- - 5) increase++ Total line number 325 + 1 = 326.
- - 6) repeate loop now printing line 326.
- */
- client.println("<hr /><b>");
- myFile = SD.open("day00.txt"); // Opens day 00 text file
- if (myFile) {
- // Serial.println("maillog.txt:"); // Testing only.
- while (myFile.available()) { // Check to see if a new character is availble
- client.write(myFile.read()); // Reads a character. and repeates loop and writes it to the client. (This MUST WRITE not READ)
- // each line of the text file (maillog.txt) must have a </br> for it to properly format the lines.
- // ex: 2013-01-14 WED 11:13 </br>
- } // end of while loop
- // close the file:
- myFile.close();
- }
- client.println("</b><br /><hr />");
- myFile = SD.open("maillog.txt"); // Opens maillog.txt
- if (myFile) {
- // Serial.println("maillog.txt:"); // Testing only.
- while (myFile.available()) { // Check to see if a new character is availble
- client.write(myFile.read()); // Reads a character. and repeates loop and writes it to the client. (This MUST WRITE not READ)
- // each line of the text file (maillog.txt) must have a </br> for it to properly format the lines.
- // ex: 2013-01-14 WED 11:13 </br>
- } // end of while loop
- // close the file:
- myFile.close();
- }
- // - - END OF HTML Page- - - - - - - -
- client.println("</html>");
- break;
- }
- if (c == '\n') {
- // you're starting a new line
- currentLineIsBlank = true;
- }
- else if (c != '\r') {
- // you've gotten a character on the current line
- currentLineIsBlank = false;
- }
- }
- }
- // give the web browser time to receive the data
- delay(1);
- // close the connection:
- client.stop();
- Serial.println("client disonnected");
- }
- // - - - - - - - - - - Push Button / XBee New Mail action - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- void dooropen() {
- doorState = HIGH; // Sets the doorState to High. This stops the above loop from repeating over and over.
- digitalWrite(ledPin, HIGH); // Makes the LED turn on to indicate the Button has been pushed.
- // RTC get it to build the data string
- //char timestamp[27] = ("2015-07-01 WED 12:31 </br>"); // Temporary 'timestamp' This will get replaced by reading from the future RTC.
- printDate();
- int mailFileSize = 0; // Is the total size of maillog.txt
- int cursorPosition = 0; // value set when I go to read the previous 5
- // char previous1[28] = ("2015-02-09 SAT 12:12 </br>");
- // char previous2[28] = ("2015-02-09 SAT 12:12 </br>");
- // char previous3[28] = ("2015-02-09 SAT 12:12 </br>");
- // char previous4[28] = ("2015-02-09 SAT 12:12 </br>");
- // char previous5[28] = ("2014-02-09 SAT 12:12 </br>");
- char prev_index = 0; // This is counting characters as it read the file.
- // char prev_char = 0;
- // Write this button push to file
- // open the file. note that only one file can be open at a time,
- // so you have to close this one before opening another.
- // - - - - - - - - - - Write to SD card - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- myFile = SD.open("maillog.txt", FILE_WRITE);
- // if the file opened okay, write to it:
- if (myFile) {
- Serial.println(myFile.size()); // Testing. File size before write
- // Serial.println(myFile.position()); // Testing. Location of 'cursor' (Should be at end of file)
- Serial.print("Writing to maillog.txt..."); // Testing. only.
- myFile.println(timestamp); // Writes the current timestamp to the file
- mailFileSize = myFile.size(); // Writes interger the total file size. Aka the 'end' byte of the file.
- myFile.close(); // close the file:
- Serial.println("done.");
- Serial.print("Total File Size ");
- Serial.println(mailFileSize);
- }
- else {
- // if the file didn't open, print an error:
- Serial.println("error opening maillog.txt");
- }
- SD.remove("day00.txt"); // Deletes day00.txt
- myFile = SD.open("day00.txt", FILE_WRITE); // Creates and opens up newly created day00.txt
- myFile.println(timestamp); // Writes single line to new day00.txt file
- myFile.close();
- // Time to create lines for the last 5 lines. and write to a new list.
- mailFileSize = mailFileSize - 139; // Subtracts back to 5 lines (To get the starting position.
- // knowing the file size from the interger. And knowing each line is = 28 bytes. x previous 5 lines.
- // 28 x 5 == 140 bytes back.
- Serial.println(mailFileSize); // Testing. Shows location number. Correct.
- myFile = SD.open("maillog.txt");
- //myFile.seek(29); // Goes to that location
- /*
- Serial.println(myFile.position()); // current position of cursor. Always starts at 0
- Serial.println(myFile.read()); // reads the current character. and auto increases the position#
- //Serial.println(myFile.read());
- Serial.println(myFile.position());
- //Serial.println(myFile.peek());
- //Serial.println(myFile.seek(mailFileSize));
- Serial.println(myFile.read());
- Serial.println(myFile.position());
- Serial.println(myFile.read());
- Serial.println(myFile.position());
- Serial.println(myFile.read());
- Serial.println(myFile.position());
- */
- while (myFile.position() < 28) {
- Serial.print(myFile.read()); // Reads the character. Spits it out as a Decimal number
- Serial.print(" "); // example: 50 48 49 53 45 48 50 45 48 57 32 83 65 84 32 49 50 58 53 49 32 60 47 98 114 62 13 10
- //previous5[prev_index] = myFile.read();
- prev_index++; // increases the index value. Since it started at 0. Counts for every character in the file.
- }
- Serial.println("end");
- //Serial.println(previous5);
- //Serial.println(myFile.peek());
- // read the next 28 characters. And set to char
- myFile.close();
- }
- // - - - - - - - - - - RTC - Real Time Clock Get time - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- byte bcdToDec(byte val) {
- // Convert binary coded decimal to normal decimal numbers
- return ( (val / 16 * 10) + (val % 16) );
- }
- void printDate() {
- // Reset the register pointer
- Wire.beginTransmission(DS1307_ADDRESS);
- byte zero = 0x00;
- Wire.write(zero);
- Wire.endTransmission();
- Wire.requestFrom(DS1307_ADDRESS, 7);
- int second = bcdToDec(Wire.read());
- int minute = bcdToDec(Wire.read());
- int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
- int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
- int monthDay = bcdToDec(Wire.read());
- int month = bcdToDec(Wire.read());
- int year = bcdToDec(Wire.read());
- if (second < 10) second = ("0", second);
- if (minute < 10) minute = ("0", minute);
- if (hour < 10) hour = ("0", hour);
- //print the date EG 3/1/11 23:59:59
- Serial.print(month);
- Serial.print("/");
- Serial.print(monthDay);
- Serial.print("/");
- Serial.print(weekDay);
- Serial.print("-");
- Serial.print(year);
- Serial.print(" ");
- Serial.print(hour);
- Serial.print(":");
- Serial.print(minute);
- Serial.print(":");
- Serial.println(second);
- char * DayOfWeek[7] = {"SUN", "MON", "TUES", "WED", "THU", "FRI", "SAT"};
- sprintf(timestamp, "20%02d-%02d-%02d %s %02d:%02d </br>", year, month, monthDay, DayOfWeek[weekDay], hour, minute);
- Serial.print("TIME2: ");
- Serial.println(timestamp);
- /*
- // String timestampbuild; // You need to build the time stamp string first...
- timestampbuild = (year + "-" + month, "-" + monthDay + " " + weekDay + " " + hour + ":" + minute + "</br>"); // This builds the variables and characters into the variable TIMESTAMPBUILD
- //timestampbuild.toCharArray(timestamp, 27); // This now converts the STRING to a CHARACTER ARRAY in which the CHAR can understand
- Serial.print("TIME2: ");
- Serial.println(timestampbuild);
- Serial.print("TIME: ");
- Serial.println(timestamp);
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement