ESP32通过RS485使用土壤氮磷钾传感器


//TX 16
//RX 17
#include <Arduino.h>
#include <HardwareSerial.h>
unsigned char item[8] = {0x01, 0x03, 0x00, 0x1E, 0x00, 0x03, 0x65, 0xCD};
void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);

  Serial2.begin(9600);
  while (!Serial)
    ; // wait for serial port to connect. Needed for Leonardo only
}

void loop()
{

  String data = "";
  char buff[128]; // 定义存储传感器数据的数组
  String info[11];
  for (int i = 0; i < 8; i++)
  {                         // 发送测温命令
    Serial2.write(item[i]); // write输出
  }
  delay(100); // 等待测温数据返回
  data = "";
  while (Serial2.available())
  {                                                   //从串口中读取数据
    unsigned char in = (unsigned char)Serial2.read(); // read读取
    // Serial.print(in, HEX);
    // Serial.print(',');
    data += in;
    data += ',';
  }
  if (data.length() > 0)
  { //先输出一下接收到的数据
    // Serial.print(data.length());
    // Serial.println();
    // Serial.println(data);
    int commaPosition = -1;
    // 用字符串数组存储
    for (int i = 0; i < 11; i++)
    {
      commaPosition = data.indexOf(',');
      if (commaPosition != -1)
      {
        info[i] = data.substring(0, commaPosition);
        data = data.substring(commaPosition + 1, data.length());
      }
      else
      {
        if (data.length() > 0)
        {
          info[i] = data.substring(0, commaPosition);
        }
      }
    }
  }

  Serial.print("氮元素含量:");
  Serial.print(info[4]);
  Serial.println("mg/kg");
  Serial.print("磷元素含量:");
  Serial.print(info[6]);
  Serial.println("mg/kg");
  Serial.print("钾元素含量:");
  Serial.print(info[8]);
  Serial.println("mg/kg");
  // linshi_d[0] = (info[3].toInt() * 256 + info[4].toInt()) / 10.0;
  // linshi_d[1] = (info[5].toInt() * 256 + info[6].toInt()) / 10.0;
  // Serial.print("tem:");
  // Serial.println(linshi_d[0]);
  // Serial.print("hem:");
  // Serial.println(linshi_d[1]);
  delay(3000);
}

如需指导,可**私聊**,适当收费

  • 3
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值