t#include <AltSoftSerial.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
#define BMP280_ADDRESS 0x76
Adafruit_BMP280 bmp;
int rs=2;
int en=3;
int D04=4;
int D05=5;
int D06=6;
int D07=7;
LiquidCrystal lcd(rs,en,D04,D05,D06,D07);
// RO to pin 8 & DI to pin 9 when using AltSoftSerial
#define RE 11
#define DE 10
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 soil_ph[] = {0x01, 0x03, 0x00, 0x03, 0x00, 0x01, 0x74, 0x0a};
const byte soil_moist[] = {0x01, 0x03, 0x00, 0x12, 0x00, 0x01, 0x24, 0x0f};
const byte temp_humid[] = {0x01, 0x03, 0x00, 0x12, 0x00, 0x02, 0x64, 0x0e};
byte values[11];
AltSoftSerial mod;
void setup() {
Serial.begin(4800);
mod.begin(4800);
lcd.begin(16,2);
lcd.print("Initializing...");
delay(1000);
lcd.clear();
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
delay( 1000 );
while ( !Serial ) delay(100); // wait for native usb
Serial.println(F("BMP280 test"));
unsigned status;
status = bmp.begin(BMP280_ADDRESS);
if (!status) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1) delay(10);
}
/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}
void loop() {
lcd.clear();
byte val1, val2, val3, val4, val5;
Serial.print(" Nitrogen: ");
val1 = nitrogen();
Serial.print(" = ");
Serial.print(val1);
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
val2 = phosphorous();
Serial.print(" = ");
Serial.print(val2);
Serial.println(" mg/kg");
Serial.print(" Potassium: ");
val3 = potassium();
Serial.print(" = ");
Serial.print(val3);
Serial.println(" mg/kg");
Serial.print(" PH: ");
val4 = ph();
Serial.print(" = ");
Serial.print(val4);
Serial.println();
Serial.print(" Moisture: ");
val5 = moisture();
Serial.print(" = ");
Serial.print(val5);
Serial.println(" %");
Serial.print(F("Temperature = "));
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print(F("Pressure = "));
Serial.print(bmp.readPressure());
Serial.println(" Pa");
Serial.print(F("Approx altitude = "));
Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
Serial.println(" m");
Serial.println();
lcd.setCursor(0,0);
lcd.print("N:");
lcd.print(val1);
lcd.setCursor(6,0);
lcd.print("P:");
lcd.print(val2);
lcd.setCursor(11,0);
lcd.print("K:");
lcd.print(val3);
lcd.setCursor(0,1);
lcd.print("pH:");
lcd.print(val4);
lcd.setCursor(4,1);
lcd.print(" M%:");
lcd.print(val5);
lcd.setCursor(10,1);
lcd.print(" *C:");
lcd.print(bmp.readTemperature());
delay(2000);
}
byte moisture() {
// clear the receive buffer
mod.flushInput();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
// write out the message
for (uint8_t i = 0; i < sizeof(soil_moist); i++ ) mod.write( soil_moist[i] );
// wait for the transmission to complete
mod.flush();
// switch RS-485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// crude delay to allow response bytes to be received!
delay(100);
// 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 ph() {
// clear the receive buffer
mod.flushInput();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
// write out the message
for (uint8_t i = 0; i < sizeof(soil_ph); i++ ) mod.write( soil_ph[i] );
// wait for the transmission to complete
mod.flush();
// switch RS-485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// crude delay to allow response bytes to be received!
delay(100);
// 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 nitrogen() {
// clear the receive buffer
mod.flushInput();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
// 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();
// switch RS-485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// crude delay to allow response bytes to be received!
delay(100);
// 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(10);
for (uint8_t i = 0; i < sizeof(phos); i++ ) mod.write( phos[i] );
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
delay(100);
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(10);
for (uint8_t i = 0; i < sizeof(pota); i++ ) mod.write( pota[i] );
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
delay(100);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
Serial.print(values[i], HEX);
Serial.print(' ');
}
return values[4];
}ype or paste code here
so i have been trying to read values from my sensor and i have tried using the modbus request i have seen from this forum as well as from the manufacturer's manual but seems i am not getting solution
@markd833 have seen you being quite helpful on this one can you assist me?
these are the results obtained using this code ,
if i used the modbus request from the manual;
const byte phos[] = {0x01, 0x03, 0x00, 0x05, 0x00, 0x01, 0x94, 0x0b};
const byte pota[] = {0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b};
i get constant readings of 88 and 104 respectively