Loic Dessap
Created February 14, 2023

Save-AgriBot

Rover collects soil and atmospheric data to predict the type of culture to be practising and help farmers practising inherited agriculture

25
Save-AgriBot

Things used in this project

Story

Read more

Schematics

rx_car_csCnZ075l5.fzz

nrf24l01_K5XEoGsclQ.fzpz

Code

tx rover control

Arduino
/*
        DIY Arduino based RC Transmitter
  by Dejan Nedelkovski, www.HowToMechatronics.com
  Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
//#include <nRF24L01.h>
#include <RF24.h>
// Define the digital inputs
#define jB1 1  // Joystick button 1

RF24 radio(7, 8);   // nRF24L01 (CE, CSN)
const byte address[6] = "00001"; // Address
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
  byte j1PotX;
  byte j1PotY;
  byte jPotY; 
  byte pot1;
  byte pot2;
  byte pot3;
  byte pot4;
  byte pot5;
  byte pot6;
  
 // byte j1Button;
};
Data_Package data; //Create a variable with the above structure
void setup() {
  Serial.begin(9600);
  // Define the radio communication
  radio.begin();
  radio.openWritingPipe(address);
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_LOW);
  
  
  // Activate the Arduino internal pull-up resistors
  pinMode(jB1, INPUT_PULLUP);  
  // Set initial default values
  data.j1PotX = 127; // Values from 0 to 255. When Joystick is in resting position, the value is in the middle, or 127. We actually map the pot value from 0 to 1023 to 0 to 255 because that's one BYTE value
  data.j1PotY = 127;
  //data.jPotX = 127;
  //data.jPotY = 127;
}
void loop() {
  // Read all analog inputs and map them to one Byte value
  data.j1PotX = map(analogRead(A3), 114, 885, 255, 0); // Convert the analog read value from 0 to 1023 into a BYTE value from 0 to 255
  data.j1PotY = map(analogRead(A0), 138, 885, 255, 0);
  /*data.j1PotX = map(analogRead(A3), 599, 433, 0, 255); // Convert the analog read value from 0 to 1023 into a BYTE value from 0 to 255
  data.j1PotY = map(analogRead(A2), 598, 421 , 0, 255);*/
 // data.jPotX = map(analogRead(A1), 421, 598, 0, 255);
  data.jPotY = map(analogRead(A2), 0, 750, 0, 255);
  data.pot1 = map(analogRead(A1), 0, 1010, 0, 255);
  data.pot2 = map(analogRead(A4), 146, 885,255, 0);
  data.pot3= map(analogRead(A5), 159, 754, 0 , 255);
  data.pot4= map(analogRead(A6), 0, 730, 255, 0);
  data.pot5 = map(analogRead(A7),0, 1023, 0, 255);
  data.pot6 = map(analogRead(A8), 120, 885, 0, 255);
  // Read all digital inputs  
  //data.j1Button = digitalRead(jB1);
 Serial.println(analogRead(A9));
  // Send the whole data from the structure to the receiver
  radio.write(&data, sizeof(Data_Package));
}

Rx rover control

Arduino
Receiver code to control the robot movement
 /*DIY Arduino based RC Transmitter Project
              == Receiver Code ==
  by Dejan Nedelkovski, www.HowToMechatronics.com
  Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <Braccio.h>

#include <Wire.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
#include <Ultrasonic.h>

Ultrasonic ultrasonic(A0);
RF24 radio(7, 8);   // nRF24L01 (CE, CSN)
const byte address[6] = "00001";
unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;
Servo ST1, ST2;

Servo ser6;
 
Servo ser5;

Servo ser4;

Servo ser3;

Servo ser2;

Servo ser1;

Servo ser0;
int pote0,pote1,pote2,pote3,pote4,pote5,pote6,pote7,pote8;
int w=11;
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
  byte j1PotX;
  byte j1PotY;
  byte jPotY;
  byte pot1;
  byte pot2;
  byte pot3;
  byte pot4;
  byte pot5;
  byte pot6;
  //byte j1Button;
};
Data_Package data; //Create a variable with the above structure
void setup() {
  Serial.begin(9600);
  //Braccio.begin();
pinMode(w,OUTPUT);
 //Braccio.ServoMovement(0,           90,  90, 90, 90, 90,  73); 
  ST1.attach( 6, 1000, 2000);
 ST2.attach(5, 1000, 2000);
 ser0.attach(13);
 ser0.write(90);
 ser1.attach(10);
 ser1.write(90);
 ser2.attach(9);
 ser2.write(90);
 ser3.attach(4);
 ser3.write(90);
 ser4.attach(3);
 ser4.write(90);
 ser5.attach(2);
 ser5.write(90);
 ser6.attach(12);
 ser6.write(90);
 //digitalWrite( w,LOW);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_LOW);
  radio.startListening(); //  Set the module as receiver
  //resetData();
}
void loop() {
  // Check whether there is data to be received
  if (radio.available()) {
    radio.read(&data, sizeof(Data_Package)); // Read the whole data and store it into the 'data' structure
    lastReceiveTime = millis(); // At this moment we have received the data
  }
  // Check whether we keep receving data, or we have a connection between the two modules
  currentTime = millis();
  if ( currentTime - lastReceiveTime > 1000 ) { // If current time is more then 1 second since we have recived the last data, that means we have lost connection
    resetData(); // If connection is lost, reset the data. It prevents unwanted behavior, for example if a drone has a throttle up and we lose connection, it can keep flying unless we reset the values
  }
  // Print the data in the Serial Monitor
  //Serial.println("j1PotX: ");
  //Serial.println(data.j1PotX);
  //Serial.println("; j1PotY: ");

   pote0 = map(data.j1PotX,0,255,0,179), ST1.write(pote0);
 
   pote1 = map(data.j1PotY,0,255,0,179), ST2.write(pote1);

   pote2=data.jPotY;

  pote3= map(data.pot1,0,255,0,179),ser1.write(pote3);

   pote4= map(data.pot2,0,255,0,179),ser2.write(pote4);
   
   pote5= map(data.pot3,0,255,165,15),ser3.write(pote5);

   pote6= map(data.pot4,0,255,179,0),ser4.write(pote6);

   pote7= map(data.pot5,0,255,0,179),ser5.write(pote7);
   pote8= map(data.pot6,0,255,0,179),ser6.write(pote8);
  
  if (pote2 >= 220) {
    digitalWrite( w,HIGH);
  } else if(pote2<=40){
    digitalWrite(w,LOW);
  }
   
   //Braccio.ServoMovement(0,           pote6,  pote5, pote4, pote3, pote2, pote7);
Serial.println(pote8);
     
     sensor();
 /* Serial.print("; button1: ");
  Serial.print(data.button1);*/
}
void resetData() {
  // Reset the values when there is no radio connection - Set initial default values
  data.j1PotX = 127;
  data.j1PotY = 127;
  //Braccio.ServoMovement(50,           90,  90, 90, 90, 90,  73);
  //data.j1Button = 1;
}
void sensor()
{
  long RangeInCentimeters;
  
 ///    Serial.println("The distance to obstacles in front is: ");
 /* RangeInInches = ultrasonic.MeasureInInches();
  Serial.print(RangeInInches);//0~157 inches
  Serial.println(" inch");
  delay(250);*/
  
  RangeInCentimeters = ultrasonic.distanceRead(); // two measurements should keep an interval
  
  if(RangeInCentimeters <=10 ){
    
    digitalWrite(w,LOW);
    
    }else{
    digitalWrite( w,HIGH);  
      }
}

data Tx code

Arduino
data collector code
#include <Seeed_BME280.h>

//#include <BME280I2C.h>
//#include <DHT.h>
#include <Wire.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <SI114X.h>

#define CE_PIN 7
#define CSN_PIN 10
//dht11 DHT;
#define DHTTYPE DHT11
#define PIN_SENSOR_DHT 6

#define         MQ_PIN                       (0)     //define which analog input channel you are going to use
#define         RL_VALUE                     (5)     //define the load resistance on the board, in kilo ohms
#define         RO_CLEAN_AIR_FACTOR          (9.83)  //RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air)/RO,
                                                     //which is derived from the chart in datasheet

/***********************Software Related Macros************************************/
#define         CALIBARAION_SAMPLE_TIMES     (10)    //define how many samples you are going to take in the calibration phase
#define         CALIBRATION_SAMPLE_INTERVAL  (100)   //define the time interal(in milisecond) between each samples in the
                                                     //cablibration phase
#define         READ_SAMPLE_INTERVAL         (10)    //define how many samples you are going to take in normal operation
#define         READ_SAMPLE_TIMES            (1)     //define the time interal(in milisecond) between each samples in 
                                                     //normal operation

/**********************Application Related Macros**********************************/
#define         GAS_LPG                      (0)
#define         GAS_CO                       (1)
#define         GAS_SMOKE                    (2)

/*****************************Globals***********************************************/
float           LPGCurve[3]  =  {2.3,0.21,-0.47};   //two points are taken from the curve. 
                                                    //with these two points, a line is formed which is "approximately equivalent"
                                                    //to the original curve. 
                                                    //data format:{ x, y, slope}; point1: (lg200, 0.21), point2: (lg10000, -0.59) 
float           COCurve[3]  =  {2.3,0.72,-0.34};    //two points are taken from the curve. 
                                                    //with these two points, a line is formed which is "approximately equivalent" 
                                                    //to the original curve.
                                                    //data format:{ x, y, slope}; point1: (lg200, 0.72), point2: (lg10000,  0.15) 
float           SmokeCurve[3] ={2.3,0.53,-0.44};    //two points are taken from the curve. 
                                                    //with these two points, a line is formed which is "approximately equivalent" 
                                                    //to the original curve.
                                                    //data format:{ x, y, slope}; point1: (lg200, 0.53), point2: (lg10000,  -0.22)                                                     
float           Ro           =  10;                 //Ro is initialized to 10 kilo ohms




//DHT SensorDHT(PIN_SENSOR_DHT,DHT11);
//BME280I2C bme;
BME280 bme280; 
int s=A2;
int g=A1;
float solar;
float moisture;
SI114X SI1145 = SI114X();
const uint64_t pipe = 0xF0F0F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);
float data[12]; 
void setup() {
 Serial.begin(9600);
 Ro = MQCalibration(MQ_PIN); 
   Wire.begin();
//  bme.begin();
//SensorDHT.begin();
radio.begin();
radio.setChannel(125);
radio.setPALevel(RF24_PA_LOW);      // set power amplifier (PA) level
  radio.setDataRate(RF24_1MBPS);      // set data rate through the air
  radio.setRetries(0, 15);
   //radio.setAutoAck(true);
   //radio.enableDynamicPayloads();// Ensure autoACK is enabled
 // radio.enableAckPayload();
  radio.setPayloadSize(32);
radio.openWritingPipe(pipe);
//radio.startListening();
  // put your setup code here, to run once:
  while (!SI1145.Begin()) {
        delay(1000);
    }
    if(!bme280.init()){
  Serial.print("dive error");
  }
}

void loop() {
   
  float t= bme280.getTemperature();
  float p= bme280.getPressure();
  float h= bme280.getHumidity();
  float a= bme280.calcAltitude(p);
//float t = SensorDHT.readTemperature();
//float h = SensorDHT.readHumidity();
float uv  =((float)SI1145.ReadUV()/100);
float ir =SI1145.ReadIR();
float vl =SI1145.ReadVisible();
float lpg= MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_LPG);
float co= MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_CO);
float sk= MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_SMOKE);
/*
float h = DHT.humidity;
float t = DHT.temperature;  
float h = dht.readHumidity();
float t = dht.readTemperature();*/  // put your main code here, to run repeatedly:
moisture=analogRead(g);
solar=analogRead(s);
solar=(solar)/1023;
solar=solar*5;
moisture=moisture/7;
data[0]=moisture;
data[1]=h;
data[2]=t;
data[3]=p/100;
data[4]=sk;
data[5]=(uv+0.5) ;
data[6]=lpg;
data[7]=co;
data[8]=solar;
data[9]=a;
data[10]=ir;
data[11]=vl; 
//Serial.println(sk);
Serial.println(data[0]);
Serial.println(data[1]);
Serial.println(data[2]);
Serial.println(data[3]);
Serial.println(data[4]);
Serial.println(data[5]);
Serial.println(data[6]);
Serial.println(data[7]);
Serial.println(data[8]);
Serial.println(data[9]);
Serial.println(data[10]);
Serial.println(data[11]);

radio.write(data,sizeof(data));
//delay(500);
}



/****************** MQResistanceCalculation ****************************************
Input:   raw_adc - raw value read from adc, which represents the voltage
Output:  the calculated sensor resistance
Remarks: The sensor and the load resistor forms a voltage divider. Given the voltage
         across the load resistor and its resistance, the resistance of the sensor
         could be derived.
************************************************************************************/ 
float MQResistanceCalculation(int raw_adc)
{
  return ( ((float)RL_VALUE*(1023-raw_adc)/raw_adc));
}

/***************************** MQCalibration ****************************************
Input:   mq_pin - analog channel
Output:  Ro of the sensor
Remarks: This function assumes that the sensor is in clean air. It use  
         MQResistanceCalculation to calculates the sensor resistance in clean air 
         and then divides it with RO_CLEAN_AIR_FACTOR. RO_CLEAN_AIR_FACTOR is about 
         10, which differs slightly between different sensors.
************************************************************************************/ 
float MQCalibration(int mq_pin)
{
  int i;
  float val=0;

  for (i=0;i<CALIBARAION_SAMPLE_TIMES;i++) {            //take multiple samples
    val += MQResistanceCalculation(analogRead(mq_pin));
    delay(CALIBRATION_SAMPLE_INTERVAL);
  }
  val = val/CALIBARAION_SAMPLE_TIMES;                   //calculate the average value

  val = val/RO_CLEAN_AIR_FACTOR;                        //divided by RO_CLEAN_AIR_FACTOR yields the Ro 
                                                        //according to the chart in the datasheet 

  return val; 
}
/*****************************  MQRead *********************************************
Input:   mq_pin - analog channel
Output:  Rs of the sensor
Remarks: This function use MQResistanceCalculation to caculate the sensor resistenc (Rs).
         The Rs changes as the sensor is in the different consentration of the target
         gas. The sample times and the time interval between samples could be configured
         by changing the definition of the macros.
************************************************************************************/ 
float MQRead(int mq_pin)
{
  int i;
  float rs=0;

  for (i=0;i<READ_SAMPLE_TIMES;i++) {
    rs += MQResistanceCalculation(analogRead(mq_pin));
    delay(READ_SAMPLE_INTERVAL);
  }

  rs = rs/READ_SAMPLE_TIMES;

  return rs;  
}

/*****************************  MQGetGasPercentage **********************************
Input:   rs_ro_ratio - Rs divided by Ro
         gas_id      - target gas type
Output:  ppm of the target gas
Remarks: This function passes different curves to the MQGetPercentage function which 
         calculates the ppm (parts per million) of the target gas.
************************************************************************************/ 
int MQGetGasPercentage(float rs_ro_ratio, int gas_id)
{
  if ( gas_id == GAS_LPG ) {
     return MQGetPercentage(rs_ro_ratio,LPGCurve);
  } else if ( gas_id == GAS_CO ) {
     return MQGetPercentage(rs_ro_ratio,COCurve);
  } else if ( gas_id == GAS_SMOKE ) {
     return MQGetPercentage(rs_ro_ratio,SmokeCurve);
  }    

  return 0;
}

/*****************************  MQGetPercentage **********************************
Input:   rs_ro_ratio - Rs divided by Ro
         pcurve      - pointer to the curve of the target gas
Output:  ppm of the target gas
Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm) 
         of the line could be derived if y(rs_ro_ratio) is provided. As it is a 
         logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic 
         value.
************************************************************************************/ 
int  MQGetPercentage(float rs_ro_ratio, float *pcurve)
{
  return (pow(10,( ((log(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0])));
}

data Rx code

Arduino
#include <SeeedOLED.h>
#include <SPI.h>
//#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 7
#define CSN_PIN 10
RF24 radio(CE_PIN, CSN_PIN);
const byte address[6] = "00001";
unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;

// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
  byte m;
  byte h;
  byte t;
  float p;
  byte sk;
  byte uv;
  byte lpg;
  byte co;
  byte solar;
  byte a;
  byte ir;
  byte vl;
  byte NH3;
  byte NO2;
  byte CH4;
  byte H2;
  byte CO2;
};
Data_Package data;

void setup() {
   Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_LOW);
  radio.startListening(); //  Set the module as receiver
  resetData();
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

  if (radio.available()) {
    radio.read(&data, sizeof(Data_Package)); // Read the whole data and store it into the 'data' structure
    lastReceiveTime = millis(); // At this moment we have received the data
  }
  // Check whether we keep receving data, or we have a connection between the two modules
  currentTime = millis();
 // if ( currentTime - lastReceiveTime > 1000 ) { // If current time is more then 1 second since we have recived the last data, that means we have lost connection
   // resetData(); // If connection is lost, reset the data. It prevents unwanted behavior, for example if a drone has a throttle up and we lose connection, it can keep flying unless we reset the values
  //}
  // Print the data in the Serial Monitor
  
  Serial.println(data.m);
  Serial.println(data.h);
  Serial.println(data.t);
  //data.p=data.p*5.1;
  Serial.println(data.p);
  Serial.println(data.sk);
  Serial.println(data.uv);
  Serial.println(data.lpg);
  Serial.println(data.co);
  Serial.println(data.solar);
  Serial.println(data.a);
  Serial.println(data.ir);
  Serial.println(data.vl);
  Serial.println(data.NH3);
  Serial.println(data.NO2);
  Serial.println(data.CH4);
  Serial.println(data.H2);
  Serial.println(data.CO2);
 /* Serial.print("; button1: ");
  Serial.print(data.button1);*/
}
void resetData() {
  // Reset the values when there is no radio connection - Set initial default values
  data.m =0;
  data.t =0;
  data.h=0;
  data.p= 0;
  data.uv=0;
  data.lpg=0;
  data.sk=0;
  data.co=0;
  data.solar=0;
  data.a=0;
  data.ir=0;
  data.vl=0;
  data.NH3=0;
  data.NO2=0;
  data.CH4=0;
  data.H2=0;
  data.CO2=0;

  /*String moisture = (String)data.m;
  String humidity = (String)data.t;
   String temperature = (String)data.h;
    String pressure = (String)data.p;
     String uv = (String)data.uv;
      String lpg= (String)data.lpg;
       String co = (String)data.co;
       String sk =(String)data.sk;
   int NData = 8;
  String str_Payload;*/
}

Credits

Loic Dessap
1 project • 1 follower

Comments