-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAgriCloud1.ino
172 lines (142 loc) · 5.45 KB
/
AgriCloud1.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//AGRICLOUD PROGRAM 1
#include <DHT.h> // Including library for dht
#include <ESP8266WiFi.h>
String apiKey = "UX3Z4FYVUJLI8PLG"; // Enter your Write API key from ThingSpeak
const char *ssid = "home"; // replace with your wifi ssid and wpa2 key
const char *pass = "12345678";
const char* server = "api.thingspeak.com";
int WET= 12; // Wet Indicator at Digital pin D0
int sensor = 13; // IR SENSOR OUT TO PIN D7
int DRY= 2; // Dry Indicator at Digital pin D4
int sense_Pin = 0; // sensor input at Analog pin A0 connect soil moisture d0 to a0
int Status = 16; // IR LED OUT STATUS TO D6
int value = 0; // just a variable
//const int buzzer = 4; IR OUTPUT BUZZER TO PIN D2 (test alter)
#define DHTPIN 5 //only for dht pin 0
DHT dht(DHTPIN, DHT11);
WiFiClient client;
void setup()
{
Serial.begin(9600);
// pinMode(buzzer, OUTPUT);
pinMode(WET, OUTPUT);
pinMode(DRY, OUTPUT);
pinMode(sensor, INPUT); // declare sensor as input
pinMode(Status, OUTPUT); // declare LED as output
pinMode(LED_BUILTIN,OUTPUT);
delay(2000);
delay(10);
dht.begin();
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid,pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop()
{
ir();
dht_THINGSPEAK();
fc_26();
delay(2000);
}
void dht_THINGSPEAK()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius, Humidity: ");
Serial.print(h);
Serial.println("%. Send to Thingspeak.");
}
// client.stop();
Serial.println("Waiting...");
// thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
// delay(10000);
}
void fc_26()
{
Serial.print("MOISTURE LEVEL : ");
value= analogRead(sense_Pin);
value= value/10;
Serial.println(value);
if(value<50)
{
digitalWrite(WET, HIGH);
}
else
{
digitalWrite(DRY,HIGH);
}
// delay(1000);
digitalWrite(WET,LOW);
digitalWrite(DRY, LOW);
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field3=";
postStr += String(value);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
// Serial.print("Temperature: ");
// Serial.print(t);
// Serial.print(" degrees Celcius, Humidity: ");
// Serial.print(h);
// Serial.println("%. Send to Thingspeak.");
}
// client.stop();
//delay(1000);
}
void ir()
{
long state = digitalRead(sensor);
if (state==0)
{
Serial.println("1");
digitalWrite(LED_BUILTIN,HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(Status,HIGH);
// delay(1000);
}
else
{
Serial.println("0");
digitalWrite(LED_BUILTIN,LOW);
digitalWrite(Status,LOW);
}
}