Example MQTT implemented on the ESP8266

Dependencies:   ESP8266Interface MQTT mbed-rtos mbed

Fork of HelloMQTT by MQTT

This is an example of how to run MQTT using the esp8266 as the network connection. This program will default to trying to talk to a public MQTT server (test.mosquitto.org). The example will subscribe to a topic and then publish to that topic. In this way it will effectively echo back to itself. You can alternatively use a private mqtt broker,[[TODO: |instructions here]]

Please note that the ESP8266 interface cannot translate hostnames to IP Addresses, it can only accept raw IP Addresses as input.

Committer:
geky
Date:
Fri Jun 05 15:15:24 2015 +0000
Revision:
17:92a64d43ee61
Modified to use ESP8266

Who changed what in which revision?

UserRevisionLine numberNew contents of line
geky 17:92a64d43ee61 1
geky 17:92a64d43ee61 2 #if !defined(MQTTESP8266_H)
geky 17:92a64d43ee61 3 #define MQTTESP8266_H
geky 17:92a64d43ee61 4
geky 17:92a64d43ee61 5 #include "MQTTmbed.h"
geky 17:92a64d43ee61 6 #include "ESP8266Interface.h"
geky 17:92a64d43ee61 7 #include "MQTTSocket.h"
geky 17:92a64d43ee61 8
geky 17:92a64d43ee61 9 // This struct is only used to workaround the order that the interfaces are initialized
geky 17:92a64d43ee61 10 // MQTTSocket contains a TCPSocketConnection which needs the ESP8266Interface to be
geky 17:92a64d43ee61 11 // instantiated first. Unfortunately the only way to instantiate a member before a superclass
geky 17:92a64d43ee61 12 // is through another superclass.
geky 17:92a64d43ee61 13 struct MQTTESP8266Holder {
geky 17:92a64d43ee61 14 MQTTESP8266Holder(PinName tx, PinName rx, PinName reset, const char *ssid, const char *pass) :
geky 17:92a64d43ee61 15 _wifi(tx, rx, reset, ssid, pass) {}
geky 17:92a64d43ee61 16
geky 17:92a64d43ee61 17 ESP8266Interface _wifi;
geky 17:92a64d43ee61 18 };
geky 17:92a64d43ee61 19
geky 17:92a64d43ee61 20 // Straightforward implementation of a MQTT interface
geky 17:92a64d43ee61 21 class MQTTESP8266 : public MQTTESP8266Holder, public MQTTSocket {
geky 17:92a64d43ee61 22 private:
geky 17:92a64d43ee61 23 MQTTESP8266Holder::_wifi;
geky 17:92a64d43ee61 24 //ESP8266Interface _wifi;
geky 17:92a64d43ee61 25
geky 17:92a64d43ee61 26 public:
geky 17:92a64d43ee61 27 MQTTESP8266(PinName tx, PinName rx, PinName reset, const char *ssid, const char *pass) :
geky 17:92a64d43ee61 28 MQTTESP8266Holder(tx, rx, reset, ssid, pass) {
geky 17:92a64d43ee61 29 _wifi.init();
geky 17:92a64d43ee61 30 _wifi.connect();
geky 17:92a64d43ee61 31 }
geky 17:92a64d43ee61 32
geky 17:92a64d43ee61 33 ESP8266Interface& getInterface() {
geky 17:92a64d43ee61 34 return _wifi;
geky 17:92a64d43ee61 35 }
geky 17:92a64d43ee61 36
geky 17:92a64d43ee61 37 void reconnect() {
geky 17:92a64d43ee61 38 _wifi.disconnect();
geky 17:92a64d43ee61 39 _wifi.connect();
geky 17:92a64d43ee61 40 }
geky 17:92a64d43ee61 41 };
geky 17:92a64d43ee61 42
geky 17:92a64d43ee61 43
geky 17:92a64d43ee61 44 #endif