简体   繁体   English

交换 PubSubClient 提供者

[英]Swap PubSubClient Provider

I'm using MQTT over ESP8266 and SIM800, they both work fine.我在 ESP8266 和 SIM800 上使用 MQTT,它们都工作正常。

I need to change dinamically, between WiFi and GPRS, depending on WiFi availability.我需要根据 WiFi 可用性在 WiFi 和 GPRS 之间进行动态更改。

My problem is , I cannot dinamically change PubSubClient, it just won't work.我的问题是,我不能动态地改变 PubSubClient,它根本行不通。

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);
}

I am using GSM and WiFi together.我同时使用 GSM 和 WiFi。 I was required to make objects on the basis of the condition of whether a WiFi or Cellular connection was available.我被要求根据 WiFi 或蜂窝连接是否可用的条件来制作对象。 This worked for me:这对我有用:

  • Make global:使全球:

    PubSubClient *mqttClient = NULL;发布订阅客户端 *mqttClient = NULL;

  • Then make new anywhere:然后在任何地方新建:

    mqttClient = new PubSubClient(gsmClient); mqttClient = new PubSubClient(gsmClient); or或者

    mqttClient = new PubSubClient(wifiClient); mqttClient = new PubSubClient(wifiClient);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM