-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgritech code.ino
222 lines (181 loc) · 5.07 KB
/
Agritech code.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <SoftwareSerial.h>
SoftwareSerial gprsSerial(4,5);
#include <String.h>
float moisture_percentage,h,t, sa;
#include <AltSoftSerial.h>
#include <DHT.h>
#define DHTPIN A0
DHT dht(DHTPIN, DHT11);
//RO to pin 8 & DI to pin 9 when using AltSoftSerial
#define RE 6
#define DE 7
const byte nitro[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
const byte phos[] = {0x01, 0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
const byte pota[] = {0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};
byte values[11];
AltSoftSerial mod;
byte val1;
byte val2;
byte val3;
void setup() {
Serial.begin(9600);
mod.begin(9600);
dht.begin();
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
// put RS-485 into receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
gprsSerial.begin(9600);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
delay(250);
Serial.println(" %");
Serial.print("Temperature = ");
Serial.print(t);
Serial.println(" °C");
Serial.print("Humidity = ");
Serial.print(h);
Serial.println(" %");
float sa= analogRead(A1);
moisture_percentage = ( 100 - ( (sa/1023.00) * 100 ) );
Serial.print("Moisture Percentage = ");
Serial.print(moisture_percentage);
Serial.print("%\n\n");
delay(250);
Serial.print("Nitrogen: ");
val1 = nitrogen();
Serial.print(" = ");
Serial.print(val1);
Serial.println(" mg/kg");
delay(250);
Serial.print("Phosphorous: ");
val2 = phosphorous();
Serial.print(" = ");
Serial.print(val2);
Serial.println(" mg/kg");
delay(250);
Serial.print("Potassium: ");
val3 = potassium();
Serial.print(" = ");
Serial.print(val3);
Serial.println(" mg/kg");
Serial.println();
delay(250);
if (gprsSerial.available())
Serial.write(gprsSerial.read());
gprsSerial.println("AT");
delay(10);
gprsSerial.println("AT+CPIN?");
delay(10);
gprsSerial.println("AT+CREG?");
delay(10);
gprsSerial.println("AT+CGATT?");
delay(100);
gprsSerial.println("AT+CIPSHUT");
delay(1000);
gprsSerial.println("AT+CIPSTATUS");
delay(20);
gprsSerial.println("AT+CIPMUX=0");
delay(2000);
ShowSerialData();
gprsSerial.println("AT+CSTT=\"airtelgprs.com\"");//start task and setting the APN,
delay(100);
ShowSerialData();
gprsSerial.println("AT+CIICR");//bring up wireless connection
delay(300);
ShowSerialData();
gprsSerial.println("AT+CIFSR");//get local IP adress
delay(200);
ShowSerialData();
gprsSerial.println("AT+CIPSPRT=0");
delay(300);
ShowSerialData();
gprsSerial.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");//start up the connection
delay(6000);
ShowSerialData();
gprsSerial.println("AT+CIPSEND");//begin send data to remote server
delay(4000);
ShowSerialData();
String str= "GET https://api.thingspeak.com/update?api_key=NHDI7GPK8UFHHUK9&field1=" +String(t) +"&field2=" + String(h)+ "&field3="+ String(moisture_percentage)+ "&field4=" +String(val1) +"&field5=" +String(val2)+"&field6=" +String(val3);
delay(4000);
gprsSerial.println(str);
//begin send data to remote server
delay(4000);
ShowSerialData();
gprsSerial.println((char)26);//sending
delay(5000);//waitting for reply, important! the time is base on the condition of internet
gprsSerial.println();
ShowSerialData();
gprsSerial.println("AT+CIPSHUT");//close the connection
delay(100);
ShowSerialData();
}
void ShowSerialData()
{
while(gprsSerial.available()!=0)
Serial.write(gprsSerial.read());
delay(5000);
}
byte nitrogen() {
// clear the receive buffer
mod.flushInput();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(nitro); i++ ) mod.write( nitro[i] );
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
Serial.print(values[i], HEX);
Serial.print(' ');
}
return values[4];
}
byte phosphorous() {
mod.flushInput();
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
for (uint8_t i = 0; i < sizeof(phos); i++ ) mod.write( phos[i] );
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
Serial.print(values[i], HEX);
Serial.print(' ');
}
return values[4];
}
byte potassium() {
mod.flushInput();
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
for (uint8_t i = 0; i < sizeof(pota); i++ ) mod.write( pota[i] );
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
Serial.print(values[i], HEX);
Serial.print(' ');
}
return values[4];
}