Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LIBRARY
- #include <SD.h>
- #include <Ethernet.h>
- #include <TextFinder.h>
- //// rtc lib
- #include <SPI.h>
- #include <Wire.h>
- // pin definition
- #define S1 A0
- #define ACT1 5
- #define LAMP 9
- // SPI bus pin definition
- const int cs=8; //chip select
- //#define SDS 4
- //#define RTCS 8
- //#define ETHS 10
- // 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);
- // i/o pin definiton
- pinMode(S1, INPUT);
- pinMode(ACT1, OUTPUT);
- pinMode(LAMP, 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();
- // RTC setup
- //RTC_init();
- // only when we need to resetup date and time in rtc device
- //SetTimeDate(11,12,13,14,15,16);
- }
- /// ====== END OF SETUP ====== ///
- /// ====== LOOP SECTION ====== ///
- void loop() {
- 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);
- // 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");
- }
- else {
- Serial.println("err");
- myFile.close();
- }
- } else {
- // flaglastConnected
- lastConnected = client.connected();
- }
- // turn on the lamp
- digitalWrite(LAMP, LOW);
- }
- // status connection
- lastConnected = client.connected();
- }
- // FUNCTION
- /// ========== 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("netSet");
- 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;
- }
- }
- //=====================================
- int RTC_init(){
- pinMode(cs,OUTPUT); // chip select
- // start the SPI library:
- SPI.begin();
- SPI.setBitOrder(MSBFIRST);
- SPI.setDataMode(SPI_MODE1); // both mode 1 & 3 should work
- //set control register
- digitalWrite(cs, LOW);
- SPI.transfer(0x8E);
- SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled
- digitalWrite(cs, HIGH);
- delay(10);
- }
- // setting date and time for rtc
- int SetTimeDate(int d, int mo, int y, int h, int mi, int s){
- int TimeDate [7]={s,mi,h,0,d,mo,y};
- for(int i=0; i<=6;i++){
- if(i==3) i++;
- int b= TimeDate[i]/10;
- int a= TimeDate[i]-b*10;
- if(i==2){
- if (b==2) b=B00000010;
- else if (b==1) b=B00000001;
- }
- TimeDate[i]= a+(b<<4);
- digitalWrite(cs, LOW);
- SPI.transfer(i+0x80);
- SPI.transfer(TimeDate[i]);
- digitalWrite(cs, HIGH);
- }
- }
- // Read Time and Date
- String ReadTimeDate(){
- String temp;
- int TimeDate [7]; //second,minute,hour,null,day,month,year
- for(int i=0; i<=6;i++){
- if(i==3)
- i++;
- digitalWrite(cs, LOW);
- SPI.transfer(i+0x00);
- unsigned int n = SPI.transfer(0x00);
- digitalWrite(cs, HIGH);
- int a=n & B00001111;
- if(i==2){
- int b=(n & B00110000)>>4; //24 hour mode
- if(b==B00000010)
- b=20;
- else if(b==B00000001)
- b=10;
- TimeDate[i]=a+b;
- }
- else if(i==4){
- int b=(n & B00110000)>>4;
- TimeDate[i]=a+b*10;
- }
- else if(i==5){
- int b=(n & B00010000)>>4;
- TimeDate[i]=a+b*10;
- }
- else if(i==6){
- int b=(n & B11110000)>>4;
- TimeDate[i]=a+b*10;
- }
- else{
- int b=(n & B01110000)>>4;
- TimeDate[i]=a+b*10;
- }
- }
- temp.concat(TimeDate[4]);
- temp.concat("/") ;
- temp.concat(TimeDate[5]);
- temp.concat("/") ;
- temp.concat(TimeDate[6]);
- temp.concat(" ") ;
- temp.concat(TimeDate[2]);
- temp.concat(":") ;
- temp.concat(TimeDate[1]);
- temp.concat(":") ;
- temp.concat(TimeDate[0]);
- return(temp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement