Swap PubSubClient Provider

182 views Asked by At

I'm using MQTT over ESP8266 and SIM800, they both work fine.

I need to change dinamically, between WiFi and GPRS, depending on WiFi availability.

My problem is, I cannot dinamically change PubSubClient, it just won't work.

A simplified example of what I've done:

TinyGsm SIM800(Serial);
TinyGsmClient GPRSclient(SIM800);
WiFiClient WiFiclient;    

if(WiFi.status()!=WL_CONNECTED){
  USE_GPRS=1;
  PubSubClient mqtt(GPRSclient);
}
if(WiFi.status()==WL_CONNECTED){
  USE_GPRS=0;
  PubSubClient mqtt(WiFiclient);
}
1

There are 1 answers

0
Wasim Ghafar On

I am using GSM and WiFi together. I was required to make objects on the basis of the condition of whether a WiFi or Cellular connection was available. This worked for me:

  • Make global:

    PubSubClient *mqttClient = NULL;

  • Then make new anywhere:

    mqttClient = new PubSubClient(gsmClient); or

    mqttClient = new PubSubClient(wifiClient);