Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LIBRARY
- #include <SPI.h>
- #include <Ethernet.h>
- #include <Wire.h>
- #include "RTClib.h"
- #include <RTC_DS3234.h>
- #include <SD.h>
- #include <TextFinder.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
- boolean lastConnected = false;
- boolean ledState = 0;
- int countAct = 0;
- // ==== 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;
- // 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);
- // disable all spi (4)
- // SDCARD device
- pinMode(SDS,OUTPUT);
- digitalWrite(SDS, HIGH);
- // RTC device (8)
- pinMode(RTCS, OUTPUT);
- digitalWrite(RTCS,HIGH);
- // ETHERNET device(10)
- pinMode(ETHS, OUTPUT);
- digitalWrite(ETHS, HIGH);
- // Start SPI and RTC
- SPI.begin();
- RTC.begin();
- //RTC.adjust(DateTime(__DATE__, __TIME__));
- // INOUT OUTPUT PIN DEFINITION
- pinMode(S1, INPUT);
- pinMode(ACT1, OUTPUT);
- pinMode(LAMP, OUTPUT);
- // 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__));
- }
- // Ethernet Setup <-- I don't know is it the right order for setup
- networkSetup();
- }
- /// ====== 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();
- digitalWrite(ETHS, HIGH);
- }
- // 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()) {
- // if sending data failed then store data in Sd-card
- // check date and time, inactive the ethernet pin and activate rtc pin
- digitalWrite(ETHS, HIGH);
- pinMode(RTCS, LOW);
- DateTime now = RTC.now();
- // set HIGH to inactive the pin
- pinMode(RTCS, HIGH);
- 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");
- // end of saving in sd, deactivate the sd pin
- digitalWrite(SDS, HIGH);
- }
- else {
- Serial.println("error");
- myFile.close();
- }
- } else {
- // flaglastConnected
- lastConnected = client.connected();
- }
- // turn on the lamp
- digitalWrite(LAMP, LOW);
- }
- // status connection
- lastConnected = client.connected();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement