Go Down

Topic: espduino: MQTT client for Arduino over ESP8266 (AT Command) (Read 319 times) previous topic - next topic

tuanpm

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
Quote
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino\HardwareSerial.cpp
Usage:
Code: [Select]

#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

wesselah

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy