0

我在做什么错 line = client.readStringUntil('\r');if (line.substring(0) == "1");

 // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
  line = client.readStringUntil('\r'); //    String
    Serial.println(line); 
  }

// Rele 1

  if (line.substring(0) == "1") 
 {
    Serial.println("Rele 1 ON");
    digitalWrite(Rele_1, LOW);
    myBr1 = 1;
 }
 else if (line.substring(0) == "0") 
 {
    Serial.println("Rele 1 OFF");
    digitalWrite(Rele_1, HIGH);
    myBr1 = 0;
 }
 else
 {
    Serial.println("Rele 1 OFF the charts - Check what you give me....");
    digitalWrite(Rele_1, HIGH);
    myBr1 = 0;
 }

当我运行此代码时,串行打印线给我:000 但继电器 1 给我:Rel 1 关闭图表 - 检查你给我的内容....如果我强制 line=001; 串行打印给我 1,而不是 001,我现在有 2 个继电器还有一点开始OTA更新。将添加更多继电器。我在混淆什么,我该如何纠正?

4

2 回答 2

0

我今天学习了“0”和“0”和0之间的差异。通过改变:

  if (line.substring(0) == "1") 
 {

 if (line.charAt(1) == '1')   // Bryter 1. 
    {

这解决了我的问题。

于 2018-04-08T07:08:28.787 回答
0

对于您的测试,您应该使用引号将 line="001" 强制为字符串,而不是 int。这就是为什么它打印 1 而不是 001

对于 的使用line.substring(0) == "1",line.substring(0) 将返回“001”而不是“1”。正确的做法是line.substring(0,1) == "1"只读取第一个字符。

于 2018-04-06T09:01:24.643 回答