Hi @rickkas7 @ScruffR @peekay123
The last post got locked before I had the chance to reply / report back.
I grabbed the boards @peekay123 recommended for talking to RS485 sensor using Boron.
Code is compiling and I am getting data from the sensor but want to run the schematic and updated code by you guys for a sanity check
#include <Particle.h>
#include <SerialBufferRK.h>
// SYSTEM_MODE(MANUAL);
// SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(AUTOMATIC);
SYSTEM_THREAD(DISABLED);
SerialBuffer<256> rs485Buffer(Serial1); // use serial1 (D9/D10 tx/rx) for rs485 communication
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};
byte values[11];
byte nitrogen() {
delay(100);
for (byte i = 0; i < sizeof(nitro); i++) {
rs485Buffer.write(nitro[i]);
}
delay(100);
if (rs485Buffer.available() >= sizeof(nitro)) {
for (byte i = 0; i < 7; i++) {
values[i] = rs485Buffer.read();
//Serial.print(values[i], HEX);
}
//Serial.println();
}
return values[4];
}
byte phosphorous() {
delay(100);
for (byte i = 0; i < sizeof(phos); i++) {
rs485Buffer.write(phos[i]);
}
delay(100);
if (rs485Buffer.available() >= sizeof(phos)) {
for (byte i = 0; i < 7; i++) {
values[i] = rs485Buffer.read();
//Serial.print(values[i], HEX);
}
//Serial.println();
}
return values[4];
}
byte potassium() {
delay(100);
for (byte i = 0; i < sizeof(pota); i++) {
rs485Buffer.write(pota[i]);
}
delay(100);
if (rs485Buffer.available() >= sizeof(pota)) {
for (byte i = 0; i < 7; i++) {
values[i] = rs485Buffer.read();
//Serial.print(values[i], HEX);
}
//Serial.println();
}
return values[4];
}
void setup() {
Serial.begin(9600);
delay(2500); // allow time to open serial port
Serial1.begin(9600);
rs485Buffer.setup();
delay(1000);
Serial.println();
}
void loop() {
// turn on power to rs485 via D11
pinMode(D11, OUTPUT);
digitalWrite(D11, HIGH);
delay(1500);
// turn on step-up-converter by setting enable to low via D8
pinMode(D8, OUTPUT);
digitalWrite(D8, HIGH);
delay(1500);
for (int counter = 0; counter <= 5; counter++){
byte val1, val2, val3;
val1 = nitrogen();
delay(250);
val2 = phosphorous();
delay(250);
val3 = potassium();
delay(250);
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.println();
// convert values to strings
String nitrogenValue = String(val1);
String phosphorousValue = String(val2);
String potassiumValue = String(val3);
delay(2500);
if (counter == 5) {
Particle.publish("NPK-Nitrogen", nitrogenValue, PRIVATE);
Particle.publish("NPK-Phosphorous", phosphorousValue, PRIVATE);
Particle.publish("NPK-Potassium", potassiumValue, PRIVATE);
Serial.println("[ULTRA-LOW-POWER-SLEEP]");
Serial.println("Going to Sleep.....");
Particle.disconnect();
delay(5000);
SystemSleepConfiguration config;
config.mode(SystemSleepMode::ULTRA_LOW_POWER).duration(1440min);
System.sleep(config);
Serial.println("Awake from Sleep, Restarting Device.....");
System.reset(); // code execution starts back in void setup()
}
}
}
I am using a step-up-converter since the sensor requires 12V.