0

请我希望有人可以帮助我。几个星期以来我一直在努力,我对此很陌生。

我想将数据从 ESP32 SIM800L 发送到 mqtt 代理。mqtt 服务器在我的本地机器上运行,ESP32 SIM800 可以完美连接到 APN。我看到很多教程使用 WIFI 连接而不是 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, byte* payload, 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();

}
4

1 回答 1

1

您将代理的名称设置为localhost

const char* broker = "localhost";

localhostIP 地址 127.0.0.1 表示“运行此代码的主机”。当您在运行代理的计算机上键入命令时,localhost将意味着该计算机。它不可能在 ESP32 上运行。

您需要指定运行代理的计算机的名称或 IP 地址。您如何找到这将取决于您正在运行的操作系统。

如果那台计算机在您的本地网络上,它可能使用的是私有 IP 地址,例如10.0.1.x192.168.1.x。如果是这种情况,您将需要在路由器中使用端口转发将数据包转发给它(然后您将使用路由器的 IP 地址而不是代理的 IP 地址)。

如果您使用的是路由器的 IP 地址,它可能会在没有警告的情况下更改,因此您需要使用动态 DNS之类的东西来保持其当前 IP 地址的最新状态。

您可能会更好地在基于云的虚拟服务器上运行网络之外的代理,或者使用其中的几种商业 MQTT 服务。他们中的大多数都有一个免费套餐,每月允许合理的流量。

无论如何,localhost永远不会在这里工作。您需要经纪人的真实公共 IP 地址或名称。

于 2020-09-02T16:42:51.000 回答