mikroavr

Untitled

Mar 6th, 2021
882
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
  3. const byte nitro[] = {0x01,0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
  4. const byte phos[] = {0x01,0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
  5. const byte pota[] = {0x01,0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};
  6.  
  7. byte values[11];
  8. #define RXD1 33
  9. #define TXD1 25
  10.  
  11.  
  12. void setup() {
  13.   Serial.begin(115200);
  14.   Serial1.begin(9600, SERIAL_8N1, RXD1, TXD1);
  15.   delay(3000);
  16. }
  17.  
  18. void loop() {
  19.   byte val1,val2,val3;
  20.   val1 = nitrogen();
  21.   delay(250);
  22.   val2 = phosphorous();
  23.   delay(250);
  24.   val3 = potassium();
  25.   delay(250);
  26.  
  27.  
  28.   Serial.print("Nitrogen: ");
  29.   Serial.print(val1);
  30.   Serial.println(" mg/kg");
  31.   Serial.print("Phosphorous: ");
  32.   Serial.print(val2);
  33.   Serial.println(" mg/kg");
  34.   Serial.print("Potassium: ");
  35.   Serial.print(val3);
  36.   Serial.println(" mg/kg");
  37.   delay(2000);
  38.  
  39.   }
  40.  
  41. byte nitrogen(){
  42.   //digitalWrite(DE,HIGH);
  43.   //digitalWrite(RE,HIGH);
  44.   delay(10);
  45.   if(Serial1.write(nitro,sizeof(nitro))==8){
  46.     //digitalWrite(DE,LOW);
  47.     //digitalWrite(RE,LOW);
  48.     for(byte i=0;i<7;i++){
  49.     //Serial.print(Serial1.read(),HEX);
  50.     values[i] = Serial1.read();
  51.     Serial.print(values[i],HEX);
  52.     }
  53.     Serial.println();
  54.   }
  55.   return values[4];
  56. }
  57.  
  58. byte phosphorous(){
  59.   //digitalWrite(DE,HIGH);
  60.   //digitalWrite(RE,HIGH);
  61.   delay(10);
  62.   if(Serial1.write(phos,sizeof(phos))==8){
  63.     //digitalWrite(DE,LOW);
  64.     //digitalWrite(RE,LOW);
  65.     for(byte i=0;i<7;i++){
  66.     //Serial.print(Serial1.read(),HEX);
  67.     values[i] = Serial1.read();
  68.     Serial.print(values[i],HEX);
  69.     }
  70.     Serial.println();
  71.   }
  72.   return values[4];
  73. }
  74.  
  75. byte potassium(){
  76.   //digitalWrite(DE,HIGH);
  77.   //digitalWrite(RE,HIGH);
  78.   delay(10);
  79.   if(Serial1.write(pota,sizeof(pota))==8){
  80.     //digitalWrite(DE,LOW);
  81.     //digitalWrite(RE,LOW);
  82.     for(byte i=0;i<7;i++){
  83.     //Serial.print(Serial1.read(),HEX);
  84.     values[i] = Serial1.read();
  85.     Serial.print(values[i],HEX);
  86.     }
  87.     Serial.println();
  88.   }
  89.   return values[4];
  90. }
RAW Paste Data