Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SdFat.h>
- #include <SdFatUtil.h>
- #include <Ethernet.h>
- #include <SPI.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include "Wire.h"
- #define DS3231_I2C_ADDRESS 0x68
- #define BUZZER_PIN 8
- #define MAXSENSORS 1
- #define MAXTEMP 30
- #define BUFSIZ 100
- // Data wire is plugged into pin 2 on the Arduino
- #define ONE_WIRE_BUS 2
- // Ported to SdFat from the native Arduino SD library example by Bill Greiman
- // On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
- //const int chipSelect = 4;
- #define chipSelect 4
- // store error strings in flash to save RAM
- #define error(s) error_P(PSTR(s))
- // Setup a oneWire instance to communicate with any OneWire devices
- // (not just Maxim/Dallas temperature ICs)
- OneWire oneWire(ONE_WIRE_BUS);
- // Pass our oneWire reference to Dallas Temperature.
- DallasTemperature sensors(&oneWire);
- static char outstr[10];
- float temp;
- unsigned long previousMillis = 0;
- unsigned long currentMillis = 0;
- long interval = 60000; // Reading Interval
- /************ ETHERNET STUFF ************/
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- byte ip[] = { 192, 168, 1, 177 };
- byte gateway[] = { 192, 168, 1, 212 }; // internet access via router
- byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
- EthernetServer server(80);
- /************ SDCARD STUFF ************/
- Sd2Card card;
- SdVolume volume;
- SdFat sd;
- SdFile root, logFile, file;
- char fileName[] = "LOG.CSV";
- void error_P(const char* str) {
- PgmPrint("error: ");
- SerialPrintln_P(str);
- if (card.errorCode()) {
- PgmPrint("SD error: ");
- // Serial.print(card.errorCode(), HEX);
- // Serial.print(',');
- // Serial.println(card.errorData(), HEX);
- }
- while(1);
- }
- // Convert binary coded decimal to normal decimal numbers
- byte bcdToDec(byte val)
- {
- return( (val / 16 * 10) + (val % 16) );
- }
- void readDS3231time(byte *second,
- byte *minute,
- byte *hour,
- byte *dayOfWeek,
- byte *dayOfMonth,
- byte *month,
- byte *year)
- {
- Wire.beginTransmission(DS3231_I2C_ADDRESS);
- Wire.write(0); // set DS3231 register pointer to 00h
- Wire.endTransmission();
- Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
- // request seven bytes of data from DS3231 starting from register 00h
- *second = bcdToDec(Wire.read() & 0x7f);
- *minute = bcdToDec(Wire.read());
- *hour = bcdToDec(Wire.read() & 0x3f);
- *dayOfWeek = bcdToDec(Wire.read());
- *dayOfMonth = bcdToDec(Wire.read());
- *month = bcdToDec(Wire.read());
- *year = bcdToDec(Wire.read());
- }
- void displayTime()
- {
- byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
- // retrieve data from DS3231
- readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
- &year);
- // send it to the file
- logFile.print(dayOfMonth, DEC);
- logFile.print("/");
- logFile.print(month, DEC);
- logFile.print("/");
- logFile.print(year, DEC);
- logFile.print(",");
- logFile.print(hour, DEC);
- // convert the byte variable to a decimal number when displayed
- logFile.print(":");
- if (minute<10)
- {
- logFile.print("0");
- }
- logFile.print(minute, DEC);
- logFile.print(":");
- if (second<10)
- {
- logFile.print("0");
- }
- logFile.print(second, DEC);
- }
- void setup() {
- Wire.begin(); // for clock DS3231
- // start serial port
- Serial.begin(9600);
- sensors.begin(); // for ds18
- pinMode(BUZZER_PIN, OUTPUT); // for buzzer
- digitalWrite(BUZZER_PIN, LOW);
- // PgmPrint("Free RAM: ");
- // Serial.println(FreeRam());
- // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
- // breadboards. use SPI_FULL_SPEED for better performance.
- //eu pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
- //eu digitalWrite(10, HIGH); // but turn off the W5100 chip!
- if (!card.init(SPI_HALF_SPEED, chipSelect))
- error("card.init failed!");
- // initialize a FAT volume
- if (!volume.init(&card)) error("vol.init failed!");
- // PgmPrint("Volume is FAT");
- // Serial.println(volume.fatType(),DEC);
- // Serial.println();
- if (!root.openRoot(&volume)) error("openRoot failed");
- // list file in root with date and size
- // PgmPrintln("Files found in root:");
- // root.ls(LS_DATE | LS_SIZE);
- // Serial.println();
- // Recursive list of all directories
- // PgmPrintln("Files found in all dirs:");
- // root.ls(LS_R);
- // Serial.println();
- // PgmPrintln("Done");
- // Debugging complete, we start the server!
- Ethernet.begin(mac, ip, gateway, subnet);
- server.begin();
- }
- void ListFiles(EthernetClient client, uint8_t flags) {
- // This code is just copied from SdFile.cpp in the SDFat library
- // and tweaked to print to the client output in html!
- dir_t p;
- //Serial.println("list!");
- root.rewind();
- client.println("<ul>");
- while (root.readDir(&p) > 0) {
- //Serial.print("read file");
- // done if past last used entry
- if (p.name[0] == DIR_NAME_FREE) break;
- // skip deleted entry and entries for . and ..
- if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue;
- // only list subdirectories and files
- if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
- // print any indent spaces
- client.print("<li><a href=\"");
- // print file name with possible blank fill
- //root.printDirName(*p, flags & (LS_DATE | LS_SIZE) ? 14 : 0);
- uint8_t w = 0;
- for (uint8_t i = 0; i < 11; i++) {
- if (p.name[i] == ' ') continue;
- if (i == 8) {
- client.print('.');
- }
- client.print((char)p.name[i]);
- }
- client.print("\">");
- // print file name with possible blank fill
- for (uint8_t i = 0; i < 11; i++) {
- if (p.name[i] == ' ') continue;
- if (i == 8) {
- client.print('.');
- }
- client.print((char)p.name[i]);
- }
- client.print("</a>");
- if (DIR_IS_SUBDIR(&p)) {
- client.print('/');
- }
- // print modify date/time if requested
- if (flags & LS_DATE) {
- root.printFatDate(p.lastWriteDate);
- client.print(' ');
- root.printFatTime(p.lastWriteTime);
- }
- // print size if requested
- if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) {
- client.print(' ');
- client.print(p.fileSize);
- }
- client.println("</li>");
- }
- client.println("</ul>");
- }
- void loop()
- {
- char clientline[BUFSIZ];
- int index = 0;
- currentMillis = millis();
- if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
- previousMillis = currentMillis;
- // call sensors.requestTemperatures() to issue a global temperature
- // request to all devices on the bus
- //Serial.print(" Requesting temperatures...");
- sensors.requestTemperatures(); // Send the command to get temperatures
- //Serial.println("DONE");
- // Initialize SdFat or print a detailed error message and halt
- // Use half speed like the native library.
- // change to SPI_FULL_SPEED for more performance.
- if (!sd.begin(chipSelect, SPI_HALF_SPEED)){
- sd.initErrorHalt();
- }//else Serial.println("Inicio SD");
- // open the file for write at end like the Native SD library
- if (!logFile.open(fileName, O_RDWR | O_CREAT | O_AT_END)) {
- sd.errorHalt("opening log.cvs for write failed");
- }//else Serial.println("abriu log.csv");
- //Serial.print("Writing to test.txt...");
- //logFile.println("testing 1, 2, 3.");
- displayTime();
- for (int i = 0; i < MAXSENSORS; i++){
- temp = sensors.getTempCByIndex(i); // 0 refers to the first IC on the wire
- dtostrf(temp, 6, 3, outstr);
- Serial.print("Temperatura ");
- logFile.print(",");
- logFile.println(outstr);
- Serial.println(outstr);
- }
- // close the file:
- logFile.close();
- //Serial.println("done.");
- if (temp > MAXTEMP){
- digitalWrite(BUZZER_PIN, LOW);}
- else{
- digitalWrite(BUZZER_PIN, HIGH);}
- }
- EthernetClient client = server.available();
- if (client) {
- // an http request ends with a blank line
- boolean current_line_is_blank = true;
- // reset the input buffer
- index = 0;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- // If it isn't a new line, add the character to the buffer
- if (c != '\n' && c != '\r') {
- clientline[index] = c;
- index++;
- // are we too big for the buffer? start tossing out data
- if (index >= BUFSIZ)
- index = BUFSIZ -1;
- // continue to read more data!
- continue;
- }
- // got a \n or \r new line, which means the string is done
- clientline[index] = 0;
- // Print it out for debugging
- Serial.println(clientline);
- // Look for substring such as a request to get the root file
- if (strstr(clientline, "GET / ") != 0) {
- // send a standard http response header
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println();
- client.println("<!DOCTYPE html>");
- client.println("<html>");
- client.println("<head>");
- client.println("<title>Arduino Web Page</title>");
- client.println("</head>");
- client.println("<body>");
- // print all the files, use a helper to keep it clean
- client.println("<h2>Files:</h2>");
- ListFiles(client,LS_SIZE);
- client.println("</body>");
- client.println("</html>");
- } else if (strstr(clientline, "GET /") != 0) {
- // this time no space after the /, so a sub-file!
- char *filename;
- filename = clientline + 5; // look after the "GET /" (5 chars)
- // a little trick, look for the " HTTP/1.1" string and
- // turn the first character of the substring into a 0 to clear it out.
- (strstr(clientline, " HTTP"))[0] = 0;
- // print the file we want
- Serial.println(filename);
- if (! file.open(&root, filename, O_READ)) {
- client.println("HTTP/1.1 404 Not Found");
- client.println("Content-Type: text/html");
- client.println();
- client.println("<h2>File Not Found!</h2>");
- break;
- }
- Serial.println("Opened!");
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/plain");
- client.println();
- int16_t c;
- while ((c = file.read()) > 0) {
- // uncomment the serial to debug (slow!)
- //Serial.print((char)c);
- client.print((char)c);
- }
- file.close();
- } else {
- // everything else is a 404
- client.println("HTTP/1.1 404 Not Found");
- client.println("Content-Type: text/html");
- client.println();
- client.println("<h2>File Not Found!</h2>");
- }
- break;
- }
- }
- // give the web browser time to receive the data
- delay(1);
- client.stop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement