Loading... Please wait...

Using MQTT with ESP8266

Posted by


This blog entry is quick outline of using MQTT with a message broker to implement a simple IOT project. We are using a Mac but the steps are similar for any supported platform.

Install MQTT Message Broker on Mac

  1. Install mosquito from mosquito.org/download: brew install mosquito
  2. Run mosquito: brew services start mosquito

Start a Listener

mosquitto_sub -t outTopic

Send a Message

mosquitto_pub -t outTopic -m "Hello World" –q 2

Note: use –q 2 to ensure message gets there and only once. When QOS is zero you can end up with dropped messages.

Install Arduino

Follow instructions here: https://www.arduino.cc/en/Guide/HomePage

Install ESP8266 Support

Once you have installed Arduino, install the Arduino ESP8266 extension by selecting Preferences and entering

http://arduino.esp8266.com/stable/package_esp8266c...

into the Additional Board Manager URLs field. Open Boards Manager from Tools > Board menu and install esp8266. Select your board and port.

Install PubSubClient Library

Use the Include Library > Manage libraries menu and install PubSubClient library.

Run the mqtt_example

Open up the mqtt_example provided with the PubSubClient library. Set your Wi-Fi SSID & password. Run the example. Change the topic name to something unique so you can recognize it from the rest of public topics. See that it appears on the dashboard.

Connect to local instance of mosquito

Change the topic names back to original. Change the broker ip address to your local instance. Note that PubSubClient only supports QOS 0 – message sent once max. So it is possible to lose messages.

//const char* mqtt_server = " broker.mqtt-dashboard.com";

byte mqtt_server[] = { 10, 1, 10, 12 };

And restart the example. The listener you created for outTopic should show your published messages.

You can also turn the ESP8266 LED on and off by publishing this message from a terminal window:

mosquitto_pub -t inTopic -m "1"

mosquitto_pub -t inTopic -m "0"



 

What's News

newsletter

Copyright 2017 oddWires. All Rights Reserved.
 Sitemap | oddWires