So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By domfe
#62958 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?
User avatar
By jeffas
#62979 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.
User avatar
By gdsports
#62981 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
User avatar
By domfe
#63066 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