-->
Page 1 of 1

HTTPClient is only making one request

PostPosted: Fri Feb 24, 2017 3:52 am
by domfe
Hello.
I'm new to ESP8266 programming and I'm having a problem with my Wemos D1 board.
I want to post data to thingspeak every five minutes
In my setup() function

Code: Select all  sendDataTS();
  sendTicker.attach(5*60, sendDataTS);


Code: Select allstatic HTTPClient httpClient;

void sendDataTS()
{
  String s;

  Serial.println(F("sendDataTS"));
  s+=F("api_key=");
  s+=apiKey;
  s+=F("&field1=");
  s+=String(SoilMoisture, 2);
  httpClient.setReuse(true);
  httpClient.begin(F("http://api.thingspeak.com/update"));
  httpClient.addHeader(F("Content-Type"), F("application/x-www-form-urlencoded"));
  int status=httpClient.POST(s);
  String payload=httpClient.getString();
  httpClient.end();
  Serial.print(status);
  Serial.print(payload);
  Serial.println(F(" sendDataTS end"));
}


The function is correctly fired but the connection is made only the first time (not from the ticker). When it is called from the ticker, POST() return always -1.
Maybe I'm missing something about closing the connection?

Re: HTTPClient is only making one request

PostPosted: Fri Feb 24, 2017 12:34 pm
by jeffas
Have you tried calling sendDataTS a second time outside the ticker? That could be useful in identifying whether the problem is either (a) sendDataTS doesn't work a second time, or (b) there's something different about being called from ticker.
Or call it only from ticker, for the same reason.

Re: HTTPClient is only making one request

PostPosted: Fri Feb 24, 2017 12:48 pm
by gdsports
From https://github.com/esp8266/Arduino/blob ... .md#ticker

Ticker

Library for calling functions repeatedly with a certain period. Two examples included.

It is currently not recommended to do blocking IO operations (network, serial, file) from Ticker callback functions. Instead, set a flag inside the ticker callback and check for that flag inside the loop function.

Here is library to simplificate Ticker usage and avoid WDT reset: TickerScheduler

Re: HTTPClient is only making one request

PostPosted: Sun Feb 26, 2017 1:07 pm
by domfe
thank you!
Now my esp8266 is stable, I think that the same problem arises when reading a DHT sensor.

Code: Select allvoid setReadyForDHTUpdate();
bool readyForDHTUpdate = true;

//declaration
void dht_wrapper(); // must be declared before the lib initialization

// Lib instantiate
PietteTech_DHT dht(DHTPIN, DHTTYPE, dht_wrapper);


in setup
Code: Select all  dht.begin(DHTPIN, DHTTYPE, dht_wrapper);
  dht.acquire();
  inSensorTicker.attach(DHT_UPDATE_INTERVAL_SECS, setReadyForDHTUpdate);



Code: Select allvoid loop()
{
  server.handleClient();

  if (readyForDHTUpdate && !dht.acquiring())
  {
    int acquireresult = dht.getStatus();
    if ( acquireresult == 0 )
      SetInSensor(dht.getCelsius(), dht.getHumidity(), 0, formatTime(dstAdjusted.time(NULL, now())));

    readyForDHTUpdate = false;
  }

  if(readyToSendDataTS)
  {
    sendDataTS(false);
    readyToSendDataTS=false;
  }

  delay(10);
}

// Interrupt function
void setReadyForDHTUpdate()
{
  dht.acquire();
  readyForDHTUpdate = true;
}


I will try to move "dht.acquire()" in the loop function