I am trying to send sensor data to MQTT broker like Cloudmqtt with sim800l.Using tinygsm MQTTClient basic sketch I was able to send status and toggle LED in mosquitto broker setup on my pc.but while i am trying to use userID and password from cloudmqtt along side broker and port it could not connect to the server.I am bit confused where to put those for authentication, i have put them on mqttConnect function here is the code.what is I am doing wrong? I am new to this, TIA
`
// Select your modem:
#define TINY_GSM_MODEM_SIM800
// #define TINY_GSM_MODEM_SIM808
// #define TINY_GSM_MODEM_SIM900
// #define TINY_GSM_MODEM_A6
// #define TINY_GSM_MODEM_A7
// #define TINY_GSM_MODEM_M590
// #define TINY_GSM_MODEM_ESP8266
#include <TinyGsmClient.h>
#include <PubSubClient.h>
#include <stdlib.h>
// Your GPRS credentials
// Leave empty, if missing user or pass
const char apn[] = "gpinternet";
const char user[] = "";
const char pass[] = "";
// Use Hardware Serial on Mega, Leonardo, Micro
//#define SerialAT Serial1
// or Software Serial on Uno, Nano
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(D8, D7); // RX, TX
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
PubSubClient mqtt(client);
//MQTT CERDITIONALS
const char* broker = "m11.cloudmqtt.com";
const int mqtt_port = *****;
const char *mqtt_user = "********";
const char *mqtt_pass = "********";
const char *mqtt_gsm_client_name = "*******";
//Topic instance
//const char* topicLed = "GsmClientTest/led";
const char* topicInit = "GsmClientTest/init";
const char* topicGasValue = "/gas_reading";
#define LED_PIN D4
int ledStatus = LOW;
long lastReconnectAttempt = 0;
unsigned long previousMillis = 0;
const long interval = 10000;
void setup() {
pinMode(LED_PIN, OUTPUT);
// Set console baud rate
Serial.begin(115200);
delay(10);
// Set GSM module baud rate
SerialAT.begin(115200);
delay(3000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
modem.restart();
String modemInfo = modem.getModemInfo();
Serial.print("Modem: ");
Serial.println(modemInfo);
// Unlock your SIM card with a PIN
//modem.simUnlock("1234");
Serial.print("Waiting for network...");
if (!modem.waitForNetwork()) {
Serial.println(" fail");
while (true);
}
Serial.println(" OK");
Serial.print("Connecting to ");
Serial.print(apn);
if (!modem.gprsConnect(apn, user, pass)) {
Serial.println(" fail");
while (true);
}
Serial.println(" OK");
// MQTT Broker setup
mqtt.setServer(broker, mqtt_port);
mqtt.setCallback(mqttCallback);
}
boolean mqttConnect() {
Serial.print("Connecting to ");
Serial.print(broker);
if (!mqtt.connect("mqtt_gsm_client_name", "mqtt_user", "mqtt_pass")) {
Serial.println(" Fail");
return false;
}
Serial.println(" OK");
mqtt.publish(topicInit, "GsmClientTest started");
//mqtt.subscribe(topicLed);
return mqtt.connected();
}
void loop() {
if (mqtt.connected()) {
mqtt.loop();
} else {
// Reconnect every 10 seconds
unsigned long t = millis();
if (t - lastReconnectAttempt > 10000L) {
lastReconnectAttempt = t;
if (mqttConnect()) {
lastReconnectAttempt = 0;
}
}
}
SendGasPpm(); // this will send the gas reading
}
void mqttCallback(char* topic, byte* payload, unsigned int len) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("]: ");
Serial.write(payload, len);
Serial.println();
// Only proceed if incoming message's topic matches
/*if (String(topic) == topicLed) {
ledStatus = !ledStatus;
digitalWrite(LED_PIN, ledStatus);
mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
}*/
}
//Sending Gas MQTT
void SendGasPpm() {
unsigned long currentMillis = millis();
int sensor = A0;
int sensorValue = 0;
if (currentMillis - previousMillis >= interval) { // checks if 10 delay is over
// save last time sensor reading
previousMillis = currentMillis;
sensorValue = analogRead(sensor);
int h = sensorValue;
if (isnan(h)) {
Serial.println("Failed to read from Gas sensor!");
//return; This will ensure that data is always sent
}
Serial.print("Gas-sensor");
Serial.print(h);
mqtt.publish(topicGasValue, "gas" );
}
}
`
I admit that I know very little about MQTT… but looking at your sketch, I can’t see anything Blynk related in it. Are you posting in the correct forum?
I am sorry if this is an inappropriate question, tinygsm library has blynk integration too with sim800l. I thought May b someone could help who is more familiar with the library
@vshymanskyy This is your baby… Can you help with this "not quite Blynk’ support here? Or is the MQTT site more appropriate? http://mqtt.org/get-involved Thanks.
No this has nothing to do with Blynk and TinyGSM. It’s related to the mqtt library in use, like PubSubClient. I don’t know the answer, you should try finding a working solution with that library and try using it with TinyGSM.
Thank you guys for your suggestions, i didn’t intend to flooding here. I should be more careful asking for help in right place.though i am not working with blynk now but i have before,next questions will be more blynk related.
I’ve never been able to get PubSubClient to accept the client name, username and password as variables. I think you have to put them directly into the MQTT.connect command surrounded by quotes.
However, you seem to be trying to connect by assigning values to variables then surrounding those variable names with quotes in the MQTT.connect command. I can’t see how that’s ever going to work.
if (!mqtt.connect(“mqtt_gsm_client_name”, “mqtt_user”, “mqtt_pass”))
I don’t have any first hand experience of the SIM800I, or using a GPRS shield for Blynk communication, but I do have experience of using the SIM900 for SMS communication.
With the SIM900 it’s difficult to establish if the SIM card is correctly registered with the mobile network and that the mobile carrier is supported by the firmware on the SIM900. I had a SIM from the UK carrier called “Three” which could receive and send SMS messages when used in a phone, but which wouldn’t work in the SIM900. II tried another SIM from the “GifGaff” provider and this worked fine. It seems that the SIM900 firmware gust didn’t work with Three.
I ended-up using an Arduino Mega (because it has multiple serial ports) to monitor communications between the SIM900 and the Arduino, by issuing an AT command to show verbose messages and viewing these in the serial monitor. I guess I could have used software serial with a standard Arduino or ESP device, but the Mega just made diagnostics easier.
The SIM900 is also very power hungry and needs it’s own power supply to function correctly. It also has a switch to select which power source (external socket or pins) is being used and this has to be set correctly. Not sure if the SIM800I is similar?
If you have an Ethernet shield and/or ESP8266 then maybe you should use that to check that all is working correctly before going back to GPRS data communications?
Once you do get a working connection to CloudMQTT then I seem to recall that you manually have to define the MQTT paths/folders that you want to read/write to. These aren’t created/handled automatically in the same was as they are in Mosquitto.
Thanks pete for your suggestions,it was helpful. I have used same sim800l for blynk communication just to check and it works ,but the connection keep reconnecting .for that sensor data was not so accurate on the widget.
As for the sim card and power issue, I have used adafruit MQTT with this sim and power supply,it works fine.I can assure that those are not the issue.With nodemcu CloudMQTT credentials works fine too.
I am not sure about manually defining the MQTT path. I am using the free plan and created topic where I am publishing sensor data.
I have attached mqtt.state() and it shows -4 which is keep alive timeout problem.changing it to 15s to 60s couldn’t resolve the problem.Any thoughts on that??
Okay, I took your code and stripped-out the GSM modem bits and replaced them with Wi-Fi code, as I don’t have a GSM modem I could try. I uploaded this to a NodeMCU and did some testing usieng my own CloudMQTT free account.
There are a few versions of PubSubClient around and I’ve had problems with this in the past. I’m using version 2.6.0 by Nick O’Leary and my findings below are based on that version.
I can’t get a connection to CloudMQTT using variable names in the connection string:
if (!mqtt.connect(mqtt_gsm_client_name, mqtt_user, mqtt_pass))
but if I use string literals as you tried later (obviously using my credentials not yours - but see more on this below), it connects okay:
if (!mqtt.connect(“GsmClientTest”, “myusername”, “mypassword”))
In this post you included the username and password that you were using.
These look like the CloudMQTT top-level account credentials and these aren’t the credentials that you should to be using. You need to define a username and password in the Manage User section of the overview page, then in the ACLs section define a rule that links this user to the topic you’ll be using “/gas_reading”) and give it read/write permissions. This username and password are the credentials you should be using in your mqtt.connect statement.
There’s one other issue, which is the last line of code:
This will write the string literal “gas” to the MQTTcloud topic of “/gas_reading” every 10 seconds, regardless of the value that your SendGasPpm() function is reading from the gas sensor. You use an integer variable “h” to capture the sensor value, and its this that you need to be publishing to CloudMQTT. Unfortunately the mqtt.publish command is expecting a char variable type, not an integer, so you need to convert the integer to a char and use this in the mqtt.publish statement.
You are the best, it works perfectly now. I did not know that I have to use Manage user credentials. And i knew about mqtt.publish(topicGasValue, “gas” ); .It was just for checking if the string can be sent. I have to calculate the ppm which is not in this code.I gave up and started to work on Adafruit MQTT, but i can work with any broker now. Thank you so much pete, you were so helpful.