1

I have an Arduino project that needs to receive data from ThingSpeak. This is the data that I'm supposed to get.

When the data is "true" it must turn the LED on. I tried this code in hope of getting "true".

void loop() { 
  Serial.println();
  Serial.println("Receiving Trigger Data...");
  if (client.connect(server, 80)) {
    Serial.println("Connected to server");
    client.println("GET /channels/344468/fields/3/last?key=XXXXXXX HTTP/1.1");
    client.println("Host: api.thingspeak.com");
    client.println("Connection: close");
    client.println();
}
while (client.available()) {
  reply = client.read();
  content.concat(reply);
}
delay(3000);
Serial.println(content);
if(content.indexOf("Content-Length: 4") > 0) {
  digitalWrite(ledPin, HIGH);
}

The Serial Monitor gives this reply. I don't see the word "true" being returned at all and I don't also see the content length. I tried putting delays but still I don't receive the "true" or even just the content length. Please help.

enter image description here

3
  • 4
    available() is non blocking. network is not so fast. at the first gap you stop reading
    – Juraj
    Commented Dec 3, 2017 at 8:18
  • 1
    I think you can to use client.connected() to determine whether there is still data coming in (since you are sending the Connection: close header).
    – Gerben
    Commented Dec 3, 2017 at 19:23
  • Thank you very much. I changed available() to connected() and it worked properly now.
    – Duckbenok
    Commented Dec 4, 2017 at 3:31

0

Your Answer

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

Browse other questions tagged or ask your own question.