Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <DHT.h>
- #include <DHT_U.h>
- #include "SoftwareSerial.h"
- #include <Wire.h>
- #include <LCD.h>
- #include <LiquidCrystal_I2C.h>
- #include "RTClib.h"
- #define DHTPIN 5
- #define DHTTYPE DHT21
- DHT dht(DHTPIN, DHTTYPE);
- #define TRIGGER_PIN 13
- #define ECHO_PIN 12
- #define USONIC_DIV 58.0
- #define MEASURE_SAMPLE_DELAY 5
- #define MEASURE_SAMPLES 25
- #define MEASURE_DELAY 250
- #define trigPin 13
- #define echoPin 12
- LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7);
- RTC_DS1307 rtc;
- int sensorPin = A0;
- // Motor 1
- int dir1PinA = 11;
- int dir2PinA = 10;
- // Motor 2
- int dir1PinB = 9;
- int dir2PinB = 8;
- //light relays
- int RELAY1 = 6;
- int RELAY2 = 7;
- //fans
- int Fan1 = 50;
- int Fan2 = 52;
- //buttons
- int inPin1 = 4; // choose the input pin (for a pushbutton)
- int inPin2 = 3; // choose the input pin (for a pushbutton)
- int inPin3 = 2; // choose the input pin (for a pushbutton)
- boolean backlight = false;
- boolean fan = false;
- String ssid = "Visnet";
- String password = "vinvisje";
- String data;
- String server = "www.sweetcaffeine.nl"; // www.example.com
- String uri = "/growbox/getfileold.php";// our example is /Serial2post.php
- void reset() {
- lcd.setCursor(0, 0);
- lcd.clear();
- lcd.print("Reseting Wifi module ");
- Serial2.println("AT+RST");
- delay(1000);
- if (Serial2.find("OK")) {
- Serial.println("Module Reset");
- Serial.println("Module OK");
- } else if (Serial2.find("Ready")) {
- Serial.println("Module Reset");
- Serial.println("Module Ready");
- } else if (Serial2.find("ready")) {
- Serial.println("Module Reset");
- Serial.println("Module ready");
- }
- else {
- lcd.setCursor(0, 0);
- lcd.clear();
- lcd.print("Something went wrong,");
- lcd.setCursor(0, 1);
- lcd.print("trying to reset again ");
- Serial.println("Module not reset, try again");
- delay(100);
- reset();
- }
- }
- boolean checkfor(char* value)
- {
- int i;
- for (i = 0; i < 10; i++)
- {
- delay(2000);
- if (Serial2.find(value)) {
- Serial.println("Found");
- Serial.println(value);
- return true;
- }
- Serial.println ("try: ") + Serial.println(i);
- lcd.setCursor(0, 0);
- lcd.clear();
- lcd.print("Trying to connect to wifi");
- lcd.setCursor(0, 2);
- lcd.print("Try: ") + lcd.print(i);;
- }
- return false;
- }
- //connect to your wifi network
- void connectWifi() {
- String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\"";
- delay(200);
- Serial2.println(cmd);
- if (!checkfor("OK")) {
- Serial.println("Cannot connect to wifi");
- lcd.setCursor(0, 0);
- lcd.clear();
- lcd.print("Couldn't connect to wifi");
- connectWifi();
- }
- Serial.println("Connected!");
- lcd.setCursor(0, 0);
- lcd.clear();
- lcd.print("Connected to wifi!");
- Serial.println("printing cmd:" + cmd);
- }
- void httppost() {
- Serial2.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
- if ( Serial2.find("OK")) {
- lcd.setCursor(0, 0);
- lcd.clear();
- lcd.print("TCP connection ready");
- Serial.println("TCP connection ready");
- }
- delay(1000);
- String postRequest =
- "POST " + uri + " HTTP/1.0\r\n" +
- "Host: " + server + "\r\n" +
- "Accept: *" + "/" + "*\r\n" +
- "Content-Length: " + data.length() + "\r\n" +
- "Content-Type: application/x-www-form-urlencoded\r\n" +
- "\r\n" + data;
- String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
- Serial2.print(sendCmd);
- Serial2.println(postRequest.length() );
- delay(500);
- if (Serial2.find(">")) {
- Serial.println("Sending.."); Serial2.print(postRequest);
- if ( Serial2.find("SEND OK")) {
- lcd.setCursor(0, 0);
- lcd.clear();
- lcd.print("Packet send");
- lcd.setCursor(0, 2);
- lcd.print(data);
- Serial.println("Packet sent");
- Serial.println(data);
- delay(5000);
- while (Serial2.available()) {
- String tmpRSerial2 = Serial2.readString();
- Serial.println(tmpRSerial2);
- }
- Serial2.println("AT+CIPCLOSE");
- }
- }
- }
- void screenupdate() {
- int btn1 = digitalRead(inPin1);
- int btn2 = digitalRead(inPin2);
- int btn3 = digitalRead(inPin3);
- int m;
- m = analogRead(sensorPin);
- float t = dht.readTemperature();
- float h = dht.readHumidity();
- lcd.clear();
- lcd.setCursor(0, 0);
- DateTime now = rtc.now();
- String hour = String(now.hour(), DEC);
- String minute = String(now.minute(), DEC);
- String second = String(now.second(), DEC);
- lcd.print("Time: ");
- lcd.print(hour + ":" + minute + ":" + second);
- lcd.setCursor(0, 1);
- lcd.print("temperature: " + String(t));
- lcd.setCursor(0, 2);
- lcd.print("humidity: " + String(h));
- lcd.setCursor(0, 3);
- lcd.print("Soil humidity: " + String(m));
- if (btn1 == HIGH) {
- if (backlight == false) {
- lcd.setBacklight(HIGH);
- delay(1000);
- backlight = true;
- } else if (backlight == true) {
- lcd.setBacklight(LOW);
- delay(1000);
- backlight = false;
- }
- }
- delay(1000);
- }
- void setup() {
- lcd.setBacklightPin(3, POSITIVE);
- lcd.setBacklight(HIGH); // NOTE: You can turn the backlight off by setting it to LOW instead of HIGH
- lcd.begin(20, 4);
- lcd.clear();
- Serial2.begin(115200);
- Serial.begin(115200);
- lcd.setCursor(0, 0);
- lcd.clear();
- lcd.print("Growbox");
- lcd.setCursor(0, 1);
- lcd.print("By: Vincent Venhuizen");
- lcd.setCursor(0, 3);
- lcd.print("Version 1 - Update 14");
- delay(2000);
- Serial.println("GROWBOX_V4");
- Serial.println("Starting reset");
- //reset();
- Serial.println("Connecting Wifi");
- //connectWifi();
- Serial.begin(115200);
- if (! rtc.begin()) {
- Serial.println("Couldn't find RTC");
- while (1);
- }
- if (! rtc.isrunning()) {
- Serial.println("RTC is NOT running!");
- // following line sets the RTC to the date & time this sketch was compiled
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- // This line sets the RTC with an explicit date & time, for example to set
- // January 21, 2014 at 3am you would call:
- // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
- }
- pinMode(TRIGGER_PIN, OUTPUT);
- pinMode(ECHO_PIN, INPUT);
- // Reset the trigger pin and wait a half a second
- digitalWrite(TRIGGER_PIN, LOW);
- delayMicroseconds(500);
- pinMode(dir1PinA, OUTPUT);
- pinMode(dir2PinA, OUTPUT);
- pinMode(dir1PinB, OUTPUT);
- pinMode(dir2PinB, OUTPUT);
- pinMode(RELAY1, OUTPUT);
- pinMode(RELAY2, OUTPUT);
- pinMode(Fan1, OUTPUT);
- pinMode(Fan2, OUTPUT);
- pinMode(inPin1, INPUT);
- pinMode(inPin2, INPUT);
- pinMode(inPin3, INPUT);
- digitalWrite(Fan1, HIGH);
- digitalWrite(Fan2, HIGH);
- }
- void loop () {
- int btn1 = digitalRead(inPin1);
- int btn2 = digitalRead(inPin2);
- int btn3 = digitalRead(inPin3);
- if (btn1 == HIGH) {
- if (backlight == false) {
- lcd.setBacklight(HIGH);
- delay(1000);
- backlight = true;
- } else if (backlight == true) {
- lcd.setBacklight(LOW);
- delay(1000);
- backlight = false;
- }
- }
- DateTime now = rtc.now();
- String hour = String(now.hour(), DEC);
- String minute = String(now.minute(), DEC);
- String second = String(now.second(), DEC);
- screenupdate();
- if (analogRead(sensorPin) > 500) {
- digitalWrite(dir1PinB, LOW);
- digitalWrite(dir2PinB, HIGH);
- } else {
- digitalWrite(dir1PinB, LOW);
- digitalWrite(dir2PinB, LOW);
- }
- if (dht.readTemperature() < 20) {
- digitalWrite(dir1PinA, LOW);
- digitalWrite(dir2PinA, HIGH);
- } else {
- digitalWrite(dir1PinA, LOW);
- digitalWrite(dir2PinA, LOW);
- }
- if ((now.hour() > 8) || (now.hour() < 23)) {
- digitalWrite(RELAY1, LOW);
- digitalWrite(RELAY2, LOW);
- } else {
- digitalWrite(RELAY1, HIGH);
- digitalWrite(RELAY2, HIGH);
- }
- //update
- if (now.hour() == 4 || now.hour() == 8 || now.hour() == 12 || now.hour() == 16 || now.hour() == 20 || now.hour() == 0 ) {
- int m;
- m = analogRead(sensorPin);
- float t = dht.readTemperature();
- float h = dht.readHumidity();
- String sendtime = hour + ":" + minute + ":" + second;
- String duday = day;
- string dumonth = month;
- data = "password=growbox&temperature=" + String(t) + "&time=" + String(sendtime) + "&humidity=" + String(h) + "&groundmoisture=" + String(m) + "&day=" + String(duday) + "&month=" String(dumonth);
- httppost();
- }
- if (btn2 == HIGH) {
- if (fan == false) {
- lcd.clear();
- lcd.print("Fan's: OFF");
- fan = true;
- digitalWrite(Fan1, LOW);
- digitalWrite(Fan2, LOW);
- delay(2000);
- } else if (fan == true) {
- lcd.clear();
- lcd.print("Fan's: ON");
- fan = false;
- digitalWrite(Fan1, HIGH);
- digitalWrite(Fan2, HIGH);
- delay(2000);
- }
- delay(1000);
- }
- if (btn3 == HIGH) {
- delay(MEASURE_DELAY);
- long distance = measure();
- lcd.setCursor(0, 0);
- lcd.clear();
- lcd.print("Distance: ");
- lcd.setCursor(10, 0);
- lcd.print(distance);
- lcd.setCursor(17, 0);
- lcd.print(" mm");
- lcd.setCursor(0, 2);
- lcd.print("Plant height:");
- lcd.print(600 - distance);
- lcd.print(" mm");
- delay(2000);
- }
- }
- long measure() {
- long measureSum = 0;
- for (int i = 0; i < MEASURE_SAMPLES; i++)
- {
- delay(MEASURE_SAMPLE_DELAY);
- measureSum += singleMeasurement();
- }
- return measureSum / MEASURE_SAMPLES;
- }
- long singleMeasurement()
- {
- long duration = 0;
- // Measure: Put up Trigger...
- digitalWrite(TRIGGER_PIN, HIGH);
- // ... wait for 11 ยตs ...
- delayMicroseconds(11);
- // ... put the trigger down ...
- digitalWrite(TRIGGER_PIN, LOW);
- // ... and wait for the echo ...
- duration = pulseIn(ECHO_PIN, HIGH);
- return (long) (((float) duration / USONIC_DIV) * 10.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement