• Stars
    star
    1,135
  • Rank 41,034 (Top 0.9 %)
  • Language
    C
  • License
    MIT License
  • Created almost 10 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

MQTT client library for ESP8266

esp_mqtt

This is MQTT client library for ESP8266, port from: MQTT client library for Contiki (thanks)

Features:

  • Support subscribing, publishing, authentication, will messages, keep alive pings and all 3 QoS levels (it should be a fully functional client).
  • Support multiple connection (to multiple hosts).
  • Support SSL connection
  • Easy to setup and use

Prerequire:

Compile:

  • Copy file include/user_config.sample.h to include/user_config.local.h and change settings, included: SSID, PASS, MQTT configurations ...

Make sure to add PYTHON PATH and compile PATH to Eclipse environment variable if using Eclipse

git clone --recursive https://github.com/tuanpmt/esp_mqtt
cd esp_mqtt
#clean
make clean
#make
make SDK_BASE=/tools/esp8266/sdk/ESP8266_NONOS_SDK ESPTOOL=tools/esp8266/esptool/esptool.py all
#flash
make ESPPORT=/dev/ttyUSB0 flash

Usage

See file: user/user_main.c

Notes

  • The client id needs to be unique. If not, When there are more than 2 clients use the same ClientID, the following logged-in client will kick the ahead logged-in client, and so on forever

Publish message and Subscribe

/* TRUE if success */
BOOL MQTT_Subscribe(MQTT_Client *client, char* topic, uint8_t qos);

BOOL MQTT_Publish(MQTT_Client *client, const char* topic, const char* data, int data_length, int qos, int retain);

Already support LWT: (Last Will and Testament)

/* Broker will publish a message with qos = 0, retain = 0, data = "offline" to topic "/lwt" if client don't send keepalive packet */
MQTT_InitLWT(&mqttClient, "/lwt", "offline", 0, 0);

Default configuration

See: include/user_config.sample.h

Define protocol name in include/user_config.local.h

#define PROTOCOL_NAMEv31	/*MQTT version 3.1 compatible with Mosquitto v0.15*/
//PROTOCOL_NAMEv311			/*MQTT version 3.11 compatible with https://eclipse.org/paho/clients/testing/*/

Create SSL Self sign

openssl req -x509 -newkey rsa:1024 -keyout key.pem -out cert.pem -days XXX

SSL Mqtt broker for test

var mosca = require('mosca')
var SECURE_KEY = __dirname + '/key.pem';
var SECURE_CERT = __dirname + '/cert.pem';
var ascoltatore = {
  //using ascoltatore
  type: 'mongo',
  url: 'mongodb://localhost:27017/mqtt',
  pubsubCollection: 'ascoltatori',
  mongo: {}
};

var moscaSettings = {
  port: 1880,
  stats: false,
  backend: ascoltatore,
  persistence: {
    factory: mosca.persistence.Mongo,
    url: 'mongodb://localhost:27017/mqtt'
  },
  secure : {
    keyPath: SECURE_KEY,
    certPath: SECURE_CERT,
    port: 1883
  }
};

var server = new mosca.Server(moscaSettings);
server.on('ready', setup);

server.on('clientConnected', function(client) {
    console.log('client connected', client.id);
});

// fired when a message is received
server.on('published', function(packet, client) {
  console.log('Published', packet.payload);
});

// fired when the mqtt server is ready
function setup() {
  console.log('Mosca server is up and running')
}

Example projects using esp_mqtt:

MQTT Broker for test

MQTT Client for test

Contributing:

Feel free to contribute to the project in any way you like!

Authors: Tuan PM

LICENSE - "MIT License"

More Repositories

1

espduino

ESP8266 network client (mqtt, restful) for Arduino
C++
381
star
2

esp32-mqtt

ESP32 MQTT sample project for
C
176
star
3

react-native-mqtt

MQTT Client native module for react-native
Objective-C
130
star
4

react-native-smartconfig

A React Native module for ESP8266 Smartconfig
Objective-C
127
star
5

esp_bridge

ESP8266 firmware SLIP Command, support mqtt, restful client
C
81
star
6

ESP8266MQTTClient

C
80
star
7

esp-request

This project is no longer supported, please use
C
62
star
8

esp-audio-player

ESP8266 Mp3 player and display spectrum on OLED
C
36
star
9

react-native-mqttjs

Pure javascript Websocket MQTT client library for react-native
JavaScript
27
star
10

esp32-fota

ESP32 Firmware update over the air
C
22
star
11

BluFi

BluFi Swift library - The protocol used to set up WiFi via Bluetooth for ESP32
Swift
19
star
12

esp_rpi_flasher

ESP32 batch programming tool
Python
18
star
13

iot-for-beginners-book

Ruby
17
star
14

lua-async

Async utilities for lua
Lua
14
star
15

esp-bootloader

ESP8266 bootloader
C++
10
star
16

esp-adf-sam

C
10
star
17

fullstack-engineer

Work environment, tools, knowledge, background required of a full stack R&D engineer genuine (Electronics, IT, IoTs)
JavaScript
8
star
18

STM32F10x_DSP_Lib

STM32F10x_DSP_Lib
Assembly
8
star
19

react-native-blufi

Java
5
star
20

html5-audio-fft

html5-audio-fft
HTML
5
star
21

esp-request-app

Sample http client for esp32
C
3
star
22

led-rgb-audio-spectrum

led-rgb-audio-spectrum
C
3
star
23

luat-dac-khu

2
star
24

CMSIS

CMSIS
C
2
star
25

piio

High performance GPIO library for raspberry pi
C++
2
star
26

BluFiAndroid

Java
1
star
27

STM32F10x_StdPeriph_Driver

STM32F10x_StdPeriph_Driver
C
1
star
28

esp-fibonacci-dualcore-test

JavaScript
1
star
29

indoor-loc-web

JavaScript
1
star