Advertisement
MarceloFerreira

Porgramação Arduino

Sep 17th, 2015
786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.07 KB | None | 0 0
  1. #include <SdFat.h>
  2. #include <SdFatUtil.h>
  3. #include <Ethernet.h>
  4. #include <SPI.h>
  5. #include <OneWire.h>
  6. #include <DallasTemperature.h>
  7. #include "Wire.h"
  8.  
  9. #define DS3231_I2C_ADDRESS 0x68
  10. #define BUZZER_PIN 8
  11. #define MAXSENSORS 1
  12. #define MAXTEMP 30
  13. #define BUFSIZ 100
  14.  
  15. // Data wire is plugged into pin 2 on the Arduino
  16. #define ONE_WIRE_BUS 2
  17.  
  18. // Ported to SdFat from the native Arduino SD library example by Bill Greiman
  19. // On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
  20. //const int chipSelect = 4;
  21. #define chipSelect 4
  22.  
  23. // store error strings in flash to save RAM
  24. #define error(s) error_P(PSTR(s))
  25.  
  26. // Setup a oneWire instance to communicate with any OneWire devices
  27. // (not just Maxim/Dallas temperature ICs)
  28. OneWire oneWire(ONE_WIRE_BUS);
  29.  
  30. // Pass our oneWire reference to Dallas Temperature.
  31. DallasTemperature sensors(&oneWire);
  32.  
  33. static char outstr[10];
  34.  
  35. float temp;
  36. unsigned long previousMillis = 0;
  37. unsigned long currentMillis = 0;
  38. long interval = 60000; // Reading Interval
  39.  
  40. /************ ETHERNET STUFF ************/
  41. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  42. byte ip[] = { 192, 168, 1, 177 };
  43. byte gateway[] = { 192, 168, 1, 212 }; // internet access via router
  44. byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
  45. EthernetServer server(80);
  46.  
  47. /************ SDCARD STUFF ************/
  48. Sd2Card card;
  49. SdVolume volume;
  50. SdFat sd;
  51. SdFile root, logFile, file;
  52.  
  53. char fileName[] = "LOG.CSV";
  54.  
  55. void error_P(const char* str) {
  56. PgmPrint("error: ");
  57. SerialPrintln_P(str);
  58. if (card.errorCode()) {
  59. PgmPrint("SD error: ");
  60. // Serial.print(card.errorCode(), HEX);
  61. // Serial.print(',');
  62. // Serial.println(card.errorData(), HEX);
  63. }
  64. while(1);
  65. }
  66.  
  67. // Convert binary coded decimal to normal decimal numbers
  68. byte bcdToDec(byte val)
  69. {
  70. return( (val / 16 * 10) + (val % 16) );
  71. }
  72.  
  73. void readDS3231time(byte *second,
  74. byte *minute,
  75. byte *hour,
  76. byte *dayOfWeek,
  77. byte *dayOfMonth,
  78. byte *month,
  79. byte *year)
  80. {
  81. Wire.beginTransmission(DS3231_I2C_ADDRESS);
  82. Wire.write(0); // set DS3231 register pointer to 00h
  83. Wire.endTransmission();
  84. Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  85. // request seven bytes of data from DS3231 starting from register 00h
  86. *second = bcdToDec(Wire.read() & 0x7f);
  87. *minute = bcdToDec(Wire.read());
  88. *hour = bcdToDec(Wire.read() & 0x3f);
  89. *dayOfWeek = bcdToDec(Wire.read());
  90. *dayOfMonth = bcdToDec(Wire.read());
  91. *month = bcdToDec(Wire.read());
  92. *year = bcdToDec(Wire.read());
  93. }
  94.  
  95. void displayTime()
  96. {
  97. byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  98. // retrieve data from DS3231
  99. readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  100. &year);
  101. // send it to the file
  102. logFile.print(dayOfMonth, DEC);
  103. logFile.print("/");
  104. logFile.print(month, DEC);
  105. logFile.print("/");
  106. logFile.print(year, DEC);
  107. logFile.print(",");
  108. logFile.print(hour, DEC);
  109. // convert the byte variable to a decimal number when displayed
  110. logFile.print(":");
  111. if (minute<10)
  112. {
  113. logFile.print("0");
  114. }
  115. logFile.print(minute, DEC);
  116. logFile.print(":");
  117. if (second<10)
  118. {
  119. logFile.print("0");
  120. }
  121. logFile.print(second, DEC);
  122. }
  123.  
  124. void setup() {
  125. Wire.begin(); // for clock DS3231
  126.  
  127. // start serial port
  128. Serial.begin(9600);
  129.  
  130. sensors.begin(); // for ds18
  131.  
  132. pinMode(BUZZER_PIN, OUTPUT); // for buzzer
  133. digitalWrite(BUZZER_PIN, LOW);
  134.  
  135. // PgmPrint("Free RAM: ");
  136. // Serial.println(FreeRam());
  137.  
  138. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  139. // breadboards. use SPI_FULL_SPEED for better performance.
  140. //eu pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
  141. //eu digitalWrite(10, HIGH); // but turn off the W5100 chip!
  142.  
  143. if (!card.init(SPI_HALF_SPEED, chipSelect))
  144. error("card.init failed!");
  145.  
  146. // initialize a FAT volume
  147. if (!volume.init(&card)) error("vol.init failed!");
  148.  
  149. // PgmPrint("Volume is FAT");
  150. // Serial.println(volume.fatType(),DEC);
  151. // Serial.println();
  152.  
  153. if (!root.openRoot(&volume)) error("openRoot failed");
  154.  
  155. // list file in root with date and size
  156. // PgmPrintln("Files found in root:");
  157. // root.ls(LS_DATE | LS_SIZE);
  158. // Serial.println();
  159.  
  160. // Recursive list of all directories
  161. // PgmPrintln("Files found in all dirs:");
  162. // root.ls(LS_R);
  163.  
  164. // Serial.println();
  165. // PgmPrintln("Done");
  166.  
  167. // Debugging complete, we start the server!
  168. Ethernet.begin(mac, ip, gateway, subnet);
  169. server.begin();
  170. }
  171.  
  172. void ListFiles(EthernetClient client, uint8_t flags) {
  173. // This code is just copied from SdFile.cpp in the SDFat library
  174. // and tweaked to print to the client output in html!
  175. dir_t p;
  176. //Serial.println("list!");
  177.  
  178. root.rewind();
  179. client.println("<ul>");
  180. while (root.readDir(&p) > 0) {
  181. //Serial.print("read file");
  182. // done if past last used entry
  183. if (p.name[0] == DIR_NAME_FREE) break;
  184.  
  185. // skip deleted entry and entries for . and ..
  186. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue;
  187.  
  188. // only list subdirectories and files
  189. if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
  190.  
  191. // print any indent spaces
  192. client.print("<li><a href=\"");
  193.  
  194. // print file name with possible blank fill
  195. //root.printDirName(*p, flags & (LS_DATE | LS_SIZE) ? 14 : 0);
  196.  
  197. uint8_t w = 0;
  198. for (uint8_t i = 0; i < 11; i++) {
  199. if (p.name[i] == ' ') continue;
  200. if (i == 8) {
  201. client.print('.');
  202. }
  203. client.print((char)p.name[i]);
  204. }
  205. client.print("\">");
  206.  
  207. // print file name with possible blank fill
  208. for (uint8_t i = 0; i < 11; i++) {
  209. if (p.name[i] == ' ') continue;
  210. if (i == 8) {
  211. client.print('.');
  212. }
  213. client.print((char)p.name[i]);
  214. }
  215. client.print("</a>");
  216.  
  217. if (DIR_IS_SUBDIR(&p)) {
  218. client.print('/');
  219. }
  220.  
  221. // print modify date/time if requested
  222. if (flags & LS_DATE) {
  223. root.printFatDate(p.lastWriteDate);
  224. client.print(' ');
  225. root.printFatTime(p.lastWriteTime);
  226. }
  227. // print size if requested
  228. if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) {
  229. client.print(' ');
  230. client.print(p.fileSize);
  231. }
  232. client.println("</li>");
  233. }
  234. client.println("</ul>");
  235. }
  236.  
  237. void loop()
  238. {
  239. char clientline[BUFSIZ];
  240. int index = 0;
  241.  
  242. currentMillis = millis();
  243.  
  244. if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
  245. previousMillis = currentMillis;
  246. // call sensors.requestTemperatures() to issue a global temperature
  247. // request to all devices on the bus
  248. //Serial.print(" Requesting temperatures...");
  249. sensors.requestTemperatures(); // Send the command to get temperatures
  250. //Serial.println("DONE");
  251.  
  252. // Initialize SdFat or print a detailed error message and halt
  253. // Use half speed like the native library.
  254. // change to SPI_FULL_SPEED for more performance.
  255. if (!sd.begin(chipSelect, SPI_HALF_SPEED)){
  256. sd.initErrorHalt();
  257. }//else Serial.println("Inicio SD");
  258.  
  259. // open the file for write at end like the Native SD library
  260. if (!logFile.open(fileName, O_RDWR | O_CREAT | O_AT_END)) {
  261. sd.errorHalt("opening log.cvs for write failed");
  262. }//else Serial.println("abriu log.csv");
  263. //Serial.print("Writing to test.txt...");
  264. //logFile.println("testing 1, 2, 3.");
  265. displayTime();
  266.  
  267. for (int i = 0; i < MAXSENSORS; i++){
  268. temp = sensors.getTempCByIndex(i); // 0 refers to the first IC on the wire
  269. dtostrf(temp, 6, 3, outstr);
  270. Serial.print("Temperatura ");
  271. logFile.print(",");
  272. logFile.println(outstr);
  273. Serial.println(outstr);
  274. }
  275. // close the file:
  276. logFile.close();
  277. //Serial.println("done.");
  278.  
  279. if (temp > MAXTEMP){
  280. digitalWrite(BUZZER_PIN, LOW);}
  281. else{
  282. digitalWrite(BUZZER_PIN, HIGH);}
  283. }
  284.  
  285. EthernetClient client = server.available();
  286.  
  287. if (client) {
  288. // an http request ends with a blank line
  289. boolean current_line_is_blank = true;
  290. // reset the input buffer
  291. index = 0;
  292.  
  293. while (client.connected()) {
  294. if (client.available()) {
  295. char c = client.read();
  296.  
  297. // If it isn't a new line, add the character to the buffer
  298. if (c != '\n' && c != '\r') {
  299. clientline[index] = c;
  300. index++;
  301. // are we too big for the buffer? start tossing out data
  302. if (index >= BUFSIZ)
  303. index = BUFSIZ -1;
  304.  
  305. // continue to read more data!
  306. continue;
  307. }
  308.  
  309. // got a \n or \r new line, which means the string is done
  310. clientline[index] = 0;
  311.  
  312. // Print it out for debugging
  313. Serial.println(clientline);
  314.  
  315. // Look for substring such as a request to get the root file
  316. if (strstr(clientline, "GET / ") != 0) {
  317.  
  318. // send a standard http response header
  319. client.println("HTTP/1.1 200 OK");
  320. client.println("Content-Type: text/html");
  321. client.println();
  322. client.println("<!DOCTYPE html>");
  323. client.println("<html>");
  324. client.println("<head>");
  325. client.println("<title>Arduino Web Page</title>");
  326. client.println("</head>");
  327. client.println("<body>");
  328. // print all the files, use a helper to keep it clean
  329. client.println("<h2>Files:</h2>");
  330. ListFiles(client,LS_SIZE);
  331. client.println("</body>");
  332. client.println("</html>");
  333. } else if (strstr(clientline, "GET /") != 0) {
  334. // this time no space after the /, so a sub-file!
  335. char *filename;
  336.  
  337. filename = clientline + 5; // look after the "GET /" (5 chars)
  338. // a little trick, look for the " HTTP/1.1" string and
  339. // turn the first character of the substring into a 0 to clear it out.
  340. (strstr(clientline, " HTTP"))[0] = 0;
  341.  
  342. // print the file we want
  343. Serial.println(filename);
  344.  
  345. if (! file.open(&root, filename, O_READ)) {
  346. client.println("HTTP/1.1 404 Not Found");
  347. client.println("Content-Type: text/html");
  348. client.println();
  349. client.println("<h2>File Not Found!</h2>");
  350. break;
  351. }
  352.  
  353. Serial.println("Opened!");
  354.  
  355. client.println("HTTP/1.1 200 OK");
  356. client.println("Content-Type: text/plain");
  357. client.println();
  358.  
  359. int16_t c;
  360. while ((c = file.read()) > 0) {
  361. // uncomment the serial to debug (slow!)
  362. //Serial.print((char)c);
  363. client.print((char)c);
  364. }
  365. file.close();
  366. } else {
  367. // everything else is a 404
  368. client.println("HTTP/1.1 404 Not Found");
  369. client.println("Content-Type: text/html");
  370. client.println();
  371. client.println("<h2>File Not Found!</h2>");
  372. }
  373. break;
  374. }
  375.  
  376. }
  377. // give the web browser time to receive the data
  378. delay(1);
  379. client.stop();
  380. }
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement