monstrenyatko / arduinomqtt Goto Github PK
View Code? Open in Web Editor NEWThis project forked from eclipse/paho.mqtt.embedded-c
MQTT client for Arduino
License: MIT License
This project forked from eclipse/paho.mqtt.embedded-c
MQTT client for Arduino
License: MIT License
Is there an example for the implementation of the MqttClient::Network class for a SIM chip with AT commands (any SIM7600, or SIM800)?
What exactly does the functions read and write have to do?
Hello, when this line:
LOG_PRINTFLN("Connection error: %i", rc);
is rturning:
Connection error: %i-5
or
Connection error: %i-6
what do these errors mean?
What is the meaning of the other numbers it can return?
I am using Arduino ESP8266 to connect Broker, but when client disconnect to Broker I check by code mqtt->isConnected()
and it not realize disconnect even. I write my code by example and change buffer
to 550
and MessageHandlersImpl
to 5
.
thank you so much for your library.
Hello, as stated in the title - it's not an issue, but is a bit annoying: the library compiles smoothly under Arduino Ide, but under Visual Studio with VisualMicro extension got an error in publish method. The source of that error is in uninitialized variable packet id. It's then set if QoS > 0, if QoS=0 remains uninitialized. Assigning any value to it doesn't disturb anything - pocketid is used (in the next step in the library) only if QoS>0, but mitigates annoying compiler errors.
And -last but not least - thank You for that library, I've been testing it for some time with SIM868 GSM module and it works great.
gimbus109
I have been testing this library for a few days (was using PubSubClient) and so far I really like it but don't understand the correct usage of mqtt->yield(30000L)
My problem is it introduces an unacceptable delay in my main loop.
Questions
mqtt->yield();
and it will automatically yield for the required time?Sorry for the many questions, I am trying to understand how it works so I can best write my code to accommodate the library.
Is there a way to manually send the keep alive ping?
FYI: I have temporarily commented out the debug output for the mqtt->yield()
function in 'MqttClient.h' as I am logging my serial output trying to find rare & random reconnects that have been causing me headaches.
Thank you in advance.
When no data available to receive or it is not a time to send keep alive
message, etc...yield
still consumes a lot of time due to the NET_MIN_TM_MS
value (see #12 discussion).
The NET_MIN_TM_MS
was introduced to guaranty reception of the entire message by single recvPacket
call.
Solution:
NET_MIN_TM_MS
value configurable, see #12NET_MIN_TM_MS
to something small like 10
or 1
msread
execution by NET_MIN_TM_MS
value each time when something receivedRisks:
May increase the number of disconnections in case of bad network connection because the library still needs to receive the entire message while single yield
call.
Hi
In this situation Task-WDT reboot will occurred in ESP32 when we used RTOS:
isConnected()
(or network.connected()
) returned trueThen when we are waiting for message ACK, Task-WDT may overflow (Depends on overflow time).
To resolve the problem, we can add feed functions like vTaskDelay()
somewhere in do while loop in waitFor()
function
or it's better add it to elapsedMs()
function.
Thanks All
Hi @monstrenyatko
when I use your library I got a problem.
I set LWT message when a disconnect is "DISCONNECTED" and I set in the broker is CONNECTED when having a client connect.
but when I set my device to connect my server. after that, I turn off the server in 3 seconds and then turn on. I got CONNECTED message came first and DISCONNECTED later.
please check that. I mean it must be DISCONNECTED came first and CONNECTED later.
I set keepalive
is 15
thank you!
I have a problem.
I checked in your code, the value NET_MIN_TM_MS
min is 100L. that mean loop
function will delay 100 ms.
can I set NET_MIN_TM_MS
smaller to make loop
function faster or another way to make loop
faster by just run when having data or something like that.
thank you so much. ๐
Hello @monstrenyatko
Thank you for your library.
I used your library in ESP8266 but when I got multiple messages in once, I got error stack buffer overflows. now I want to move ArrayBuffer
to Heap to fix that error.
or do you have any idea for that issue?
when I got any message I will feedback to another topic over MQTT. but when I got too many messages in once, my device can't feedback instant for each message. it will feedback after received done.
Ex: Sender A sends 3 messages in once. My device got all messages after that my device send feedback. but when A sends more than 3 messages my device can't handle it and got error stack buffer overflows.
Can you help me with this error.
thank you so much and have a good day.
Need to add the callback method to detect the network disconnection.
It should speed up the identification of MQTT connection failure.
The keepalive
mechanism might be too slow.
The Arduino::Client::connected API could be used to get the network connection status.
I have a problem with this code. I use the example code for my program. I add some pub topics and sub topics but the server freses out and cant connect.
void MQTTinitalization() { // this is on void setup
Ethernet.init(W5500SS);
Ethernet.begin(mac, ip);
client.begin(mqttServer, net);
client.onMessage(messageReceived);
connect();
}
void MQTTroutine() {// this is on void loop
client.loop();
if (!client.connected())connect();
sendMessage();
}
The client stays in this stage and my mosuito broquer writes me my ip is 172.34.0.34 but my ip was set to 192.168.0.194
Some times the server connect up but some times stuck on a connect...... loop and try to connect with inwalid ip adress and my mosquito broker gives me back error.
I dont know what can be a problem.
void connect() {
Serial.print("connecting...");
while (!client.connect(deviceName, mqttUsername, mqttPassword)) {
Serial.print(".");
delay(1000);
}
client.subscribe("/home");
/for (int i = 0; i < OUTPUTSINSYSTEM; i++) client.subscribe(subsrcibeOut[i]);
for (int i = 0; i < INPUTSINSYSTEM; i++) client.subscribe(subsrcibeIn[i]);/
}
void messageReceived(String &topic, String &payload) {
int payloadValue;
payloadValue = payload.toInt();
Serial.println("incoming: " + topic + " - " + payload);
for (int i = 0; i < OUTPUTSINSYSTEM; i++) {
if (topic == subsrcibeOut[i] && OutAsTermostate[i] == false) {
OUTPUTSTATES[i] = payloadValue;
if (AtachSwitc[i] == true) {
if (SwitchType[i] == true)INPUTINTEGRATION[OutSwitch[i]] = payloadValue;
else INPUTSTATES[OutSwitch[i]] = payloadValue;
}
}
}
for (int i = 0; i < INPUTSINSYSTEM; i++) {
if (topic == subsrcibeIn[i]) {
if (InputAsSwitch[i] == true) {
if (InputSwitchType[i] == true)INPUTSTATES[i] = payload.toInt();
else INPUTINTEGRATION[i] = payload.toInt();
}
}
}
}
void sendMessage()
{
if (millis() - lastMillis > 5000)
{
client.publish("/hello", "world");
lastMillis = millis();
for (int i = 0; i < OUTPUTSINSYSTEM; i++)if (OutAsTermostate[i] == false)client.publish(publisOut[i], String(OUTPUTSTATES[i]));
for (int i = 0; i < INPUTSINSYSTEM ; i++) {
if (InputAsSwitch[i] == true) {
if (InputType[i] == true) {
client.publish(publisIn[i], String(analogRead(INPUTS[i])));//This gives back a raw analog data in 10bit (0-1023) with 5V reference
}
else {
client.publish(publisIn[i], String(INPUTSTATES[i])); //This will gives back the input state that has been setted up you can modify this states on switch and push buttan also thanks to the input integration function that reverses the input eve on switch...
}
}
}
#ifdef TEMPSENSOR
for (int i = 0; i < 8; i++)client.publish(PubTopicTemp[i], String(tempState[i])); //Here we send the mesured temperatures
#endif*
}
}
So sorry to ask here, but I can't get a straight answer on this anywhere.
I wonder if your library has support Lastwill message?
Hi @monstrenyatko
me again ๐
I got Exception(28)
when call function mqtt->yield(30000L);
in loop()
function in ESP8266 when I turn off my WiFi modem.
I tried to test in so many cases and I figure out is in mqtt->yield(30000L)
function. and I also try in your example. It was the same.
could you please check that.
thank you so much.
Hello. First of all, thanks a lot for publishing the library. Please mark this post as question and I'm sorry if this is a noob question (I am not from C background). While reading your example sketch: ConnectEsp8266WiFiClient.ino, I noticed the following codes in the loop() :
// Start new MQTT connection
MqttClient::ConnectResult connectResult;
// Connect
{
MQTTPacket_connectData options = MQTTPacket_connectData_initializer;
options.MQTTVersion = 4;
options.clientID.cstring = (char*)MQTT_ID;
options.cleansession = true;
options.keepAliveInterval = 15; // 15 seconds
MqttClient::Error::type rc = mqtt->connect(options, connectResult);
if (rc != MqttClient::Error::SUCCESS) {
LOG_PRINTFLN("Connection error: %i", rc);
return;
}
}
I don't understand what is the meaning of curly braces "without parent" right just below //Connect. As per my understanding, curly braces normally encapsulate code inside if, loop, function declaration, etc. But in your example, it's just simply a pair of curly braces. If I remove the curly braces, the code will also compile fine. I must be missing something, please enlighten me.
Also I am planning to use qos 2. I noticed in the description that this library support "simplified" qos 2. What does it mean? Lastly, is this library stable for production use?
Hello,
I can't get the topic name in useful format (e.g string) from "MqttClient :: MessageData & md" object in message callback.
I tried to do this via md.topicName.cstring but it's empty? The variable md.topicName.lenstring.data does contain the topic name but with the payload appended without any separator...
Finally, I did it through the so-called "substring" using md.topicName.lenstring.data and md.topicName.lenstring.len, but could this be done easier? Is there any function or variable with plain topic name in message callback function?
I'm not sure if this is something to bring up on a Sloeber board, or with this library, but I thought I'd try here first. I have a sketch working in Arduino IDE using this ArduinoMqtt library, and wanted to "port" it into an Eclipse project using the Sloeber plugin. The Eclipse project is basically going to be wrapper for this library that handles some of the project specific lower higher level stuff (hope that makes sense). Anyway, some rejigging was needed to move from a single .ino
to a .h
and .cpp
. During the reshuffle I now get an error during compilation when I call mqtt->subscribe()
.
I can post the full files somewhere if needed, but maybe the following will point out my issue?
In CustomMqtt.cpp
I have:
void CustomMqtt::processMessage (MqttClient::MessageData& md) {
...
}
and
int8_t CustomMqtt::subscribe (char *topic, MqttClient::QoS qos) {
MqttClient::Error::type rc = mqtt->subscribe(
topic, qos, processMessage
);
...
}
Both the above are declared in CustomMqtt.h
also.
When I compile I get the error:
no matching function for call to 'MqttClient::subscribe(char*&, MqttClient::QoS&, <unresolved overloaded function type>)'
To me this looks like the same/similar implementation as in the ArduinoMqtt examples, but is there some nuance I am overlooking? Like I said full code can be made available if needed, its just a bit messy at the moment as not complete.
I am a newbye on this platform and i use adafruit mqtt library first because it has qos1 supportfor publish and subscribe and last will with also qos1,but on arduino mega it doesn't work.
i found your library and it's look very nice but i can't find much information to set lastwill and qos1 for subscribe. can you up an example much detailed? thank you so much.
During compiling on MKR GSM 1400 any of examples I am getting error while compiling. Do you know why ?
Part of the code
static MqttClient *mqtt = NULL;
// ============== Object to supply system functions ============================
class System: public MqttClient::System {
public:
unsigned long millis() const {
return ::millis();
}
void yield(void) {
::yield();
}
};
Compile error
error: expected class-name before '{' token
class System: public MqttClient::System {
^
Z:\2019_11_15_First_prototype\00_final_arduinomqtt_qos1_ArduinoMqtt\00_final_arduinomqtt_qos1_ArduinoMqtt.ino: In function 'void setup()':
00_final_arduinomqtt_qos1_ArduinoMqtt:138:15: error: 'System' is not a member of 'MqttClient'
MqttClient::System *mqttSystem = new System;
^~~~~~
00_final_arduinomqtt_qos1_ArduinoMqtt:138:23: error: 'mqttSystem' was not declared in this scope
MqttClient::System *mqttSystem = new System;
^~~~~~~~~~
Z:\2019_11_15_First_prototype\00_final_arduinomqtt_qos1_ArduinoMqtt\00_final_arduinomqtt_qos1_ArduinoMqtt.ino:138:23: note: suggested alternative: 'System'
MqttClient::System *mqttSystem = new System;
^~~~~~~~~~
System
I am getting some weird behavior where an incoming message on the subscribe topic seems to be handled as a publish packet, then instead of being handled by my callback function is reported as an unexpected message.
If it is relevant, I am using Sloeber (Eclipse), the board is an ESP12F and I am connecting using WiFiClientSecure
. Publishing works perfectly.
The log displays the following when a message is published by the broker on the subscription topic.
MQTT - Process message, type: 3
MQTT - Publish received, qos: 0
MQTT - Unexpected message
I have wrapped the MQTT functionality in a separate class. Code is as follows.
in project.cpp
void setup () {
wifiWrapper.connect("ssid", "password");
mqttWrapper.setup();
mqttWrapper.loadCertificate();
yield();
mqttWrapper.connect("mqtt-server.com", 8883, "username", "password");
mqttWrapper.mqtt->subscribe("test/subscribe", MqttClient::QOS0, subCallback);
yield();
}
void loop () {
mqttWrapper.mqtt->yield(10000);
}
in project.h
void subCallback (MqttClient::MessageData& md) {
const MqttClient::Message& msg = md.message;
char payload[msg.payloadLen + 1];
memcpy(payload, msg.payload, msg.payloadLen);
payload[msg.payloadLen] = '\0';
Serial.println(payload);
}
in mqttWrapper.cpp
void MqttWrapper::setup () {
MqttClient::System *mqttSystem = new System;
MqttClient::Logger *mqttLogger = new MqttClient::LoggerImpl<HardwareSerial>(Serial);
MqttClient::Network * mqttNetwork = new MqttClient::NetworkClientImpl<WiFiClient>(wifiClientSecure, *mqttSystem);
MqttClient::Buffer *mqttSendBuffer = new MqttClient::ArrayBuffer<MQTT_BUFFER_SIZE>();
MqttClient::Buffer *mqttRecvBuffer = new MqttClient::ArrayBuffer<MQTT_BUFFER_SIZE>();
MqttClient::MessageHandlers *mqttMessageHandlers = new MqttClient::MessageHandlersImpl<MQTT_SIMULTANEOUS_SUBS>();
MqttClient::Options mqttOptions;
mqttOptions.commandTimeoutMs = 10000;
mqtt = new MqttClient(
mqttOptions, *mqttLogger, *mqttSystem, *mqttNetwork, *mqttSendBuffer,
*mqttRecvBuffer, *mqttMessageHandlers
);
}
int8_t MqttWrapper::loadCertificate () {
yield();
int8_t status = 0;
Serial.println("Loading CA certificate...");
if (!SPIFFS.begin()) {
Serial.println("Failed to mount SPIFFS");
status = -1;
return status;
}
Serial.println("SPIFFS mounted");
Serial.println(caCertPath);
File caCert = SPIFFS.open(caCertPath, "r");
if (!caCert) {
Serial.println("Failed to open CA certificate");
status = -2;
return status;
}
Serial.println("CA certificate opened");
if (!wifiClientSecure.loadCACert(caCert)) {
Serial.println("CA certificate failed to load into client");
status = -3;
return status;
}
Serial.println("CA certificate loaded into client");
status = 1;
return status;
}
int8_t MqttWrapper::connect (char server[STR_BUFFER_LENGTH], uint16_t port, char username[STR_BUFFER_LENGTH], char password[STR_BUFFER_LENGTH]) {
int8_t status = 0;
Serial.println("Attempting TCP/MQTT connection...");
wifiClientSecure.connect(server, port);
if (wifiClientSecure.connected()) {
Serial.println("Secure TCP connection established");
} else {
Serial.println("Secure TCP connection failed");
status = -1;
return status;
}
Serial.println("Attempting MQTT connection");
MqttClient::ConnectResult connectResult;
MQTTPacket_connectData options = MQTTPacket_connectData_initializer;
options.MQTTVersion = 4;
options.clientID.cstring = deviceId;
options.cleansession = true;
options.keepAliveInterval = 15;
options.username.cstring = username;
options.password.cstring = password;
MqttClient::Error::type returnCode = mqtt->connect(options, connectResult);
if (returnCode == MqttClient::Error::SUCCESS) {
Serial.println("MQTT connection successful");
status = 1;
} else {
Serial.println("MQTT connection failed");
status = 0;
}
return status;
}
Any glaring errors? Or any ideas where to start looking?
In the PubSub example the payloadLen property of the message object is set to the length of the message string + 1.
message.payloadLen = strlen(buf) + 1;
This results in the MQTT payload published containing the string's terminating null character. In my case the added null character was causing the JSON payload parsing to fail. Unless you have a reason for the MQTT message to contain the terminating null character there is no need to add one to the string length.
message.payloadLen = strlen(buf);
hi @monstrenyatko
I tested on MQTT use SSL when sent 3 messages in once. The device just receives the first message and then disconnected.
I also tried in MQTT not have SSL, it works fine.
My message has the length about 100 bytes for each.
NET_MIN_TM_MS
I set default is 10
My device is ESP8266
thank you and have a good day.
I cannot for the life of me see where to enter a username and key for a broker. I've tried adding the following in amongst the other options that are set in the examples:
unsigned char user[10] = "mqtt_username";
unsigned char pass[40] = "mqtt_key";
unsigned char *uPoint = user;
unsigned char *pPoint = pass;
writeMQTTString(&uPoint, options.username);
writeMQTTString(&pPoint, options.password);
This compiles, but I still cannot get anything in or out of the broker. I am assuming this is to do with my credentials. Error output as below:
.
.
.
Connected to WiFi
IP: 192.168.1.118
Connecting
MQTT - Connect, clean-session: 1, ts: 47011
MQTT - Wait for message, type: 2, tm: 9999 ms
MQTT - Process message, type: 2
MQTT - Connect ack received
MQTT - Connect ack, code: 0
MQTT - Keepalive interval: 12 sec
MQTT - Session is not present => reset subscription
MQTT - Subscribe, to: mqtt_username/feed/sub, qos: 0
MQTT - Wait for message, type: 9, tm: 9995 ms
MQTT - Process message, type: 3
MQTT - Publish received, qos: 0
MQTT - Unexpected message
MQTT - Process message, type: 9
MQTT - Subscribe ack received
Subscribe error: -2
Drop connection
MQTT - Disconnecting, ts: 47249
Connecting
Can't establish the TCP connection
This is running on an ESP Huzzah with a WiFiClient. Can anyone point me in the right direction?
Hi @monstrenyatko
I setup everything in setup
functions.
and I want to subscribe to topic subtest
when receiving message subtest
from broker. It was returned Success but when I publish a message to subtest
topic. my device cannot receive any data from topic subtest
.
I tested subscribes 2 topics while setup MQTT, its success, and my device can receive data from both.
can you help me figure out that issue?
thank you so much.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.