Another problem that I encounter is when I'm using the display codes for the OLED. I get nothing from the Serial Monitor and the OLED is not lighting up.
Here is my updated code:
#include <AltSoftSerial.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// 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};
const byte ph[] = { 0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b }; // New
const byte mois[] = { 0x01, 0x03, 0x00, 0x12, 0x00, 0x01, 0x24, 0x0F };
const byte temp[] = { 0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0xD5, 0xCA };
byte values[11];
AltSoftSerial mod;
float soil_ph = 0.0, soil_moisture = 0.0, soil_temp = 0.0;
byte val1 = 0, val2 = 0, val3 = 0, val4 = 0, val5 = 0, val6 = 0;
void setup() {
Serial.begin(9600);
mod.begin(4800);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
// put RS-485 into receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
//Initializing and Configuring OLED display
// display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
// delay(500);
// display.clearDisplay();
// display.setCursor(25, 15);
// display.setTextSize(1);
// display.setTextColor(WHITE);
// display.println("Soil NPK Sensor");
// display.setCursor(25, 35);
// display.setTextSize(1);
// display.print("Initializing");
// display.display();
// delay(3000);
}
void loop() {
byte val1, val2, val3, val4, val5, val6;
val1 = nitrogen();
delay(300);
val2 = phosphorous();
delay(300);
val3 = potassium();
delay(300);
val4 = phydrogen() / 10; // New
soil_ph = val4;
delay(300);
val5 = moisture();
soil_moisture = val5 / 10.0;
delay(300);
val6 = temperature();
soil_temp = temperature() / 10.0; // New
delay(300);
Serial.print("Nitrogen: ");
Serial.print(val1);
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
Serial.print(val2);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(val3);
Serial.println(" mg/kg");
Serial.print("ph: "); // New
Serial.print(soil_ph);
Serial.println(" ph");
Serial.print("Moisture: ");
Serial.print(soil_moisture);
Serial.println(" %"); //
Serial.print("Temperature: ");
Serial.print(soil_temp);
Serial.println(" C");
Serial.println(); // New
delay(2000);
// display.clearDisplay();
//
// display.setTextSize(1);
// display.setCursor(0, 5);
// display.print("N: ");
// display.print(val1);
// display.setTextSize(1);
// display.print(" mg/kg");
//
// display.setTextSize(1);
// display.setCursor(0, 15);
// display.print("P: ");
// display.print(val2);
// display.setTextSize(1);
// display.print(" mg/kg");
//
// display.setTextSize(1);
// display.setCursor(0, 25);
// display.print("K: ");
// display.print(val3);
// display.setTextSize(1);
// display.print(" mg/kg");
//
// display.setTextSize(1); // New
// display.setCursor(0, 35);
// display.print("pH: ");
// display.print(soil_ph);
// display.setTextSize(1);
// display.print(" pH");
//
// display.setTextSize(1);
// display.setCursor(0, 45);
// display.print("Moisture: ");
// display.print(soil_moisture);
// display.setTextSize(1);
// display.print(" %");
//
// display.setTextSize(1);
// display.setCursor(0, 55);
// display.print("Temperature: ");
// display.print(soil_temp);
// display.setTextSize(1);
// display.print(" C"); // New
//
// display.display();
}
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];
}
byte phydrogen() {
// clear the receive buffer
mod.flush();
// 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(ph); i++) mod.write(ph[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(1000);
// 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 moisture() {
// clear the receive buffer
mod.flush();
// 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(mois); i++) mod.write(mois[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(1000);
// 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 temperature() {
// clear the receive buffer
mod.flush();
// 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(temp); i++) mod.write(temp[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(1000);
// 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[3] << 8 | values[4];
}