-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrx.ino
More file actions
37 lines (30 loc) · 1.55 KB
/
Copy pathrx.ino
File metadata and controls
37 lines (30 loc) · 1.55 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
#include <SoftwareSerial.h>
SoftwareSerial xbee(2, 3); // XBee: RX → D2, TX → D3
void setup() {
Serial.begin(9600); // Serial Monitor
xbee.begin(9600); // XBee Communication
}
void loop() {
if (xbee.available()) {
String receivedData = xbee.readStringUntil('\n'); // Read incoming data until newline
receivedData.trim(); // Remove any leading/trailing spaces or newlines
Serial.print("Received Data: ");
Serial.println(receivedData);
// Split the received data (format: Moisture,Status,Temperature,Humidity)
int firstComma = receivedData.indexOf(',');
int secondComma = receivedData.indexOf(',', firstComma + 1);
int thirdComma = receivedData.indexOf(',', secondComma + 1);
if (firstComma > 0 && secondComma > firstComma && thirdComma > secondComma) {
String moisture = receivedData.substring(0, firstComma);
String status = receivedData.substring(firstComma + 1, secondComma);
String temperature = receivedData.substring(secondComma + 1, thirdComma);
String humidity = receivedData.substring(thirdComma + 1);
Serial.print("Soil Moisture: "); Serial.println(moisture);
Serial.print("Moisture Status: "); Serial.println(status);
Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" °C");
Serial.print("Humidity: "); Serial.print(humidity); Serial.println(" %");
} else {
Serial.println("Error: Incorrect data format received!");
}
}
}