Example MQTT implemented on the ESP8266

Dependencies:   ESP8266Interface MQTT mbed-rtos mbed

Fork of HelloMQTT by MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTESP8266.h Source File

MQTTESP8266.h

00001 
00002 #if !defined(MQTTESP8266_H)
00003 #define MQTTESP8266_H
00004 
00005 #include "MQTTmbed.h"
00006 #include "ESP8266Interface.h"
00007 #include "MQTTSocket.h"
00008 
00009 // This struct is only used to workaround the order that the interfaces are initialized
00010 // MQTTSocket contains a TCPSocketConnection which needs the ESP8266Interface to be 
00011 // instantiated first. Unfortunately the only way to instantiate a member before a superclass 
00012 // is through another superclass.
00013 struct MQTTESP8266Holder {
00014     MQTTESP8266Holder(PinName tx, PinName rx, PinName reset, const char *ssid, const char *pass) :
00015             _wifi(tx, rx, reset, ssid, pass) {}
00016     
00017     ESP8266Interface _wifi;
00018 };
00019 
00020 // Straightforward implementation of a MQTT interface
00021 class MQTTESP8266 : public MQTTESP8266Holder, public MQTTSocket {    
00022 private:
00023     MQTTESP8266Holder::_wifi;
00024     //ESP8266Interface _wifi;
00025     
00026 public:    
00027     MQTTESP8266(PinName tx, PinName rx, PinName reset, const char *ssid, const char *pass) :
00028             MQTTESP8266Holder(tx, rx, reset, ssid, pass) {
00029         _wifi.init();
00030         _wifi.connect();
00031     }
00032     
00033     ESP8266Interface& getInterface() {
00034         return _wifi;
00035     }
00036     
00037     void reconnect() {
00038         _wifi.disconnect();
00039         _wifi.connect();
00040     }
00041 };
00042 
00043 
00044 #endif