Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include "RTClib.h"
- #include <SD.h>
- #include <SPI.h>
- // This code is a modification of the code found here (https://github.com/kromadg/soil-sensor)
- //START LOGGER CODE HEADER
- #define LOG_INTERVAL 1000 // mills between entries
- #define ECHO_TO_SERIAL 1 // echo data to serial port
- //END LOGGER CODE HEADER
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- #define RE 8
- #define DE 7
- //LOGGER CODE
- RTC_DS1307 RTC; // define the Real Time Clock object
- // for the data logging shield, we use digital pin 10 for the SD cs line
- const int chipSelect = 10;
- // the logging file
- File logfile;
- void error(char *str)
- {
- Serial.print("error: ");
- Serial.println(str);
- while(1);
- }
- //LOGGER CODE
- const byte hum_temp_ec[8] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x03, 0x05, 0xCB};
- byte sensorResponse[12] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
- byte sensor_values[11];
- SoftwareSerial mod(2, 3); // RX, TX
- void setup(void) {
- Serial.begin(9600);
- Serial.println();
- pinMode(RE, OUTPUT);
- pinMode(DE, OUTPUT);
- digitalWrite(RE, LOW);
- digitalWrite(DE, LOW);
- delay(1000);
- mod.begin(4800);
- delay(100);
- // Initializes display
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (128x64)
- delay(500);
- display.clearDisplay();
- display.setCursor(13, 15);
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.println(" THC Soil Sensor");
- display.setCursor(21, 35);
- display.setTextSize(1);
- display.print("Initializing...");
- display.display();
- delay(5000);
- //START LOGGER CODE SETUP
- // initialize the SD card
- Serial.print("Initializing SD card...");
- // make sure that the default chip select pin is set to
- // output, even if you don't use it:
- pinMode(10, OUTPUT);
- // see if the card is present and can be initialized:
- if (!SD.begin(chipSelect)) {
- Serial.println("Card failed, or not present");
- // don't do anything more:
- return;
- }
- Serial.println("card initialized.");
- // create a new file
- char filename[] = "LOGGER00.CSV";
- for (uint8_t i = 0; i < 100; i++) {
- filename[6] = i/10 + '0';
- filename[7] = i%10 + '0';
- if (! SD.exists(filename)) {
- // only open a new file if it doesn't exist
- logfile = SD.open(filename, FILE_WRITE);
- break; // leave the loop!
- }
- }
- if (! logfile) {
- error("couldnt create file");
- }
- Serial.print("Logging to: ");
- Serial.println(filename);
- // connect to RTC
- Wire.begin();
- if (!RTC.begin()) {
- logfile.println("RTC failed");
- #if ECHO_TO_SERIAL
- Serial.println("RTC failed");
- #endif //ECHO_TO_SERIAL
- }
- logfile.println("millis,time,light,temp");
- }
- // If you want to set the aref to something other than 5v
- //analogReference(EXTERNAL);
- void loop(void) {
- /************** Soil EC Reading *******************/
- digitalWrite(DE, HIGH);
- digitalWrite(RE, HIGH);
- memset(sensor_values, 0, sizeof(sensor_values));
- delay(100);
- if (mod.write(hum_temp_ec, sizeof(hum_temp_ec)) == 8) {
- digitalWrite(DE, LOW);
- digitalWrite(RE, LOW);
- for (byte i = 0; i < 12; i++) {
- sensorResponse[i] = mod.read();
- yield();
- }
- }
- delay(250);
- // get sensor response data
- float soil_hum = 0.1 * int(sensorResponse[3] << 8 | sensorResponse[4]);
- float soil_temp = 0.1 * int(sensorResponse[5] << 8 | sensorResponse[6]);
- int soil_ec = int(sensorResponse[7] << 8 | sensorResponse[8]);
- /************* Calculations and sensor corrections *************/
- float as_read_ec = soil_ec;
- // This equation was obtained from calibration using distilled water and a 1.1178mS/cm solution.
- soil_ec = 1.93*soil_ec - 270.8;
- soil_ec = soil_ec/(1.0+0.019*(soil_temp-25));
- // soil_temp was left the same because the Teros and chinese sensor values are similar
- // quadratic aproximation
- // the teros bulk_permittivity was calculated from the teros temperature, teros bulk ec and teros pwec by Hilhorst 2000 model
- float soil_apparent_dieletric_constant = 1.3088 + 0.1439 * soil_hum + 0.0076 * soil_hum * soil_hum;
- float soil_bulk_permittivity = soil_apparent_dieletric_constant; /// Hammed 2015 (apparent_dieletric_constant is the real part of permittivity)
- float soil_pore_permittivity = 80.3 - 0.37 * (soil_temp - 20); /// same as water 80.3 and corrected for temperature
- // converting bulk EC to pore water EC
- float soil_pw_ec;
- if (soil_bulk_permittivity > 4.1)
- soil_pw_ec = ((soil_pore_permittivity * soil_ec) / (soil_bulk_permittivity - 4.1) / 1000); /// from Hilhorst 2000.
- else
- soil_pw_ec = 0;
- //LCD
- display.clearDisplay();
- display.setTextSize(1);
- display.setCursor(0, 0);
- display.print("Humi: ");
- display.setTextSize(2);
- display.print(soil_hum);
- display.setTextSize(1);
- display.print(" %");
- display.setTextSize(1);
- display.setCursor(0, 17);
- display.print("Temp: ");
- display.setTextSize(2);
- display.print(soil_temp);
- display.setTextSize(1);
- display.print(" C");
- display.setTextSize(1);
- display.setCursor(0, 34);
- display.print("EC: ");
- display.setTextSize(2);
- display.print(soil_ec);
- display.setTextSize(1);
- display.print(" us/cm");
- display.setTextSize(1);
- display.setCursor(0, 50);
- display.print("pwEC: ");
- display.setTextSize(2);
- display.print(soil_pw_ec);
- display.setTextSize(1);
- display.print(" dS/m");
- display.display();
- //LCD
- //START LOGGER CODE LOOP
- DateTime now;
- // delay for the amount of time we want between readings
- delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
- // log milliseconds since starting
- uint32_t m = millis();
- logfile.print(m); // milliseconds since start
- logfile.print(", ");
- #if ECHO_TO_SERIAL
- Serial.print(m); // milliseconds since start
- Serial.print(", ");
- #endif
- // fetch the time
- now = RTC.now();
- // log time
- logfile.print(now.unixtime()); // seconds since 1/1/1970
- logfile.print(", ");
- logfile.print('"');
- logfile.print(now.year(), DEC);
- logfile.print("/");
- logfile.print(now.month(), DEC);
- logfile.print("/");
- logfile.print(now.day(), DEC);
- logfile.print(" ");
- logfile.print(now.hour(), DEC);
- logfile.print(":");
- logfile.print(now.minute(), DEC);
- logfile.print(":");
- logfile.print(now.second(), DEC);
- logfile.print('"');
- #if ECHO_TO_SERIAL
- Serial.print(now.unixtime()); // seconds since 1/1/1970
- Serial.print(", ");
- Serial.print('"');
- Serial.print(now.year(), DEC);
- Serial.print("/");
- Serial.print(now.month(), DEC);
- Serial.print("/");
- Serial.print(now.day(), DEC);
- Serial.print(" ");
- Serial.print(now.hour(), DEC);
- Serial.print(":");
- Serial.print(now.minute(), DEC);
- Serial.print(":");
- Serial.print(now.second(), DEC);
- Serial.print('"');
- #endif //ECHO_TO_SERIAL
- //END LOGGER CODE LOOP
- Serial.print("Humidity:");
- Serial.print(soil_hum);
- Serial.print(",");
- Serial.print("Temperature:");
- Serial.print(soil_temp);
- Serial.print(",");
- Serial.print("EC:");
- Serial.print(soil_ec);
- Serial.print(",");
- Serial.print("READEC:");
- Serial.print(as_read_ec);
- Serial.print(",");
- Serial.print("pwEC:");
- Serial.print(soil_pw_ec);
- Serial.print(",");
- Serial.print("soil_bulk_permittivity:");
- Serial.println(soil_bulk_permittivity);
- logfile.print(", ");
- logfile.print(soil_hum);
- logfile.print(", ");
- logfile.print(soil_temp);
- logfile.print(", ");
- logfile.print(soil_pw_ec);
- logfile.println();
- }
Add Comment
Please, Sign In to add comment