Advertisement
andrewmovic

Revision SPI with Switch

Oct 18th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. // LIBRARY
  2. #include <SPI.h>
  3. #include <Ethernet.h>
  4. #include <Wire.h>
  5. #include "RTClib.h"
  6. #include <RTC_DS3234.h>
  7. #include <SD.h>
  8. #include <TextFinder.h>
  9.  
  10. // pin definition
  11. #define S1 A0
  12. #define ACT1 5
  13. #define LAMP 9
  14.  
  15. // SPI bus pin definition
  16. #define SDS 4
  17. #define RTCS 8
  18. #define ETHS 10
  19.  
  20. // progmem for avoid spurious warning in RTC
  21. #undef PROGMEM
  22. #define PROGMEM __attribute__(( section(".progmem.data") ))
  23. #undef PSTR
  24. #define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))
  25.  
  26. // Create an RTC instance, RTC in pin 8
  27. RTC_DS3234 RTC(RTCS);
  28.  
  29. // Syste Variable
  30. File myFile;                  // SD file
  31. int ave1;                     // avreaging variable
  32. char filename[] = "L000.CSV";  // INIT LOCAL FILE NAME
  33. boolean lastConnected = false;
  34. boolean ledState = 0;
  35. int countAct = 0;
  36.  
  37. // ==== CONFIGURABLE FROM SDCARD == //
  38. int LOCATION_ID = 1;         // CONF 9
  39. int DELAY_READ  = 1;         // CONF 10 (MIN)
  40. int NSTEP = 4 ;              // CONF 11
  41. int CHECK_SETTING = 5;       // CONF 12 (MIN)
  42. int LOWER_LIMIT = 100;       // CONF 14 (VAL)
  43. int ACT_PERIOD = 3;          // CONF 15 (SECOND)
  44. // ==== END OF CONFIGURABLE PARAMS== //
  45.  
  46. // TIMER MILLIS
  47. unsigned long lastReading, periodRead;
  48. unsigned long lastCheck, periodCheck;
  49.  
  50. // NETWORK SETTINGS
  51. byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x92, 0x5A };
  52. byte ip[] = { 192, 168, 11, 221 };                  
  53. byte dnsGw[] = { 192, 168, 11, 1 };
  54. char remoteServer[] = "agrieye.host22.com";
  55. IPAddress netmask(255,255,255,0);
  56. EthernetClient client;
  57. TextFinder finder (client);
  58.  
  59. /// ========== SETUP ======== ///
  60. void setup() {
  61.   Serial.begin(9600);
  62.  
  63.   // disable all spi (4)
  64.   // SDCARD device
  65.   pinMode(SDS,OUTPUT);
  66.   digitalWrite(SDS, HIGH);
  67.  
  68.   // RTC device (8)
  69.   pinMode(RTCS, OUTPUT);
  70.   digitalWrite(RTCS,HIGH);
  71.  
  72.   // ETHERNET device(10)
  73.   pinMode(ETHS, OUTPUT);
  74.   digitalWrite(ETHS, HIGH);
  75.  
  76.   // Start SPI and RTC
  77.   SPI.begin();
  78.   RTC.begin();
  79.  
  80.   //RTC.adjust(DateTime(__DATE__, __TIME__));
  81.  
  82.   // INOUT OUTPUT PIN DEFINITION
  83.   pinMode(S1, INPUT);
  84.   pinMode(ACT1, OUTPUT);
  85.   pinMode(LAMP, OUTPUT);
  86.  
  87.   // adjust the RTC
  88.   if (!RTC.isrunning()) {
  89.     Serial.println("Not Running!");
  90.     // following line sets the RTC to the date & time this sketch was compiled
  91.     RTC.adjust(DateTime(__DATE__, __TIME__));
  92.   }
  93.  
  94.   // Ethernet Setup <-- I don't know is it the right order for setup
  95.   networkSetup();
  96. }
  97. /// ====== END OF SETUP ====== ///
  98.  
  99.  
  100. /// ====== LOOP SECTION ====== ///
  101. void loop() {  
  102.   if (client.available()) {
  103.     char c = client.read();
  104.     Serial.print(c);
  105.    }
  106.  
  107.   if(!client.connected() && lastConnected) {
  108.     Serial.println("disc");
  109.     client.stop();
  110.     digitalWrite(ETHS, HIGH);
  111.     }
  112.  
  113.   // 1. READING SENSOR  
  114.   unsigned long timerReading = millis();
  115.   periodRead = DELAY_READ;
  116.   periodRead = periodRead*15*1000;
  117.   if(timerReading - lastReading > periodRead) {
  118.     lastReading = timerReading;
  119.    
  120.     // turn on the lamp
  121.     digitalWrite(LAMP, HIGH);    
  122.     // read the sensor
  123.     readSensor();
  124.    
  125.     // sending data to server
  126.     if(!sendServer()) {
  127.       // if sending data failed then store data in Sd-card
  128.       // check date and time, inactive the ethernet pin and activate rtc pin
  129.       digitalWrite(ETHS, HIGH);
  130.       pinMode(RTCS, LOW);
  131.       DateTime now = RTC.now();
  132.       // set HIGH to inactive the pin
  133.       pinMode(RTCS, HIGH);
  134.      
  135.       Serial.print("file:");
  136.       Serial.println(filename);
  137.       digitalWrite(SDS, LOW);
  138.      
  139.       sdCardSetup();
  140.      
  141.       // open files and write it
  142.       myFile = SD.open(filename, FILE_WRITE);
  143.       if(myFile) {
  144.        
  145.         myFile.print(now.year(), DEC);
  146.         myFile.print('/');
  147.         myFile.print(now.month(), DEC);
  148.         myFile.print('/');
  149.         myFile.print(now.day(), DEC);
  150.         myFile.print(' ');
  151.         myFile.print(now.hour(), DEC);
  152.         myFile.print(':');
  153.         myFile.print(now.minute(), DEC);
  154.         myFile.print(':');
  155.         myFile.print(now.second(), DEC);  
  156.         myFile.print(",");
  157.        
  158.         myFile.print(LOCATION_ID);
  159.         myFile.print(",");
  160.         myFile.print(ave1);
  161.         myFile.print(",0,0,0,0,0");
  162.         myFile.print(",");
  163.         myFile.println(ledState);
  164.         myFile.close();
  165.         Serial.println("svd");
  166.  
  167.         // end of saving in sd, deactivate the sd pin
  168.         digitalWrite(SDS, HIGH);
  169.       }
  170.       else {
  171.         Serial.println("error");
  172.         myFile.close();
  173.       }
  174.     } else {
  175.     // flaglastConnected
  176.     lastConnected = client.connected();
  177.     }
  178.    
  179.     // turn on the lamp
  180.     digitalWrite(LAMP, LOW);
  181.   }
  182.    
  183.    // status connection
  184.    lastConnected = client.connected();
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement