0
\$\begingroup\$

I am using a NodeMCU ESP8266 WiFi board for collecting N - P - K sensor data. I am using RS485 as interface.

This is the code:


    #include <SoftwareSerial.h>
    
    #define RE D6
    #define DE D7
    
    #define RO D0
    #define DI D1
    
    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];
    SoftwareSerial mod(RO , DI); //R0 and DI
    
    void setup(){
    
    
      pinMode(RE, OUTPUT);
      pinMode(DE, OUTPUT);
     
      // put RS-485 into receive mode
      digitalWrite(DE, LOW);
      digitalWrite(RE, LOW);
    
    
      Serial.begin(9600); 
      mod.begin(9600);
     
      Serial.println("Code started");
    
      byte nitrogen_value = nitrogen(); //N value
      delay(1000);
    
      byte phosphorous_value = phosphorous(); //P value
      delay(1000);
    
      byte pottasium_value = potassium(); // K value
      delay(1000);
    
      
      Serial.println("Values are:");
      Serial.println(nitrogen_value);
      Serial.println(phosphorous_value);
      Serial.println(pottasium_value);
    
    }
    
    void loop(){
    
    }
    
     byte nitrogen(){
    
      digitalWrite(DE,HIGH);
      digitalWrite(RE,HIGH);
      
      delay(1000);
      
      if(mod.write(nitro,sizeof(nitro))==8){
        
        digitalWrite(DE,LOW);
        digitalWrite(RE,LOW);
    
    Serial.print("Executing Nitrogen");
        for(byte i=0;i<7;i++){
      
        
        values[i] = mod.read();
        
        Serial.print(values[i],HEX);
        }
        Serial.println();
      }
      return values[4];
    }
      
    byte phosphorous(){
      
      digitalWrite(DE,HIGH);
      digitalWrite(RE,HIGH);
      
      delay(1000);
      
      if(mod.write(phos,sizeof(phos))==8){
        
      
        digitalWrite(DE,LOW);
        digitalWrite(RE,LOW);
        
        for(byte i=0;i<7;i++){
        
        
    
        values[i] = mod.read();
    
        Serial.print("Executing Phosporous");
        
        Serial.print(values[i],HEX);
        }
        Serial.println();
      }
      return values[4];
    }
      
    byte potassium(){
      
      digitalWrite(DE,HIGH);
      digitalWrite(RE,HIGH);
    
      delay(1000);
      
      if(mod.write(pota,sizeof(pota))==8){
         Serial.print("Executing pottasium");
    
        digitalWrite(DE,LOW);
        digitalWrite(RE,LOW);
        
        for(byte i=0;i<7;i++){
    
       
        
        values[i] = mod.read();
        Serial.print(values[i],HEX);
    
        }
        Serial.println();
      }
      return values[4];
    }


During execution I always get 255 as output

enter image description here

Please help me to find the mistake and error.

\$\endgroup\$
10
  • 3
    \$\begingroup\$ What precisely does this mean --> N - P - K sensor's data? What is the sensor and where can its data sheet be found? \$\endgroup\$
    – Andy aka
    Commented Feb 19, 2023 at 11:08
  • 1
    \$\begingroup\$ We'd also need to know how the software serial is implemented. Do you know how it works? Sounds like Arduino code so you may have better luck in Arduino.SE, if this is not cleary an Electronics question. \$\endgroup\$
    – Justme
    Commented Feb 19, 2023 at 11:14
  • \$\begingroup\$ @Andyaka NPK soil sensor.Here is the link : [link] (google.com/…) \$\endgroup\$
    – Steve54
    Commented Feb 19, 2023 at 11:26
  • 1
    \$\begingroup\$ Please provide a definitive data sheet link. Put that link in your question i.e. edit your question with this information. \$\endgroup\$
    – Andy aka
    Commented Feb 19, 2023 at 11:29
  • \$\begingroup\$ @Andyaka Sorry I don't understand you !! But this is a reference link : [link] (lastminuteengineers.com/soil-npk-sensor-arduino-tutorial) \$\endgroup\$
    – Steve54
    Commented Feb 19, 2023 at 11:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.