Guest User

Untitled

a guest
Apr 19th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <Wire.h>
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_SSD1306.h>
  5. #include "RTClib.h"
  6. #include <SD.h>
  7. #include <SPI.h>
  8.  
  9. // This code is a modification of the code found here (https://github.com/kromadg/soil-sensor)
  10.  
  11. //START LOGGER CODE HEADER
  12. #define LOG_INTERVAL 1000 // mills between entries
  13. #define ECHO_TO_SERIAL 1 // echo data to serial port
  14. //END LOGGER CODE HEADER
  15.  
  16. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  17. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  18. #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  19. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  20.  
  21. #define RE 8
  22. #define DE 7
  23.  
  24. //LOGGER CODE
  25. RTC_DS1307 RTC; // define the Real Time Clock object
  26.  
  27.  
  28. // for the data logging shield, we use digital pin 10 for the SD cs line
  29. const int chipSelect = 10;
  30.  
  31. // the logging file
  32. File logfile;
  33.  
  34. void error(char *str)
  35. {
  36. Serial.print("error: ");
  37. Serial.println(str);
  38. while(1);
  39. }
  40. //LOGGER CODE
  41. const byte hum_temp_ec[8] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x03, 0x05, 0xCB};
  42. byte sensorResponse[12] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  43. byte sensor_values[11];
  44.  
  45. SoftwareSerial mod(2, 3); // RX, TX
  46.  
  47. void setup(void) {
  48. Serial.begin(9600);
  49. Serial.println();
  50. pinMode(RE, OUTPUT);
  51. pinMode(DE, OUTPUT);
  52. digitalWrite(RE, LOW);
  53. digitalWrite(DE, LOW);
  54. delay(1000);
  55. mod.begin(4800);
  56. delay(100);
  57.  
  58. // Initializes display
  59. display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (128x64)
  60. delay(500);
  61. display.clearDisplay();
  62. display.setCursor(13, 15);
  63. display.setTextSize(1);
  64. display.setTextColor(WHITE);
  65. display.println(" THC Soil Sensor");
  66. display.setCursor(21, 35);
  67. display.setTextSize(1);
  68. display.print("Initializing...");
  69. display.display();
  70. delay(5000);
  71.  
  72. //START LOGGER CODE SETUP
  73.  
  74.  
  75. // initialize the SD card
  76. Serial.print("Initializing SD card...");
  77. // make sure that the default chip select pin is set to
  78. // output, even if you don't use it:
  79. pinMode(10, OUTPUT);
  80.  
  81. // see if the card is present and can be initialized:
  82. if (!SD.begin(chipSelect)) {
  83. Serial.println("Card failed, or not present");
  84. // don't do anything more:
  85. return;
  86. }
  87. Serial.println("card initialized.");
  88.  
  89. // create a new file
  90. char filename[] = "LOGGER00.CSV";
  91. for (uint8_t i = 0; i < 100; i++) {
  92. filename[6] = i/10 + '0';
  93. filename[7] = i%10 + '0';
  94. if (! SD.exists(filename)) {
  95. // only open a new file if it doesn't exist
  96. logfile = SD.open(filename, FILE_WRITE);
  97. break; // leave the loop!
  98. }
  99. }
  100.  
  101. if (! logfile) {
  102. error("couldnt create file");
  103. }
  104.  
  105. Serial.print("Logging to: ");
  106. Serial.println(filename);
  107.  
  108. // connect to RTC
  109. Wire.begin();
  110. if (!RTC.begin()) {
  111. logfile.println("RTC failed");
  112. #if ECHO_TO_SERIAL
  113. Serial.println("RTC failed");
  114. #endif //ECHO_TO_SERIAL
  115. }
  116.  
  117.  
  118. logfile.println("millis,time,light,temp");
  119. }
  120.  
  121.  
  122. // If you want to set the aref to something other than 5v
  123. //analogReference(EXTERNAL);
  124.  
  125.  
  126. void loop(void) {
  127. /************** Soil EC Reading *******************/
  128. digitalWrite(DE, HIGH);
  129. digitalWrite(RE, HIGH);
  130. memset(sensor_values, 0, sizeof(sensor_values));
  131. delay(100);
  132.  
  133. if (mod.write(hum_temp_ec, sizeof(hum_temp_ec)) == 8) {
  134. digitalWrite(DE, LOW);
  135. digitalWrite(RE, LOW);
  136. for (byte i = 0; i < 12; i++) {
  137. sensorResponse[i] = mod.read();
  138. yield();
  139. }
  140. }
  141.  
  142. delay(250);
  143.  
  144. // get sensor response data
  145. float soil_hum = 0.1 * int(sensorResponse[3] << 8 | sensorResponse[4]);
  146. float soil_temp = 0.1 * int(sensorResponse[5] << 8 | sensorResponse[6]);
  147. int soil_ec = int(sensorResponse[7] << 8 | sensorResponse[8]);
  148.  
  149. /************* Calculations and sensor corrections *************/
  150.  
  151. float as_read_ec = soil_ec;
  152.  
  153. // This equation was obtained from calibration using distilled water and a 1.1178mS/cm solution.
  154. soil_ec = 1.93*soil_ec - 270.8;
  155. soil_ec = soil_ec/(1.0+0.019*(soil_temp-25));
  156.  
  157. // soil_temp was left the same because the Teros and chinese sensor values are similar
  158.  
  159. // quadratic aproximation
  160. // the teros bulk_permittivity was calculated from the teros temperature, teros bulk ec and teros pwec by Hilhorst 2000 model
  161. float soil_apparent_dieletric_constant = 1.3088 + 0.1439 * soil_hum + 0.0076 * soil_hum * soil_hum;
  162.  
  163. float soil_bulk_permittivity = soil_apparent_dieletric_constant; /// Hammed 2015 (apparent_dieletric_constant is the real part of permittivity)
  164. float soil_pore_permittivity = 80.3 - 0.37 * (soil_temp - 20); /// same as water 80.3 and corrected for temperature
  165.  
  166. // converting bulk EC to pore water EC
  167. float soil_pw_ec;
  168. if (soil_bulk_permittivity > 4.1)
  169. soil_pw_ec = ((soil_pore_permittivity * soil_ec) / (soil_bulk_permittivity - 4.1) / 1000); /// from Hilhorst 2000.
  170. else
  171. soil_pw_ec = 0;
  172.  
  173. //LCD
  174. display.clearDisplay();
  175.  
  176. display.setTextSize(1);
  177. display.setCursor(0, 0);
  178. display.print("Humi: ");
  179. display.setTextSize(2);
  180. display.print(soil_hum);
  181. display.setTextSize(1);
  182. display.print(" %");
  183.  
  184. display.setTextSize(1);
  185. display.setCursor(0, 17);
  186. display.print("Temp: ");
  187. display.setTextSize(2);
  188. display.print(soil_temp);
  189. display.setTextSize(1);
  190. display.print(" C");
  191.  
  192. display.setTextSize(1);
  193. display.setCursor(0, 34);
  194. display.print("EC: ");
  195. display.setTextSize(2);
  196. display.print(soil_ec);
  197. display.setTextSize(1);
  198. display.print(" us/cm");
  199.  
  200. display.setTextSize(1);
  201. display.setCursor(0, 50);
  202. display.print("pwEC: ");
  203. display.setTextSize(2);
  204. display.print(soil_pw_ec);
  205. display.setTextSize(1);
  206. display.print(" dS/m");
  207.  
  208. display.display();
  209. //LCD
  210. //START LOGGER CODE LOOP
  211.  
  212. DateTime now;
  213.  
  214. // delay for the amount of time we want between readings
  215. delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
  216.  
  217.  
  218. // log milliseconds since starting
  219. uint32_t m = millis();
  220. logfile.print(m); // milliseconds since start
  221. logfile.print(", ");
  222. #if ECHO_TO_SERIAL
  223. Serial.print(m); // milliseconds since start
  224. Serial.print(", ");
  225. #endif
  226.  
  227. // fetch the time
  228. now = RTC.now();
  229. // log time
  230. logfile.print(now.unixtime()); // seconds since 1/1/1970
  231. logfile.print(", ");
  232. logfile.print('"');
  233. logfile.print(now.year(), DEC);
  234. logfile.print("/");
  235. logfile.print(now.month(), DEC);
  236. logfile.print("/");
  237. logfile.print(now.day(), DEC);
  238. logfile.print(" ");
  239. logfile.print(now.hour(), DEC);
  240. logfile.print(":");
  241. logfile.print(now.minute(), DEC);
  242. logfile.print(":");
  243. logfile.print(now.second(), DEC);
  244. logfile.print('"');
  245. #if ECHO_TO_SERIAL
  246. Serial.print(now.unixtime()); // seconds since 1/1/1970
  247. Serial.print(", ");
  248. Serial.print('"');
  249. Serial.print(now.year(), DEC);
  250. Serial.print("/");
  251. Serial.print(now.month(), DEC);
  252. Serial.print("/");
  253. Serial.print(now.day(), DEC);
  254. Serial.print(" ");
  255. Serial.print(now.hour(), DEC);
  256. Serial.print(":");
  257. Serial.print(now.minute(), DEC);
  258. Serial.print(":");
  259. Serial.print(now.second(), DEC);
  260. Serial.print('"');
  261. #endif //ECHO_TO_SERIAL
  262.  
  263. //END LOGGER CODE LOOP
  264.  
  265. Serial.print("Humidity:");
  266. Serial.print(soil_hum);
  267. Serial.print(",");
  268. Serial.print("Temperature:");
  269. Serial.print(soil_temp);
  270. Serial.print(",");
  271. Serial.print("EC:");
  272. Serial.print(soil_ec);
  273. Serial.print(",");
  274. Serial.print("READEC:");
  275. Serial.print(as_read_ec);
  276. Serial.print(",");
  277. Serial.print("pwEC:");
  278. Serial.print(soil_pw_ec);
  279. Serial.print(",");
  280. Serial.print("soil_bulk_permittivity:");
  281. Serial.println(soil_bulk_permittivity);
  282.  
  283.  
  284. logfile.print(", ");
  285. logfile.print(soil_hum);
  286. logfile.print(", ");
  287. logfile.print(soil_temp);
  288. logfile.print(", ");
  289. logfile.print(soil_pw_ec);
  290. logfile.println();
  291. }
Add Comment
Please, Sign In to add comment