Hello Arduino Community,
Perhaps with a little help of y'all I can get pointed into the right direction.
I'm trying to build an agricultural IoT sensor, but I can't seem to connect the sensor to the Arduino and read the values correctly.
IDE/libs:
-
Visual Studio Code with PlatformIO
-
Arduino.h
-
ArduinoRS485.h
Components:
Circuit Diagram:
(* in this diagram the GSM board is backwards)
Code:
(* to simplify the code, I'm only showing what I've written for 1 of the 7 measurements: nitrogen)
-
Sensor nitrogen device address:
-
RS485 switches:
-
ON
-
ON
-
OFF
-
-
Source code:
#include <Arduino.h>
#include <ArduinoRS485.h>
#define RE A5
#define DE A6
const byte nitro[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xB5, 0xCC};
byte values[11];
byte nitrogen()
{
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
if (RS485.write(nitro, sizeof(nitro)) == 8)
{
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++)
{
//Serial.print(mod.read(),HEX);
values[i] = RS485.read();
Serial.print(values[i], HEX);
}
Serial.println();
}
return values[4];
}
void setup()
{
Serial.begin(9600);
RS485.begin(9600);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
}
void loop()
{
RS485.beginTransmission();
byte val1;
val1 = nitrogen();
RS485.endTransmission();
delay(250);
Serial.print("Nitrogen: ");
Serial.print(val1);
Serial.println(" mg/kg");
delay(2000);
}
Result:
I keep getting a value of 255 mg/kg and this result shows even if I have the sensor "unplugged" which tells me that it is code based instead of actually showing the sensor value.
Any help would be greatly appreciated.
Thanks!
CG
** Sensor datasheet:
RS485-Soil Multi-parameter Sensor.pdf (352.1 KB)