-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathardui-code
More file actions
141 lines (116 loc) · 3.67 KB
/
ardui-code
File metadata and controls
141 lines (116 loc) · 3.67 KB
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
//-----------------------------------------------------------------Arduino code---------------------------------------------------------------------------------------------------
#include <ModbusMaster.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int RO_PIN = 10;
const int DI_PIN = 11;
const int RE_PIN = 8;
const int DE_PIN = 7;
SoftwareSerial swSerial(RO_PIN, DI_PIN); // Receive (data in) pin, Transmit (data out) pin
ModbusMaster node;
// OLED display configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1 // Reset pin not used with this display
#define OLED_ADDRESS 0x3C // Replace with your OLED display's I2C address
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Put the MAX485 into transmit mode
void preTransmission() {
digitalWrite(RE_PIN, 1);
digitalWrite(DE_PIN, 1);
}
// Put the MAX485 into receive mode
void postTransmission() {
digitalWrite(RE_PIN, 0);
digitalWrite(DE_PIN, 0);
}
void setup() {
Serial.begin(9600);
// configure the MAX485 RE & DE control signals and enable receive mode
pinMode(RE_PIN, OUTPUT);
pinMode(DE_PIN, OUTPUT);
digitalWrite(DE_PIN, 0);
digitalWrite(RE_PIN, 0);
// Modbus communication runs at 9600 baud
swSerial.begin(9600);
// Modbus slave ID of NPK sensor is 1
node.begin(1, swSerial);
// Callbacks to allow us to set the RS485 Tx/Rx direction
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
delay(1000);
}
void loop() {
uint8_t result;
// remove any characters from the receive buffer
// ask for 3x 16-bit words starting at register address 0x0000
result = node.readHoldingRegisters(0x0000, 3);
if (result == node.ku8MBSuccess) {
// Clear display
display.clearDisplay();
// Print sensor readings on the OLED display
display.setCursor(0, 0);
display.print("N: ");
display.print(node.getResponseBuffer(0) * 10);
display.println(" mg/kg");
display.setCursor(0, 10);
display.print("P: ");
display.print(node.getResponseBuffer(1) * 10);
display.println(" mg/kg");
display.setCursor(0, 20);
display.print("K: ");
display.print(node.getResponseBuffer(2) * 10);
display.println(" mg/kg");
// Display the updated content
display.display();
} else {
printModbusError(result);
}
delay(10000);
}
// print out the error received from the Modbus library
void printModbusError( uint8_t errNum )
{
switch ( errNum ) {
case node.ku8MBSuccess:
Serial.println(F("Success"));
break;
case node.ku8MBIllegalFunction:
Serial.println(F("Illegal Function Exception"));
break;
case node.ku8MBIllegalDataAddress:
Serial.println(F("Illegal Data Address Exception"));
break;
case node.ku8MBIllegalDataValue:
Serial.println(F("Illegal Data Value Exception"));
break;
case node.ku8MBSlaveDeviceFailure:
Serial.println(F("Slave Device Failure"));
break;
case node.ku8MBInvalidSlaveID:
Serial.println(F("Invalid Slave ID"));
break;
case node.ku8MBInvalidFunction:
Serial.println(F("Invalid Function"));
break;
case node.ku8MBResponseTimedOut:
Serial.println(F("Response Timed Out"));
break;
case node.ku8MBInvalidCRC:
Serial.println(F("Invalid CRC"));
break;
default:
Serial.println(F("Unknown Error"));
break;
}
}