Git Product home page Git Product logo

esp-mqtt-arduino's Introduction

MQTT

A Wrapper around mqtt for Arduino to be used with esp8266 modules.

It wraps a slightly modified version of mqtt for esp8266 ported by Tuan PM. Original code for esp: https://github.com/tuanpmt/esp_mqtt Original code for contiki: https://github.com/esar/contiki-mqtt

====

secure libssl:

If you want to use secure communication, please use the secure-branch!

esp-mqtt-arduino's People

Contributors

i-n-g-o avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

esp-mqtt-arduino's Issues

Fatal exception 29 error

Hello
I'm using 2 esp8266, 1 as server mqtt using your "uMQTTBrokerSampleOOFull" example, and the other with the mqtt_pub.ino example,

I came across the following:
when I first connect the client to the server everything works fine, but if for some reason the client restarts, the server gives me a Fatal exception 29 error (StoreProhibitedCause).
I will put my code on both the server and the client and the respective log with the error. I hope someone can help me to find out where the error for this happened

//------------ Client--------------------------

#include <ESP8266WiFi.h>
#include <MQTT.h>

void myDataCb(const char* topic, uint32_t length, const char* data, uint32_t Alength);
void myPublishedCb();
void myDisconnectedCb();
void myConnectedCb();

#define CLIENT_ID "client1"
#define CLIENT_RX_MQTT "RX"

// create MQTT object
MQTT myMqtt(CLIENT_ID, "192.168.4.1", 1883);

//
const char* ssid = "ESP8266";
const char* password = "123456789";

/*
WiFi init stuff
*/
void startWiFiClient()
{
Serial.println("Connecting to " + (String)ssid);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(10);
Serial.print(".");
}
Serial.println("");

Serial.println("WiFi connected");
Serial.println("IP address: " + WiFi.localIP().toString());

Serial.println("Connecting to MQTT server");

// myMqtt.setUserPwd("ESP", "HELLO");

// setup callbacks
myMqtt.onConnected(myConnectedCb);
myMqtt.onDisconnected(myDisconnectedCb);
myMqtt.onPublished(myPublishedCb);
myMqtt.onData(myDataCb);

Serial.println("connect mqtt...");
myMqtt.connect();

while (myMqtt.isConnected() == false) {
delay(10);
}

/*
Subscribe to anything
*/
Serial.println("subscribe to topic...");
myMqtt.subscribe("#");

}

//
void setup() {
Serial.begin(115200);
delay(1000);

Serial.println();
startWiFiClient();

delay(10);
}

//
void loop() {
/*
int value = analogRead(A0);

String topic = WiFi.macAddress();


char Socket_TX_buffer[255];


Socket_TX_buffer[0] = 170;
Socket_TX_buffer[1] = 10;
Socket_TX_buffer[2] = 1;
Socket_TX_buffer[3] = 0;
Socket_TX_buffer[4] = 49;
Socket_TX_buffer[5] = 0;
Socket_TX_buffer[6] = 1;
Socket_TX_buffer[7] = 6;
Socket_TX_buffer[8] = 100;
Socket_TX_buffer[9] = 0;
Socket_TX_buffer[10] = 0;
Socket_TX_buffer[11] = 0;


// publish value to topic
boolean result = myMqtt.publish(topic, Socket_TX_buffer, 11);

delay(1000);

*/

if (myMqtt.isConnected() == false) {
// startWiFiClient();
}

}

/*

*/
void myConnectedCb()
{
Serial.println("connected to MQTT server");
}

void myDisconnectedCb()
{
Serial.println("disconnected. try to reconnect...");
delay(500);
myMqtt.connect();
}

void myPublishedCb()
{
//Serial.println("published.");
}

//void myDataCb(String& topic, String& data)

void myDataCb(const char* topic, uint32_t length, const char* data, uint32_t Alength)
{
String Atopic = topic; // convert const char in String
/*
Serial.println(topic); //
Serial.println(length); // length topic
Serial.println(data);
Serial.println(Alength); // length data
*/

if (Atopic != WiFi.macAddress()) { // filtra para nao receber o que enviou
char data_str[Alength + 1];
os_memcpy(data_str, data, Alength);

for (int i = 0; i < Alength ; i++) {
  Serial.write(data_str[i]);
}

Serial.println();

}

}

//------------Server-----------------
//---------------------
// Important: Use the setting "lwip Variant: 1.4 High Bandwidth" in the "Tools" menu
//----------------------------------

//https://github.com/martin-ger/uMQTTBroker

/*
uMQTTBroker demo for Arduino (C++-style)

The program defines a custom broker class with callbacks,
starts it, subscribes locally to anything, and publishs a topic every second.
Try to connect from a remote client and publish something - the console will show this as well.
*/

#include <ESP8266WiFi.h>
#include "uMQTTBroker.h"

#define SERVER_TX_MQTT "SERVER_TX"// esta invertido por causa os clientes
/*
Your WiFi config here
*/
char ssid[] = "ESP8266"; // your network SSID (name)
char pass[] = "123456789"; // your network password
bool WiFiAP = true; // Do yo want the ESP as AP?

/*
Custom broker class with overwritten callback functions
*/
class myMQTTBroker: public uMQTTBroker
{
public:
virtual bool onConnect(IPAddress addr, uint16_t client_count) {
Serial.println(addr.toString() + " connected");
return true;
}

virtual bool onAuth(String username, String password) {
  Serial.println("Username/Password: " + username + "/" + password);
  // Aqui pode ser vadidar o user e a pass
  return true;
}

virtual void onData(String topic, const char *data, uint32_t length) {
  if (topic != SERVER_TX_MQTT) { // filtra para nao receber o que enviou
    char data_str[length + 1];
    os_memcpy(data_str, data, length);
    // data_str[length] = '\0';

    for (int i = 0; i < length ; i++) {
      Serial.write(data_str[i]);
    }
  }
}

};

myMQTTBroker myBroker;

/*
WiFi init stuff
*/
void startWiFiClient()
{
Serial.println("Connecting to " + (String)ssid);
WiFi.begin(ssid, pass);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");

Serial.println("WiFi connected");
Serial.println("IP address: " + WiFi.localIP().toString());
}

void startWiFiAP()
{
WiFi.softAP(ssid, pass);
Serial.println("AP started");
Serial.println("IP address: " + WiFi.softAPIP().toString());
}

void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println();

// Start WiFi
if (WiFiAP)
startWiFiAP();
else
startWiFiClient();

// Start the broker
Serial.println("Starting MQTT broker");

myBroker.init();

/*
Subscribe to anything
*/
myBroker.subscribe("#");
}

int counter = 0;

void loop()
{
/*
Publish the counter value as String
*/
// myBroker.publish(SERVER_TX_MQTT, (String)counter++);

// wait a second
delay(1000);
MQTT_server_cleanupClientCons();
}

log .txt

"Last Will and Testament"

Hi guy !
At first, thank for your library, it's very nice. I have a question, how can i use LWT ? I opened your code and saw this "MQTT_InitLWT(&mqttClient, "/lwt", "offline", 0, 0);", but when i disconnected my esp, i couldnt get any message in /lwt topic. Can you help me ? .
Sr for my bad English.

Keep alive

after 2 minutes client disconnects and never reconnect... (it goes only once to void myDisconnectedCb() )

I tried the latest and previous releases.

Using class member methods as callbacks

Hi,

I've been trying to create a class of my own that uses your wrapper but I can't seem to be able to get it working with method callbacks (I'm not experienced with C++). Is there any good way this can be done?

Thank you.

change mqtt broker address

your library is so good, but i have problem.
i can't change mqtt broker address, because MQTT myMqtt(" ","192.168.1.20",1883);
can you fix it?
so thank

using Async connection for a Server - Clinets connection

hi @i-n-g-o sorry to make the issue for my question , actually I've planned to set a ESP module as AP and other ESP module as Station mode and like IOT home , the server received and send command to those , Iggre told me I should use asyinc lib instead of Esp8266WebServer lib , I really appreciated if you could guide me to what strategy should follow

undefined symbols

diff --git a/src/MQTT.cpp b/src/MQTT.cpp
index 3a48ea4..2bdfb84 100644
--- a/src/MQTT.cpp
+++ b/src/MQTT.cpp
@@ -133,12 +133,12 @@ MQTT::~MQTT()
  */
 void MQTT::setClientId(const char* client_id)
 {
-       MQTT_SetUserId(&mqttClient, client_id);
+       //MQTT_SetUserId(&mqttClient, client_id);
 }

 void MQTT::setUserPwd(const char* user, const char* pwd)
 {
-       MQTT_SetUserPwd(&mqttClient, user, pwd);
+       //MQTT_SetUserPwd(&mqttClient, user, pwd);
 }

Secure connection

The library works for unsecured connections. I was happy when I saw, that it is able to use secure connections, too. I am not experienced with secure connections. Is there a description/tutorial available?

Hallo Herr Ingo

Guten Tag,

ich habe versucht diese Code verstehen aber ich habe keine erfolg zu probieren mit mosquitto

#define CLIENT_ID "client3"
#define TOPIC "/client1/value"
MQTT myMqtt(CLIENT_ID, "192.168.178.34", 1883);

wo 192.168.178.34 muss mein pc sein oder?

dann unter:

mosquitto_pub -h localhost -p 1883 -t '/client1/value' -m "Hallo"

passiert gar nicht. :(

Es ist bestimmt "einen Grundschule" frage für Sie aber bin ich schon verzweifelt :(

Vielen dank im Voraus.
SG,
Pablo.

Any update?

This lib is better than any other mqtt libs because it works without any problem and very easy to use.

Last commit was 10 months ago will you discontinue @i-n-g-o ?

multiple subcribe

hi, sorry my english ... but how do I make multiple subscribers? moreover in case of user and password on the mqtt server how are they set?

thank you

setServer function

Why doesn't exists a setServer function which allow to change the server name dinamically?

Conditinal include

I have a variable in eeprom.

How can i do
if variable is true #include MQTT.h or change buffer sizes in mqtt_config.h ?

rc 2.3 wifi connection problem

I asked and answered myself :)

myMqtt.setUserPwd(CLIENT_ID, mqtt_password);

+++
Waaaww it really publish very fast without error :) no delay in loop 👍You are crazy!

void loop() {
String got = String(random(987654321, 9876543210));
String topic("users/34efdc34fre");
myMqtt.publish(topic, got + got + got );
}
+++

Current examples can't connect to wifi because of some problem with rc2.3 i think...

Maybe you can try to make something new with https://github.com/tzapu/WiFiManager it has some mqtt parameter when connecting to wifi

QOS

I use your library to do some tests. So far, it works good in both directions.
Now I tried QOS 1 for a subscription (topic: command). To test, I send a command: OFF from the broker (cloudMQTT) and reset the ESP. During boot I send a Command:ON from the broker. Theory (as I understand) says, that the library should now get the ON command right after startup. But it does not get it.

Multiple connections

Hello,

Long story short, I'm trying to automize my program to make multiple connections to a Mosquitto brocker, without having to change the code every time I have to make a new connection. So far I've added 3 more connections from a single NodeMCU-ESP8266 with adding a PubSubClient client(espClient) and a reconnect() with just changing the name (client). My question is this: Is it possible to add some sort of counter (client counter), so when an event occurs, it to make a new connection?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.