Skip to content

Commit 7403cf3

Browse files
Merge pull request #4 from sohamadhikari/sohamadhikari-patch-final
Update irrigation-frontend.ino
2 parents 0788f92 + a6b9e3a commit 7403cf3

File tree

1 file changed

+122
-25
lines changed

1 file changed

+122
-25
lines changed

irrigation-frontend.ino

+122-25
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,117 @@
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
46

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

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

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
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
1623

17-
int ldrStatus = analogRead(ldrPin); //LDR reading
18-
if (ldrStatus <= 200) {
19-
digitalWrite(ledPin, HIGH);
20-
}
21-
else {
22-
digitalWrite(ledPin, LOW);
23-
}
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");
2431

25-
moisturePercentage = ( 100.00 - ( (analogRead(moisturePin) / 1023.00) * 100.00 ) ); //read and publish moisture sensor data
26-
Serial.print(moisturePercentage);
27-
Serial.println("%");
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
2850
}
29-
if (moisturePercentage < 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+
65+
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+
90+
91+
if (moisturePercentage < 35) {
3092
digitalWrite(motorPin, HIGH); // tun on motor
3193
}
32-
if (moisturePercentage > 50 && moisturePercentage < 55) {
94+
if (moisturePercentage > 35 && moisturePercentage < 37) {
3395
digitalWrite(motorPin, HIGH); //turn on motor pump
3496
}
35-
if (moisturePercentage > 56) {
97+
if (moisturePercentage > 38) {
3698
digitalWrite(motorPin, LOW); // turn off mottor
3799
}
38-
if (! AgricultureData.publish(moisturePercentage)) {
39-
delay(5000);
40-
}
41100

42101

43-
Adafruit_MQTT_Subscribe * subscription; //read subsribed feeds
102+
if (! AgricultureData.publish(moisturePercentage))
103+
{
104+
delay(5000);
105+
}
106+
Adafruit_MQTT_Subscribe * subscription;
44107
while ((subscription = mqtt.readSubscription(5000)))
45108
{
109+
46110
if (subscription == &LED)
47111
{
112+
//Print the new value to the serial monitor
48113
Serial.println((char*) LED.lastread);
114+
49115
if (!strcmp((char*) LED.lastread, "OFF"))
50116
{
51117
digitalWrite(ledPin, HIGH);
@@ -55,9 +121,12 @@ while ((subscription = mqtt.readSubscription(5000)))
55121
digitalWrite(ledPin, LOW);
56122
}
57123
}
58-
if (subscription == &Pump)
124+
125+
if (subscription == &Pump)
59126
{
127+
//Print the new value to the serial monitor
60128
Serial.println((char*) Pump.lastread);
129+
61130
if (!strcmp((char*) Pump.lastread, "OFF"))
62131
{
63132
digitalWrite(motorPin, HIGH);
@@ -66,3 +135,31 @@ if (subscription == &Pump)
66135
{
67136
digitalWrite(motorPin, LOW);
68137
}
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)