Advertisement
M0n5t3r

Growbox V10

Jan 26th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.74 KB | None | 0 0
  1. #include <DHT.h>
  2. #include <DHT_U.h>
  3. #include "SoftwareSerial.h"
  4. #include <Wire.h>
  5. #include <LCD.h>
  6. #include <LiquidCrystal_I2C.h>
  7. #include "RTClib.h"
  8.  
  9.  
  10. #define DHTPIN 5
  11. #define DHTTYPE DHT21
  12. DHT dht(DHTPIN, DHTTYPE);
  13. #define TRIGGER_PIN 13
  14. #define ECHO_PIN 12
  15. #define USONIC_DIV 58.0
  16. #define MEASURE_SAMPLE_DELAY 5
  17. #define MEASURE_SAMPLES 25
  18. #define MEASURE_DELAY 250
  19. #define trigPin 13
  20. #define echoPin 12
  21.  
  22. LiquidCrystal_I2C  lcd(0x27, 2, 1, 0, 4, 5, 6, 7);
  23. RTC_DS1307 rtc;
  24.  
  25. int sensorPin = A0;
  26.  
  27. // Motor 1
  28. int dir1PinA = 11;
  29. int dir2PinA = 10;
  30.  
  31. // Motor 2
  32. int dir1PinB = 9;
  33. int dir2PinB = 8;
  34.  
  35. //light relays
  36. int RELAY1 = 6;
  37. int RELAY2 = 7;
  38.  
  39. //fans
  40. int Fan1 = 50;
  41. int Fan2 = 52;
  42.  
  43. //buttons
  44. int inPin1 = 4;   // choose the input pin (for a pushbutton)
  45. int inPin2 = 3;   // choose the input pin (for a pushbutton)
  46. int inPin3 = 2;   // choose the input pin (for a pushbutton)
  47.  
  48. boolean backlight = false;
  49. boolean fan = false;
  50.  
  51. String ssid = "Visnet";
  52. String password = "vinvisje";
  53.  
  54. String data;
  55. String server = "www.sweetcaffeine.nl"; // www.example.com
  56. String uri = "/growbox/getfileold.php";// our example is /Serial2post.php
  57.  
  58. void reset() {
  59.   lcd.setCursor(0, 0);
  60.   lcd.clear();
  61.   lcd.print("Reseting Wifi module ");
  62.   Serial2.println("AT+RST");
  63.   delay(1000);
  64.   if (Serial2.find("OK")) {
  65.     Serial.println("Module Reset");
  66.     Serial.println("Module OK");
  67.   } else if (Serial2.find("Ready")) {
  68.     Serial.println("Module Reset");
  69.     Serial.println("Module Ready");
  70.   } else if (Serial2.find("ready")) {
  71.     Serial.println("Module Reset");
  72.     Serial.println("Module ready");
  73.   }
  74.   else {
  75.     lcd.setCursor(0, 0);
  76.     lcd.clear();
  77.     lcd.print("Something went wrong,");
  78.     lcd.setCursor(0, 1);
  79.     lcd.print("trying to reset again ");
  80.     Serial.println("Module not reset, try again");
  81.     delay(100);
  82.     reset();
  83.   }
  84.  
  85. }
  86.  
  87. boolean checkfor(char* value)
  88. {
  89.   int i;
  90.   for (i = 0; i < 10; i++)
  91.   {
  92.     delay(2000);
  93.     if (Serial2.find(value)) {
  94.       Serial.println("Found");
  95.       Serial.println(value);
  96.       return true;
  97.     }
  98.     Serial.println ("try: ") + Serial.println(i);
  99.     lcd.setCursor(0, 0);
  100.     lcd.clear();
  101.     lcd.print("Trying to connect to wifi");
  102.     lcd.setCursor(0, 2);
  103.     lcd.print("Try:  ") +  lcd.print(i);;
  104.   }
  105.   return false;
  106. }
  107.  
  108. //connect to your wifi network
  109.  
  110. void connectWifi() {
  111.   String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\"";
  112.   delay(200);
  113.   Serial2.println(cmd);
  114.   if (!checkfor("OK")) {
  115.     Serial.println("Cannot connect to wifi");
  116.     lcd.setCursor(0, 0);
  117.     lcd.clear();
  118.     lcd.print("Couldn't connect to wifi");
  119.     connectWifi();
  120.   }
  121.   Serial.println("Connected!");
  122.   lcd.setCursor(0, 0);
  123.   lcd.clear();
  124.   lcd.print("Connected to wifi!");
  125.   Serial.println("printing cmd:" + cmd);
  126. }
  127.  
  128. void httppost() {
  129.   Serial2.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
  130.   if ( Serial2.find("OK")) {
  131.     lcd.setCursor(0, 0);
  132.     lcd.clear();
  133.     lcd.print("TCP connection ready");
  134.     Serial.println("TCP connection ready");
  135.   }
  136.   delay(1000);
  137.   String postRequest =
  138.  
  139.     "POST " + uri + " HTTP/1.0\r\n" +
  140.     "Host: " + server + "\r\n" +
  141.     "Accept: *" + "/" + "*\r\n" +
  142.     "Content-Length: " + data.length() + "\r\n" +
  143.     "Content-Type: application/x-www-form-urlencoded\r\n" +
  144.     "\r\n" + data;
  145.   String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
  146.  
  147.   Serial2.print(sendCmd);
  148.   Serial2.println(postRequest.length() );
  149.   delay(500);
  150.  
  151.   if (Serial2.find(">")) {
  152.     Serial.println("Sending.."); Serial2.print(postRequest);
  153.     if ( Serial2.find("SEND OK")) {
  154.       lcd.setCursor(0, 0);
  155.       lcd.clear();
  156.       lcd.print("Packet send");
  157.       lcd.setCursor(0, 2);
  158.       lcd.print(data);
  159.       Serial.println("Packet sent");
  160.       Serial.println(data);
  161.       delay(5000);
  162.       while (Serial2.available()) {
  163.         String tmpRSerial2 = Serial2.readString();
  164.         Serial.println(tmpRSerial2);
  165.       }
  166.  
  167.       Serial2.println("AT+CIPCLOSE");
  168.  
  169.     }
  170.  
  171.   }
  172. }
  173.  
  174. void screenupdate() {
  175.   int btn1 = digitalRead(inPin1);
  176.   int btn2 = digitalRead(inPin2);
  177.   int btn3 = digitalRead(inPin3);
  178.  
  179.   int m;
  180.   m = analogRead(sensorPin);
  181.   float t = dht.readTemperature();
  182.   float h = dht.readHumidity();
  183.  
  184.   lcd.clear();
  185.   lcd.setCursor(0, 0);
  186.  
  187.   DateTime now = rtc.now();
  188.   String hour = String(now.hour(), DEC);
  189.   String minute = String(now.minute(), DEC);
  190.   String second = String(now.second(), DEC);
  191.  
  192.   lcd.print("Time: ");
  193.   lcd.print(hour + ":" + minute + ":" + second);
  194.   lcd.setCursor(0, 1);
  195.   lcd.print("temperature: " + String(t));
  196.   lcd.setCursor(0, 2);
  197.   lcd.print("humidity: " + String(h));
  198.   lcd.setCursor(0, 3);
  199.   lcd.print("Soil humidity: " + String(m));
  200.   if (btn1 == HIGH) {
  201.     if (backlight == false) {
  202.       lcd.setBacklight(HIGH);
  203.       delay(1000);
  204.       backlight = true;
  205.     } else if (backlight == true) {
  206.       lcd.setBacklight(LOW);
  207.       delay(1000);
  208.       backlight = false;
  209.     }
  210.   }
  211.   delay(1000);
  212. }
  213.  
  214. void setup() {
  215.   lcd.setBacklightPin(3, POSITIVE);
  216.   lcd.setBacklight(HIGH); // NOTE: You can turn the backlight off by setting it to LOW instead of HIGH
  217.   lcd.begin(20, 4);
  218.   lcd.clear();
  219.   Serial2.begin(115200);
  220.   Serial.begin(115200);
  221.   lcd.setCursor(0, 0);
  222.   lcd.clear();
  223.   lcd.print("Growbox");
  224.   lcd.setCursor(0, 1);
  225.   lcd.print("By: Vincent Venhuizen");
  226.   lcd.setCursor(0, 3);
  227.   lcd.print("Version 1 - Update 14");
  228.   delay(2000);
  229.   Serial.println("GROWBOX_V4");
  230.   Serial.println("Starting reset");
  231.   //reset();
  232.   Serial.println("Connecting Wifi");
  233.   //connectWifi();
  234.   Serial.begin(115200);
  235.  
  236.   if (! rtc.begin()) {
  237.     Serial.println("Couldn't find RTC");
  238.     while (1);
  239.   }
  240.  
  241.   if (! rtc.isrunning()) {
  242.     Serial.println("RTC is NOT running!");
  243.     // following line sets the RTC to the date & time this sketch was compiled
  244.     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  245.     // This line sets the RTC with an explicit date & time, for example to set
  246.     // January 21, 2014 at 3am you would call:
  247.     // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  248.   }
  249.  
  250.   pinMode(TRIGGER_PIN, OUTPUT);
  251.   pinMode(ECHO_PIN, INPUT);
  252.  
  253.   // Reset the trigger pin and wait a half a second
  254.   digitalWrite(TRIGGER_PIN, LOW);
  255.   delayMicroseconds(500);
  256.  
  257.  
  258.   pinMode(dir1PinA, OUTPUT);
  259.   pinMode(dir2PinA, OUTPUT);
  260.   pinMode(dir1PinB, OUTPUT);
  261.   pinMode(dir2PinB, OUTPUT);
  262.   pinMode(RELAY1, OUTPUT);
  263.   pinMode(RELAY2, OUTPUT);
  264.   pinMode(Fan1, OUTPUT);
  265.   pinMode(Fan2, OUTPUT);
  266.  
  267.   pinMode(inPin1, INPUT);
  268.   pinMode(inPin2, INPUT);
  269.   pinMode(inPin3, INPUT);
  270.  
  271.   digitalWrite(Fan1, HIGH);
  272.   digitalWrite(Fan2, HIGH);
  273.  
  274. }
  275.  
  276. void loop () {
  277.  
  278.   int btn1 = digitalRead(inPin1);
  279.   int btn2 = digitalRead(inPin2);
  280.   int btn3 = digitalRead(inPin3);
  281.   if (btn1 == HIGH) {
  282.     if (backlight == false) {
  283.       lcd.setBacklight(HIGH);
  284.       delay(1000);
  285.       backlight = true;
  286.     } else if (backlight == true) {
  287.       lcd.setBacklight(LOW);
  288.       delay(1000);
  289.       backlight = false;
  290.     }
  291.   }
  292.  
  293.   DateTime now = rtc.now();
  294.   String hour = String(now.hour(), DEC);
  295.   String minute = String(now.minute(), DEC);
  296.   String second = String(now.second(), DEC);
  297.   screenupdate();
  298.   if (analogRead(sensorPin) > 500) {
  299.     digitalWrite(dir1PinB, LOW);
  300.     digitalWrite(dir2PinB, HIGH);
  301.   } else {
  302.     digitalWrite(dir1PinB, LOW);
  303.     digitalWrite(dir2PinB, LOW);
  304.   }
  305.   if (dht.readTemperature() < 20) {
  306.     digitalWrite(dir1PinA, LOW);
  307.     digitalWrite(dir2PinA, HIGH);
  308.   } else {
  309.     digitalWrite(dir1PinA, LOW);
  310.     digitalWrite(dir2PinA, LOW);
  311.   }
  312.   if ((now.hour() > 8) || (now.hour() < 23)) {
  313.     digitalWrite(RELAY1, LOW);
  314.     digitalWrite(RELAY2, LOW);
  315.   } else {
  316.     digitalWrite(RELAY1, HIGH);
  317.     digitalWrite(RELAY2, HIGH);
  318.   }
  319.  
  320.   //update
  321.   if (now.hour() == 4 || now.hour() == 8 || now.hour() == 12 || now.hour() == 16 || now.hour() == 20 || now.hour() == 0 ) {
  322.     int m;
  323.     m = analogRead(sensorPin);
  324.     float t = dht.readTemperature();
  325.     float h = dht.readHumidity();
  326.     String sendtime = hour + ":" + minute + ":" + second;
  327.     String duday = day;
  328.     string dumonth = month;
  329.  
  330.     data = "password=growbox&temperature=" + String(t) + "&time=" + String(sendtime) + "&humidity=" + String(h) + "&groundmoisture=" + String(m) + "&day=" + String(duday) + "&month=" String(dumonth);
  331.            httppost();
  332.   }
  333.  
  334.  
  335.   if (btn2 == HIGH) {
  336.     if (fan == false) {
  337.       lcd.clear();
  338.       lcd.print("Fan's: OFF");
  339.       fan = true;
  340.       digitalWrite(Fan1, LOW);
  341.       digitalWrite(Fan2, LOW);
  342.       delay(2000);
  343.     } else if (fan == true) {
  344.       lcd.clear();
  345.       lcd.print("Fan's: ON");
  346.       fan = false;
  347.       digitalWrite(Fan1, HIGH);
  348.       digitalWrite(Fan2, HIGH);
  349.       delay(2000);
  350.     }
  351.  
  352.     delay(1000);
  353.   }
  354.  
  355.  
  356.   if (btn3 == HIGH) {
  357.     delay(MEASURE_DELAY);
  358.  
  359.     long distance = measure();
  360.     lcd.setCursor(0, 0);
  361.     lcd.clear();
  362.     lcd.print("Distance: ");
  363.     lcd.setCursor(10, 0);
  364.     lcd.print(distance);
  365.     lcd.setCursor(17, 0);
  366.     lcd.print(" mm");
  367.     lcd.setCursor(0, 2);
  368.     lcd.print("Plant height:");
  369.     lcd.print(600 - distance);
  370.     lcd.print(" mm");
  371.     delay(2000);
  372.   }
  373.  
  374. }
  375.  
  376. long measure() {
  377.   long measureSum = 0;
  378.   for (int i = 0; i < MEASURE_SAMPLES; i++)
  379.   {
  380.     delay(MEASURE_SAMPLE_DELAY);
  381.     measureSum += singleMeasurement();
  382.   }
  383.   return measureSum / MEASURE_SAMPLES;
  384. }
  385.  
  386. long singleMeasurement()
  387. {
  388.   long duration = 0;
  389.   // Measure: Put up Trigger...
  390.   digitalWrite(TRIGGER_PIN, HIGH);
  391.   // ... wait for 11 ยตs ...
  392.   delayMicroseconds(11);
  393.   // ... put the trigger down ...
  394.   digitalWrite(TRIGGER_PIN, LOW);
  395.   // ... and wait for the echo ...
  396.   duration = pulseIn(ECHO_PIN, HIGH);
  397.   return (long) (((float) duration / USONIC_DIV) * 10.0);
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement