-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenpk v2
108 lines (93 loc) · 2.48 KB
/
enpk v2
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
#include <SoftwareSerial.h>
#include <Wire.h>
// RE and DE Pins set the RS485 module
// to Receiver or Transmitter mode
#define RE 8
#define DE 7
// Modbus RTU requests for reading NPK values
const byte nitro[] = {0x01,0x03, 0x00, 0x1e, 0x00, 0x02, 0xe4, 0x0c};
const byte phos[] = {0x01,0x03, 0x00, 0x1f, 0x00, 0x02, 0xb5, 0xcc};
const byte pota[] = {0x01,0x03, 0x00, 0x20, 0x00, 0x02, 0x85, 0xc0};
// A variable used to store NPK values
unsigned int values[3];
// Sets up a new SoftwareSerial object
// Digital pins 10 and 11 should be used with a Mega or Mega 2560
SoftwareSerial mod(2, 3);
//SoftwareSerial mod(10, 11);
void setup() {
// Set the baud rate for the Serial port
Serial.begin(9600);
// Set the baud rate for the SerialSoftware object
mod.begin(9600);
// Define pin modes for RE and DE
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
delay(500);
}
void loop() {
// Read values
values[0] = nitrogen();
delay(250);
values[1] = phosphorous();
delay(250);
values[2] = potassium();
delay(250);
// Print values to the serial monitor
Serial.print("Nitrogen: ");
Serial.print(values[0]);
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
Serial.print(values[1]);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(values[2]);
Serial.println(" mg/kg");
delay(2000);
}
unsigned int nitrogen(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(mod.write(nitro,sizeof(nitro))==8){
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
byte highByte = mod.read();
byte lowByte = mod.read();
if (result == node.ku8MBSuccess)
{
Serial.print(" Nitrogen: ");
Serial.print(word(node.getResponseBuffer(0x0), node.getResponseBuffer(0x1)));
Serial.println(" mg/kg");
}
}
}
unsigned int phosphorous(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(mod.write(phos,sizeof(phos))==8){
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
byte highByte = mod.read();
byte lowByte = mod.read();
unsigned int value = word(highByte,lowByte);
Serial.print(value);
Serial.println();
return value;
}
}
unsigned int potassium(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(mod.write(pota,sizeof(pota))==8){
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
byte highByte = mod.read();
byte lowByte = mod.read();
unsigned int value = word(highByte,lowByte);
Serial.print(value);
Serial.println();
return value;
}
}