2

Hello I have been using the WifiEsp to send and receive data from Thingspeak. All is well until I put my http request on the Void Loop() section.

My Goal: I want to receive data every loop not on only on setup. Here is my code

#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <SoftwareSerial.h>
SoftwareSerial Serial1(6, 7); // RX, TX
WiFiEspClient  client; 
int status = WL_IDLE_STATUS; 

#define SSID "Benok4G"      
#define PASS "password"
char reply;
char server[] = "184.106.153.149";

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial1.begin(9600);
WiFi.init(&Serial1);
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(SSID);
// Connect to WPA/WPA2 network
status = WiFi.begin(SSID, PASS);
}
Serial.println("You're connected to the network");

}

void loop() {
Serial.println();
Serial.println("Receiving Data...");
if (client.connect(server, 80)) {
Serial.println("Connected to server");
client.println("GET /channels/344468/fields/3/last?key=ABCDEFGH 
HTTP/1.1");
client.println("Host: api.thingspeak.com");
client.println("Connection: close");
client.println();

while (client.available()) {
char c = client.read();
Serial.write(c);     
}

if (!client.connected()) {
Serial.println();
Serial.println("Disconnecting from server...");
client.stop();
while(true);
 }
}

I can receive the letter "a" properly. However, I want it too loop and give me updated value every loop.

enter image description here

BUT! when I put the code from the void setup() to the the void loop(). I get this error.. enter image description here

Any ideas on how can I loop it without getting the error? I tried adding delays and putting it on another function, still the same. It will only work when I put in inside the void setup()

Thanks!

5
  • @MarkSmith Thank you for pointing that out. I have posted my complete code. The code i posted is when I put the request in the loop and gives the timeout error.
    – Duckbenok
    Commented Oct 15, 2017 at 7:58
  • By "this error", do you mean the timeout? It looks to be working regardless... What's the actual problem?
    – Mark Smith
    Commented Oct 15, 2017 at 10:11
  • When I put it in the loop.I don't see the picture with OK response. It's just blank and sometimes it returns the one with square. Is this normal?
    – Duckbenok
    Commented Oct 15, 2017 at 17:01
  • Could it be that your setup() is taking too long to execute and is timing out? Commented Oct 16, 2017 at 14:37
  • "Host: api.thingspeak.com" Some of the free services provided by Thingspeak prohibit multiple consecutive transactions unless there is an intervening pause of X seconds which could cause the timeout you appear to be experiencing. Check the rules of your subscription.
    – 6v6gt
    Commented Jul 28, 2023 at 9:26

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.