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