无法将数据发布到我的本地mqtt服务器

问题描述

请我希望有人可以帮助我。几周以来,我一直在努力工作,对此我还很陌生。

我想将数据从ESP32 SIM800L发送到mqtt代理。 mqtt服务器在我的本地机器上运行,ESP32 SIM800可以完美地连接到APN。 我看到许多教程都是通过WIFI连接实现的,但没有GPRS(我正在使用的是GPRS)。

我终于找到了:tinyGSM和这个:arduino mqtt mongodb

我按如下方法进行了修改,但仍然无法连接:

// Your GPRS credentials (leave empty,if not needed)
const char apn[]      = "internet.tn"; // APN (example: internet.vodafone.pt) use https://wiki.apnchanger.org
const char gprsUser[] = ""; // GPRS User
const char gprsPass[] = ""; // GPRS Password

// SIM card PIN (leave empty,if not defined)
const char simPIN[]   = ""; 

uint32_t lastReconnectAttempt = 0;

// TTGO T-Call pins
#define MODEM_RST            5
#define MODEM_PWKEY          4
#define MODEM_POWER_ON       23
#define MODEM_TX             27
#define MODEM_RX             26
#define I2C_SDA              21
#define I2C_SCL              22


// Set serial for debug console (to Serial Monitor,default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to SIM800 module)
#define SerialAT Serial1

// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800      // Modem is SIM800
#define TINY_GSM_RX_BUFFER   1024  // Set RX buffer to 1Kb

#include <Wire.h>
#include <TinyGsmClient.h>
#include <PubSubClient.h>

#ifdef DUMP_AT_COMMANDS
    #include <StreamDebugger.h>
    StreamDebugger debugger(SerialAT,SerialMon);
    TinyGsm modem(debugger);
#else
    TinyGsm modem(SerialAT);
#endif



// I2C for SIM800 (to keep it running when powered from battery)
TwoWire I2CPower = TwoWire(0);

const char* broker = "localhost";

const char* topicInit = "GsmClientTest/init";
// Function prototypes
void subscribeReceive(char* topic,byte* payload,unsigned int length);

// TinyGSM Client for Internet connection
// gsm and MQTT related objects
TinyGsmClient client(modem);
PubSubClient mqtt(client);

long mqtttimer = 0;                 // Timer for counting 5 seconds and retrying mqtt connection
byte mqtttarea = 1;   

#define uS_TO_S_FACTOR 1000000     /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  3600        /* Time ESP32 will go to sleep (in seconds) 3600 seconds = 1 hour */

void mqttCallback(char* topic,unsigned int len) {
    SerialMon.print("Message arrived [");
    SerialMon.print(topic);
    SerialMon.print("]: ");
    SerialMon.write(payload,len);
    SerialMon.println();}

boolean mqttConnect() {
    SerialMon.print("Connecting to ");
    SerialMon.print(broker);

    // Connect to MQTT Broker
    boolean status = mqtt.connect("GsmClientTest");

    // Or,if you want to authenticate MQTT:
    //boolean status = mqtt.connect("GsmClientName","mqtt_user","mqtt_pass");

    if (status == false) {
        SerialMon.println(" fail");
        return false;
    }
    SerialMon.println(" success");
    mqtt.publish(topicInit,"GsmClientTest started");
    // mqtt.subscribe(topicLed);
    return mqtt.connected();}
void setup() {

    SerialMon.begin(9600);

    // Start I2C communication
    I2CPower.begin(I2C_SDA,I2C_SCL,400000);

    // Set modem reset,enable,power pins
    pinMode(MODEM_PWKEY,OUTPUT);
    pinMode(MODEM_RST,OUTPUT);
    pinMode(MODEM_POWER_ON,OUTPUT);
    digitalWrite(MODEM_PWKEY,LOW);
    digitalWrite(MODEM_RST,HIGH);
    digitalWrite(MODEM_POWER_ON,HIGH);

    // Set GSM module baud rate and UART pins
    SerialAT.begin(9600,SERIAL_8N1,MODEM_RX,MODEM_TX);
    delay(3000);

    // Restart SIM800 module,it takes quite some time
    // To skip it,call init() instead of restart()
    SerialMon.println("Initializing modem...");
    modem.restart();
    // use modem.init() if you don't need the complete restart

    // Unlock your SIM card with a PIN if needed
    if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
        modem.simUnlock(simPIN);
    }
    SerialMon.print("Connecting to APN: ");
    SerialMon.print(apn);
    if (!modem.gprsConnect(apn,gprsUser,gprsPass)) {
        SerialMon.println(" fail");
    }
    else {
        SerialMon.println(" OK");
    }
    // MQTT Broker setup
    mqtt.setServer(broker,1883);
    mqtt.setCallback(mqttCallback);
    esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
}


void loop() {
    // This is needed at the top of the loop!
    if (!mqtt.connected()) {
        SerialMon.println("=== MQTT NOT CONNECTED ===");
        // Reconnect every 10 seconds
        uint32_t t = millis();
        if (t - lastReconnectAttempt > 10000L) {
            lastReconnectAttempt = t;
            if (mqttConnect()) {
                lastReconnectAttempt = 0;
            }
        }
        delay(100);
        return;
    }
    mqtt.publish(topicInit,"Hello");
    mqtt.loop();

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)