1
1
#include < ESP8266WiFi.h>
2
2
#include " Adafruit_MQTT.h"
3
3
#include " Adafruit_MQTT_Client.h"
4
+ const char *ssid = " Galaxy-M20" ; // Enter your WiFi Name
5
+ const char *pass = " ac312124" ; // Enter your WiFi Password
4
6
5
- const char *ssid = " WiFi Name" ; // Enter your WiFi Name
6
- const char *pass = " Password" ; // Enter your WiFi Password
7
+ WiFiClient client;
7
8
8
9
#define MQTT_SERV " io.adafruit.com"
9
10
#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 "
12
13
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
16
23
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 " );
24
31
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
28
50
}
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 ) {
30
92
digitalWrite (motorPin, HIGH); // tun on motor
31
93
}
32
- if (moisturePercentage > 50 && moisturePercentage < 55 ) {
94
+ if (moisturePercentage > 35 && moisturePercentage < 37 ) {
33
95
digitalWrite (motorPin, HIGH); // turn on motor pump
34
96
}
35
- if (moisturePercentage > 56 ) {
97
+ if (moisturePercentage > 38 ) {
36
98
digitalWrite (motorPin, LOW); // turn off mottor
37
99
}
38
- if (! AgricultureData.publish(moisturePercentage)) {
39
- delay (5000 );
40
- }
41
-
42
- Adafruit_MQTT_Subscribe * subscription; // read subsribed feeds
100
+
101
+
102
+ if (! AgricultureData.publish (moisturePercentage))
103
+ {
104
+ delay (5000 );
105
+ }
106
+ Adafruit_MQTT_Subscribe * subscription;
43
107
while ((subscription = mqtt.readSubscription (5000 )))
44
108
{
109
+
45
110
if (subscription == &LED)
46
111
{
112
+ // Print the new value to the serial monitor
47
113
Serial.println ((char *) LED.lastread );
114
+
48
115
if (!strcmp ((char *) LED.lastread , " OFF" ))
49
116
{
50
117
digitalWrite (ledPin, HIGH);
@@ -54,9 +121,12 @@ while ((subscription = mqtt.readSubscription(5000)))
54
121
digitalWrite (ledPin, LOW);
55
122
}
56
123
}
57
- if (subscription == &Pump)
124
+
125
+ if (subscription == &Pump)
58
126
{
127
+ // Print the new value to the serial monitor
59
128
Serial.println ((char *) Pump.lastread );
129
+
60
130
if (!strcmp ((char *) Pump.lastread , " OFF" ))
61
131
{
62
132
digitalWrite (motorPin, HIGH);
@@ -65,3 +135,31 @@ if (subscription == &Pump)
65
135
{
66
136
digitalWrite (motorPin, LOW);
67
137
}
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