Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ===============================
- * Bismillahirrohmanirrohim
- * Agrieye Arduino System
- * Author: andrew@ugm.ac.id
- * Created : Fall October 2012
- * Version : REV 00.2
- * ==============================
- */
- // LIBRARY
- #include <SD.h>
- #include <Ethernet.h>
- #include <TextFinder.h>
- // rtc lib
- /*
- #include <RTClib.h>
- #include <SPI.h>
- #include <Wire.h>
- #include <RTC_DS3234.h>
- */
- // pin definition
- #define S1 A0
- #define ACT1 5
- #define LAMP 9
- // SPI bus pin definition
- #define SDS 4
- #define RTCS 8
- #define ETHS 10
- /*
- // progmem for avoid spurious warning in RTC
- #undef PROGMEM
- #define PROGMEM __attribute__(( section(".progmem.data") ))
- #undef PSTR
- #define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))
- // Create an RTC instance, RTC in pin 8
- RTC_DS3234 RTC(RTCS);
- */
- // Syste Variable
- File myFile; // SD file
- int ave1; // avreaging variable
- char filename[] = "L000.CSV"; // INIT LOCAL FILE NAME
- // ==== CONFIGURABLE FROM SDCARD == //
- int LOCATION_ID = 1; // CONF 9
- int DELAY_READ = 1; // CONF 10 (MIN)
- int NSTEP = 4 ; // CONF 11
- int CHECK_SETTING = 5; // CONF 12 (MIN)
- int LOWER_LIMIT = 100; // CONF 14 (VAL)
- int ACT_PERIOD = 3; // CONF 15 (SECOND)
- // ==== END OF CONFIGURABLE PARAMS== //
- // TIMER MILLIS
- unsigned long lastReading, periodRead;
- unsigned long lastCheck, periodCheck;
- // pump flag notification
- boolean lastConnected = false;
- boolean ledState = 0;
- int countAct = 0;
- // NETWORK SETTINGS
- byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x92, 0x5A };
- byte ip[] = { 192, 168, 11, 221 };
- byte dnsGw[] = { 192, 168, 11, 1 };
- char remoteServer[] = "agrieye.host22.com";
- IPAddress netmask(255,255,255,0);
- EthernetClient client;
- TextFinder finder (client);
- /// ========== SETUP ======== ///
- void setup() {
- Serial.begin(9600);
- //SPI.begin();
- //RTC.begin();
- // i/o pin definiton
- pinMode(S1, INPUT);
- pinMode(ACT1, OUTPUT);
- pinMode(LAMP, OUTPUT);
- //pinMode(SDS, OUTPUT);
- //setup sd card
- sdCardSetup();
- // filename generator from 0 - 999
- for(uint8_t i = 0; i < 1000 ; i++) {
- uint8_t j = i/10;
- filename[1] = j/10 + '0';
- filename[2] = j%10 + '0';
- filename[3] = i%10 + '0';
- if(!SD.exists(filename)) {
- break;
- }
- }
- // start the Ethernet connection:
- networkSetup();
- /*
- // adjust the RTC
- if (!RTC.isrunning()) {
- Serial.println("Not Running!");
- // following line sets the RTC to the date & time this sketch was compiled
- RTC.adjust(DateTime(__DATE__, __TIME__));
- } */
- }
- /// ====== END OF SETUP ====== ///
- /// ====== LOOP SECTION ====== ///
- void loop() {
- // timer rtc
- //DateTime now = RTC.now();
- if (client.available())
- {
- char c = client.read();
- Serial.print(c);
- }
- if(!client.connected() && lastConnected)
- {
- Serial.println("disc");
- client.stop();
- }
- // 1. READING SENSOR
- unsigned long timerReading = millis();
- periodRead = DELAY_READ;
- periodRead = periodRead*15*1000;
- if(timerReading - lastReading > periodRead) {
- lastReading = timerReading;
- // turn on the lamp
- //digitalWrite(LAMP, HIGH);
- // read the sensor
- readSensor();
- // sending data to server
- if(!sendServer()) {
- // store data in sdcard
- Serial.print("file:");
- Serial.println(filename);
- //digitalWrite(SDS, LOW);
- //sdCardSetup();
- // open files and write it
- myFile = SD.open(filename, FILE_WRITE);
- if(myFile) {
- /*
- myFile.print(now.year(), DEC);
- myFile.print('/');
- myFile.print(now.month(), DEC);
- myFile.print('/');
- myFile.print(now.day(), DEC);
- myFile.print(' ');
- myFile.print(now.hour(), DEC);
- myFile.print(':');
- myFile.print(now.minute(), DEC);
- myFile.print(':');
- myFile.print(now.second(), DEC);
- myFile.print(",");
- */
- myFile.print(LOCATION_ID);
- myFile.print(",");
- myFile.print(ave1);
- myFile.print(",0,0,0,0,0");
- myFile.print(",");
- myFile.println(ledState);
- myFile.close();
- Serial.println("svd");
- //digitalWrite(SDS, HIGH);
- }
- else {
- Serial.println("err");
- myFile.close();
- }
- } else {
- // flaglastConnected
- lastConnected = client.connected();
- }
- // turn on the lamp
- digitalWrite(LAMP, LOW);
- }
- // status connection
- lastConnected = client.connected();
- }
- /// ========== Method and Function ========== //
- // SDCARD SETUP METHOD
- void sdCardSetup() {
- Serial.print("SD ");
- pinMode(10, OUTPUT);
- if(!SD.begin(4)) {
- Serial.println("x");
- digitalWrite(LAMP,HIGH);
- } else {
- Serial.println("ok");
- }
- }
- // NETWORK SETUP
- void networkSetup() {
- Serial.print("Net ");
- Serial.println("defc");
- Ethernet.begin(mac, ip, dnsGw, dnsGw, netmask);
- }
- // READSENSOR METHOD
- void readSensor() {
- // Turn on lamp, for indicator
- Serial.print("re ");
- // reset teh ave value to 0
- ave1 = 0;
- // read each pin with n step
- for(int i=1; i<= NSTEP; i++)
- {
- // accumulate value or each sensor
- ave1 += analogRead(S1);
- }
- // averging data section
- Serial.print("av ");
- ave1 = ave1/NSTEP;
- }
- // SENDING DATA TO SERVER
- boolean sendServer() {
- if(client.connect(remoteServer, 80)) {
- Serial.print("sn ");
- // make http request
- client.print("GET /feed/api.php?lid=");
- client.print(LOCATION_ID);
- client.print("&s1=");
- client.print(ave1);
- client.print("&s2=0&s3=0&s4=0&s5=0&s6=0&status=");
- client.print(ledState);
- client.print(" HTTP/1.1\n");
- client.print("Host: agrieye.host22.com\n");
- client.println("Connection: close");
- client.println();
- Serial.println("ok");
- return true;
- } else {
- Serial.println("x");
- lastConnected = client.connected();
- client.stop();
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement