Advertisement
andrewmovic

Test 18102012

Oct 17th, 2012
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.15 KB | None | 0 0
  1. /* ===============================
  2. *  Bismillahirrohmanirrohim
  3. *  Agrieye Arduino System
  4. *  Author: andrew@ugm.ac.id
  5. *  Created : Fall October 2012
  6. *  Version : REV 00.2
  7. *  ==============================
  8. */
  9.  
  10. // LIBRARY
  11. #include <SD.h>
  12. #include <Ethernet.h>
  13. #include <TextFinder.h>
  14.  
  15. // rtc lib
  16. /*
  17. #include <RTClib.h>
  18. #include <SPI.h>
  19. #include <Wire.h>
  20. #include <RTC_DS3234.h>
  21. */
  22.  
  23. // pin definition
  24. #define S1 A0
  25. #define ACT1 5
  26. #define LAMP 9
  27.  
  28. // SPI bus pin definition
  29. #define SDS 4
  30. #define RTCS 8
  31. #define ETHS 10
  32.  
  33. /*
  34. // progmem for avoid spurious warning in RTC
  35. #undef PROGMEM
  36. #define PROGMEM __attribute__(( section(".progmem.data") ))
  37. #undef PSTR
  38. #define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))
  39.  
  40. // Create an RTC instance, RTC in pin 8
  41. RTC_DS3234 RTC(RTCS);
  42. */
  43. // Syste Variable
  44. File myFile;                  // SD file
  45. int ave1;                     // avreaging variable
  46. char filename[] = "L000.CSV";  // INIT LOCAL FILE NAME
  47.  
  48. // ==== CONFIGURABLE FROM SDCARD == //
  49. int LOCATION_ID = 1;         // CONF 9
  50. int DELAY_READ  = 1;         // CONF 10 (MIN)
  51. int NSTEP = 4 ;              // CONF 11
  52. int CHECK_SETTING = 5;       // CONF 12 (MIN)
  53. int LOWER_LIMIT = 100;       // CONF 14 (VAL)
  54. int ACT_PERIOD = 3;         // CONF 15 (SECOND)
  55. // ==== END OF CONFIGURABLE PARAMS== //
  56.  
  57. // TIMER MILLIS
  58. unsigned long lastReading, periodRead;
  59. unsigned long lastCheck, periodCheck;
  60.  
  61. // pump flag notification
  62. boolean lastConnected = false;
  63. boolean ledState = 0;
  64. int countAct = 0;
  65.  
  66. // NETWORK SETTINGS
  67. byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x92, 0x5A };
  68. byte ip[] = { 192, 168, 11, 221 };                  
  69. byte dnsGw[] = { 192, 168, 11, 1 };
  70. char remoteServer[] = "agrieye.host22.com";
  71. IPAddress netmask(255,255,255,0);
  72. EthernetClient client;
  73. TextFinder finder (client);
  74.  
  75. /// ========== SETUP ======== ///
  76. void setup() {
  77.   Serial.begin(9600);
  78.  
  79.   //SPI.begin();
  80.   //RTC.begin();
  81.  
  82.   // i/o pin definiton
  83.   pinMode(S1, INPUT);
  84.   pinMode(ACT1, OUTPUT);
  85.   pinMode(LAMP, OUTPUT);
  86.  
  87.  
  88.   //pinMode(SDS, OUTPUT);
  89.  
  90.   //setup sd card  
  91.   sdCardSetup();
  92.  
  93.   // filename  generator from 0 - 999
  94.   for(uint8_t i = 0; i < 1000 ; i++) {
  95.       uint8_t j = i/10;
  96.       filename[1] = j/10 + '0';
  97.       filename[2] = j%10 + '0';
  98.       filename[3] = i%10 + '0';
  99.       if(!SD.exists(filename)) {
  100.         break;
  101.       }
  102.   }
  103.  
  104.   // start the Ethernet connection:
  105.   networkSetup();
  106.    /*
  107.   // adjust the RTC
  108.   if (!RTC.isrunning()) {
  109.     Serial.println("Not Running!");
  110.     // following line sets the RTC to the date & time this sketch was compiled
  111.     RTC.adjust(DateTime(__DATE__, __TIME__));
  112.   } */
  113.  
  114. }
  115. /// ====== END OF SETUP ====== ///
  116.  
  117.  
  118. /// ====== LOOP SECTION ====== ///
  119. void loop() {  
  120.   // timer rtc
  121.   //DateTime now = RTC.now();
  122.  
  123.   if (client.available())
  124.     {
  125.     char c = client.read();
  126.     Serial.print(c);
  127.     }
  128.  
  129.   if(!client.connected() && lastConnected)
  130.     {
  131.     Serial.println("disc");
  132.     client.stop();
  133.     }
  134.  
  135.   // 1. READING SENSOR  
  136.   unsigned long timerReading = millis();
  137.   periodRead = DELAY_READ;
  138.   periodRead = periodRead*15*1000;
  139.   if(timerReading - lastReading > periodRead) {
  140.     lastReading = timerReading;
  141.    
  142.     // turn on the lamp
  143.     //digitalWrite(LAMP, HIGH);
  144.     // read the sensor
  145.     readSensor();
  146.    
  147.     // sending data to server
  148.     if(!sendServer()) {
  149.       // store data in sdcard
  150.       Serial.print("file:");
  151.       Serial.println(filename);
  152.      
  153.       //digitalWrite(SDS, LOW);
  154.       //sdCardSetup();
  155.      
  156.       // open files and write it
  157.       myFile = SD.open(filename, FILE_WRITE);
  158.       if(myFile) {
  159.         /*
  160.         myFile.print(now.year(), DEC);
  161.         myFile.print('/');
  162.         myFile.print(now.month(), DEC);
  163.         myFile.print('/');
  164.         myFile.print(now.day(), DEC);
  165.         myFile.print(' ');
  166.         myFile.print(now.hour(), DEC);
  167.         myFile.print(':');
  168.         myFile.print(now.minute(), DEC);
  169.         myFile.print(':');
  170.         myFile.print(now.second(), DEC);  
  171.         myFile.print(",");
  172.         */
  173.         myFile.print(LOCATION_ID);
  174.         myFile.print(",");
  175.         myFile.print(ave1);
  176.         myFile.print(",0,0,0,0,0");
  177.         myFile.print(",");
  178.         myFile.println(ledState);
  179.         myFile.close();
  180.         Serial.println("svd");
  181.        
  182.         //digitalWrite(SDS, HIGH);
  183.       }
  184.       else {
  185.         Serial.println("err");
  186.         myFile.close();
  187.       }
  188.     } else {
  189.     // flaglastConnected
  190.     lastConnected = client.connected();
  191.     }
  192.    
  193.     // turn on the lamp
  194.     digitalWrite(LAMP, LOW);
  195.   }
  196.    
  197.    // status connection
  198.    lastConnected = client.connected();
  199. }
  200.  
  201.  
  202. /// ========== Method and Function ========== //
  203. // SDCARD SETUP METHOD
  204. void sdCardSetup() {
  205.   Serial.print("SD ");
  206.   pinMode(10, OUTPUT);
  207.   if(!SD.begin(4)) {
  208.     Serial.println("x");
  209.     digitalWrite(LAMP,HIGH);
  210.   } else {
  211.     Serial.println("ok");
  212.   }
  213. }
  214.  
  215. // NETWORK SETUP
  216. void networkSetup() {
  217.   Serial.print("Net ");
  218.   Serial.println("defc");
  219.   Ethernet.begin(mac, ip, dnsGw, dnsGw, netmask);
  220. }
  221.  
  222. // READSENSOR METHOD
  223. void readSensor() {
  224.   // Turn on lamp, for indicator
  225.   Serial.print("re ");
  226.   // reset teh ave value to 0
  227.   ave1 = 0;
  228.  
  229.   // read each pin with n step
  230.   for(int i=1; i<= NSTEP; i++)
  231.     {    
  232.     // accumulate value or each sensor
  233.     ave1 +=  analogRead(S1);  
  234.     }
  235.   // averging data section
  236.   Serial.print("av ");
  237.   ave1 = ave1/NSTEP;
  238. }
  239.  
  240. // SENDING DATA TO SERVER
  241. boolean sendServer() {
  242.    if(client.connect(remoteServer, 80)) {
  243.       Serial.print("sn ");
  244.       // make http request
  245.       client.print("GET /feed/api.php?lid=");
  246.       client.print(LOCATION_ID);
  247.       client.print("&s1=");
  248.       client.print(ave1);
  249.       client.print("&s2=0&s3=0&s4=0&s5=0&s6=0&status=");
  250.      client.print(ledState);
  251.      client.print(" HTTP/1.1\n");
  252.      client.print("Host: agrieye.host22.com\n");
  253.      client.println("Connection: close");
  254.      client.println();
  255.      Serial.println("ok");
  256.      return true;
  257.    } else {
  258.      Serial.println("x");
  259.      lastConnected = client.connected();
  260.      client.stop();
  261.      return false;
  262.    }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement