#include <SoftwareSerial.h>
#define RE 7
#define DE 6
const uint32_t TIMEOUT = 500UL;
const byte moist[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A};
const byte temp[] = {0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0xD5, 0xCA};
const byte EC[] = {0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x25, 0xCA};
const byte PH[] = {0x01, 0x03, 0x00, 0x03, 0x00, 0x01, 0x74, 0x0A};
byte values[11];
SoftwareSerial mod(2, 3); // Rx pin, Tx pin
void setup() {
// Serial.begin(9600);
Serial.begin(4800);
mod.begin(4800);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
delay(500);
}
void loop() {
uint16_t val1, val2, val3, val4;
Serial.println("Moisture: ");
val1 = moisture();
float Val1 = val1 * 0.1;
delay(1000);
//Serial.print(val1);
Serial.print(Val1);
Serial.println(" %");
Serial.println("-----");
//delay(1000);
Serial.println("Temperature: ");
val2 = temperature();
float Val2 = val2 * 0.1;
delay(1000);
//Serial.print(val2);
Serial.print(Val2);
Serial.println(" *C");
Serial.println("-----");
//delay(1000);
Serial.println("Conductivity: ");
val3 = conductivity();
delay(1000);
Serial.print(val3);
Serial.println(" us/cm");
Serial.println("-----");
Serial.println("Ph: ");
val4 = ph();
float Val4 = val4 * 0.1;
delay(1000);
//Serial.print(val4);
Serial.print(Val4);
Serial.println(" ph");
Serial.println("-----");
delay(5000);
}
int16_t moisture() {
uint32_t startTime = 0;
uint8_t byteCount = 0;
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
mod.write(moist, sizeof(moist));
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
startTime = millis();
while ( millis() - startTime <= TIMEOUT ) {
if (mod.available() && byteCount < sizeof(values) ) {
values[byteCount++] = mod.read();
printHexByte(values[byteCount - 1]);
}
}
Serial.println();
return (int16_t)(values[4] << 8 | values[5]);
}
int16_t temperature() {
uint32_t startTime = 0;
uint8_t byteCount = 0;
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
mod.write(temp, sizeof(temp));
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
startTime = millis();
while ( millis() - startTime <= TIMEOUT ) {
if (mod.available() && byteCount < sizeof(values) ) {
values[byteCount++] = mod.read();
printHexByte(values[byteCount - 1]);
}
}
Serial.println();
return (int16_t)(values[4] << 8 | values[5]);
}
int16_t conductivity() {
uint32_t startTime = 0;
uint8_t byteCount = 0;
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
mod.write(EC, sizeof(EC));
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
startTime = millis();
while ( millis() - startTime <= TIMEOUT ) {
if (mod.available() && byteCount < sizeof(values) ) {
values[byteCount++] = mod.read();
printHexByte(values[byteCount - 1]);
}
}
Serial.println();
return (int16_t)(values[4] << 8 | values[5]);
}
int16_t ph() {
uint32_t startTime = 0;
uint8_t byteCount = 0;
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
mod.write(PH, sizeof(PH));
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
startTime = millis();
while ( millis() - startTime <= TIMEOUT ) {
if (mod.available() && byteCount < sizeof(values) ) {
values[byteCount++] = mod.read();
printHexByte(values[byteCount - 1]);
}
}
Serial.println();
return (int16_t)(values[4] << 8 | values[5]);
}
void printHexByte(byte b)
{
Serial.print((b >> 4) & 0xF, HEX);
Serial.print(b & 0xF, HEX);
Serial.print(' ');type or paste code here
Welcome to the forum
As your topic does not relate directly to the installation or operation of the IDE it has been moved to the Programming category of the forum
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination
https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum
Please post your sketch, using code tags when you do
Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
okay this is noted, im just new to this forum and kind of set up
Looks like you have a "}" before the end of the code in loop() . Should be obvious when you get the format looking proper.
what do you mean by that sir?
For every "{" in your program you must have a closing "}". You have an opening "{" for the loop() function. Follow down the code a short ways and you will see the closing "}" with a whole bunch of code following it. That code will all be ignored by the compiler. Did you look at the messages that were printed after the compile?
after i run it on the compiler, it verifies and upload just fine but there's no output readings on the serial monitor even if i already put the sensor itself on soil
What is that stuff you showed on the serial monitor? Looks like reading values.
it only stays in zero
Did you ever read the value from the sensor?
no values at all as it only prompts zero, even if i already put the sensor in the soil
Then you need to spend some time debugging the code where you actually get those values.
i only need accurate decimal values, can someone help :<<
Why do you consider the help you have been getting as "not-help?" If you consider this help as "not help" then why should anyone help?
im so sorry for my words, i am just pressured rn and overwhelmed since i only have few days left to fix this for our proposal defense.
So you put your pressure for your poor planning on people helping you? Do we get the grade and the course credit?
What if you just ask questions, read the helpful answers, and use them toward helping you with your code... and leave time and poor planning of your group out of it?
What do you mean by this? The image in post #14 shows no errors.
how can i get rid of hex values and only print decimal values that are accurate