Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #include <LiquidCrystal.h>
- #include <SimpleDHT.h>
- #define pinDHT11 5
- SimpleDHT11 dht11(pinDHT11);
- #include <OneWire.h>
- #include <DallasTemperature.h>
- int ONE_WIRE_BUS = 4;
- OneWire oneWire (ONE_WIRE_BUS);
- DallasTemperature sensors (&oneWire);
- const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
- LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
- #define moisturepin A0
- #define relaypin A2
- #define RE 7
- #define DE 6
- const uint32_t TIMEOUT = 500UL;
- const byte moist[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A};
- const byte temp[] = {0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0xD5, 0xCA};
- const byte EC[] = {0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x25, 0xCA};
- const byte PH[] = {0x01, 0x03, 0x00, 0x03, 0x00, 0x01, 0x74, 0x0A};
- byte values[11];
- SoftwareSerial mod(2, 3); // Rx pin, Tx pin
- void setup() {
- pinMode(moisturepin,INPUT); // mositure sensor
- pinMode(relaypin,OUTPUT); // Relay module control
- pinMode(5,INPUT);
- Serial.begin(115200);
- sensors.begin();
- lcd.begin(20, 4);
- mod.begin(4800);
- pinMode(RE, OUTPUT);
- pinMode(DE, OUTPUT);
- delay(500);
- }
- void loop() {
- uint16_t val1, val2, val3, val4;
- Serial.println("Moisture: ");
- val1 = moisture();
- float Val1 = val1*0.1;
- Serial.print(Val1);
- Serial.println(" %");
- Serial.println("-----");
- Serial.println("Temperature: ");
- val2 = temperature();
- float Val2 = val2*0.1;
- Serial.print(Val2);
- Serial.println(" *C");
- Serial.println("-----");
- Serial.println("Conductivity: ");
- val3 = conductivity();
- Serial.print(val3);
- Serial.println(" us/cm");
- Serial.println("-----");
- Serial.println("Ph: ");
- val4 = ph();
- float Val4 = val4*0.1;
- Serial.print(Val4);
- Serial.println(" ph");
- Serial.println("-----");
- lcd.setCursor(0, 0);
- lcd.print("Tg");
- lcd.setCursor(8, 0);
- lcd.print("C,");
- lcd.setCursor(0, 1);
- lcd.print("Mg");
- lcd.setCursor(8, 1);
- lcd.print("%,");
- lcd.setCursor(0, 2);
- lcd.print("ECg");
- //lcd.setCursor(7, 2);
- lcd.setCursor(0, 3);
- lcd.print("PHg");
- lcd.setCursor(7, 3);
- lcd.print("ph,");
- lcd.setCursor(10, 0);
- lcd.print("Tp");
- lcd.setCursor(19, 0);
- lcd.print("C");
- lcd.setCursor(10, 1);
- lcd.print("Ta");
- lcd.setCursor(19, 1);
- lcd.print("C");
- lcd.setCursor(10, 2);
- lcd.print("Mp");
- lcd.setCursor(10, 3);
- lcd.print("Ha");
- lcd.setCursor(19, 3);
- lcd.print("%");
- lcd.setCursor(2, 1);
- lcd.print(Val1);
- lcd.setCursor(2, 0);
- lcd.print(Val2);
- lcd.setCursor(3, 2);
- lcd.print(val3);
- lcd.setCursor(3, 3);
- lcd.print(Val4);
- lcd.setCursor(12, 0);
- lcd.print(sensors.getTempCByIndex(0));
- //Moisture probe
- int sensorValue = analogRead(moisturepin);
- Serial.println ("MOISTURE READING");
- Serial.print (sensorValue);
- lcd.setCursor(12, 2);
- lcd.print(sensorValue);
- //dht section
- // start working...
- Serial.println("=================================");
- Serial.println("Sample DHT11...");
- // read without samples.
- byte temperature = 0;
- byte humidity = 0;
- int err = SimpleDHTErrSuccess;
- if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
- Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err));
- Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(1000);
- return;
- }
- Serial.print("Sample OK: ");
- Serial.print((int)temperature); Serial.print(" *C, ");
- Serial.print((int)humidity); Serial.println(" H");
- lcd.setCursor(12, 1);
- lcd.print((int)temperature);
- lcd.setCursor(12, 3);
- lcd.print((int)humidity);
- //Temperature probe
- sensors.requestTemperatures ();
- Serial.print("Celsius temperature: ");
- Serial.print(sensors.getTempCByIndex(0));
- Serial.print(" - Fahrenheit temperature: ");
- Serial.println (sensors.getTempFByIndex(0));
- delay(3000);
- if (Val1 <= 55.00) { //value for minimum value to open the valve
- analogWrite(relaypin, 255);
- Serial.print("Valve on");
- lcd.setCursor(18, 2);
- lcd.print("ON");
- Serial.print("ON");
- }
- if (Val1 >= 80.00) { //value for maximum value to close the valve
- analogWrite(relaypin, 0);
- Serial.print("Valve off");
- lcd.setCursor(17, 2);
- lcd.print("OFF");
- Serial.print("OFF");
- }
- }
- int16_t moisture() {
- uint32_t startTime = 0;
- uint8_t byteCount = 0;
- digitalWrite(DE, HIGH);
- digitalWrite(RE, HIGH);
- delay(10);
- mod.write(moist, sizeof(moist));
- mod.flush();
- digitalWrite(DE, LOW);
- digitalWrite(RE, LOW);
- startTime = millis();
- while ( millis() - startTime <= TIMEOUT ) {
- if (mod.available() && byteCount<sizeof(values) ) {
- values[byteCount++] = mod.read();
- printHexByte(values[byteCount-1]);
- }
- }
- Serial.println();
- return (int16_t)(values[4] << 8 | values[5]);
- }
- int16_t temperature() {
- uint32_t startTime = 0;
- uint8_t byteCount = 0;
- digitalWrite(DE, HIGH);
- digitalWrite(RE, HIGH);
- delay(10);
- mod.write(temp, sizeof(temp));
- mod.flush();
- digitalWrite(DE, LOW);
- digitalWrite(RE, LOW);
- startTime = millis();
- while ( millis() - startTime <= TIMEOUT ) {
- if (mod.available() && byteCount<sizeof(values) ) {
- values[byteCount++] = mod.read();
- printHexByte(values[byteCount-1]);
- }
- }
- Serial.println();
- return (int16_t)(values[4] << 8 | values[5]);
- }
- int16_t conductivity() {
- uint32_t startTime = 0;
- uint8_t byteCount = 0;
- digitalWrite(DE, HIGH);
- digitalWrite(RE, HIGH);
- delay(10);
- mod.write(EC, sizeof(EC));
- mod.flush();
- digitalWrite(DE, LOW);
- digitalWrite(RE, LOW);
- startTime = millis();
- while ( millis() - startTime <= TIMEOUT ) {
- if (mod.available() && byteCount<sizeof(values) ) {
- values[byteCount++] = mod.read();
- printHexByte(values[byteCount-1]);
- }
- }
- Serial.println();
- return (int16_t)(values[4] << 8 | values[5]);
- }
- int16_t ph() {
- uint32_t startTime = 0;
- uint8_t byteCount = 0;
- digitalWrite(DE, HIGH);
- digitalWrite(RE, HIGH);
- delay(10);
- mod.write(PH, sizeof(PH));
- mod.flush();
- digitalWrite(DE, LOW);
- digitalWrite(RE, LOW);
- startTime = millis();
- while ( millis() - startTime <= TIMEOUT ) {
- if (mod.available() && byteCount<sizeof(values) ) {
- values[byteCount++] = mod.read();
- printHexByte(values[byteCount-1]);
- }
- }
- Serial.println();
- return (int16_t)(values[4] << 8 | values[5]);
- }
- void printHexByte(byte b)
- {
- Serial.print((b >> 4) & 0xF, HEX);
- Serial.print(b & 0xF, HEX);
- Serial.print(' ');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement