How do I publish sensor data to EMQX broker on Ubuntu from a remote Arduino (no WiFi) interfaced with SIM800L?

22 Views Asked by At

I am attempting to publish sensor data to the EMQX broker installed on an Ubuntu machine that is connected to the internet. I have installed EMQX for Ubuntu 20.04 and EMQX Explorer from mqttx.app on the Ubuntu machine. My hardware setup includes an Arduino Uno (not connected to the internet) and a SIM800L v2 module (2G/GSM/GPRS).

I suspect there might be something simple that I'm missing, especially considering that I haven't used EMQX with the SIM800L v2 module before.

Here is my sample code:

#include <SoftwareSerial.h>
#include <PubSubClient.h>
#include <GSM.h>

#define SIM800_TX_PIN 3 // SIM800L Tx pin connected to Arduino pin 3
#define SIM800_RX_PIN 2 // SIM800L Rx pin connected to Arduino pin 2

SoftwareSerial sim800(SIM800_TX_PIN, SIM800_RX_PIN);

const char* mqtt_server = "**************"; // MQTT broker host
const int mqtt_port = 1883; // MQTT broker port
const char* mqtt_username = "********"; // MQTT username
const char* mqtt_password = "********"; // MQTT password
const char* mqtt_client_id = "**********"; // MQTT client ID
const char* mqtt_topic_temp = "test/temperature"; // MQTT topic for temperature data
const char* mqtt_topic_hum = "test/humidity"; // MQTT topic for humidity data

GSM gsmAccess;
GPRS gprs;
GSMClient gsmClient;
PubSubClient mqttClient(gsmClient); // MQTT client object

void setup() {
  Serial.begin(9600);
  sim800.begin(9600);
  delay(1000);

  connectToMqtt();
}

void loop() {
  if (!mqttClient.connected()) {
    connectToMqtt();
  }
  mqttClient.loop();

  float temperature = random(20, 30); // Generate random temperature data between 20 and 30 degrees Celsius
  float humidity = random(40, 60); // Generate random humidity data between 40% and 60%

  // Publish temperature data
  char tempPayload[10];
  dtostrf(temperature, 4, 2, tempPayload);
  mqttClient.publish(mqtt_topic_temp, tempPayload);

  // Publish humidity data
  char humPayload[10];
  dtostrf(humidity, 4, 2, humPayload);
  mqttClient.publish(mqtt_topic_hum, humPayload);

  delay(10000); // Publish data every 10 seconds
}

void connectToMqtt() {
  Serial.println("Connecting to GPRS network...");
  while (gsmAccess.begin() != GSM_READY) {
    delay(1000);
  }

  Serial.println("Connecting to MQTT broker...");
  if (mqttClient.connect(mqtt_client_id, mqtt_username, mqtt_password)) {
    Serial.println("Connected to MQTT broker.");
  } else {
    Serial.print("Failed to connect to MQTT broker, rc=");
    Serial.print(mqttClient.state());
    Serial.println(" Trying again in 5 seconds...");
    delay(5000);
  }
}

Here is the error:

Arduino: 1.8.19 (Linux), Board: "Arduino Uno"
...
/tmp/arduino_build_880997/libraries/GSM/GSM3SoftSerial.cpp.o (symbol from plugin): In function `GSM3SoftSerial::spaceAvailable()':
(.text+0x0): multiple definition of `__vector_5'
/tmp/arduino_build_880997/libraries/SoftwareSerial/SoftwareSerial.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
Using library SoftwareSerial at version 1.0 in folder: /home/xxx/snap/arduino/85/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/SoftwareSerial 
Using library PubSubClient at version 2.8 in folder: /home/xxx/snap/arduino/current/Arduino/libraries/PubSubClient 
Using library GSM at version 1.0.6 in folder: /snap/arduino/85/libraries/GSM 
exit status 1
Error compiling for board Arduino Uno.
0

There are 0 best solutions below