Advertisement
andrewmovic

22102102-BLD

Oct 22nd, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.33 KB | None | 0 0
  1. // LIBRARY
  2. #include <SD.h>
  3. #include <Ethernet.h>
  4. #include <TextFinder.h>
  5.  
  6. //// rtc lib
  7. #include <SPI.h>
  8. #include <Wire.h>
  9.  
  10. // pin definition
  11. #define S1 A0
  12. #define ACT1 5
  13. #define LAMP 9
  14.  
  15. // SPI bus pin definition
  16. const int  cs=8; //chip select
  17.  
  18. //#define SDS 4
  19. //#define RTCS 8
  20. //#define ETHS 10
  21.  
  22. // Syste Variable
  23. File myFile;                  // SD file
  24. int ave1;                     // avreaging variable
  25. char filename[] = "L000.CSV";  // INIT LOCAL FILE NAME
  26.  
  27. // ==== CONFIGURABLE FROM SDCARD == //
  28. int LOCATION_ID = 1;         // CONF 9
  29. int DELAY_READ  = 1;         // CONF 10 (MIN)
  30. int NSTEP = 4 ;              // CONF 11
  31. int CHECK_SETTING = 5;       // CONF 12 (MIN)
  32. int LOWER_LIMIT = 100;       // CONF 14 (VAL)
  33. int ACT_PERIOD = 3;         // CONF 15 (SECOND)
  34. // ==== END OF CONFIGURABLE PARAMS== //
  35.  
  36. // TIMER MILLIS
  37. unsigned long lastReading, periodRead;
  38. unsigned long lastCheck, periodCheck;
  39.  
  40. // pump flag notification
  41. boolean lastConnected = false;
  42. boolean ledState = 0;
  43. int countAct = 0;
  44.  
  45. // NETWORK SETTINGS
  46. byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x92, 0x5A };
  47. byte ip[] = { 192, 168, 11, 221 };                  
  48. byte dnsGw[] = { 192, 168, 11, 1 };
  49. char remoteServer[] = "agrieye.host22.com";
  50. IPAddress netmask(255,255,255,0);
  51. EthernetClient client;
  52. TextFinder finder (client);
  53.  
  54. /// ========== SETUP ======== ///
  55. void setup() {
  56.   Serial.begin(9600);
  57.  
  58.   // i/o pin definiton
  59.   pinMode(S1, INPUT);
  60.   pinMode(ACT1, OUTPUT);
  61.   pinMode(LAMP, OUTPUT);
  62.  
  63.   //setup sd card  
  64.   sdCardSetup();
  65.  
  66.   // filename  generator from 0 - 999
  67.   for(uint8_t i = 0; i < 1000 ; i++) {
  68.       uint8_t j = i/10;
  69.       filename[1] = j/10 + '0';
  70.       filename[2] = j%10 + '0';
  71.       filename[3] = i%10 + '0';
  72.       if(!SD.exists(filename)) {
  73.         break;
  74.       }
  75.   }
  76.  
  77.   // start the Ethernet connection:
  78.   networkSetup();
  79.  
  80.   // RTC setup
  81.   //RTC_init();
  82.  
  83.   // only when we need to resetup date and time in rtc device
  84.   //SetTimeDate(11,12,13,14,15,16);
  85. }
  86.  
  87. /// ====== END OF SETUP ====== ///
  88.  
  89.  
  90. /// ====== LOOP SECTION ====== ///
  91. void loop() {
  92.   if (client.available()) {
  93.     char c = client.read();
  94.     Serial.print(c);
  95.     }
  96.  
  97.   if(!client.connected() && lastConnected) {
  98.     Serial.println("disc");
  99.     client.stop();
  100.     }
  101.  
  102.   // 1. READING SENSOR  
  103.   unsigned long timerReading = millis();
  104.   periodRead = DELAY_READ;
  105.   periodRead = periodRead*15*1000;
  106.   if(timerReading - lastReading > periodRead) {
  107.     lastReading = timerReading;
  108.    
  109.     // turn on the lamp
  110.     digitalWrite(LAMP, HIGH);
  111.     // read the sensor
  112.     readSensor();
  113.    
  114.     // sending data to server
  115.     if(!sendServer()) {
  116.       // store data in sdcard
  117.       Serial.print("file:");
  118.       Serial.println(filename);
  119.  
  120.       // open files and write it
  121.       myFile = SD.open(filename, FILE_WRITE);
  122.       if(myFile) {
  123.         /*        
  124.         myFile.print(now.year(), DEC);
  125.         myFile.print('/');
  126.         myFile.print(now.month(), DEC);
  127.         myFile.print('/');
  128.         myFile.print(now.day(), DEC);
  129.         myFile.print(' ');
  130.         myFile.print(now.hour(), DEC);
  131.         myFile.print(':');
  132.         myFile.print(now.minute(), DEC);
  133.         myFile.print(':');
  134.         myFile.print(now.second(), DEC);  
  135.         myFile.print(",");
  136.         */
  137.         myFile.print(LOCATION_ID);
  138.         myFile.print(",");
  139.         myFile.print(ave1);
  140.         myFile.print(",0,0,0,0,0");
  141.         myFile.print(",");
  142.         myFile.println(ledState);
  143.         myFile.close();
  144.         Serial.println("svd");
  145.        
  146.       }
  147.       else {
  148.         Serial.println("err");
  149.         myFile.close();
  150.       }
  151.     } else {
  152.     // flaglastConnected
  153.     lastConnected = client.connected();
  154.     }
  155.    
  156.     // turn on the lamp
  157.     digitalWrite(LAMP, LOW);
  158.   }
  159.    
  160.    // status connection
  161.    lastConnected = client.connected();
  162. }
  163.  
  164. // FUNCTION
  165. /// ========== Method and Function ========== //
  166. // SDCARD SETUP METHOD
  167. void sdCardSetup() {
  168.   Serial.print("SD ");
  169.   pinMode(10, OUTPUT);
  170.   if(!SD.begin(4)) {
  171.     Serial.println("x");
  172.     digitalWrite(LAMP,HIGH);
  173.   } else {
  174.     Serial.println("ok");
  175.   }
  176. }
  177.  
  178. // NETWORK SETUP
  179. void networkSetup() {
  180.   Serial.print("netSet");
  181.   Ethernet.begin(mac, ip, dnsGw, dnsGw, netmask);
  182. }
  183.  
  184. // READSENSOR METHOD
  185. void readSensor() {
  186.   // Turn on lamp, for indicator
  187.   Serial.print("re ");
  188.   // reset teh ave value to 0
  189.   ave1 = 0;
  190.  
  191.   // read each pin with n step
  192.   for(int i=1; i<= NSTEP; i++)
  193.     {    
  194.     // accumulate value or each sensor
  195.     ave1 +=  analogRead(S1);  
  196.     }
  197.   // averging data section
  198.   Serial.print("av ");
  199.   ave1 = ave1/NSTEP;
  200. }
  201.  
  202. // SENDING DATA TO SERVER
  203. boolean sendServer() {
  204.    if(client.connect(remoteServer, 80)) {
  205.       Serial.print("sn ");
  206.       // make http request
  207.       client.print("GET /feed/api.php?lid=");
  208.       client.print(LOCATION_ID);
  209.       client.print("&s1=");
  210.       client.print(ave1);
  211.       client.print("&s2=0&s3=0&s4=0&s5=0&s6=0&status=");
  212.       client.print(ledState);
  213.       client.print(" HTTP/1.1\n");
  214.       client.print("Host: agrieye.host22.com\n");
  215.       client.println("Connection: close");
  216.       client.println();
  217.       Serial.println("ok");
  218.       return true;
  219.    } else {
  220.      Serial.println("x");
  221.      lastConnected = client.connected();
  222.      client.stop();
  223.      return false;
  224.    }
  225. }
  226.  
  227. //=====================================
  228. int RTC_init(){
  229.   pinMode(cs,OUTPUT); // chip select
  230.   // start the SPI library:
  231.   SPI.begin();
  232.   SPI.setBitOrder(MSBFIRST);
  233.   SPI.setDataMode(SPI_MODE1); // both mode 1 & 3 should work
  234.   //set control register
  235.   digitalWrite(cs, LOW);  
  236.   SPI.transfer(0x8E);
  237.   SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled
  238.   digitalWrite(cs, HIGH);
  239.   delay(10);
  240. }
  241.  
  242. // setting date and time for rtc
  243. int SetTimeDate(int d, int mo, int y, int h, int mi, int s){
  244.   int TimeDate [7]={s,mi,h,0,d,mo,y};
  245.   for(int i=0; i<=6;i++){
  246.     if(i==3) i++;
  247.     int b= TimeDate[i]/10;
  248.     int a= TimeDate[i]-b*10;
  249.     if(i==2){
  250.        if (b==2) b=B00000010;
  251.        else if (b==1) b=B00000001;
  252.        }   
  253.     TimeDate[i]= a+(b<<4);
  254.    
  255.     digitalWrite(cs, LOW);
  256.     SPI.transfer(i+0x80);
  257.     SPI.transfer(TimeDate[i]);        
  258.     digitalWrite(cs, HIGH);
  259.   }
  260. }
  261.  
  262. // Read Time and Date
  263. String ReadTimeDate(){
  264.     String temp;
  265.     int TimeDate [7]; //second,minute,hour,null,day,month,year     
  266.     for(int i=0; i<=6;i++){
  267.         if(i==3)
  268.             i++;
  269.         digitalWrite(cs, LOW);
  270.         SPI.transfer(i+0x00);
  271.         unsigned int n = SPI.transfer(0x00);        
  272.         digitalWrite(cs, HIGH);
  273.         int a=n & B00001111;    
  274.         if(i==2){  
  275.             int b=(n & B00110000)>>4; //24 hour mode
  276.             if(b==B00000010)
  277.                 b=20;        
  278.             else if(b==B00000001)
  279.                 b=10;
  280.             TimeDate[i]=a+b;
  281.         }
  282.         else if(i==4){
  283.             int b=(n & B00110000)>>4;
  284.             TimeDate[i]=a+b*10;
  285.         }
  286.         else if(i==5){
  287.             int b=(n & B00010000)>>4;
  288.             TimeDate[i]=a+b*10;
  289.         }
  290.         else if(i==6){
  291.             int b=(n & B11110000)>>4;
  292.             TimeDate[i]=a+b*10;
  293.         }
  294.         else{  
  295.             int b=(n & B01110000)>>4;
  296.             TimeDate[i]=a+b*10;
  297.             }
  298.     }
  299.     temp.concat(TimeDate[4]);
  300.     temp.concat("/") ;
  301.     temp.concat(TimeDate[5]);
  302.     temp.concat("/") ;
  303.     temp.concat(TimeDate[6]);
  304.     temp.concat("     ") ;
  305.     temp.concat(TimeDate[2]);
  306.     temp.concat(":") ;
  307.     temp.concat(TimeDate[1]);
  308.     temp.concat(":") ;
  309.     temp.concat(TimeDate[0]);
  310.   return(temp);
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement