-->
Page 1 of 8

Rock solid esp8266 wifi mqtt, restful client for arduino

PostPosted: Tue Jan 06, 2015 6:21 am
by tuanpm
UPDATE: 7 Jan 2015, IT WORKING NOW
UPDATE: 5 March 2015, Using SLIP Command replace AT COMMAND
Add module: RESTful
Please see update here: http://tuanpm.net/post/espduino


Like Native MQTT client library for ESP82666, but now is for Arduino, you only need ESP8266 run with AT COMMAND 0.2 and fixed:
you have to change the SERIAL_BUFFER_SIZE minimum is 64 bytes in
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino\HardwareSerial.cpp

Almost everything is done, can send MQTT connect message, subscribe message, and receive data
GITHUB: https://github.com/tuanpmt/espduino
WIKI: wiki/doku.php?id=espduino
Example code:
Code: Select all

#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();
}



Best regards

Re: MQTT client for Arduino over ESP8266

PostPosted: Wed Jan 07, 2015 1:09 pm
by bogdanioanliviu
Hello
Thank you for this. I´m waiting for my esp8266 to be able to test.
Regards

Re: MQTT client for Arduino over ESP8266

PostPosted: Tue Jan 20, 2015 2:39 pm
by sfinx
Tried the example, works great!

When I also try to include the FastLED library in my sketch however no connection to the MQTT daemon is made. I only see 'WIFI: Connected' and 'CWJAP failed' repeatedly on the debug serial. I'm no Arduino expert, but maybe there is a collision between the two libraries somewhere?

Re: MQTT client for Arduino over ESP8266

PostPosted: Tue Jan 20, 2015 3:17 pm
by hreintke
Hello Tuanm,
Seen several mqtt implementation, esp native, lua and now arduino, quite impressive.
Is a esp_iot_rtos_sdk native implementation also on your roadmap ?
Herman