espduino: MQTT client for Arduino over ESP8266 (AT Command)

UPDATE: 5 March 2015, Using SLIP Command replace AT COMMAND with custom firmware for ESP8266
Add module: RESTful
Please see update here: https://github.com/tuanpmt/espduino

This is MQTT client for arduino connect to broker via ESP8266 AT command 0.2, port from MQTT client library for Contiki

Features

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

Repository:
https://github.com/tuanpmt/espduino

Warning
you have to change the SERIAL_BUFFER_SIZE minimum is 64 bytes in

C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino\HardwareSerial.cpp

Usage:

#include <espduino.h>
#include <SoftwareSerial.h>

SoftwareSerial debugPort(2, 3); // RX, TX

ESP esp(&Serial, &debugPort, 13);


void wifiCb(uint8_t status)
{
  debugPort.println("WIFI: Connected");
  esp.mqttConnect("mqtt.domain.io", 1880);
}

void mqttConnected(uint32_t* args)
{
  debugPort.println("MQTT:Connected");
  esp.subscribe("/topic");
}

void mqttDisconnected(uint32_t* args)
{
  debugPort.println("MQTT:Disconnected");
}

void mqttPublished(uint32_t* args)
{
  debugPort.println("MQTT:Published");
}

void mqttData(uint32_t* args)
{
  mqtt_event_data_t *event_data = (mqtt_event_data_t *)args;
  char topic[16];
  char data[32];

  memcpy(topic, event_data->topic, event_data->topic_length);
  memcpy(data, event_data->data, event_data->data_length);
  topic[event_data->topic_length] = 0;
  data[event_data->data_length] = 0;
  debugPort.print("Received, topic:");
  debugPort.print(topic);
  debugPort.print(", data:");
  debugPort.println(data);
}

const char clientId[] = "clientId";
const char user[] = "user";
const char pass[] = "pass";


void setup() {
  delay(200);
  Serial.begin(115200);
  debugPort.begin(9600);

  /* setup event */
  esp.wifiCb.attach(&wifiCb);
  esp.mqttConnected.attach(&mqttConnected);
  esp.mqttDisconnected.attach(&mqttDisconnected);
  esp.mqttPublished.attach(&mqttPublished);
  esp.mqttData.attach(&mqttData);

  /* Init data */
  esp.initMqttClient(clientId, user, pass, 30);

  /* wifi connect */
  esp.wifiConnect("DVES_HOME", "yourpassword");
}

void loop() {
  esp.process();
}

This code still much to do, however the functions of MQTT client is doing a great job

Test working with Adruino Nano

Best regards

Has anyone else got this successfully working with Arduino Uno and IDE 1.5.8? Looks like the path to HardwareSerial.cpp is changed and I cannot find SERIAL_BUFFER_SIZE in that file so wondering if I am looking at the right path. The sample sketch (with required changes) compiles and uploads for me. The sketch also run successfully and ESP8266 connects to WiFi and MQTT server but nothing happens when someone publish the data on the topic.

Can someone using this post some comments please.

Few more things to report.

  • Publish works for me while Arduino Uno is connected to a computer through USB
  • Subscribe still does not work
  • Nothing works when Arduino is working away from computer on external power supply
  • When not connected to computer, at the moment I do not have a way to debug so not sure where it is stuck and not working
  • When connected to the computer, debug messages are received from the espduino library on the serial port but not from the Sketch
  • Changing the pins of debugPort has no impact even if the pin numbers are wrong, I can see the debug output from espduino library
  • I have not yet analyzed the library but my C++ is not so good anymore so may take some time

You mentioned at command version 0.2 but can not find it or do you mean 0.92? Great job bye the way

i have testing on arduino uno with MQTT broker on linux . everything is OK , but i can subscribe only one topic and publish only one topic . how to subscribe multiple topic and publish multiple topic ? :astonished:

UPDATE: 5 March 2015, Using SLIP Command replace AT COMMAND with custom firmware for ESP8266 Add module: RESTful Please see update here: https://github.com/tuanpmt/espduino

Hi everyone, (Kind attention : tuanpm)

Query on using Espduino:-

1.Do i have to do any change in ESP8266 (ESP-12) module? i have purchased new one ,getting version no. 0018000902-AI03 after sending command AT+GMR.

2.Can you guide me for writing an arduino code just to publish and subscribe topic using espduino library?

Well i am bit novice in Mqtt , but i have used and tested Pubnub publish and subscribe channel concept.

Could anyone help me get started ? I'm not getting anywhere at the moment. I've used ESP modules before with arduinos but not with this firmware. Here's what I do ; 1. i flash the newest AT commands firmware using esptool from https://github.com/espressif/esp8266_at at this point i can still connect to the modules using AT commands 2 I flash the 2 bin files from https://github.com/tuanpmt/espduino/tree/master/esp8266/release I'm no longer able to get anything back from the esp with a terminal program 3 running the example code doesn't work :S

some questions; what bautrate does the module use after flashing the espduino firmware ? Is it normal that i'm unable to commuicate with the esp Does it react to AT commands after flashing the espduino firmware ?

Thanks

Thanks for putting this firmware out there. I'm having trouble loading it onto an ESP8266.

The first step uses a python script to load the firmware.

esp8266/tools/esptool.py -p COM1 write_flash 0x00000 esp8266/release/0x00000.bin 0x40000 esp8266/release/0x40000.bin

I'm a newbie, so please excuse the newbie question. I couldn't figure out what I should replace "COM1" with. If I'm using this on a Raspberry Pi, should it be something like "/dev/ttyUSB0" instead of "COM1"?

What's an easy way to load this? On a Windows machine? I tried installing Python on a Win7 machine and running that command, replacing "COM1" with the comm port of the FTDI adapter. But I get a bunch of "print" command errors.

Thanks for any help.