Thing.handle blocking (when connected to WiFi but not to server)


#1

I have problem with thing.handle function when the ESP module has WiFi but can’t reach server as our internet service provider is offline.
I logged if wifi disconected ever meanwhile but it never did.
But the WAN Internet Provider connection is quite unreliable.

As the thinger has simple data Streming API and i made a nice dashboard already 1 year ago, so i would like to make it more useful.

So how to prevent thing.handle wasting 10 seconds every loop cycle, I would need non blocking behavior, checking server connection periodically every few seconds, but how.

I have a nextion display as a local interface on ESP and i tried with interupts, but as they were used to run everithing else than thinger aka abused ESP rarely connected to thinger.

This code works fine when it is conneected to thiger, otherwise i can’t even update it OTA or normally use LCD interface.

#include <ESP8266WiFi.h>
#include <ThingerWifi.h>
ThingerWifi thing(“Matej”, “*************”, “***********”);

/* #include <Ticker.h>
Ticker SecondTick; */

#include <Wire.h>
#include <EEPROM.h>

#include <PCF8574.h>
PCF8574 PCF_01(0x20);
PCF8574 PCF_02(0x38);

#include <Nextion.h>

#include <NTPClient.h>
#include <WiFiUdp.h>
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, “europe.pool.ntp.org”, 3600, 60000);

#include <SoftwareSerial.h>
SoftwareSerial swSer(14, 2, false, 256); // 14-TX(Debug) 2-RX(SenzorBoard)
Nextion myNextion(Serial, 9600);

#include <MQTT.h>

#include <ESP8266mDNS.h>
#include <ArduinoOTA.h>

WiFiClient net;
MQTTClient client;

#include<Casovnik.h>

void setup() {
// thing.add_wifi("******", “***************”);
analogWriteRange(255);
myNextion.init();
swSer.begin(9600);
PCF_01.begin();
PCF_02.begin();
// ina219.begin();
timeClient.begin();
EEPROM.begin(40); // 40 bytes available
WiFi.mode(WIFI_STA);

beriEEPROM();
ThingerIzpis(); // thinger outs
ThingerDrsniki(); // thinger ins
DataIzEproma = 1;
myNextion.setComponentText(“Stanje”, “Pocakaj”);

client.begin(“192.168.1.100”, net);
// client.onMessage(messageReceived);
connect();

OTA_MK();
ArduinoOTA.begin();
Serial.println(“Ready”);
client.publish("/************/Status", “Vklop”);

}

void loop() {
LoopStart = millis();
ArduinoOTA.handle();

CasPredThing = millis();
if (WiFi.status() == WL_CONNECTED)
{
// thing.handle();
}

CasPoThing = millis();

if (TmrUkazov.Pridobi() == 1)
{
CasUkz1 = millis();
getPage();
WiFiconfig();
Histereze();
Luci();
SetingsVoda();
Pumpe();
Vent();
Grelci();
OutPokrovi();
ShraniNastav();
NocniNacin();
MeritveSerial();
DataIzEproma = 0;
CasUkz2 = millis();
MQTTcikel();
}

if (TmrUra.Pridobi() == 1)
{
CasLcd1 = millis();
URA();
LCDizpis();
VsiliMeritve();

CasLcd2 = millis();
// swSer.println("NovaNastavitev: " + String(NovaNastavitev));
swSer.println("Cas Ukazov: " + String(CasUkz2 - CasUkz1));
swSer.println("Cas LCD: " + String(CasLcd2 - CasLcd1));
swSer.println("Cas Thing: " + String(CasPoThing - CasPredThing));



if (UraFirst == 0) {
  UraUpdate();
}

}
if (TmrUraUpdate.Pridobi() == 1) {
UraUpdate();

}
LoopEnd = millis();
}

Just to make invisible parts logical: I connect to WiFi via WiFi.begin(ssid,pwd) elsewhere in the program and also define thinger resorces(ThingerIzpis(); // thinger outs ThingerDrsniki(); // thinger ins)

Loop takes 500 - 700 ms when thinger.handle is commented out, otherwise when connection is ok It is streaming 35 resources in 150 ms, but when server is unreachable i have no idea how to make program run as fast as when connected.

It would be nice to set timeout as a parameter of function .handle(int timeout), but i don’t know yet where to use it in the function.

Thanks for help in advance
Cheers Matej