为什么我的客户端不接收来自服务器的中断功能发送的数据?

时间:2019-07-06 13:57:59

标签: arduino-ide nodemcu arduino-esp8266

我必须在中断功能中向客户端发送数据。但是显然,客户端无法接收数据。服务器确实获得了从客户端发送的数据,但是客户端无法回复。

当我在空循环中尝试通信时,它是成功的。但是一旦将其放入中断中,就无法取回数据。服务器必须发送的数据只是一个数字,该数字随后将在客户端触发led。如果有人可以帮助我解决问题,那真的很有帮助。

这是服务器代码的一部分。

void ICACHE_FLASH_ATTR detectsMovement() {
 LightValue = LightSensor.GetLightIntensity();  
 Serial.print("Light: ");
 Serial.println(LightValue);
 if (LightValue < threshold )
 {
    Serial.println("MOTION DETECTED!!!");
    analogWrite(led, 1023);
    startTimer = true;
    lastTrigger = millis(); 
    WiFiClient client = server.available();
    if (client) {
      if (client.connected()) 
      {
         digitalWrite(ledPin, LOW);  // to show the communication only 
         (inverted logic)
         Serial.println("");
         String request = client.readStringUntil('\r');    // receives the 
          message from the client
         Serial.print("From client: "); Serial.println(request);
         client.flush();
         client.println("1\r");      // sends the answer to the client
         digitalWrite(ledPin, HIGH);
      }
    client.stop();                // terminates the connection with the client
   }
  }
}

void setup() {
  Serial.begin(115200);                   // only for debug
  WiFi.config(ip, gateway, subnet);       // forces to use the fix IP
  WiFi.begin(ssid, pass);                 // connects to the WiFi router
  while (WiFi.status() != WL_CONNECTED) 
  {
    Serial.print(".");
    delay(500);
  }

   pinMode(motionSensor, INPUT_PULLUP);
   // Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
   attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);

   server.begin();        // starts the server

   pinMode(light, INPUT);
   LightSensor.begin();  
   Serial.println("Running...");

   pinMode(ledPin, OUTPUT);
   pinMode(led, OUTPUT);
}

还有客户端代码

   void loop () {
  client.connect(server, 80);   // Connection to the server
  digitalWrite(ledPin, LOW);    // to show the communication only (inverted logic)
  Serial.println(".");
  client.println("Hello server! Are you sleeping?\r");  // sends the message to the server
  String answer = client.readStringUntil('\r');   // receives the answer from the server
  Serial.println("from server: " + answer);
  if (answer  == "1")
  {
    Serial.println("Success!");
    analogWrite(D0, 512);
  }
  client.flush();4
  digitalWrite(ledPin, HIGH);
  delay(2000);                  // client will trigger the communication after two seconds
}

客户端应该获得值“ 1”。我不知道该怎么办。

0 个答案:

没有答案