/*
This project is to simulate a soil NPK sensor. A standard NPK sensor uses RS485 MODBUS
to communicate with a controller. To induce simplicity, we have dispatched with the
RS485 MODBUS part. But have kept the same type of command response structure in the
simulation.
To Add this chip to your project:
1. Create a new project.
2. Add a custom chip with the name "npk-sensor", choose c for your coding language.
3. copy entire code from the XXX.chip.json file from this project and paste to your
projects XXX.chip.json file. make sure old contents are deleted.
4. copy entire code from the XXX.chip.c file from this project and paste to your
projects XXX.chip.c file. make sure old contents are deleted.
5. make the appropriate connections, pin 17 of ESP -> custom chip RX and
custom chip TX -> pin 16 of ESP. Make the appropriate power connections also.
How to get values from the custom chip:
3 single byte commands are sent via uart2 to the custom chip to get N , P and K values.
The custom chip sends back a single byte in response to the command byte.
The commands are, 0x01- Get Nitrogen(N) value, 0x03- get Phosphorous(P) value and 0x05- get
Potassium (K) value.
If wrong command byte is given, the chip will stay silent.
Please use the command response structure used below,
1. Number of commands defined.
2. All used commands defined through array.
3. All returned values are stored in array.
4. All response strings which may be integrated with return values are stored in array.
5. Use a simple for loop to iterate through all commands and responses.
*/
#include <HardwareSerial.h>
//HardwareSerial SerialPort(2);
#define ncom 3 // number of commands.
char commar[ncom] = {0x1,0x3,0x5}; // Actual commands
// Response Strings can be stored like this
char respar[ncom][30]={"Nitrogen value is: ","Phosphorous value is: ","Potassium value is: "};
uint8_t rtValue[ncom]; // Store the return values from the custom chip in here. you can use the same
//values to forward to the IOT part.
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); // initialize console
Serial.println("Hello, ESP32!");
Serial2.begin(15200, SERIAL_8N1, 16, 17); //initialize the custom chip communication line.
}
void loop() {
for (uint8_t i = 0; i< ncom;i++)
{
Serial2.print((char)commar[i]); // send the command stored in ncom array through serial2
if (Serial2.available())
{ //if serial2 data is there
rtValue[i]= Serial2.read();// read serial2
Serial2.flush();// flush serial2, very important. otherwise extra bits may interfere with communication
Serial.print(respar[i]); // print the response array to the console.
Serial.println(rtValue[i]); // print the return value with newline at console
}
}
delay(500); // this speeds up the simulation
}