Skip to content

Commit 6db3127

Browse files
Merge pull request #5 from sohamadhikari/sohamadhikari-patch-4
Added feature
2 parents 7403cf3 + bf9b1c3 commit 6db3127

File tree

1 file changed

+26
-124
lines changed

1 file changed

+26
-124
lines changed

irrigation-frontend.ino

+26-124
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,50 @@
11
#include <ESP8266WiFi.h>
22
#include "Adafruit_MQTT.h"
33
#include "Adafruit_MQTT_Client.h"
4-
const char *ssid = "Galaxy-M20"; // Enter your WiFi Name
5-
const char *pass = "ac312124"; // Enter your WiFi Password
64

7-
WiFiClient client;
5+
const char *ssid = "WiFi Name"; // Enter your WiFi Name
6+
const char *pass = "Password"; // Enter your WiFi Password
87

98
#define MQTT_SERV "io.adafruit.com"
109
#define MQTT_PORT 1883
11-
#define MQTT_NAME "choudharyas"
12-
#define MQTT_PASS "988c4e045ef64c1b9bc8b5bb7ef5f2d9"
10+
#define MQTT_NAME "User Name"
11+
#define MQTT_PASS "AIO Key"
1312

14-
const int ledPin = D6;
15-
const int ldrPin = D1;
16-
const int moisturePin = A0; // moisteure sensor pin
17-
const int motorPin = D0;
18-
unsigned long interval = 10000;
19-
unsigned long previousMillis = 0;
20-
unsigned long interval1 = 1000;
21-
unsigned long previousMillis1 = 0;
22-
float moisturePercentage; //moisture reading
13+
Adafruit_MQTT_Publish AgricultureData = Adafruit_MQTT_Publish(&mqtt,MQTT_NAME "/f/AgricultureData"); //publishing feed setup
14+
Adafruit_MQTT_Subscribe LED = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/LED");
15+
Adafruit_MQTT_Subscribe Pump = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/Pump"); //subscribed feed setup
2316

24-
//Set up the feed you're publishing to
25-
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, MQTT_PASS);
26-
Adafruit_MQTT_Publish AgricultureData = Adafruit_MQTT_Publish(&mqtt,MQTT_NAME "/f/AgricultureData");
27-
28-
//Set up the feed you're subscribing to
29-
Adafruit_MQTT_Subscribe LED = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/LED");
30-
Adafruit_MQTT_Subscribe Pump = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/Pump");
31-
32-
void setup()
33-
{
34-
Serial.begin(115200);
35-
delay(10);
36-
mqtt.subscribe(&LED);
37-
mqtt.subscribe(&Pump);
38-
pinMode(motorPin, OUTPUT);
39-
pinMode(ledPin, OUTPUT);
40-
pinMode(ldrPin, INPUT);
41-
digitalWrite(motorPin, LOW); // keep motor off initally
42-
43-
Serial.println("Connecting to ");
44-
Serial.println(ssid);
45-
WiFi.begin(ssid, pass);
46-
while (WiFi.status() != WL_CONNECTED)
47-
{
48-
delay(500);
49-
Serial.print("."); // print ... till not connected
50-
}
51-
Serial.println("");
52-
Serial.println("WiFi connected");
53-
}
54-
55-
void loop()
56-
57-
{
58-
MQTT_connect();
59-
60-
61-
int ldrStatus = analogRead(ldrPin);
62-
63-
if (ldrStatus <= 200) {
64-
17+
int ldrStatus = analogRead(ldrPin); //LDR reading
18+
if (ldrStatus <= 200) {
6519
digitalWrite(ledPin, HIGH);
66-
67-
Serial.print("Its DARK, Turn on the LED : ");
68-
69-
Serial.println(ldrStatus);
70-
71-
}
72-
else {
73-
74-
digitalWrite(ledPin, LOW);
75-
76-
Serial.print("Its BRIGHT, Turn off the LED : ");
77-
78-
Serial.println(ldrStatus);
79-
80-
}
81-
82-
moisturePercentage = ( 100.00 - ( (analogRead(moisturePin) / 1023.00) * 100.00 ) );
83-
84-
85-
Serial.print("Soil Moisture is = ");
86-
Serial.print(moisturePercentage);
87-
Serial.println("%");
88-
89-
20+
}
21+
else {
22+
digitalWrite(ledPin, LOW);
23+
}
9024

91-
if (moisturePercentage < 35) {
25+
moisturePercentage = ( 100.00 - ( (analogRead(moisturePin) / 1023.00) * 100.00 ) ); //read and publish moisture sensor data
26+
Serial.print(moisturePercentage);
27+
Serial.println("%");
28+
}
29+
if (moisturePercentage < 50) {
9230
digitalWrite(motorPin, HIGH); // tun on motor
9331
}
94-
if (moisturePercentage > 35 && moisturePercentage < 37) {
32+
if (moisturePercentage > 50 && moisturePercentage < 55) {
9533
digitalWrite(motorPin, HIGH); //turn on motor pump
9634
}
97-
if (moisturePercentage > 38) {
35+
if (moisturePercentage > 56) {
9836
digitalWrite(motorPin, LOW); // turn off mottor
9937
}
100-
101-
102-
if (! AgricultureData.publish(moisturePercentage))
103-
{
104-
delay(5000);
105-
}
106-
Adafruit_MQTT_Subscribe * subscription;
38+
if (! AgricultureData.publish(moisturePercentage)) {
39+
delay(5000);
40+
}
41+
42+
Adafruit_MQTT_Subscribe * subscription; //read subsribed feeds
10743
while ((subscription = mqtt.readSubscription(5000)))
10844
{
109-
11045
if (subscription == &LED)
11146
{
112-
//Print the new value to the serial monitor
11347
Serial.println((char*) LED.lastread);
114-
11548
if (!strcmp((char*) LED.lastread, "OFF"))
11649
{
11750
digitalWrite(ledPin, HIGH);
@@ -121,12 +54,9 @@ while ((subscription = mqtt.readSubscription(5000)))
12154
digitalWrite(ledPin, LOW);
12255
}
12356
}
124-
125-
if (subscription == &Pump)
57+
if (subscription == &Pump)
12658
{
127-
//Print the new value to the serial monitor
12859
Serial.println((char*) Pump.lastread);
129-
13060
if (!strcmp((char*) Pump.lastread, "OFF"))
13161
{
13262
digitalWrite(motorPin, HIGH);
@@ -135,31 +65,3 @@ while ((subscription = mqtt.readSubscription(5000)))
13565
{
13666
digitalWrite(motorPin, LOW);
13767
}
138-
139-
}
140-
}
141-
}
142-
void MQTT_connect()
143-
{
144-
int8_t ret;
145-
146-
// Stop if already connected.
147-
if (mqtt.connected())
148-
{
149-
return;
150-
}
151-
152-
uint8_t retries = 3;
153-
while ((ret = mqtt.connect()) != 0) // connect will return 0 for connected
154-
{
155-
156-
mqtt.disconnect();
157-
delay(5000); // wait 5 seconds
158-
retries--;
159-
if (retries == 0)
160-
{
161-
// basically die and wait for WDT to reset me
162-
while (1);
163-
}
164-
}
165-
}

0 commit comments

Comments
 (0)