#include #include #include // Include the LCD library // set the LCD number of columns and rows int lcdColumns = 16; int lcdRows = 2; // set LCD address, number of columns and rows LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows); // === Moisture Sensor === #define MOISTURE_PIN A0 // Analog pin for soil moisture sensor // === NeoPixel Setup === #define LED_PIN 12 #define POWER_PIN 11 #define NUMPIXELS 1 Adafruit_NeoPixel pixels(NUMPIXELS, LED_PIN, NEO_GRB + NEO_KHZ800); // === Serial Communication === int id = 2; // Receiver ID String message = ""; void setup() { // Serial Setup Serial.begin(9600); Serial1.begin(115200); // Moisture Sensor Power (optional) pinMode(POWER_PIN, OUTPUT); digitalWrite(POWER_PIN, HIGH); // NeoPixel Init pixels.begin(); pixels.setBrightness(50); // initialize LCD lcd.init(); // turn on LCD backlight lcd.backlight(); // Display a welcome message lcd.clear(); lcd.setCursor(0, 0); lcd.print("Moisture Monitor"); delay(1000); } void loop() { // === Moisture Sensor Reading === int moistureValue = analogRead(MOISTURE_PIN); // === Display on Serial === Serial.print("Moisture: "); Serial.println(moistureValue); // === Display on LCD === // set cursor to first column, first row lcd.setCursor(0, 0); // print message lcd.print("Moisture Value:"); lcd.setCursor(0, 1); lcd.print(moistureValue); // Display moisture value on second row delay(1000); // clears the display to print new message lcd.clear(); // set cursor to first column, second row // === Forward Input from Serial to Serial1 === if (Serial.available() > 0) { String myInput = Serial.readString(); myInput = id + ": " + myInput; Serial1.println(myInput); } // === Receive and Process Message from Serial1 === if (Serial1.available() > 0) { String networkIncoming = Serial1.readString(); int specialCharIndex = networkIncoming.indexOf('@'); if (specialCharIndex != -1) { message = networkIncoming.substring(0, specialCharIndex); String destination = networkIncoming.substring(specialCharIndex + 1); int receiver = destination.toInt(); if (receiver == id) { int r, g, b; int firstComma = message.indexOf(','); int secondComma = message.indexOf(',', firstComma + 1); Serial.println("Message: " + message + ", Receiver: " + String(receiver)); if (firstComma != -1 && secondComma != -1) { r = message.substring(0, firstComma).toInt(); g = message.substring(firstComma + 1, secondComma).toInt(); b = message.substring(secondComma + 1).toInt(); setColor(r, g, b); } } else { Serial.println("Not my message"); } } } delay(1000); // Refresh every 1 second } // === Set NeoPixel Color === void setColor(int r, int g, int b) { pixels.setPixelColor(0, pixels.Color(r, g, b)); pixels.show(); }