Home › Forums › Mayfly Data Logger › DigiXBeeWifi class mqtt codeflow example › Reply To: DigiXBeeWifi class mqtt codeflow example
Thank you for your response.
I used TinyGSM directly and got into this problem. I was deploying broker locally,, the code on the inside check for dns lookup.. Currently I commented this code out and tinygsm works.
https://github.com/issues/created?issue=vshymanskyy%7CTinyGSM%7C854
This is what my full code looks without tinygsm:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
#define XBEE_PWR 18 #define XBEE_SERIAL Serial1 #include <ModularSensors.h> #include <modems/DigiXBeeWifi.h> #include <ArduinoMqttClient.h> // the broker id doesn't matter as client is already connected to that ip with xctu const char broker[] = "asdasd"; const int port = 1883; const char* mqttClientID = "MayflyXBee"; const int8_t powerPin = 18; const int8_t statusPin = 19; const int8_t modemResetPin = 20; const int8_t modemSleepRqPin = 23; const char* ssid = "USU-guest"; const char* pwd = ""; DigiXBeeWifi modemXBWF(&Serial1, powerPin, statusPin, false, modemResetPin, modemSleepRqPin, ssid, pwd, false); class XBeeTransparentClient : public Client { public: XBeeTransparentClient(Stream& s) : _stream(s) {} int connect(IPAddress ip, uint16_t port) override { return 1; } int connect(const char* host, uint16_t port) override { return 1; } void stop() override {} uint8_t connected() override { return 1; } operator bool() override { return true; } size_t write(uint8_t b) override { return _stream.write(b); } size_t write(const uint8_t* buf, size_t size) override { return _stream.write(buf, size); } int available() override { return _stream.available(); } int read() override { return _stream.read(); } int read(uint8_t* buf, size_t size) override { return _stream.readBytes(buf, size); } int peek() override { return _stream.peek(); } void flush() override {} private: Stream& _stream; }; XBeeTransparentClient xbeeClient(Serial1); MqttClient mqttClient(xbeeClient); void setup() { Serial.begin(9600); pinMode(XBEE_PWR, OUTPUT); digitalWrite(XBEE_PWR, HIGH); delay(2000); XBEE_SERIAL.begin(9600); if (!checkForConnection()) { Serial.println("Could not connect to WiFi"); return; } Serial.println("WiFi connected!"); delay(5000); mqttClient.setId(mqttClientID); Serial.println("starting mqtt"); if (!mqttClient.connect(broker, port)) { Serial.print("MQTT failed, error: "); Serial.println(mqttClient.connectError()); } else { Serial.println("MQTT connected!"); } } void loop() { if (!mqttClient.connected()) { Serial.println("MQTT disconnected, reconnecting..."); delay(3000); mqttClient.connect(broker, port); return; } mqttClient.poll(); mqttClient.beginMessage("sensors/data"); mqttClient.print("temperature=23.5"); mqttClient.endMessage(); Serial.println("Message sent"); delay(2000); } bool checkForConnection() { String status = "F"; int attempts = 0; XBEE_SERIAL.print("+++"); delay(1100); clearXbeeSerialBuffer(); while (status != "0" && attempts < 50) { XBEE_SERIAL.println("ATAI"); delay(500); if (XBEE_SERIAL.available()) { String response = XBEE_SERIAL.readStringUntil('\n'); response.trim(); status = response; } attempts++; Serial.println(status); delay(1000); } XBEE_SERIAL.println("ATCN"); delay(3000); clearXbeeSerialBuffer(); delay(2000); return status == "0"; } void clearXbeeSerialBuffer() { while (XBEE_SERIAL.available()) { XBEE_SERIAL.read(); } } |
This is how I am implementing tinygsm
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
// this is the working code // but the mqtt connection closes immediately after first time // need to figure out the reason #define TINY_GSM_MODEM_XBEE #define TINY_GSM_RX_BUFFER 256 #define TINY_GSM_USE_GPRS false #define TINY_GSM_USE_WIFI true #define TINY_GSM_DEBUG Serial #include <Arduino.h> #include <TinyGsmClient.h> #include <ArduinoMqttClient.h> #include <StreamDebugger.h> #define SerialMon Serial #define SerialAT Serial1 #define XBEE_PWR 18 const char* broker = "raspberrypi1.mypc.usu.edu"; const int port = 1883; const char* topicTemp = "sensor/temperature"; StreamDebugger debugger(SerialAT, SerialMon); TinyGsm modem(debugger); TinyGsmClient client(modem); MqttClient mqtt(client); void setup() { SerialMon.begin(9600); delay(20); pinMode(XBEE_PWR, OUTPUT); digitalWrite(XBEE_PWR, HIGH); delay(2000); SerialAT.begin(9600); delay(3000); SerialMon.println("Waiting for WiFi..."); delay(5000); SerialMon.print("Local IP: "); SerialMon.println(modem.localIP()); mqtt.setId("ArduinoTempSensor"); mqtt.setTxPayloadSize(512); SerialMon.print("Connecting to MQTT broker: "); SerialMon.println(broker); if (!mqtt.connect(broker, port)) { SerialMon.print("MQTT connection failed, error code: "); SerialMon.println(mqtt.connectError()); } else { SerialMon.println("MQTT connected!"); } } void loop() { // Reconnect if dropped if (!mqtt.connected()) { SerialMon.print("MQTT disconnected, reconnecting... "); delay(1000); if (!mqtt.connect(broker, port)) { SerialMon.print("failed, error code: "); SerialMon.println(mqtt.connectError()); delay(5000); return; } SerialMon.println("reconnected!"); } mqtt.poll(); // Publish temperature every 10 seconds static unsigned long lastMsg = 0; if (millis() - lastMsg > 10000L) { lastMsg = millis(); float temperature = 20.0 + random(0, 150) / 10.0; SerialMon.println(); SerialMon.print("This is the console output Publishing temperature: "); SerialMon.println(temperature); SerialMon.println(); mqtt.beginMessage(topicTemp); mqtt.print(temperature); mqtt.endMessage(); } } |
Tinygsm implementation will result in flaky broker connection like this:
1773954855: New client connected from 144.39.141.49:49164 as ArduinoTempSensor (p4, c1, k15). 1773954865: Client ArduinoTempSensor [144.39.141.49:49164] disconnected: connection closed by client. 1773954865: New connection from 144.39.141.49:49165 on port 1883. 1773954865: Protocol error from 144.39.141.49:49165: First packet not CONNECT (30). 1773954865: Client 144.39.141.49 [144.39.141.49:49165] disconnected: protocol error.
Welcome to EnviroDIY, a community for do-it-yourself environmental science and monitoring. EnviroDIY is part of